Skip to content

Commit cad1b95

Browse files
committed
discovery: enforce non-zero timestamp in gossip messages
In this commit, we add validation for channel updates and node announcements to ensure that we reject gossip messages with zero timestamps at the discovery layer. From BOLT 7: "MUST set timestamp to greater than 0, AND to greater than any previously-sent channel_update for this short_channel_id." This validation is performed in the gossip handlers (handleNodeAnnouncement and handleChanUpdate) rather than at the wire protocol level. This approach ensures we can still decode messages from disk or embedded in onion errors while rejecting invalid gossip from peers. Remote peers sending zero-timestamp gossip will have their ban score incremented.
1 parent eea4840 commit cad1b95

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

discovery/gossiper.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,22 @@ func (d *AuthenticatedGossiper) handleNodeAnnouncement(ctx context.Context,
25022502
"node=%x, source=%x", nMsg.peer, timestamp, nodeAnn.NodeID,
25032503
nMsg.source.SerializeCompressed())
25042504

2505+
// Although not explicitly required by BOLT 7 for node announcements
2506+
// (unlike channel updates), we still enforce non-zero timestamps as a
2507+
// sanity check. A timestamp of zero is likely indicative of a bug or
2508+
// uninitialized message.
2509+
if nodeAnn.Timestamp == 0 {
2510+
err := fmt.Errorf("rejecting node announcement with zero "+
2511+
"timestamp for node %x", nodeAnn.NodeID)
2512+
2513+
log.Warnf("Rejecting node announcement from peer=%v: %v",
2514+
nMsg.peer, err)
2515+
2516+
nMsg.err <- err
2517+
2518+
return nil, false
2519+
}
2520+
25052521
// We'll quickly ask the router if it already has a newer update for
25062522
// this node so we can skip validating signatures if not required.
25072523
if d.cfg.Graph.IsStaleNode(ctx, nodeAnn.NodeID, timestamp) {
@@ -3056,6 +3072,27 @@ func (d *AuthenticatedGossiper) handleChanUpdate(ctx context.Context,
30563072
// quickly reject it.
30573073
timestamp := time.Unix(int64(upd.Timestamp), 0)
30583074

3075+
// Per BOLT 7, the timestamp MUST be greater than 0.
3076+
if upd.Timestamp == 0 {
3077+
err := fmt.Errorf("rejecting channel update with zero "+
3078+
"timestamp for short_chan_id(%v)", shortChanID)
3079+
3080+
// Only increase ban score for remote peers.
3081+
if nMsg.isRemote {
3082+
log.Warnf("Increasing ban score for peer=%v: %v",
3083+
nMsg.peer, err)
3084+
3085+
dcErr := d.handleBadPeer(nMsg.peer)
3086+
if dcErr != nil {
3087+
err = dcErr
3088+
}
3089+
}
3090+
3091+
nMsg.err <- err
3092+
3093+
return nil, false
3094+
}
3095+
30593096
// Fetch the SCID we should be using to lock the channelMtx and make
30603097
// graph queries with.
30613098
graphScid, err := d.cfg.FindBaseByAlias(upd.ShortChannelID)

discovery/gossiper_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,78 @@ func TestExtraDataNodeAnnouncementValidation(t *testing.T) {
29302930
require.NoError(t, err, "unable to process announcement")
29312931
}
29322932

2933+
// TestZeroTimestampNodeAnnouncementRejection tests that a NodeAnnouncement with
2934+
// a zero timestamp is rejected per BOLT 7.
2935+
func TestZeroTimestampNodeAnnouncementRejection(t *testing.T) {
2936+
t.Parallel()
2937+
ctx := t.Context()
2938+
2939+
tCtx, err := createTestCtx(t, 0, false)
2940+
require.NoError(t, err, "can't create context")
2941+
2942+
remotePeer := &mockPeer{
2943+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
2944+
}
2945+
2946+
// Create a node announcement with a zero timestamp.
2947+
nodeAnn, err := createNodeAnnouncement(remoteKeyPriv1, 0)
2948+
require.NoError(t, err, "can't create node announcement")
2949+
2950+
// Processing the announcement should fail with a zero timestamp error.
2951+
select {
2952+
case err = <-tCtx.gossiper.ProcessRemoteAnnouncement(
2953+
ctx, nodeAnn, remotePeer,
2954+
):
2955+
case <-time.After(2 * time.Second):
2956+
t.Fatal("did not process remote announcement")
2957+
}
2958+
require.Error(t, err)
2959+
require.Contains(t, err.Error(), "zero timestamp")
2960+
}
2961+
2962+
// TestZeroTimestampChannelUpdateRejection tests that a ChannelUpdate with a
2963+
// zero timestamp is rejected per BOLT 7.
2964+
func TestZeroTimestampChannelUpdateRejection(t *testing.T) {
2965+
t.Parallel()
2966+
ctx := t.Context()
2967+
2968+
tCtx, err := createTestCtx(t, 0, false)
2969+
require.NoError(t, err, "can't create context")
2970+
2971+
remotePeer := &mockPeer{
2972+
remoteKeyPriv1.PubKey(), nil, nil, atomic.Bool{},
2973+
}
2974+
2975+
// First, we need to process a channel announcement so that the channel
2976+
// update has a valid channel to refer to.
2977+
chanAnn, err := tCtx.createRemoteChannelAnnouncement(0)
2978+
require.NoError(t, err, "unable to create chan ann")
2979+
2980+
select {
2981+
case err = <-tCtx.gossiper.ProcessRemoteAnnouncement(
2982+
ctx, chanAnn, remotePeer,
2983+
):
2984+
case <-time.After(2 * time.Second):
2985+
t.Fatal("did not process remote announcement")
2986+
}
2987+
require.NoError(t, err, "unable to process chan ann")
2988+
2989+
// Now create a channel update with a zero timestamp.
2990+
chanUpdAnn, err := createUpdateAnnouncement(0, 0, remoteKeyPriv1, 0)
2991+
require.NoError(t, err, "unable to create chan update")
2992+
2993+
// Processing the update should fail with a zero timestamp error.
2994+
select {
2995+
case err = <-tCtx.gossiper.ProcessRemoteAnnouncement(
2996+
ctx, chanUpdAnn, remotePeer,
2997+
):
2998+
case <-time.After(2 * time.Second):
2999+
t.Fatal("did not process remote announcement")
3000+
}
3001+
require.Error(t, err)
3002+
require.Contains(t, err.Error(), "zero timestamp")
3003+
}
3004+
29333005
// assertBroadcast checks that num messages are being broadcasted from the
29343006
// gossiper. The broadcasted messages are returned.
29353007
func assertBroadcast(t *testing.T, ctx *testCtx, num int) []lnwire.Message {

docs/release-notes/release-notes-0.20.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
103103
# Technical and Architectural Updates
104104
## BOLT Spec Updates
105105

106+
* [Enforce non-zero timestamps](https://github.com/lightningnetwork/lnd/pull/10469)
107+
for `channel_update` (as required by BOLT 7) and `node_announcement` messages.
108+
Gossip messages with zero timestamps are now rejected. For `channel_update`
109+
messages, remote peers sending such invalid messages will have their ban score
110+
incremented.
111+
106112
## Testing
107113

108114
## Database

0 commit comments

Comments
 (0)