Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

security(dng): bound tile decoding and IFD traversal (#2952)

Earl Co539c746058fe
diff --git a/crates/dng/src/raw.rs b/crates/dng/src/raw.rs
index b1c448ffc..4e69e0944 100644
--- a/crates/dng/src/raw.rs
+++ b/crates/dng/src/raw.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<RawImage> {
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<Result<Vec<u16>>> = chunks.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect();
- #[cfg(not(feature = "threads"))]
- let decoded: Vec<Result<Vec<u16>>> = 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 {
diff --git a/crates/dng/src/tiff.rs b/crates/dng/src/tiff.rs
index ba479649c..1e8e79c42 100644
--- a/crates/dng/src/tiff.rs
+++ b/crates/dng/src/tiff.rs
@@ -5,12 +5,16 @@
//! outside the file is an error when it is read, the walk visits at most [`MAX_IFDS`] directories,
//! never the same one twice (cyclic chains end), and goes at most [`MAX_DEPTH`] SubIFD levels deep.
+use std::collections::HashSet;
+
use crate::{Error, Result};
/// Most IFDs one file may have (main chain and SubIFDs together).
pub const MAX_IFDS: usize = 64;
/// Deepest SubIFD nesting followed.
pub const MAX_DEPTH: usize = 4;
+/// Invalid SubIFD edges count too: otherwise a file can make failed offset probes quadratic.
+const MAX_IFD_ATTEMPTS: usize = MAX_IFDS * 16;
/// TIFF field types (TIFF 6.0 §2, plus type 13 IFD from TIFF Technical Note 1).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -156,18 +160,22 @@ impl<'a> Tiff<'a> {
/// each. Bounded by [`MAX_IFDS`] and [`MAX_DEPTH`]; a directory reached twice ends that branch.
pub fn all_ifds(&self, first: u64) -> Result<Vec<Ifd>> {
let mut out: Vec<Ifd> = Vec::new();
- let mut seen: Vec<u64> = Vec::new();
+ let mut seen = HashSet::new();
+ let mut attempts = 0usize;
let mut stack: Vec<(u64, usize)> = vec![(first, 0)];
while let Some((off, depth)) = stack.pop() {
let mut next = off;
while next != 0 {
- if seen.contains(&next) {
+ if !seen.insert(next) {
break;
}
+ attempts += 1;
+ if attempts > MAX_IFD_ATTEMPTS {
+ return Err(Error::Malformed("too many IFD offsets"));
+ }
if out.len() >= MAX_IFDS {
return Err(Error::Malformed("too many IFDs"));
}
- seen.push(next);
let (ifd, after) = match self.read_ifd(next, depth) {
Ok(x) => x,
// a broken directory ends the chain; the first one must be readable