Merge PR 'Fix HTLC loss when a closed channel's ID is reused in open_channel' (#5562)
What changed, and why it matters
This patch fixes a bug in the Lightning Dev Kit where a forwarded payment (HTLC) could be permanently lost if a new channel reused the same temporary ID as a recently closed channel. Previously, the code could look up the new, unfunded channel instead of the closed one, skip writing the payment secret (preimage) to the closed channel's on-chain safety copy (ChannelMonitor), and let the payment expire unclaimed. The fix both rejects incoming channel open attempts that reuse such an ID and ensures claims still reach the correct closed channel's monitor even if an unfunded channel exists under the same ID.
Upgrade to a release containing this commit. Nodes forwarding payments are at risk of losing HTLCs if a peer can induce channel ID reuse and force-close timing. No runtime workaround is described.
Security signals we found
Loss of forwarded HTLC preimage persistence
Temporary channel ID reuse collision
Closed ChannelMonitor update path bypass
Unfunded channel shadowing closed channel state
Payment forwarding event blocked or delayed
Evidence from the diff
In channelmanager.rs, two changes are made. First, when handling open_channel, a temporary_channel_id collision is now also checked against closed_channel_monitor_update_ids, preventing a new channel from reusing an ID whose closed channel may still require ChannelMonitor updates. Second, in the HTLC claim path, if the looked-up channel_by_id entry is unfunded, the code no longer returns early; instead it falls through to claim against the closed channel’s ChannelMonitor. A regression test demonstrates both the rejection and the fallback claim behavior.
Changed components
lightning/src/ln/channelmanager.rsChannelMonitor update logic for closed channelsopen_channel temporary_channel_id collision handlingHTLC claim/fulfill pathInspect captured patch +109 / −2
### lightning/src/ln/chanmon_update_fail_tests.rs
@@ -4567,6 +4567,104 @@ fn test_claim_to_closed_channel_blocks_forwarded_preimage_removal() {
.any(|(_, (_, preimage))| *preimage == Some(payment_preimage)));
}
+#[test]
+fn test_claim_to_closed_channel_with_reused_temporary_channel_id() {
+ // A new, unfunded channel may sit in `channel_by_id` under the ID of a closed channel if that
+ // ID is reused as the `temporary_channel_id`. We reject such `open_channel`s from our
+ // counterparty as we may still need to update the closed channel's `ChannelMonitor`, but if
+ // an unfunded channel shares its ID anyway, previously, when we then had to claim an HTLC
+ // which had been received on the closed channel we'd find the unfunded channel, conclude
+ // there was nothing to do, and never write the preimage to the closed channel's
+ // `ChannelMonitor`, losing the HTLC once it timed out on-chain.
+ // This tests that we reject such `open_channel`s, and that claims still reach the closed
+ // channel's `ChannelMonitor` if an unfunded channel shares its ID anyway.
+ let chanmon_cfgs = create_chanmon_cfgs(3);
+ let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
+ let legacy_cfg = test_legacy_channel_config();
+ let node_chanmgrs =
+ create_node_chanmgrs(3, &node_cfgs, &[Some(legacy_cfg.clone()), Some(legacy_cfg), None]);
+ let nodes = create_network(3, &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 node_c_id = nodes[2].node.get_our_node_id();
+
+ // First open channels, route a payment, and force-close the first hop.
+ let chan_a =
+ create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000);
+ let chan_b =
+ create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000);
+
+ let (payment_preimage, payment_hash, ..) =
+ route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
+
+ let message = "Channel force-closed".to_owned();
+ nodes[0]
+ .node
+ .force_close_broadcasting_latest_txn(&chan_a.2, &node_b_id, message.clone())
+ .unwrap();
+ check_added_monitors(&nodes[0], 1);
+ let a_reason = ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(true), message };
+ check_closed_event(&nodes[0], 1, a_reason, &[node_b_id], 1000000);
+ check_closed_broadcast(&nodes[0], 1, true);
+
+ let as_commit_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
+ assert_eq!(as_commit_tx.len(), 1);
+
+ mine_transaction(&nodes[1], &as_commit_tx[0]);
+ let b_reason = ClosureReason::CommitmentTxConfirmed;
+ check_closed_event(&nodes[1], 1, b_reason, &[node_a_id], 1000000);
+ check_closed_broadcast(&nodes[1], 1, true);
+ check_added_monitors(&nodes[1], 1);
+
+ // Now have A try to open a new channel with B, reusing the closed channel's ID as the
+ // `temporary_channel_id`. B should reject it as it may still need to update the closed
+ // channel's `ChannelMonitor`.
+ nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, Some(chan_a.2), None).unwrap();
+ let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
+ nodes[1].node.handle_open_channel(node_a_id, &open_channel);
+ assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+ let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
+ assert_eq!(msg_events.len(), 1);
+ match &msg_events[0] {
+ MessageSendEvent::HandleError {
+ action: msgs::ErrorAction::SendErrorMessage { msg },
+ node_id,
+ } => {
+ assert_eq!(*node_id, node_a_id);
+ assert_eq!(msg.channel_id, chan_a.2);
+ },
+ _ => panic!("Unexpected event"),
+ }
+
+ // Still, have B itself open a new (never funded) channel to A under the closed channel's ID
+ // so that an unfunded channel sits in `channel_by_id` under that ID.
+ nodes[1].node.create_channel(node_a_id, 100_000, 0, 42, Some(chan_a.2), None).unwrap();
+ get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, node_a_id);
+
+ // Claim the payment on C and give B the preimage for it. B must write the preimage to the
+ // closed channel A's `ChannelMonitor` (rather than doing nothing because it found the
+ // unfunded channel under the same ID), and release the `Event::PaymentForwarded` once it
+ // has.
+ nodes[2].node.claim_funds(payment_preimage, Default::default());
+ check_added_monitors(&nodes[2], 1);
+ expect_payment_claimed!(nodes[2], payment_hash, 1_000_000);
+
+ let mut updates = get_htlc_update_msgs(&nodes[2], &node_b_id);
+ nodes[1].node.handle_update_fulfill_htlc(node_c_id, updates.update_fulfill_htlcs.remove(0));
+ check_added_monitors(&nodes[1], 1);
+ assert!(get_monitor!(nodes[1], chan_a.2).get_stored_preimages().contains_key(&payment_hash));
+ expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], None, true, false);
+
+ // With the preimage durably in channel A's `ChannelMonitor` the RAA for channel B is no
+ // longer blocked and we can let the preimage go from channel B's `ChannelMonitor`.
+ do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, false, false);
+ assert!(!get_monitor!(nodes[1], chan_b.2)
+ .get_all_current_outbound_htlcs()
+ .iter()
+ .any(|(_, (_, preimage))| *preimage == Some(payment_preimage)));
+}
+
#[test]
fn test_claim_to_closed_channel_blocks_claimed_event() {
// One of the last features for async persistence we implemented was the correct blocking of
### lightning/src/ln/channelmanager.rs
@@ -10853,8 +10853,14 @@ impl<
};
},
}
+ return;
}
- return;
+ // If the entry is an unfunded channel it cannot be the one the HTLC was received
+ // on. Rather, the channel was closed and a new, unfunded channel has since been
+ // opened under the same ID (e.g. we opened a channel ourselves reusing the closed
+ // channel's ID as its `temporary_channel_id`; the set of `ChannelMonitor`s is
+ // what prevents duplicates on-chain). Fall through and claim the HTLC against the
+ // closed channel's `ChannelMonitor` below.
}
}
@@ -12500,7 +12506,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
}
let channel_id = common_fields.temporary_channel_id;
- let channel_exists = peer_state.has_channel(&channel_id);
+ // Closed channels which may still need `ChannelMonitorUpdate`s (e.g. to claim HTLCs) are
+ // tracked by their ID as well, so don't let a new channel share an ID with one of those.
+ let channel_exists = peer_state.has_channel(&channel_id)
+ || peer_state.closed_channel_monitor_update_ids.contains_key(&channel_id);
if channel_exists {
return Err(MsgHandleErrInternal::send_err_msg_no_close(
"temporary_channel_id collision for the same peer!".to_owned(),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.