From 7ad164ff8a8938965774fc2581e6545404839b6a Mon Sep 17 00:00:00 2001 From: Alex Chambers Date: Sun, 15 Nov 2026 00:26:42 +0100 Subject: [PATCH] craft-heif: refuse a too-large item before copying any of it `Iloc::gather` copied extents until it passed its limit and then returned `TooLarge`, and `scan` charges its byte budget only for items it returns. So 1,024 items, each naming one 64 KiB region 257 times, copied 16 GiB from a 2 MiB file, and `read` up to 1 GiB. Check every extent and sum the lengths first, and copy only when the total fits. The new test counts the bytes copied over the full 1,024-gather scan and `read`'s 64 candidates; it copied 16 GiB before. --- crates/heif/src/items.rs | 39 ++++++++++++++++++----- crates/heif/src/items/tests.rs | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/crates/heif/src/items.rs b/crates/heif/src/items.rs index d00dbdf06..2d86b3027 100644 --- a/crates/heif/src/items.rs +++ b/crates/heif/src/items.rs @@ -589,7 +589,9 @@ impl<'a> Iloc<'a> { } /// An item's bytes, its extents in order from the file (construction method 0) or from the - /// `meta` box's `idat` (method 1), at most `limit` of them. + /// `meta` box's `idat` (method 1), at most `limit` of them. Every extent is checked and the + /// lengths summed before anything is copied, so an item that is too large (say, the same + /// region named by thousands of extents) costs no copying. fn gather(&self, file: &[u8], idat: Option<&[u8]>, place: &Place, limit: usize) -> Gathered { // data reference 0 is this file let source = match (place.data_reference, place.method, idat) { @@ -597,14 +599,25 @@ impl<'a> Iloc<'a> { (0, 1, Some(idat)) => idat, _ => return Gathered::Unreadable, }; - let mut out = Vec::new(); - let mut at = place.extents_at; - for _ in 0..place.extents { - let Some((piece, next)) = self.extent(source, place.base, at) else { return Gathered::Unreadable }; - at = next; - if out.len().saturating_add(piece.len()) > limit { + let pieces = || { + let mut at = place.extents_at; + (0..place.extents).map(move |_| { + let (piece, next) = self.extent(source, place.base, at)?; + at = next; + Some(piece) + }) + }; + let mut total = 0usize; + for piece in pieces() { + let Some(piece) = piece else { return Gathered::Unreadable }; + total = total.saturating_add(piece.len()); + if total > limit { return Gathered::TooLarge; } + } + let mut out = Vec::with_capacity(total); + for piece in pieces().flatten() { + bytes_copied(piece.len()); out.extend_from_slice(piece); } Gathered::Bytes(out) @@ -635,6 +648,18 @@ fn iloc_entry_visited() { ILOC_ENTRIES.with(|n| n.set(n.get().saturating_add(1))); } +#[cfg(test)] +thread_local! { + /// Item bytes copied on this thread, for the tests that bound the work. + static COPIED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg_attr(not(test), allow(unused_variables))] +fn bytes_copied(n: usize) { + #[cfg(test)] + COPIED.with(|c| c.set(c.get().saturating_add(n))); +} + /// A big-endian unsigned field of `size` bytes (0 reads as 0). fn uint(b: &[u8], at: usize, size: usize) -> Option { match size { diff --git a/crates/heif/src/items/tests.rs b/crates/heif/src/items/tests.rs index 49cc9d789..6f73ff6c1 100644 --- a/crates/heif/src/items/tests.rs +++ b/crates/heif/src/items/tests.rs @@ -1128,6 +1128,63 @@ fn iloc_resolved_extents_still_obey_bounds_and_budget() { } } +/// A file whose MAX_SCANNED `Exif` items each name one 64 KiB region 257 times: every item is +/// too large, and finding that out copies nothing, for [`scan`] (all 1,024 gathers) and for +/// [`read`] (its MAX_CANDIDATES). Before, each gather copied MAX_ITEM bytes before giving up, +/// about 16 GiB for the scan of a 2 MiB file. +#[test] +fn too_large_items_are_refused_before_copying() { + const REGION: u32 = 64 << 10; + const REPEATS: u16 = 257; + let count = MAX_SCANNED as u16; + let mut iinf = count.to_be_bytes().to_vec(); + for id in 1..=count { + let mut e = vec![2u8, 0, 0, 0]; + e.extend_from_slice(&id.to_be_bytes()); + e.extend_from_slice(&[0, 0]); + e.extend_from_slice(b"Exif\0"); + iinf.extend(bx(b"infe", &e)); + } + let iloc = |region_at: u32| { + let mut l = vec![0x44, 0x00]; + l.extend_from_slice(&count.to_be_bytes()); + for id in 1..=count { + l.extend_from_slice(&id.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); // method 0, this file + l.extend_from_slice(&REPEATS.to_be_bytes()); + for _ in 0..REPEATS { + l.extend_from_slice(®ion_at.to_be_bytes()); + l.extend_from_slice(®ION.to_be_bytes()); + } + } + full_box(b"iloc", 1, &l) + }; + let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + let meta = |region_at| full_box(b"meta", 0, &[full_box(b"iinf", 0, &iinf), iloc(region_at)].concat()); + let region_at = (ftyp.len() + meta(0).len() + 8) as u32; + let f = [ftyp, meta(region_at), bx(b"mdat", &vec![0; REGION as usize])].concat(); + + COPIED.with(|c| c.set(0)); + let s = scan(&f).unwrap(); + assert!(s.complete()); + let found: Vec = s.collect(); + assert_eq!(found.len(), MAX_SCANNED); + assert!(found.iter().all(|it| it.data == Err(Unreadable::TooLarge))); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "scan copied bytes of items it refused"); + + COPIED.with(|c| c.set(0)); + assert_eq!(read(&f).unwrap(), (None, None)); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "read copied bytes of items it refused"); + + // an item that fits is copied once, extent by extent + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::Idat, 3); + h.iloc = (1, [4, 4, 4, 0]); + let f = h.build(); + COPIED.with(|c| c.set(0)); + assert_eq!(sure(&f), Some(vec![(2, Kind::Exif, exif_item(&[], &SWEEP_TIFF))])); + assert_eq!(COPIED.with(std::cell::Cell::get), SWEEP_TIFF.len() + 4); +} + /// `iloc` is walked once, however many metadata items there are to place (one walk per item /// took over a second on a 48 MB `iloc` with 64 items). #[test]