From 56ac43a7f1f8afe1c217e2c0d5fe0831c291f0d8 Mon Sep 17 00:00:00 2001 From: Earl Co Date: Sat, 24 Oct 2026 08:48:51 +0000 Subject: [PATCH] security(dng): bound tile decoding and IFD traversal (#2952) --- crates/raw/src/dng_lines.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index b1c448ffc..4e69e0944 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -1,14 +1,14 @@ //! The raw image data of a DNG: finding the main raw IFD, and reading its strips or tiles //! (uncompressed or lossless JPEG) into one array of 16-bit samples. -#[cfg(feature = "threads")] -use rayon::prelude::*; - use crate::tiff::{Ifd, Tiff, tags}; use crate::{Error, Result, ljpeg}; /// Largest raw image decoded, in samples (width × height × samples per pixel). pub const MAX_SAMPLES: usize = 1 << 27; +/// Decoded tiles are padded to tile boundaries, so their aggregate can exceed the useful image. +/// Keep that temporary work within the same bound as a complete decoded raw frame. +const MAX_DECODED_TILE_SAMPLES: usize = MAX_SAMPLES; /// Photometric interpretations of DNG raw data. pub const PHOTOMETRIC_CFA: u64 = 32803; @@ -225,23 +225,23 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { if total / 8 > coded { return Err(Error::Truncated); } + let mut decoded_samples = 0usize; for c in &chunks { let n = c.w.checked_mul(c.h).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?; - if n > MAX_SAMPLES { + decoded_samples = decoded_samples.checked_add(n).ok_or(Error::TooLarge)?; + if n > MAX_SAMPLES || decoded_samples > MAX_DECODED_TILE_SAMPLES { return Err(Error::TooLarge); } if n / 8 > c.bytes.len() { return Err(Error::Truncated); } } - #[cfg(feature = "threads")] - let decoded: Vec>> = chunks.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - #[cfg(not(feature = "threads"))] - let decoded: Vec>> = chunks.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); let mut data = vec![0u16; total]; let row_len = l.width * spp; - for (c, d) in chunks.iter().zip(decoded) { - let d = d?; + for c in &chunks { + // Copy each padded tile immediately. Retaining every decoded tile until assembly lets + // repeated ranges multiply memory beyond the final raw frame's bounded allocation. + let d = decode_chunk(c, &l, t.big_endian)?; let cw = c.w.min(l.width - c.x); let ch = c.h.min(l.height - c.y); for r in 0..ch {