Skip to content

Commit 1c7dd91

Browse files
committed
Reject channel_update messages with timestamps too old or new
Because we time out channel info that is older than two weeks now, we should also reject new channel info that is older than two weeks, in addition to rejecting future channel info.
1 parent 8576574 commit 1c7dd91

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,13 +1130,19 @@ impl NetworkGraph {
11301130
/// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
11311131
/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
11321132
/// routing messages from a source using a protocol other than the lightning P2P protocol.
1133+
///
1134+
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
1135+
/// materially in the future will be rejected.
11331136
pub fn update_channel<T: secp256k1::Verification>(&self, msg: &msgs::ChannelUpdate, secp_ctx: &Secp256k1<T>) -> Result<(), LightningError> {
11341137
self.update_channel_intern(&msg.contents, Some(&msg), Some((&msg.signature, secp_ctx)))
11351138
}
11361139

11371140
/// For an already known (from announcement) channel, update info about one of the directions
11381141
/// of the channel without verifying the associated signatures. Because we aren't given the
11391142
/// associated signatures here we cannot relay the channel update to any of our peers.
1143+
///
1144+
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
1145+
/// materially in the future will be rejected.
11401146
pub fn update_channel_unsigned(&self, msg: &msgs::UnsignedChannelUpdate) -> Result<(), LightningError> {
11411147
self.update_channel_intern(msg, None, None::<(&secp256k1::Signature, &Secp256k1<secp256k1::VerifyOnly>)>)
11421148
}
@@ -1146,6 +1152,19 @@ impl NetworkGraph {
11461152
let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
11471153
let chan_was_enabled;
11481154

1155+
#[cfg(all(feature = "std", not(test), not(feature = "_test_utils")))]
1156+
{
1157+
// Note that many tests rely on being able to set arbitrarily old timestamps, thus we
1158+
// disable this check during tests!
1159+
let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs();
1160+
if (msg.timestamp as u64) < time - STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS {
1161+
return Err(LightningError{err: "channel_update is older than two weeks old".to_owned(), action: ErrorAction::IgnoreError});
1162+
}
1163+
if msg.timestamp as u64 > time + 60 * 60 * 24 {
1164+
return Err(LightningError{err: "channel_update is older than two weeks old".to_owned(), action: ErrorAction::IgnoreError});
1165+
}
1166+
}
1167+
11491168
let mut channels = self.channels.write().unwrap();
11501169
match channels.get_mut(&msg.short_channel_id) {
11511170
None => return Err(LightningError{err: "Couldn't find channel for update".to_owned(), action: ErrorAction::IgnoreError}),

0 commit comments

Comments
 (0)