@@ -40,6 +40,10 @@ use std::collections::btree_map::Entry as BtreeEntry;
40
40
use std:: ops:: Deref ;
41
41
use bitcoin:: hashes:: hex:: ToHex ;
42
42
43
+ /// The maximum number of extra bytes which we do not understand in a gossip message before we will
44
+ /// refuse to relay the message.
45
+ const MAX_EXCESS_BYTES_FOR_RELAY : usize = 1024 ;
46
+
43
47
/// Represents the network as nodes and channels between them
44
48
#[ derive( PartialEq ) ]
45
49
pub struct NetworkGraph {
@@ -139,13 +143,15 @@ macro_rules! secp_verify_sig {
139
143
impl < C : Deref + Sync + Send , L : Deref + Sync + Send > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
140
144
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
141
145
self . network_graph . write ( ) . unwrap ( ) . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
142
- Ok ( msg. contents . excess_data . is_empty ( ) && msg. contents . excess_address_data . is_empty ( ) )
146
+ Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
147
+ msg. contents . excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
148
+ msg. contents . excess_data . len ( ) + msg. contents . excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
143
149
}
144
150
145
151
fn handle_channel_announcement ( & self , msg : & msgs:: ChannelAnnouncement ) -> Result < bool , LightningError > {
146
152
self . network_graph . write ( ) . unwrap ( ) . update_channel_from_announcement ( msg, & self . chain_access , & self . secp_ctx ) ?;
147
153
log_trace ! ( self . logger, "Added channel_announcement for {}{}" , msg. contents. short_channel_id, if !msg. contents. excess_data. is_empty( ) { " with excess uninterpreted data!" } else { "" } ) ;
148
- Ok ( msg. contents . excess_data . is_empty ( ) )
154
+ Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
149
155
}
150
156
151
157
fn handle_htlc_fail_channel_update ( & self , update : & msgs:: HTLCFailChannelUpdate ) {
@@ -164,7 +170,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
164
170
165
171
fn handle_channel_update ( & self , msg : & msgs:: ChannelUpdate ) -> Result < bool , LightningError > {
166
172
self . network_graph . write ( ) . unwrap ( ) . update_channel ( msg, & self . secp_ctx ) ?;
167
- Ok ( msg. contents . excess_data . is_empty ( ) )
173
+ Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
168
174
}
169
175
170
176
fn get_next_channel_announcements ( & self , starting_point : u64 , batch_amount : u8 ) -> Vec < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
@@ -680,7 +686,10 @@ impl NetworkGraph {
680
686
}
681
687
}
682
688
683
- let should_relay = msg. excess_data . is_empty ( ) && msg. excess_address_data . is_empty ( ) ;
689
+ let should_relay =
690
+ msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
691
+ msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
692
+ msg. excess_data . len ( ) + msg. excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY ;
684
693
node. announcement_info = Some ( NodeAnnouncementInfo {
685
694
features : msg. features . clone ( ) ,
686
695
last_update : msg. timestamp ,
@@ -773,7 +782,8 @@ impl NetworkGraph {
773
782
node_two : msg. node_id_2 . clone ( ) ,
774
783
two_to_one : None ,
775
784
capacity_sats : utxo_value,
776
- announcement_message : if msg. excess_data . is_empty ( ) { full_msg. cloned ( ) } else { None } ,
785
+ announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
786
+ { full_msg. cloned ( ) } else { None } ,
777
787
} ;
778
788
779
789
match self . channels . entry ( msg. short_channel_id ) {
@@ -902,7 +912,8 @@ impl NetworkGraph {
902
912
chan_was_enabled = false ;
903
913
}
904
914
905
- let last_update_message = if msg. excess_data. is_empty( ) { full_msg. cloned( ) } else { None } ;
915
+ let last_update_message = if msg. excess_data. len( ) <= MAX_EXCESS_BYTES_FOR_RELAY
916
+ { full_msg. cloned( ) } else { None } ;
906
917
907
918
let updated_channel_dir_info = DirectionalChannelInfo {
908
919
enabled: chan_enabled,
@@ -1002,7 +1013,7 @@ impl NetworkGraph {
1002
1013
mod tests {
1003
1014
use chain;
1004
1015
use ln:: features:: { ChannelFeatures , InitFeatures , NodeFeatures } ;
1005
- use routing:: network_graph:: { NetGraphMsgHandler , NetworkGraph } ;
1016
+ use routing:: network_graph:: { NetGraphMsgHandler , NetworkGraph , MAX_EXCESS_BYTES_FOR_RELAY } ;
1006
1017
use ln:: msgs:: { Init , OptionalField , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
1007
1018
UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate , HTLCFailChannelUpdate ,
1008
1019
ReplyChannelRange , ReplyShortChannelIdsEnd , QueryChannelRange , QueryShortChannelIds , MAX_VALUE_MSAT } ;
@@ -1124,7 +1135,7 @@ mod tests {
1124
1135
} ;
1125
1136
1126
1137
unsigned_announcement. timestamp += 1000 ;
1127
- unsigned_announcement. excess_data . push ( 1 ) ;
1138
+ unsigned_announcement. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
1128
1139
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1129
1140
let announcement_with_data = NodeAnnouncement {
1130
1141
signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1292,7 +1303,7 @@ mod tests {
1292
1303
1293
1304
// Don't relay valid channels with excess data
1294
1305
unsigned_announcement. short_channel_id += 1 ;
1295
- unsigned_announcement. excess_data . push ( 1 ) ;
1306
+ unsigned_announcement. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
1296
1307
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1297
1308
let valid_announcement = ChannelAnnouncement {
1298
1309
node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1422,7 +1433,7 @@ mod tests {
1422
1433
}
1423
1434
1424
1435
unsigned_channel_update. timestamp += 100 ;
1425
- unsigned_channel_update. excess_data . push ( 1 ) ;
1436
+ unsigned_channel_update. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
1426
1437
let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
1427
1438
let valid_channel_update = ChannelUpdate {
1428
1439
signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1722,7 +1733,7 @@ mod tests {
1722
1733
htlc_maximum_msat : OptionalField :: Absent ,
1723
1734
fee_base_msat : 10000 ,
1724
1735
fee_proportional_millionths : 20 ,
1725
- excess_data : [ 1 ; 3 ] . to_vec ( )
1736
+ excess_data : [ 1 ; MAX_EXCESS_BYTES_FOR_RELAY + 1 ] . to_vec ( )
1726
1737
} ;
1727
1738
let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
1728
1739
let valid_channel_update = ChannelUpdate {
@@ -1851,7 +1862,7 @@ mod tests {
1851
1862
alias : [ 0 ; 32 ] ,
1852
1863
addresses : Vec :: new ( ) ,
1853
1864
excess_address_data : Vec :: new ( ) ,
1854
- excess_data : [ 1 ; 3 ] . to_vec ( ) ,
1865
+ excess_data : [ 1 ; MAX_EXCESS_BYTES_FOR_RELAY + 1 ] . to_vec ( ) ,
1855
1866
} ;
1856
1867
let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
1857
1868
let valid_announcement = NodeAnnouncement {
0 commit comments