EXR: read only the channels and rows a frame needs, and a crafted DWA file no longer crashes (#1953)

This commit is contained in:
Valentin Klein authored and GitHub committed 2026-11-16 21:29:31 -05:00
1 parent e7b247c09f
commit 5fd8db24b1
7 files changed
+3266 -5

No files matched your search

+16 -3
View File
@@ -281,8 +281,9 @@ pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData> {
return Err(Error::LimitExceeded(format!("{total} bytes across {} parts exceed {}", meta.headers.len(), limits.max_bytes)));
}
// A flat part's samples are allocated before its chunks are read, so the file must be able
// to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top and reach
// about 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from
// to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top (RLE up to
// 64:1, zlib up to about 1032:1, about 66000:1 together), so DWA parts are counted at a quarter
// (with room to spare). Deep parts size their samples from
// the file's own counts and are not counted here.
let declared = flat.saturating_sub(dwa).saturating_add(dwa / 4);
let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20);
@@ -308,7 +309,7 @@ fn text_attributes(header: &Header) -> Vec<(String, String)> {
out
}
fn part_info(index: usize, header: &Header) -> PartInfo {
pub(crate) fn part_info(index: usize, header: &Header) -> PartInfo {
let (w, h) = window_size(header.layer_size);
let display = &header.shared_attributes.display_window;
let (dw, dh) = window_size(display.size);
@@ -449,6 +450,7 @@ pub fn decode_display_part(
header_limits.max_pixels = u64::MAX;
header_limits.max_bytes = u64::MAX;
let meta = read_meta(bytes, &header_limits)?;
check_dwa_chunks(bytes, &meta)?;
let info = meta.headers.get(index).map(|header| part_info(index, header)).ok_or_else(|| malformed(format!("part {index}")))?;
if info.deep {
return Err(Error::Unsupported("deep data (use the deep reader)".into()));
@@ -756,6 +758,16 @@ fn tables_start(bytes: &[u8]) -> Result<u64> {
Ok(cursor.position())
}
/// Checks the chunks of the file's DWAA/DWAB parts before the `exr` crate decompresses them (see
/// [`crate::dwa_guard`]: a few bytes of counters can make it allocate terabytes and abort).
fn check_dwa_chunks(bytes: &[u8], meta: &MetaData) -> Result<()> {
if !meta.headers.iter().any(|h| crate::dwa_guard::is_dwa(h.compression)) {
return Ok(());
}
let tables = usize::try_from(tables_start(bytes)?).map_err(|_| malformed("header size"))?;
crate::dwa_guard::check_file(bytes, meta, tables).map_err(malformed)
}
/// Stable dense ordinal without materializing every tile. The library's block iterator
/// internally allocates a Vec of all coordinates, which is unsuitable for cropped reads.
pub(super) fn flat_block_ordinal(header: &Header, tile: TileCoordinates) -> Result<usize> {
@@ -944,6 +956,7 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m
/// [`Error::Unsupported`]; the file's other parts may be deep.
pub fn decode_part(bytes: &[u8], index: usize, keep: &dyn Fn(&str) -> bool, limits: &Limits) -> Result<Part> {
let meta = read_meta(bytes, limits)?;
check_dwa_chunks(bytes, &meta)?;
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?;
if header.deep {
return Err(Error::Unsupported("deep data (use the deep reader)".into()));
+2 -2
View File
@@ -354,13 +354,13 @@ fn mixed_headers_with_deep_size(
}
/// Sample `i` of a flat channel: `R` is 1, 2, 3, …, `G` 10 more and `B` 100 more, top row first.
fn mixed_flat(channel: f32, i: usize) -> f32 {
pub(super) fn mixed_flat(channel: f32, i: usize) -> f32 {
channel + 1.0 + i as f32
}
/// A mixed file with a 3 × 2 flat part, and its chunk offsets as `offsets[part][chunk]`. The
/// tiled flat part's chunks hold the tiles at (0, 0) and (1, 0).
fn mixed_file(deep_first: bool, tiled: bool) -> (Vec<u8>, [[usize; 2]; 2]) {
pub(super) fn mixed_file(deep_first: bool, tiled: bool) -> (Vec<u8>, [[usize; 2]; 2]) {
let (w, h) = (3usize, 2usize);
let mut out = mixed_headers(deep_first, tiled, (w, h));
let chunks: Vec<Vec<Vec<u8>>> = (0..2i32)
+161
View File
@@ -0,0 +1,161 @@
//! A check of DWAA/DWAB chunks before the `exr` crate decompresses them.
//!
//! A DWA payload starts with eleven little-endian `u64` counters, which the `exr` crate (1.74.3,
//! `src/compression/dwa/chunk_header.rs`, `DwaHeader::parse`) reads in this order: version,
//! unknown uncompressed size, unknown compressed size, AC compressed size, DC compressed size,
//! RLE compressed size, RLE uncompressed size, RLE raw size, AC count, DC count, AC compression.
//! The crate checks them only against `i64::MAX`, then inflates sections with these sizes as the
//! size hint (`src/compression/dwa/section_stream.rs`: `inflate(section, expected)` with the
//! unknown uncompressed size, `ac_count * 2` for deflated AC, `dc_count * 2`, and the RLE
//! uncompressed size), and its inflater allocates the hint up front. A 100-byte chunk declaring
//! 2^44 DC values makes it allocate 32 TiB: the process aborts, which `catch_unwind` can't stop.
//!
//! What a valid chunk declares is bounded by its block (`width × height` pixels of all the
//! part's channels, `raw` bytes uncompressed):
//! - the unknown (losslessly zipped) channels and the RLE channels are some of the block's
//! channels stored planar, so their uncompressed sizes are at most `raw`;
//! - the RLE token stream adds at most one count byte per 127 literal bytes (runs of three or
//! more shrink), so it stays within `2 × raw`, a bound that leaves room for any encoder;
//! - the lossy channels are cut into 8 × 8 DCT blocks over the block padded to multiples of 8:
//! one DC value and at most 63 AC values per DCT block and channel.
use exr::block::chunk::{CompressedBlock, CompressedScanLineBlock, CompressedTileBlock, TileCoordinates};
use exr::math::Vec2;
use exr::meta::header::Header;
use exr::prelude::Compression;
/// The counters' bytes at the start of a DWA payload.
const COUNTERS: usize = 11 * 8;
/// Whether chunks of `compression` go through this check.
pub(crate) fn is_dwa(compression: Compression) -> bool {
matches!(compression, Compression::DWAA(_) | Compression::DWAB(_))
}
/// What the check needs of a DWA part: the bytes of one pixel of all its channels, and how many
/// channels it has. `None` when the pixel's bytes overflow.
fn shape(header: &Header) -> Option<(usize, usize)> {
let pixel_bytes = header.channels.list.iter().map(|c| c.sample_type.bytes_per_sample()).try_fold(0usize, usize::checked_add)?;
Some((pixel_bytes, header.channels.list.len()))
}
/// Checks the DWA payload `payload` of a block of `width × height` pixels of the part `header`
/// describes: an error message when a counter declares more than a valid chunk of that block
/// can hold. A payload as long as the block's raw bytes is stored uncompressed (the crate doesn't
/// read counters then); one shorter than the counters is left to the crate, which refuses it.
pub(crate) fn check_payload(header: &Header, size: (usize, usize), payload: &[u8]) -> Result<(), String> {
if !is_dwa(header.compression) {
return Ok(());
}
check_shaped(shape(header).ok_or("a DWA pixel size overflows")?, size, payload)
}
/// [`check_payload`] with the part's [`shape`] worked out already.
fn check_shaped((pixel_bytes, channels): (usize, usize), (width, height): (usize, usize), payload: &[u8]) -> Result<(), String> {
let raw = pixel_bytes.checked_mul(width).and_then(|b| b.checked_mul(height)).ok_or("a DWA block size overflows")?;
if payload.len() == raw {
return Ok(());
}
let Some(counters) = payload.get(..COUNTERS) else { return Ok(()) };
let counter = |i: usize| counters.get(i * 8..i * 8 + 8).and_then(|b| <[u8; 8]>::try_from(b).ok()).map_or(u64::MAX, u64::from_le_bytes);
let dct_blocks = width.div_ceil(8).checked_mul(height.div_ceil(8)).and_then(|n| n.checked_mul(channels)).ok_or("a DWA block size overflows")?;
let raw = raw as u64;
let bounds: [(usize, &str, u64); 5] = [
(1, "unknown uncompressed size", raw),
(6, "RLE uncompressed size", raw.saturating_mul(2)),
(7, "RLE raw size", raw),
(8, "AC count", (dct_blocks as u64).saturating_mul(63)),
(9, "DC count", dct_blocks as u64),
];
for (i, name, most) in bounds {
let value = counter(i);
// (also keeps `count * 2` within a 32-bit usize: the bounds are block sizes, which fit)
if value > most || usize::try_from(value.saturating_mul(2)).is_err() {
return Err(format!("a DWA chunk declares a {name} of {value}, more than its {width} × {height} block can hold ({most})"));
}
}
Ok(())
}
/// The pixel size of the block the `exr` crate decompresses a chunk of the part `header`
/// describes into, from the chunk's coordinates: the crate's own placement
/// (`Header::get_block_data_indices`, then `get_absolute_block_pixel_coordinates`), so a scan-line
/// `y` the crate rounds into a block (it divides toward zero: rows just above the data window land
/// in block 0) is sized as that block. An error where the crate can't place the chunk.
fn block_size(header: &Header, block: &CompressedBlock) -> Result<(usize, usize), String> {
if let CompressedBlock::Tile(tile) = block {
// (the crate's level size asserts on levels of 64 and more; no valid file has 32)
if tile.coordinates.level_index.0 >= 32 || tile.coordinates.level_index.1 >= 32 {
return Err("a DWA tile of an impossible level".into());
}
}
let tile = header.get_block_data_indices(block).map_err(|e| format!("a DWA chunk the decoder can't place: {e}"))?;
let bounds = header.get_absolute_block_pixel_coordinates(tile).map_err(|e| format!("a DWA chunk the decoder can't place: {e}"))?;
Ok((bounds.size.0, bounds.size.1))
}
/// Checks every chunk the offset tables point at that is, or names, a flat DWA part (`tables` is
/// where the offset tables start), before the `exr` crate's whole-image reader, which offers no
/// hook between reading a chunk and decompressing it, gets the bytes. It fails closed: a chunk of
/// a DWA part whose part number, coordinates, size or payload can't be read, or whose block can't
/// be placed, is an error, not left to the crate.
pub(crate) fn check_file(bytes: &[u8], meta: &exr::meta::MetaData, tables: usize) -> Result<(), String> {
if !meta.headers.iter().any(|h| !h.deep && is_dwa(h.compression)) {
return Ok(());
}
let multipart = meta.requirements.is_multilayer();
let word = |at: usize| bytes.get(at..at.checked_add(4)?).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes);
let cut = || "a DWA chunk cut off by the end of the file".to_string();
// Per part: its shape when it is a flat DWA part (worked out once, not per chunk).
let shapes: Vec<Option<(usize, usize)>> = meta
.headers
.iter()
.map(|h| if !h.deep && is_dwa(h.compression) { shape(h).ok_or("a DWA pixel size overflows").map(Some) } else { Ok(None) })
.collect::<Result<_, _>>()?;
let mut table = tables;
for (table_part, header) in meta.headers.iter().enumerate() {
let entries =
bytes.get(table..table.saturating_add(header.chunk_count.saturating_mul(8))).ok_or("the offset tables run past the end of the file")?;
table = table.saturating_add(header.chunk_count.saturating_mul(8));
for entry in entries.as_chunks::<8>().0 {
let offset = usize::try_from(u64::from_le_bytes(*entry)).ok();
// The part the crate decodes the chunk as: the one it names (multi-part files).
let (part, mut at) = match (multipart, offset) {
(false, Some(at)) => (Some(table_part), at),
(true, Some(at)) => (word(at).and_then(|p| usize::try_from(p).ok()), at.saturating_add(4)),
(_, None) => (None, 0),
};
let named = part.and_then(|p| shapes.get(p).copied().flatten().map(|s| (p, s)));
let Some((part, shape)) = named else {
// (not a DWA chunk, unless it sits in a DWA part's table and can't be read)
if shapes.get(table_part).copied().flatten().is_some() && part.is_none_or(|p| p >= meta.headers.len()) {
return Err(cut());
}
continue;
};
let header = meta.headers.get(part).ok_or_else(cut)?;
let block = match header.blocks {
exr::meta::BlockDescription::ScanLines => {
let y = word(at).ok_or_else(cut)?;
at = at.saturating_add(4);
CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: Vec::new() })
}
exr::meta::BlockDescription::Tiles(_) => {
let tile = [word(at), word(at.saturating_add(4)), word(at.saturating_add(8)), word(at.saturating_add(12))];
at = at.saturating_add(16);
let [Some(x), Some(y), Some(lx), Some(ly)] = tile else { return Err(cut()) };
let index = |v: i32| usize::try_from(v).map_err(|_| "a DWA tile at a negative position".to_string());
let coordinates = TileCoordinates { tile_index: Vec2(index(x)?, index(y)?), level_index: Vec2(index(lx)?, index(ly)?) };
CompressedBlock::Tile(CompressedTileBlock { coordinates, compressed_pixels_le: Vec::new() })
}
};
let size = block_size(header, &block)?;
let len = word(at).ok_or_else(cut)?;
let len = usize::try_from(len).map_err(|_| "a DWA chunk declaring a negative size".to_string())?;
let start = at.saturating_add(4);
let payload = bytes.get(start..start.saturating_add(len)).ok_or_else(cut)?;
check_shaped(shape, size, payload)?;
}
}
Ok(())
}
+8
View File
@@ -15,6 +15,8 @@
//! - [`decode`]: bounded part listing, flat decoding (scan lines and tiles, every compression the
//! `exr` crate reads) and writing, on top of the `exr` crate.
//! - [`deep`]: deep scan lines and tiles (NONE, RLE, ZIPS) and their flattening.
//! - [`seek`]: any channels, display rows and reduced resolution of a file's flat parts, read from
//! a seekable source and delivered band by band (or collected into planes).
//! - [`stream`]: one flat scan-line part read block by block, for pictures larger than memory.
//! - [`cryptomatte_samples`]: a file's Cryptomatte layers and their per-pixel (ID, coverage) ranks.
@@ -36,4 +38,10 @@ pub mod deep;
#[cfg(all(test, feature = "decode"))]
mod deep_tests;
#[cfg(feature = "decode")]
mod dwa_guard;
#[cfg(feature = "decode")]
pub mod seek;
#[cfg(all(test, feature = "decode"))]
mod seek_tests;
#[cfg(feature = "decode")]
pub mod stream;
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+3
View File
@@ -262,6 +262,9 @@ impl<R: Read + Seek> Decoder<R> {
}
self.source.read_exact(bytes).map_err(invalid)?;
}
// (a DWA chunk's counters can make the crate allocate terabytes: see `crate::dwa_guard`)
let lines = self.rows.min((self.info.height as usize).saturating_sub(row as usize));
crate::dwa_guard::check_payload(header, (self.info.width as usize, lines), &packed).map_err(invalid)?;
let chunk = Chunk {
layer_index: 0,
compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }),