PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Codecs: bound the TIFF IFD walk by a file-size entry budget (#3683) (#3711)
Valentin Klein476f9838765f
diff --git a/crates/codecs/src/codecs/tiff_ifd.rs b/crates/codecs/src/codecs/tiff_ifd.rsindex 45fe9ee75..199d44ecc 100644--- a/crates/codecs/src/codecs/tiff_ifd.rs+++ b/crates/codecs/src/codecs/tiff_ifd.rs@@ -145,6 +145,14 @@ impl<'a> File<'a> {Some(if self.le { u64::from_le_bytes(s) } else { u64::from_be_bytes(s) })}+ /// The declared entry count of the directory at `at`, before its table is parsed.+ pub fn entry_count(&self, at: u64) -> Option<u64> {+ if at < self.header_len() {+ return None;+ }+ if self.big { self.u64_at(at) } else { self.u16_at(at).map(u64::from) }+ }+/// The directory at `at`: its whole table, up to and including the next-IFD pointer, must/// be inside the file and after the header.pub fn ifd(&self, at: u64) -> Option<Ifd> {@@ -355,6 +363,10 @@ struct Walk<'a, 'f> {seen: HashSet<u64>,pages: Vec<TiffPage>,complete: bool,+ /// Directory entries that may still be parsed. Real directory tables are disjoint, so their+ /// total size is bounded by the file; overlapping tables (a hostile file) would otherwise+ /// make every one of thousands of directories cost a full 65,535-entry parse.+ entry_budget: u64,}impl Walk<'_, '_> {@@ -366,6 +378,15 @@ impl Walk<'_, '_> {self.complete = false;return;}+ let Some(n) = self.f.entry_count(at) else {+ self.complete = false;+ return;+ };+ if n > self.entry_budget {+ self.complete = false;+ return;+ }+ self.entry_budget -= n;let Some(ifd) = self.f.ifd(at) else {self.complete = false;return;@@ -391,10 +412,37 @@ pub fn tiff_info(bytes: &[u8]) -> Result<TiffInfo, CodecError> {}pub(crate) fn info_of(f: &File<'_>) -> Result<TiffInfo, CodecError> {- let mut w = Walk { f, seen: HashSet::new(), pages: Vec::new(), complete: true };+ let mut w = Walk { f, seen: HashSet::new(), pages: Vec::new(), complete: true, entry_budget: (f.b.len() as u64) / 12 + 64 };w.chain(f.first_ifd, None, 0);if w.pages.is_empty() {return Err(CodecError::malformed(Format::Tiff, "the first image directory is missing or cut off"));}Ok(TiffInfo { big_tiff: f.big, little_endian: f.le, pages: w.pages, complete: w.complete })}++#[cfg(test)]+mod tests {+ use super::*;++ /// 10,000 directories 4 bytes apart, each declaring 65,535 overlapping entries: the walk must+ /// stop after the file-size entry budget instead of parsing ~6.5e8 entries.+ #[test]+ fn overlapping_directory_tables_exhaust_the_entry_budget() {+ let first = 8usize;+ let table = 2 + 65_535 * 12 + 4;+ let mut b = vec![0u8; first + table + 4 * 10_000];+ b[..8].copy_from_slice(&[b'I', b'I', 42, 0, first as u8, 0, 0, 0]);+ for i in 0..10_000usize {+ let at = first + 4 * i;+ b[at..at + 2].copy_from_slice(&u16::MAX.to_le_bytes());+ let next_cell = at + 2 + 65_535 * 12;+ let next = (first + 4 * (i + 1)) as u32;+ if let Some(cell) = b.get_mut(next_cell..next_cell + 4) {+ cell.copy_from_slice(&next.to_le_bytes());+ }+ }+ let info = tiff_info(&b).expect("the first directory is readable");+ assert!(!info.complete);+ assert!(info.pages.len() < 200, "{} directories parsed", info.pages.len());+ }+}