@@ -40,6 +40,10 @@ use std::collections::btree_map::Entry as BtreeEntry;
4040use std:: ops:: Deref ;
4141use bitcoin:: hashes:: hex:: ToHex ;
4242
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+
4347/// Represents the network as nodes and channels between them
4448#[ derive( PartialEq ) ]
4549pub struct NetworkGraph {
@@ -139,13 +143,15 @@ macro_rules! secp_verify_sig {
139143impl < C : Deref + Sync + Send , L : Deref + Sync + Send > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
140144 fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
141145 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 )
143149 }
144150
145151 fn handle_channel_announcement ( & self , msg : & msgs:: ChannelAnnouncement ) -> Result < bool , LightningError > {
146152 self . network_graph . write ( ) . unwrap ( ) . update_channel_from_announcement ( msg, & self . chain_access , & self . secp_ctx ) ?;
147153 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 )
149155 }
150156
151157 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
164170
165171 fn handle_channel_update ( & self , msg : & msgs:: ChannelUpdate ) -> Result < bool , LightningError > {
166172 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 )
168174 }
169175
170176 fn get_next_channel_announcements ( & self , starting_point : u64 , batch_amount : u8 ) -> Vec < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
@@ -680,7 +686,10 @@ impl NetworkGraph {
680686 }
681687 }
682688
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 ;
684693 node. announcement_info = Some ( NodeAnnouncementInfo {
685694 features : msg. features . clone ( ) ,
686695 last_update : msg. timestamp ,
@@ -773,7 +782,8 @@ impl NetworkGraph {
773782 node_two : msg. node_id_2 . clone ( ) ,
774783 two_to_one : None ,
775784 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 } ,
777787 } ;
778788
779789 match self . channels . entry ( msg. short_channel_id ) {
@@ -902,7 +912,8 @@ impl NetworkGraph {
902912 chan_was_enabled = false ;
903913 }
904914
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 } ;
906917
907918 let updated_channel_dir_info = DirectionalChannelInfo {
908919 enabled: chan_enabled,
@@ -1002,7 +1013,7 @@ impl NetworkGraph {
10021013mod tests {
10031014 use chain;
10041015 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 } ;
10061017 use ln:: msgs:: { Init , OptionalField , RoutingMessageHandler , UnsignedNodeAnnouncement , NodeAnnouncement ,
10071018 UnsignedChannelAnnouncement , ChannelAnnouncement , UnsignedChannelUpdate , ChannelUpdate , HTLCFailChannelUpdate ,
10081019 ReplyChannelRange , ReplyShortChannelIdsEnd , QueryChannelRange , QueryShortChannelIds , MAX_VALUE_MSAT } ;
@@ -1124,7 +1135,7 @@ mod tests {
11241135 } ;
11251136
11261137 unsigned_announcement. timestamp += 1000 ;
1127- unsigned_announcement. excess_data . push ( 1 ) ;
1138+ unsigned_announcement. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
11281139 msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
11291140 let announcement_with_data = NodeAnnouncement {
11301141 signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1292,7 +1303,7 @@ mod tests {
12921303
12931304 // Don't relay valid channels with excess data
12941305 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 ) ;
12961307 msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
12971308 let valid_announcement = ChannelAnnouncement {
12981309 node_signature_1 : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1422,7 +1433,7 @@ mod tests {
14221433 }
14231434
14241435 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 ) ;
14261437 let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
14271438 let valid_channel_update = ChannelUpdate {
14281439 signature : secp_ctx. sign ( & msghash, node_1_privkey) ,
@@ -1722,7 +1733,7 @@ mod tests {
17221733 htlc_maximum_msat : OptionalField :: Absent ,
17231734 fee_base_msat : 10000 ,
17241735 fee_proportional_millionths : 20 ,
1725- excess_data : [ 1 ; 3 ] . to_vec ( )
1736+ excess_data : [ 1 ; MAX_EXCESS_BYTES_FOR_RELAY + 1 ] . to_vec ( )
17261737 } ;
17271738 let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_channel_update. encode( ) [ ..] ) [ ..] ) ;
17281739 let valid_channel_update = ChannelUpdate {
@@ -1851,7 +1862,7 @@ mod tests {
18511862 alias : [ 0 ; 32 ] ,
18521863 addresses : Vec :: new ( ) ,
18531864 excess_address_data : Vec :: new ( ) ,
1854- excess_data : [ 1 ; 3 ] . to_vec ( ) ,
1865+ excess_data : [ 1 ; MAX_EXCESS_BYTES_FOR_RELAY + 1 ] . to_vec ( ) ,
18551866 } ;
18561867 let msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
18571868 let valid_announcement = NodeAnnouncement {
0 commit comments