Refuse to sign messages that alias channel_announcement serializations
What changed, and why it matters
This commit fixes a security gap in how the Lightning Dev Kit signs arbitrary messages. Previously, the code refused to sign messages that could be mistaken for a 'node_announcement' gossip message, because such a signature could be replayed as a forged gossip signature. However, it did not check for 'channel_announcement' or 'channel_update' messages. An attacker could craft a message that, after adding the standard 'Lightning Signed Message:' prefix, looks like a valid channel announcement or update. The victim's signature on that harmless-looking message could then be used as a fake signature on a real gossip message. The patch now refuses to sign any message that serializes as one of these three gossip types, closing the forgery path.
Treat this as a security fix. Review any downstream code that calls `sign_message` or `message_signing::sign` and ensure it handles the new `Result` return and the expanded refusal cases. Backport to maintained release branches. No immediate runtime mitigation is needed beyond applying the patch.
Security signals we found
Signature replay / cross-protocol forgery prevention
Expansion of existing anti-aliasing guard to additional gossip message types
Commit message explicitly describes forged gossip signature risk
New unit tests demonstrate concrete channel_announcement and channel_update aliasing
Changelog frames change as security-relevant API update
Evidence from the diff
The sign_message API and message_signing::sign function prefix user-supplied bytes with Lightning Signed Message: and produce a recoverable ECDSA signature over the sha256d hash of the prefixed bytes. Lightning gossip signatures use the same sha256d construction, so if the prefixed bytes happen to be a valid serialization of a gossip message, the resulting signature verifies as that gossip message. The original code only checked for UnsignedNodeAnnouncement. Because node_announcement and channel_announcement both start with a 2-byte feature-vector length, the prefix bytes Li (0x4c69) are interpreted as a 19,537-byte feature vector, leaving the remaining fields (including chain_hash in channel_announcement) attacker-controlled. That means a channel_announcement can be forged with a real genesis block hash, bypassing the prior assumption that chain_hash validation would save it. channel_update begins with chain_hash, so aliasing it would require a genesis hash starting with the prefix, which is implausible but not structurally guaranteed. The patch extends is_gossip_serialization to round-trip check UnsignedNodeAnnouncement, UnsignedChannelAnnouncement, and UnsignedChannelUpdate, and updates documentation and tests accordingly.
Changed components
lightning/src/util/message_signing.rslightning/src/sign/mod.rsNodeSigner::sign_message APImessage_signing::sign helperInspect captured patch +69 / −14
### lightning/src/sign/mod.rs
@@ -1018,8 +1018,8 @@ pub trait NodeSigner {
///
/// A receiver knowing the node's id and the message can be sure that the signature was generated by the caller.
/// An `Err` can be returned to signal that the signer is unavailable / cannot produce a valid
- /// signature, including if the prefixed message would itself be a valid `node_announcement`
- /// serialization, as its signature could otherwise be replayed as a gossip message signature.
+ /// signature, including if the prefixed message would itself be the serialization of a signed
+ /// gossip message, as its signature could otherwise be replayed as a gossip message signature.
fn sign_message(&self, msg: &[u8]) -> Result<String, ()>;
}
### lightning/src/util/message_signing.rs
@@ -20,7 +20,9 @@
//! <https://lightning.readthedocs.io/lightning-signmessage.7.html>
//! <https://api.lightning.community/#signmessage>
-use crate::ln::msgs::UnsignedNodeAnnouncement;
+use crate::ln::msgs::{
+ UnsignedChannelAnnouncement, UnsignedChannelUpdate, UnsignedNodeAnnouncement,
+};
#[allow(unused)]
use crate::prelude::*;
use crate::util::base32;
@@ -31,15 +33,19 @@ use bitcoin::secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey};
static LN_MESSAGE_PREFIX: &[u8] = b"Lightning Signed Message:";
-/// Returns whether the given bytes are exactly the serialization of a `node_announcement`, i.e.
-/// whether a signature over their sha256d hash would verify as a `node_announcement` signature.
+/// Returns whether the given bytes are exactly the serialization of a signed gossip message, i.e.
+/// whether a signature over their sha256d hash would verify as a gossip message signature.
///
/// Gossip message signatures cover the same sha256d hash construction used here, so refusing to
-/// sign such bytes prevents a signed message from being replayed as a (forged) `node_announcement`.
+/// sign such bytes prevents a signed message from being replayed as (forged) gossip.
///
-/// Other gossip message types are not checked: their `chain_hash` is validated against the genesis
-/// hash before their signature is processed, and no real chain's genesis hash starts with "Li",
-/// whereas `node_announcement` has no such validation.
+/// `node_announcement` and `channel_announcement` serializations both begin with a two-byte
+/// feature vector length, which the "Li" prefix parses as a 0x4c69-byte vector, so every field
+/// that follows, including `channel_announcement`'s `chain_hash`, is caller-controlled. A
+/// `channel_update` serialization instead begins with its `chain_hash`, which would have to start
+/// with "Lightning Signed Message:" for the prefixed message to alias it; it is checked here all the
+/// same so that the refusal is structural rather than relying on no real chain's genesis hash
+/// having that prefix.
fn is_gossip_serialization(bytes: &[u8]) -> bool {
fn round_trips<T: LengthReadable + Writeable>(bytes: &[u8]) -> bool {
match T::read_from_fixed_length_buffer(&mut &bytes[..]) {
@@ -48,6 +54,8 @@ fn is_gossip_serialization(bytes: &[u8]) -> bool {
}
}
round_trips::<UnsignedNodeAnnouncement>(bytes)
+ || round_trips::<UnsignedChannelAnnouncement>(bytes)
+ || round_trips::<UnsignedChannelUpdate>(bytes)
}
fn sigrec_encode(sig_rec: RecoverableSignature) -> Vec<u8> {
@@ -76,8 +84,8 @@ fn sigrec_decode(sig_rec: Vec<u8>) -> Result<RecoverableSignature, Error> {
/// A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller.
/// Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
///
-/// Returns an `Err` if the prefixed message is a valid `node_announcement` serialization, as the
-/// signature could otherwise be replayed as a (forged) `node_announcement` signature.
+/// Returns an `Err` if the prefixed message is the serialization of a signed gossip message, as
+/// the signature could otherwise be replayed as a (forged) gossip message signature.
pub fn sign(msg: &[u8], sk: &SecretKey) -> Result<String, ()> {
let prefixed = [LN_MESSAGE_PREFIX, msg].concat();
if is_gossip_serialization(&prefixed) {
@@ -121,6 +129,8 @@ mod test {
use crate::util::message_signing::{
is_gossip_serialization, recover_pk, sign, verify, LN_MESSAGE_PREFIX,
};
+ use bitcoin::constants::ChainHash;
+ use bitcoin::network::Network;
use bitcoin::secp256k1::constants::ONE;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use core::str::FromStr;
@@ -176,6 +186,51 @@ mod test {
assert_eq!(sign(&msg, &one_key), Err(()));
}
+ #[test]
+ fn test_sign_refuses_channel_announcement_serialization() {
+ let one_key = SecretKey::from_slice(&ONE).unwrap();
+ let node_id = PublicKey::from_secret_key(&Secp256k1::signing_only(), &one_key).serialize();
+
+ // Like a `node_announcement`, a `channel_announcement` begins with a two-byte feature
+ // vector length that the prefix parses as 0x4c69 bytes. Its `chain_hash` follows that
+ // vector, however, so unlike a `node_announcement` it is entirely caller-controlled and
+ // can be set to a real genesis hash, passing the chain hash check gossip processing
+ // performs before verifying the signature.
+ let mut msg = vec![0u8; 19_538]; // rest of the 0x4c69-byte feature vector
+ msg.extend_from_slice(&ChainHash::using_genesis_block(Network::Bitcoin).to_bytes());
+ msg.extend_from_slice(&0u64.to_be_bytes()); // short_channel_id
+ msg.extend_from_slice(&node_id); // node_id_1
+ msg.extend_from_slice(&node_id); // node_id_2
+ msg.extend_from_slice(&node_id); // bitcoin_key_1
+ msg.extend_from_slice(&node_id); // bitcoin_key_2
+
+ assert!(is_gossip_serialization(&[LN_MESSAGE_PREFIX, &msg[..]].concat()));
+ assert_eq!(sign(&msg, &one_key), Err(()));
+ }
+
+ #[test]
+ fn test_sign_refuses_channel_update_serialization() {
+ let one_key = SecretKey::from_slice(&ONE).unwrap();
+
+ // A `channel_update` serialization begins with its `chain_hash`, so aliasing it requires
+ // the chain hash itself to start with "Lightning Signed Message:". No real chain's
+ // genesis hash does, but the check is applied all the same so that refusing to sign does
+ // not depend on that.
+ let mut msg = vec![0; 7]; // completes the prefix into the chain hash
+ msg.extend_from_slice(&0u64.to_be_bytes()); // short_channel_id
+ msg.extend_from_slice(&0u32.to_be_bytes()); // timestamp
+ msg.extend_from_slice(&1u8.to_be_bytes()); // message_flags
+ msg.extend_from_slice(&0u8.to_be_bytes()); // channel_flags
+ msg.extend_from_slice(&0u16.to_be_bytes()); // cltv_expiry_delta
+ msg.extend_from_slice(&0u64.to_be_bytes()); // htlc_minimum_msat
+ msg.extend_from_slice(&0u32.to_be_bytes()); // fee_base_msat
+ msg.extend_from_slice(&0u32.to_be_bytes()); // fee_proportional_millionths
+ msg.extend_from_slice(&0u64.to_be_bytes()); // htlc_maximum_msat
+
+ assert!(is_gossip_serialization(&[LN_MESSAGE_PREFIX, &msg[..]].concat()));
+ assert_eq!(sign(&msg, &one_key), Err(()));
+ }
+
#[test]
fn test_verify_ground_truth_ish() {
// There are no standard tests vectors for Sign/Verify, using the same tests vectors as c-lightning to see if they are compatible.
### pending_changelog/sign-message-gossip-collision.txt
@@ -1,6 +1,6 @@
# API Updates
* `lightning::util::message_signing::sign` now returns a `Result`, and refuses to sign messages
- which, once prefixed with "Lightning Signed Message:", serialize as a `node_announcement`. A
- signature over such a message could otherwise be replayed as a (forged) `node_announcement`
- signature (#386).
+ which, once prefixed with "Lightning Signed Message:", serialize as a signed gossip message
+ (`node_announcement`, `channel_announcement`, or `channel_update`). A signature over such a
+ message could otherwise be replayed as a (forged) gossip message signature (#386).Why this scored 76/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.