@@ -217,17 +217,28 @@ struct Ctx<'a> {
217217 ancestors : Vec < u64 > ,
218218 /// IFDs produced so far, counting every copy of a shared one (bounded by `max_ifds`).
219219 visits : usize ,
220- /// IFDs already parsed, by (offset, base): a second pointer to one (two `SubIFDs` entries, or an Exif pointer
221- /// shared by two directories) gets a copy instead of being dropped.
222- parsed : HashMap < ( u64 , u64 ) , ( Ifd , u64 ) > ,
220+ /// IFDs already parsed, by (offset, base, depth): a second pointer to one (two `SubIFDs` entries, or an Exif
221+ /// pointer shared by two directories) gets a copy instead of being dropped. Keyed by depth too, so a copy has
222+ /// exactly the children a fresh parse at that depth would have; a copy is charged the IFDs, entries and value
223+ /// bytes of the first parse, so copies stay inside every limit.
224+ parsed : HashMap < ( u64 , u64 , usize ) , Parsed > ,
223225 budget : u64 ,
224226 entries_total : usize ,
225227}
226228
227- /// The number of IFDs in `ifd`'s tree, itself included.
228- fn tree_size ( ifd : & Ifd ) -> usize {
229- let boxed = [ & ifd. exif , & ifd. gps , & ifd. interop ] . into_iter ( ) . flatten ( ) . map ( |b| tree_size ( b) ) ;
230- ifd. sub_ifds . iter ( ) . map ( tree_size) . chain ( boxed) . fold ( 1usize , usize:: saturating_add)
229+ /// What parsing one IFD and its children used up: a copy of it is charged the same.
230+ #[ derive( Clone , Copy ) ]
231+ struct Cost {
232+ ifds : usize ,
233+ entries : usize ,
234+ bytes : u64 ,
235+ }
236+
237+ /// An IFD parsed once, kept for later pointers to it.
238+ struct Parsed {
239+ ifd : Ifd ,
240+ next : u64 ,
241+ cost : Cost ,
231242}
232243
233244impl < ' a > Ctx < ' a > {
@@ -248,23 +259,40 @@ impl<'a> Ctx<'a> {
248259 if self . ancestors . contains ( & offset) {
249260 return Err ( TiffError :: Invalid ( format ! ( "IFD loop at {offset}" ) ) ) ;
250261 }
251- if let Some ( ( ifd, next) ) = self . parsed . get ( & ( offset, base) ) {
252- let visits = self . visits . saturating_add ( tree_size ( ifd) ) ;
262+ let key = ( offset, base, depth) ;
263+ if let Some ( c) = self . parsed . get ( & key) {
264+ // a copy costs what the first parse did: its IFDs, entries and value bytes
265+ let visits = self . visits . saturating_add ( c. cost . ifds ) ;
253266 if visits > self . opts . max_ifds {
254267 return Err ( TiffError :: Limit ( "IFD count" ) ) ;
255268 }
269+ let entries = self . entries_total . saturating_add ( c. cost . entries ) ;
270+ if entries > self . opts . max_entries_total {
271+ return Err ( TiffError :: Limit ( "entry count" ) ) ;
272+ }
273+ if c. cost . bytes > self . budget {
274+ return Err ( TiffError :: Limit ( "value bytes" ) ) ;
275+ }
256276 self . visits = visits;
257- return Ok ( ( ifd. clone ( ) , * next) ) ;
277+ self . entries_total = entries;
278+ self . budget = self . budget . saturating_sub ( c. cost . bytes ) ;
279+ return Ok ( ( c. ifd . clone ( ) , c. next ) ) ;
258280 }
259281 if self . visits >= self . opts . max_ifds {
260282 return Err ( TiffError :: Limit ( "IFD count" ) ) ;
261283 }
284+ let ( visits0, entries0, budget0) = ( self . visits , self . entries_total , self . budget ) ;
262285 self . visits = self . visits . saturating_add ( 1 ) ;
263286 self . ancestors . push ( offset) ;
264287 let r = self . parse_dir ( offset, base, depth) ;
265288 self . ancestors . pop ( ) ;
266289 let ( ifd, next) = r?;
267- self . parsed . insert ( ( offset, base) , ( ifd. clone ( ) , next) ) ;
290+ let cost = Cost {
291+ ifds : self . visits . saturating_sub ( visits0) ,
292+ entries : self . entries_total . saturating_sub ( entries0) ,
293+ bytes : budget0. saturating_sub ( self . budget ) ,
294+ } ;
295+ self . parsed . insert ( key, Parsed { ifd : ifd. clone ( ) , next, cost } ) ;
268296 Ok ( ( ifd, next) )
269297 }
270298
@@ -480,6 +508,16 @@ mod tests {
480508 assert ! ( t. ifds[ 0 ] . exif. is_none( ) ) ;
481509 }
482510
511+ /// Hostile input: many pointers to one shared IFD must not clone past the entry limit (each child has one
512+ /// entry, so three copies need three entries plus the root's two).
513+ #[ test]
514+ fn shared_children_count_against_the_entry_limit ( ) {
515+ let opts = ParseOptions { max_entries_total : 4 , ..Default :: default ( ) } ;
516+ let t = Tiff :: parse_with ( & shared_children ( 38 , 38 , 38 , 0 ) , & opts) . unwrap ( ) ;
517+ assert_eq ! ( t. ifds[ 0 ] . sub_ifds. len( ) , 2 ) ;
518+ assert ! ( t. ifds[ 0 ] . exif. is_none( ) , "the third copy would pass the entry limit" ) ;
519+ }
520+
483521 #[ test]
484522 fn ascii_keeps_every_nul_separated_string ( ) {
485523 let v = decode ( ByteOrder :: Little , FieldType :: Ascii , b"one\0 two\0 \0 " ) ;
0 commit comments