@@ -9,7 +9,9 @@ use lightning::ln::msgs::{
99 DecodeError , ErrorAction , LightningError , SocketAddress , UnsignedChannelUpdate ,
1010 UnsignedNodeAnnouncement ,
1111} ;
12- use lightning:: routing:: gossip:: { NetworkGraph , NodeAlias , NodeId } ;
12+ use lightning:: routing:: gossip:: {
13+ NetworkGraph , NodeAlias , NodeId , CHAN_COUNT_ESTIMATE , NODE_COUNT_ESTIMATE ,
14+ } ;
1315use lightning:: util:: logger:: Logger ;
1416use lightning:: util:: ser:: { BigSize , FixedLengthReader , Readable } ;
1517use lightning:: { log_debug, log_given_level, log_gossip, log_trace, log_warn} ;
@@ -115,17 +117,27 @@ where
115117 }
116118 } ;
117119
120+ const MAX_NODE_COUNT : u32 = ( NODE_COUNT_ESTIMATE as u32 ) * 10 ;
121+ const MAX_CHANNEL_COUNT : u64 = ( CHAN_COUNT_ESTIMATE as u64 ) * 10 ;
122+
118123 let node_id_count: u32 = Readable :: read ( read_cursor) ?;
124+ if node_id_count > MAX_NODE_COUNT {
125+ return Err ( LightningError {
126+ err : "RGS data contained nonsense number of nodes to update" . to_owned ( ) ,
127+ action : ErrorAction :: IgnoreError ,
128+ }
129+ . into ( ) ) ;
130+ }
119131 let mut node_ids: Vec < NodeId > = Vec :: with_capacity ( core:: cmp:: min (
120132 node_id_count,
121133 MAX_INITIAL_NODE_ID_VECTOR_CAPACITY ,
122134 ) as usize ) ;
123-
124135 let network_graph = & self . network_graph ;
125136 let mut node_modifications: Vec < UnsignedNodeAnnouncement > = Vec :: new ( ) ;
126137
138+ let read_only_network_graph = network_graph. read_only ( ) ;
139+
127140 if parse_node_details {
128- let read_only_network_graph = network_graph. read_only ( ) ;
129141 for _ in 0 ..node_id_count {
130142 let mut pubkey_bytes = [ 0u8 ; 33 ] ;
131143 read_cursor. read_exact ( & mut pubkey_bytes) ?;
@@ -237,9 +249,12 @@ where
237249 }
238250 }
239251
252+ let original_graph_channel_count = read_only_network_graph. channels ( ) . len ( ) as u32 ;
253+ core:: mem:: drop ( read_only_network_graph) ;
254+
240255 let mut previous_scid: u64 = 0 ;
241256 let announcement_count: u32 = Readable :: read ( read_cursor) ?;
242- for _ in 0 ..announcement_count {
257+ for i in 0 ..announcement_count {
243258 let features = Readable :: read ( read_cursor) ?;
244259
245260 // handle SCID
@@ -284,6 +299,10 @@ where
284299 }
285300 }
286301
302+ if ( original_graph_channel_count as u64 ) + ( i as u64 ) > MAX_CHANNEL_COUNT {
303+ continue ;
304+ }
305+
287306 let announcement_result = network_graph. add_channel_from_partial_announcement (
288307 short_channel_id,
289308 funding_sats,
@@ -329,6 +348,13 @@ where
329348 previous_scid = 0 ;
330349
331350 let update_count: u32 = Readable :: read ( read_cursor) ?;
351+ if update_count as u64 > MAX_CHANNEL_COUNT {
352+ return Err ( LightningError {
353+ err : "RGS data contained nonsense number of channels to update" . to_owned ( ) ,
354+ action : ErrorAction :: IgnoreError ,
355+ }
356+ . into ( ) ) ;
357+ }
332358 log_debug ! ( self . logger, "Processing RGS update from {} with {} nodes, {} channel announcements and {} channel updates." ,
333359 latest_seen_timestamp, node_id_count, announcement_count, update_count) ;
334360 if update_count == 0 {
0 commit comments