From c6f42346821504dfd445baa420e265727e3f106d Mon Sep 17 00:00:00 2001 From: Arshiya Rahgozar <57055687+marshiyar@users.noreply.github.com> Date: Sun, 1 Nov 2026 20:15:44 +0000 Subject: [PATCH] dng: bound lossless-JPEG decoding by the data it has and reject shared tile bytes (#3680) * dng: bound lossless-JPEG decoding by the data it has and reject shared tile bytes (#2776) Remaining amplification holes after the decoded-sample cap: the per-chunk decode cap had 256 Ki samples of slack on top of the chunk's size, a patched frame header could ask for far more samples than the entropy-coded bytes can hold (zero bits were fed past the end), and strips/tiles pointing at the same bytes were each decoded in full. ljpeg::decode now fails with Truncated when width*height*components needs more than one bit per sample of the remaining data, the caller caps at exactly the chunk's sample count, and raw::chunks rejects overlapping strip/tile ranges. Tested with a tiny lj92 stream whose SOF is patched to 512x512 and a tiled file whose second tile reuses the first tile's bytes. Fixes #2776 * dng: keep the slack for a lossless JPEG coded larger than its chunk The entropy-length bound already caps the allocation; a last strip coded at full RowsPerStrip holds more samples than the chunk needs and is truncated. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/raw/src/dng_lines.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index 4e69e0944..b69ba967e 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -145,6 +145,7 @@ fn chunks<'a>(t: &Tiff<'a>, ifd: &Ifd, l: &RawLayout) -> Result>> return Err(Error::Malformed("fewer strips or tiles than the image needs")); } let mut out = Vec::with_capacity(n); + let mut ranges = Vec::with_capacity(n); for i in 0..n { let (x, y) = ((i % across) * tw, (i / across) * th); let start = usize::try_from(offs[i]).map_err(|_| Error::Truncated)?; @@ -154,6 +155,14 @@ fn chunks<'a>(t: &Tiff<'a>, ifd: &Ifd, l: &RawLayout) -> Result>> // strips end at the image; tiles are coded whole let h = if ifd.get(tags::TILE_OFFSETS).is_some() { th } else { th.min(l.height - y) }; out.push(Chunk { x, y, w: tw, h, bytes }); + if len != 0 { + ranges.push((start, end)); + } + } + // chunks that share bytes would decode the same data again and again + ranges.sort_unstable(); + if ranges.windows(2).any(|w| w[1].0 < w[0].1) { + return Err(Error::Malformed("strips or tiles overlap")); } Ok(out) } @@ -200,6 +209,8 @@ fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool) -> Result> _ => { // the JPEG's samples fill the chunk in raster order (its own width × components may // differ from the chunk's, e.g. two components of half the width) + // slack for a JPEG coded larger than its chunk (e.g. a last strip at full RowsPerStrip); + // the samples past `need` are dropped below let d = ljpeg::decode(c.bytes, need.saturating_add(65_536 * 4))?; if d.samples.len() < need { return Err(Error::Malformed("lossless JPEG smaller than its tile or strip")); @@ -222,7 +233,7 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { let chunks = chunks(t, ifd, &l)?; // every sample takes at least one bit of file (a Huffman code is at least one bit long) let coded: usize = chunks.iter().map(|c| c.bytes.len()).fold(0usize, usize::saturating_add); - if total / 8 > coded { + if total.div_ceil(8) > coded { return Err(Error::Truncated); } let mut decoded_samples = 0usize; @@ -232,7 +243,7 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { if n > MAX_SAMPLES || decoded_samples > MAX_DECODED_TILE_SAMPLES { return Err(Error::TooLarge); } - if n / 8 > c.bytes.len() { + if n.div_ceil(8) > c.bytes.len() { return Err(Error::Truncated); } }