Merge PR 'Refuse to sign messages that alias channel_announcement serializations' (#5727)
What changed, and why it matters
This change tightens a security guardrail in LDK's message-signing helper. Previously, the code refused to sign arbitrary text if the prefixed text happened to look like a 'node_announcement' gossip message, because an attacker could replay that signature as fake gossip. The patch extends the same refusal to 'channel_announcement' and 'channel_update' serializations, closing a gap where a crafted message could still be replayed as forged Lightning gossip.
Review and merge; ensure downstream signers using the `NodeSigner` trait honor the documented refusal policy. No immediate incident response is indicated, but users should upgrade to prevent the theoretical replay vectors for channel announcements and updates.
Security signals we found
signature-replay hardening
gossip-message aliasing prevention
defensive signing policy expansion
test coverage for channel_announcement and channel_update collisions
Evidence from the diff
The sign_message/message_signing::sign functions prefix user data with ‘Lightning Signed Message:’ and produce a recoverable ECDSA signature over sha256d(prefixed_data). Because Lightning gossip signatures also cover sha256d(serialized_gossip_message), a maliciously chosen user message whose prefixed bytes equal a valid gossip serialization could be signed and then replayed as a gossip signature. The existing check only rejected UnsignedNodeAnnouncement serializations. This commit adds UnsignedChannelAnnouncement and UnsignedChannelUpdate to is_gossip_serialization, and updates documentation/tests accordingly. It is a defensive hardening patch, not a fix for an active exploit, and it does not change the public API shape beyond the already-changed Result return type.
Changed components
lightning/src/util/message_signing.rslightning/src/sign/mod.rs (NodeSigner trait documentation)pending_changelog/sign-message-gossip-collision.txtInspect 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 62/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.