lnwallet: refine revocation validation order
What changed, and why it matters
This change tightens the order of checks when LND processes a revocation message from the other party in a Lightning channel. Previously, the code could add a secret to its revocation store before confirming the revocation was actually expected and matched the right commitment point. Now it checks those conditions first, so a malformed or unexpected revocation cannot partially update local state. The patch is defensive hardening rather than a clear fix for an active exploit.
Review the change as a defensive security improvement. No immediate incident response is indicated, but operators should upgrade to a version containing this commit to reduce the risk of state corruption from malformed or unexpected revocation messages. Continue monitoring for related disclosures from Lightning Labs.
Security signals we found
State-transition validation moved before persistent revocation bookkeeping
New explicit error for revocation received in invalid commitment state
Wrong-secret revocation now rejected before RevocationStore is mutated
Tests added for rejected revocation cases in tweakless and taproot channels
Defensive hardening of revocation processing order
Evidence from the diff
ReceiveRevocation in lnwallet/channel.go now validates that the remote commitment chain has an un-acked commitment (hasUnackedCommitment) and that the derived commitment point matches RemoteCurrentRevocation before adding the revocation secret to the RevocationStore. A new sentinel error ErrUnexpectedRevocation is returned when no un-acked commitment exists. Tests were added for both the unexpected-state and wrong-secret cases across tweakless and taproot channels, verifying that revocation points and the revocation store remain unchanged on rejection and that a subsequent valid transition still succeeds.
Changed components
lnwallet/channel.go: ReceiveRevocationlnwallet/channel_test.goInspect captured patch +198 / −12
### lnwallet/channel.go
@@ -132,6 +132,11 @@ var (
ErrForceCloseLocalDataLoss = errors.New("cannot force close " +
"channel with local data loss")
+ // ErrUnexpectedRevocation is returned when a revocation cannot be
+ // applied in the current commitment state.
+ ErrUnexpectedRevocation = errors.New("received revocation in " +
+ "invalid commitment state")
+
// errNoNonce is returned when a nonce is required, but none is found.
errNoNonce = errors.New("no nonce found")
@@ -5919,10 +5924,8 @@ func extractRevokeAndAckNonce(revMsg *lnwire.RevokeAndAck,
}
// ReceiveRevocation processes a revocation sent by the remote party for the
-// lowest unrevoked commitment within their commitment chain. We receive a
-// revocation either during the initial session negotiation wherein revocation
-// windows are extended, or in response to a state update that we initiate. If
-// successful, then the remote commitment chain is advanced by a single
+// lowest unrevoked commitment within their commitment chain. If validation
+// succeeds, then the remote commitment chain is advanced by a single
// commitment, and a log compaction is attempted.
//
// The returned values correspond to:
@@ -5936,25 +5939,33 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) (
lc.Lock()
defer lc.Unlock()
- // Ensure that the new pre-image can be placed in preimage store.
- store := lc.channelState.RevocationStore
- revocation, err := chainhash.NewHash(revMsg.Revocation[:])
- if err != nil {
- return nil, nil, err
- }
- if err := store.AddNextEntry(revocation); err != nil {
- return nil, nil, err
+ // Ensure the remote commitment chain can advance before updating any
+ // revocation state.
+ if !lc.commitChains.Remote.hasUnackedCommitment() {
+ return nil, nil, ErrUnexpectedRevocation
}
// Verify that if we use the commitment point computed based off of the
// revealed secret to derive a revocation key with our revocation base
// point, then it matches the current revocation of the remote party.
+ // We check this before adding the secret to the store, so a revocation
+ // for the wrong point leaves our state untouched.
currentCommitPoint := lc.channelState.RemoteCurrentRevocation
derivedCommitPoint := input.ComputeCommitmentPoint(revMsg.Revocation[:])
if !derivedCommitPoint.IsEqual(currentCommitPoint) {
return nil, nil, fmt.Errorf("revocation key mismatch")
}
+ // Ensure that the new pre-image can be placed in preimage store.
+ store := lc.channelState.RevocationStore
+ revocation, err := chainhash.NewHash(revMsg.Revocation[:])
+ if err != nil {
+ return nil, nil, err
+ }
+ if err := store.AddNextEntry(revocation); err != nil {
+ return nil, nil, err
+ }
+
// Now that we've verified that the prior commitment has been properly
// revoked, we'll advance the revocation state we track for the remote
// party: the new current revocation is what was previously the next
### lnwallet/channel_test.go
@@ -6106,6 +6106,181 @@ func TestSignCommitmentFailNotLockedIn(t *testing.T) {
}
}
+// TestReceiveRevocationInvalidState tests that rejected revocations leave the
+// channel state unchanged and don't prevent a later valid transition.
+func TestReceiveRevocationInvalidState(t *testing.T) {
+ t.Parallel()
+
+ testCases := []struct {
+ name string
+ chanType channeldb.ChannelType
+
+ // dropNextPoint clears Alice's copy of Bob's next revocation
+ // point.
+ dropNextPoint bool
+ }{
+ {
+ name: "tweakless",
+ chanType: channeldb.SingleFunderTweaklessBit,
+ },
+ {
+ name: "tweakless no next point",
+ chanType: channeldb.SingleFunderTweaklessBit,
+ dropNextPoint: true,
+ },
+ {
+ name: "taproot",
+ chanType: channeldb.SimpleTaprootFeatureBit |
+ channeldb.SingleFunderTweaklessBit |
+ channeldb.AnchorOutputsBit,
+ },
+ {
+ name: "taproot no next point",
+ chanType: channeldb.SimpleTaprootFeatureBit |
+ channeldb.SingleFunderTweaklessBit |
+ channeldb.AnchorOutputsBit,
+ dropNextPoint: true,
+ },
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ aliceChannel, bobChannel, err := CreateTestChannels(
+ t, tc.chanType,
+ )
+ require.NoError(t, err)
+
+ aliceState := aliceChannel.channelState
+ if tc.dropNextPoint {
+ aliceState.RemoteNextRevocation = nil
+ }
+
+ // Generate a revocation message for the
+ // current commitment state.
+ bobHeight := bobChannel.commitChains.Local.tail().height
+ revMsg, err := bobChannel.generateRevocation(bobHeight)
+ require.NoError(t, err)
+
+ curPoint := aliceState.RemoteCurrentRevocation
+ nextPoint := aliceState.RemoteNextRevocation
+
+ _, _, err = aliceChannel.ReceiveRevocation(revMsg)
+ require.ErrorIs(t, err, ErrUnexpectedRevocation)
+
+ // The rejected transition must leave the
+ // revocation state unchanged.
+ require.Equal(
+ t, curPoint, aliceState.RemoteCurrentRevocation,
+ )
+ require.Equal(
+ t, nextPoint, aliceState.RemoteNextRevocation,
+ )
+ _, err = aliceState.RevocationStore.LookUp(bobHeight)
+ require.Error(t, err)
+
+ if tc.dropNextPoint {
+ return
+ }
+
+ // A normal state transition should still
+ // succeed afterwards.
+ require.NoError(
+ t, ForceStateTransition(
+ aliceChannel, bobChannel,
+ ),
+ )
+ _, err = aliceState.RevocationStore.LookUp(bobHeight)
+ require.NoError(t, err)
+ })
+ }
+}
+
+// TestReceiveRevocationWrongSecret tests that a revocation for the wrong
+// commitment point is rejected before the remote party's revocation state is
+// modified, and that the correct revocation can still be processed afterwards.
+func TestReceiveRevocationWrongSecret(t *testing.T) {
+ t.Parallel()
+
+ testCases := []struct {
+ name string
+ chanType channeldb.ChannelType
+ }{
+ {
+ name: "tweakless",
+ chanType: channeldb.SingleFunderTweaklessBit,
+ },
+ {
+ name: "taproot",
+ chanType: channeldb.SimpleTaprootFeatureBit |
+ channeldb.SingleFunderTweaklessBit |
+ channeldb.AnchorOutputsBit,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ aliceChannel, bobChannel, err := CreateTestChannels(
+ t, tc.chanType,
+ )
+ require.NoError(t, err)
+
+ // Give Alice an outstanding commitment for Bob so the
+ // revocation reaches the secret validation.
+ commitSigs, err := aliceChannel.SignNextCommitment(ctxb)
+ require.NoError(t, err)
+ err = bobChannel.ReceiveNewCommitment(
+ commitSigs.CommitSigs,
+ )
+ require.NoError(t, err)
+
+ bobHeight := bobChannel.commitChains.Local.tail().height
+ bobRevocation, _, _, err := bobChannel.
+ RevokeCurrentCommitment()
+ require.NoError(t, err)
+
+ aliceState := aliceChannel.channelState
+ currentPoint := aliceState.RemoteCurrentRevocation
+ nextPoint := aliceState.RemoteNextRevocation
+
+ // Corrupt the revealed secret so it no longer
+ // derives the remote party's current revocation
+ // point.
+ wrongRevocation := *bobRevocation
+ wrongRevocation.Revocation[0] ^= 1
+
+ _, _, err = aliceChannel.ReceiveRevocation(
+ &wrongRevocation,
+ )
+ require.EqualError(t, err, "revocation key mismatch")
+
+ // The rejected revocation must not advance
+ // either point or enter the bad secret in the
+ // revocation store.
+ require.Equal(
+ t, currentPoint,
+ aliceState.RemoteCurrentRevocation,
+ )
+ require.Equal(
+ t, nextPoint, aliceState.RemoteNextRevocation,
+ )
+ _, err = aliceState.RevocationStore.LookUp(bobHeight)
+ require.Error(t, err)
+
+ // The real revocation should still be accepted
+ // afterwards.
+ _, _, err = aliceChannel.ReceiveRevocation(
+ bobRevocation,
+ )
+ require.NoError(t, err)
+ _, err = aliceState.RevocationStore.LookUp(bobHeight)
+ require.NoError(t, err)
+ })
+ }
+}
+
// TestLockedInHtlcForwardingSkipAfterRestart ensures that after a restart, a
// state machine doesn't attempt to re-forward any HTLC's that were already
// locked in, but in a prior state.Why this scored 59/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.