Merge PR 'Reject RAA before our commitment_signed has been sent' (#5561)
What changed, and why it matters
This commit fixes a bug in the Lightning Dev Kit where a node could incorrectly accept a 'revoke_and_ack' message from its counterparty before it had actually sent out a new commitment transaction. Under the right timing conditions, this could cause the node to sign a commitment that its own channel monitor had never been told about, potentially leaving it unable to recover funds or claim HTLCs if that commitment later confirmed on-chain. The fix adds extra state checks so the node rejects such premature messages and closes the channel instead.
Upgrade to a rust-lightning release containing this commit. Nodes running versions without this fix are at risk of signing commitments not tracked by their ChannelMonitor if a counterparty sends a premature revoke_and_ack during async signing or monitor persistence delays. Operators relying on async signers or slow monitor persistence should prioritize this patch.
Security signals we found
State-machine validation bug in Lightning channel protocol handling
Potential ChannelMonitor desynchronization leading to inability to claim HTLCs on revoked/confirmed commitment
Premature acceptance of revoke_and_ack before commitment_signed transmission
Regression tests added for both monitor-pending and async-signer pending paths
Channel closure now enforced instead of accepting invalid protocol transition
Evidence from the diff
The patch changes channel.rs’s revoke_and_ack handling. Previously, the code only checked is_awaiting_remote_revoke(), which is set as soon as a new counterparty commitment is built. However, the actual commitment_signed may still be held back because either a ChannelMonitorUpdate is pending (monitor_pending_commitment_signed) or the signer is async and has not yet produced the signature (signer_pending_commitment_update). Accepting a revoke_and_ack in that window would advance the counterparty commitment number, then later send a commitment_signed for a commitment the ChannelMonitor never saw. The fix rejects RAA in either pending state and closes the channel with ‘Received an unexpected revoke_and_ack’. Two regression tests are added: one for the monitor-pending case and one for the async-signer case, plus a test helper forge_counterparty_raa.
Changed components
lightning/src/ln/channel.rslightning/src/ln/async_signer_tests.rslightning/src/ln/functional_tests.rslightning/src/ln/functional_test_utils.rsLDK Lightning channel state machineChannelMonitor synchronization logicAsync signer integrationInspect captured patch +163 / −1
### lightning/src/ln/async_signer_tests.rs
@@ -350,6 +350,64 @@ fn do_test_async_commitment_signature_for_commitment_signed_revoke_and_ack(
}
}
+#[test]
+fn test_raa_before_commitment_signed_sent_async_signer() {
+ // Test that we reject an RAA which arrives after we've built our counterparty's next
+ // commitment transaction but before we've actually sent them the `commitment_signed` for it,
+ // here because our signer cannot yet produce the commitment signature. This covers the
+ // `signer_pending_commitment_update` disjunct of the check in `revoke_and_ack`;
+ // `test_counterparty_raa_before_commitment_signed_sent` covers the
+ // `monitor_pending_commitment_signed` disjunct.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let node_a_id = nodes[0].node.get_our_node_id();
+ let node_b_id = nodes[1].node.get_our_node_id();
+
+ let (_, _, channel_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1);
+
+ // Node A sends an HTLC and a `commitment_signed`, which node B processes while its signer
+ // cannot sign the `commitment_signed` node B owes node A.
+ let (route, payment_hash, _, payment_secret) =
+ get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
+ let onion = RecipientOnionFields::secret_only(payment_secret, 1_000_000);
+ let id = PaymentId(payment_hash.0);
+ nodes[0].node.send_payment_with_route(route, payment_hash, onion, id).unwrap();
+ check_added_monitors(&nodes[0], 1);
+
+ let send_event =
+ SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+ nodes[1].node.handle_update_add_htlc(node_a_id, &send_event.msgs[0]);
+
+ nodes[1].disable_channel_signer_op(
+ &node_a_id,
+ &channel_id,
+ SignerOp::SignCounterpartyCommitment,
+ );
+ nodes[1].node.handle_commitment_signed_batch_test(node_a_id, &send_event.commitment_msg);
+ check_added_monitors(&nodes[1], 1);
+
+ // Node B responds with its `revoke_and_ack` but holds back its `commitment_signed` until its
+ // signer is available. We drop the `revoke_and_ack` as node A's state is irrelevant here.
+ let _ = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, node_a_id);
+ assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+ // Node A now revokes its current commitment as if it had received node B's
+ // `commitment_signed`, which node B has not yet sent.
+ let raa = forge_counterparty_raa(&nodes[0], &node_b_id, &channel_id);
+ nodes[1].node.handle_revoke_and_ack(node_a_id, &raa);
+ assert_eq!(
+ check_closed_broadcast(&nodes[1], 1, true).pop().unwrap().data,
+ "Received an unexpected revoke_and_ack"
+ );
+ check_added_monitors(&nodes[1], 1);
+ let reason =
+ ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() };
+ check_closed_event(&nodes[1], 1, reason, &[node_a_id], 100000);
+}
+
#[test]
fn test_funding_signed_0conf() {
do_test_funding_signed_0conf(vec![
### lightning/src/ln/channel.rs
@@ -9481,7 +9481,15 @@ where
}
}
- if !self.context.channel_state.is_awaiting_remote_revoke() {
+ // `AWAITING_REMOTE_REVOKE` is set as soon as we build a new counterparty commitment, but
+ // the corresponding `commitment_signed` may still be held back pending a monitor update or
+ // our signer, in which case our counterparty cannot have received it yet. If we accepted
+ // an RAA anyway, we'd step the counterparty commitment number forward and the eventual
+ // `commitment_signed` would sign a commitment the `ChannelMonitor` has never seen.
+ if !self.context.channel_state.is_awaiting_remote_revoke()
+ || self.context.monitor_pending_commitment_signed
+ || self.context.signer_pending_commitment_update
+ {
// Our counterparty seems to have burned their coins to us (by revoking a state when we
// haven't given them a new commitment transaction to broadcast). We should probably
// take advantage of this by updating our channel monitor, sending them an error, and
### lightning/src/ln/functional_test_utils.rs
@@ -2093,6 +2093,51 @@ pub fn create_announced_chan_between_nodes_with_value<'a, 'b, 'c: 'd, 'd>(
(chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
}
+/// Constructs a `revoke_and_ack` which `node`'s counterparty would send if it had received and
+/// processed a `commitment_signed` from `node`, without `node` ever sending one.
+///
+/// The returned message revokes `node`'s current holder commitment, so it is only accepted by the
+/// counterparty if its signer is still willing to release the corresponding secrets here.
+#[cfg(any(test, feature = "_externalize_tests"))]
+pub fn forge_counterparty_raa(
+ node: &Node, counterparty_node_id: &PublicKey, channel_id: &ChannelId,
+) -> msgs::RevokeAndACK {
+ use crate::ln::channel::Channel;
+ use crate::sign::ChannelSigner;
+ use bitcoin::secp256k1::Secp256k1;
+
+ let per_commitment_secret;
+ let next_per_commitment_point;
+ {
+ let per_peer_state = node.node.per_peer_state.read().unwrap();
+ let guard = per_peer_state.get(counterparty_node_id).unwrap().lock().unwrap();
+ let keys =
+ guard.channel_by_id.get(channel_id).and_then(Channel::as_funded).unwrap().get_signer();
+
+ const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
+
+ // Make signer believe we got a counterparty signature, so that it allows the revocation
+ keys.get_enforcement_state().last_holder_commitment -= 1;
+ per_commitment_secret = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER).unwrap();
+
+ // Must revoke without gaps
+ keys.get_enforcement_state().last_holder_commitment -= 1;
+ keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1).unwrap();
+
+ keys.get_enforcement_state().last_holder_commitment -= 1;
+ let sec = keys.release_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2).unwrap();
+ let key = SecretKey::from_slice(&sec).unwrap();
+ next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(), &key);
+ }
+
+ msgs::RevokeAndACK {
+ channel_id: *channel_id,
+ per_commitment_secret,
+ next_per_commitment_point,
+ release_htlc_message_paths: Vec::new(),
+ }
+}
+
pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(
nodes: &'a Vec<Node<'b, 'c, 'd>>, a: usize, b: usize, channel_value: u64, push_msat: u64,
) -> (msgs::ChannelReady, Transaction) {
### lightning/src/ln/functional_tests.rs
@@ -6840,6 +6840,57 @@ pub fn test_counterparty_raa_skip_no_crash() {
check_closed_event(&nodes[1], 1, reason, &[node_a_id], 100000);
}
+#[xtest(feature = "_externalize_tests")]
+pub fn test_counterparty_raa_before_commitment_signed_sent() {
+ // Test that we reject an RAA which arrives after we've built our counterparty's next
+ // commitment transaction but before we've actually sent them the `commitment_signed` for it
+ // (here because the `ChannelMonitorUpdate` for it is still in progress). Previously we only
+ // checked `AWAITING_REMOTE_REVOKE`, which is set when the commitment is built, so such an RAA
+ // was accepted. We'd then step the counterparty commitment number forward and, once the
+ // monitor update completed, sign a commitment transaction the `ChannelMonitor` had never been
+ // told about, leaving it unable to claim any HTLCs in it if it confirmed.
+ let chanmon_cfgs = create_chanmon_cfgs(2);
+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let node_a_id = nodes[0].node.get_our_node_id();
+ let node_b_id = nodes[1].node.get_our_node_id();
+
+ let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1).2;
+
+ // Node A sends an HTLC and a `commitment_signed`, which node B processes while its monitor
+ // persistence is still in progress, holding back its own RAA and `commitment_signed`.
+ let (route, payment_hash, _, payment_secret) =
+ get_route_and_payment_hash!(nodes[0], nodes[1], 1_000_000);
+ let onion = RecipientOnionFields::secret_only(payment_secret, 1_000_000);
+ let id = PaymentId(payment_hash.0);
+ nodes[0].node.send_payment_with_route(route, payment_hash, onion, id).unwrap();
+ check_added_monitors(&nodes[0], 1);
+
+ let send_event =
+ SendEvent::from_event(nodes[0].node.get_and_clear_pending_msg_events().remove(0));
+ nodes[1].node.handle_update_add_htlc(node_a_id, &send_event.msgs[0]);
+
+ chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
+ nodes[1].node.handle_commitment_signed_batch_test(node_a_id, &send_event.commitment_msg);
+ check_added_monitors(&nodes[1], 1);
+ assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+
+ // Node A now revokes its current commitment as if it had received node B's
+ // `commitment_signed`, which node B has not yet sent.
+ let raa = forge_counterparty_raa(&nodes[0], &node_b_id, &channel_id);
+ nodes[1].node.handle_revoke_and_ack(node_a_id, &raa);
+ assert_eq!(
+ check_closed_broadcast(&nodes[1], 1, true).pop().unwrap().data,
+ "Received an unexpected revoke_and_ack"
+ );
+ check_added_monitors(&nodes[1], 1);
+ let reason =
+ ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() };
+ check_closed_event(&nodes[1], 1, reason, &[node_a_id], 100000);
+}
+
#[xtest(feature = "_externalize_tests")]
pub fn test_bump_txn_sanitize_tracking_maps() {
// Sanitizing pending_claim_request and claimable_outpoints used to be buggy,Why this scored 68/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.