lightningd: store the revocation secret before acting on its HTLCs
What changed, and why it matters
This commit fixes an ordering bug in Core Lightning's handling of 'revoke_and_ack' messages from a peer. Previously, the node would apply pending HTLC (payment channel) changes—such as forwarding payments or settling/failing them—before verifying and storing the peer's revocation secret. If that secret was invalid, the channel would be closed anyway, but only after the HTLC state had already been altered. The fix moves the revocation-secret verification and storage to happen before any HTLC state changes, preventing a potentially inconsistent or exploitable state.
Treat as a security-relevant correctness fix. Review whether the prior ordering could have led to loss of funds or channel inconsistency in adversarial scenarios, and consider backporting to supported releases. No immediate emergency response is indicated by the diff alone, but a security advisory may be warranted if further analysis confirms exploitability.
Security signals we found
State-machine ordering bug: HTLC effects applied before revocation secret validation
Potential inconsistency if invalid revocation leads to channel failure after HTLC mutation
Fix explicitly moves secret storage before HTLC state changes
Reported by external contributor Erick C (@erickcestari)
Evidence from the diff
In peer_got_revoke(), the code that processes changed HTLC transitions (via peer_accepted_htlc() and changed_htlc()) was executed before the revocation number bounds check, shachain validation (wallet_shachain_add_hash()), and secret storage. If wallet_shachain_add_hash() rejected the secret, channel_internal_error() would close the channel, but only after HTLC state had already been modified. The patch relocates the HTLC-processing loop to occur after the revocation checks and secret storage. The commit message notes crash consistency is preserved because sd_msg_read() wraps each channeld message in a single DB transaction.
Changed components
lightningd/peer_htlcs.cpeer_got_revoke()HTLC revocation handlingInspect captured patch +25 / −24
### lightningd/peer_htlcs.c
@@ -2763,30 +2763,6 @@ void peer_got_revoke(struct channel *channel, const u8 *msg)
"got revoke %"PRIu64": %zu changed",
revokenum, tal_count(changed));
- /* Save any immediate failures for after we reply. */
- badonions = tal_arrz(msg, enum onion_wire, tal_count(changed));
- failmsgs = tal_arrz(msg, u8 *, tal_count(changed));
- for (i = 0; i < tal_count(changed); i++) {
- /* BOLT #2:
- * A node:
- * - until an incoming HTLC has been irrevocably committed:
- * - MUST NOT offer the corresponding outgoing HTLC
- * (`update_add_htlc`) in response to that incoming HTLC.
- */
- /* If we're doing final accept, we need to forward */
- if (changed[i].newstate == RCVD_ADD_ACK_REVOCATION) {
- peer_accepted_htlc(failmsgs,
- channel, changed[i].id, false,
- &badonions[i], &failmsgs[i]);
- } else {
- if (!changed_htlc(channel, &changed[i])) {
- channel_internal_error(channel,
- "got_revoke: update failed");
- return;
- }
- }
- }
-
if (revokenum >= (1ULL << 48)) {
channel_internal_error(channel, "got_revoke: too many txs %"PRIu64,
revokenum);
@@ -2819,6 +2795,31 @@ void peer_got_revoke(struct channel *channel, const u8 *msg)
return;
}
+ /* Store the revocation secret before acting on the HTLCs it
+ * locks in. */
+ badonions = tal_arrz(msg, enum onion_wire, tal_count(changed));
+ failmsgs = tal_arrz(msg, u8 *, tal_count(changed));
+ for (i = 0; i < tal_count(changed); i++) {
+ /* BOLT #2:
+ * A node:
+ * - until an incoming HTLC has been irrevocably committed:
+ * - MUST NOT offer the corresponding outgoing HTLC
+ * (`update_add_htlc`) in response to that incoming HTLC.
+ */
+ /* If we're doing final accept, we need to forward */
+ if (changed[i].newstate == RCVD_ADD_ACK_REVOCATION) {
+ peer_accepted_htlc(failmsgs,
+ channel, changed[i].id, false,
+ &badonions[i], &failmsgs[i]);
+ } else {
+ if (!changed_htlc(channel, &changed[i])) {
+ channel_internal_error(channel,
+ "got_revoke: update failed");
+ return;
+ }
+ }
+ }
+
tal_free(channel->fee_states);
channel->fee_states = tal_steal(channel, fee_states);
Why this scored 63/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.