From ad50dfa8976f4ca2a19b1db9d9e3c1956a8022fc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 17:46:03 -0800 Subject: [PATCH 1/5] EXR: decode a flat part of a file that also holds deep parts decode_part refused every part of a file with any deep part, and decode_display_part, which checked only the selected part, still failed because the exr crate's image reader validates every header and refuses deep ones before it reads a chunk. Both now decide from the selected part's own header. When any part is deep, the selected flat part is read chunk by chunk: the flat headers are validated, every part's offset table is walked from the end of the headers, and only the selected table's chunks are read. A chunk must start past the last offset table and inside the file, carry the selected part's number and hold a block no other chunk of the part holds; anything else is Malformed. Files with only flat parts keep the crate's reader. The file-size ratio check, which was skipped for every part when any part was deep, now applies to the flat parts of such a file; deep parts are not counted. --- crates/exr/src/decode.rs | 145 +++++++++++++++++------ crates/exr/src/decode_tests.rs | 203 ++++++++++++++++++++++++++++++++- 2 files changed, 313 insertions(+), 35 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index ab21064ce..31b2bce09 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -7,7 +7,7 @@ //! - [`decode_part`] decodes the channels of one flat part that a filter keeps, in their stored //! sample type, over the part's data window. Every compression the `exr` crate reads is read, //! scan lines and tiles alike (the largest resolution level). Other parts' chunks are skipped -//! unread, and channels the filter drops are never allocated. +//! unread, and channels the filter drops are never allocated. Other parts may be deep. //! - [`write`] writes one scan-line part. //! //! The `exr` crate sizes buffers from headers before it reads pixels, so everything that reaches @@ -18,7 +18,7 @@ use std::io::Cursor; use exr::block::UncompressedBlock; -use exr::block::chunk::TileCoordinates; +use exr::block::chunk::{Chunk, TileCoordinates}; use exr::image::read::image::ReadImage; use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels}; use exr::meta::MetaData; @@ -248,7 +248,7 @@ fn window_size(size: exr::math::Vec2) -> (u32, u32) { fn read_meta(bytes: &[u8], limits: &Limits) -> Result { check_previews(bytes)?; let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(map_exr)?; - let (mut total, mut dwa) = (0u64, 0u64); + let (mut total, mut flat, mut dwa) = (0u64, 0u64, 0u64); for header in meta.headers.iter() { let (w, h) = window_size(header.layer_size); if w > limits.max_width || h > limits.max_height { @@ -260,23 +260,24 @@ fn read_meta(bytes: &[u8], limits: &Limits) -> Result { } let bytes = pixels.saturating_mul(header.channels.list.len().max(1) as u64 * 4); total = total.saturating_add(bytes); - if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) { - dwa = dwa.saturating_add(bytes); + if !header.deep { + flat = flat.saturating_add(bytes); + if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) { + dwa = dwa.saturating_add(bytes); + } } } if total > limits.max_bytes { return Err(Error::LimitExceeded(format!("{total} bytes across {} parts exceed {}", meta.headers.len(), limits.max_bytes))); } - // The crate allocates a flat part's samples before reading its chunks, so the file must be - // able to hold them. 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 the - // file's own counts. - if !meta.requirements.has_deep_data { - let declared = total.saturating_sub(dwa).saturating_add(dwa / 4); - let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20); - if declared > capacity { - return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len()))); - } + // 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 + // 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); + if declared > capacity { + return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len()))); } Ok(meta) } @@ -459,19 +460,24 @@ pub fn decode_display_part( } let block_limit = std::cell::Cell::new(None); let select = DisplaySelect { index, keep, fill, limits, seen: std::cell::Cell::new(0), block_limit: &block_limit }; - let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}) - .from_buffered(Cursor::new(bytes)) - .map_err(|error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error)))?; - let channels = image - .layer_data - .into_iter() - .nth(index) - .ok_or_else(|| malformed("the part was not read"))? - .channel_data - .into_iter() - .flatten() - .map(|(name, samples)| DisplayChannel { name, samples }) - .collect(); + let exr_error = |error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error)); + let planes = if meta.headers.iter().any(|header| header.deep) { + let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; + select.seen.set(index); + let mut reader = select.create_channels_reader(header).map_err(exr_error)?; + read_flat_part_chunks(bytes, &meta, index, &mut reader)?; + reader.into_channels() + } else { + ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}) + .from_buffered(Cursor::new(bytes)) + .map_err(exr_error)? + .layer_data + .into_iter() + .nth(index) + .ok_or_else(|| malformed("the part was not read"))? + .channel_data + }; + let channels = planes.into_iter().flatten().map(|(name, samples)| DisplayChannel { name, samples }).collect(); Ok(DisplayPart { info, channels }) } @@ -727,22 +733,93 @@ impl ChannelsReader for SelectReader { } } +/// Where the headers end and the offset tables begin: [`MetaData::read_from_buffered`] consumes +/// exactly the version field and the headers. +fn tables_start(bytes: &[u8]) -> Result { + let mut cursor = Cursor::new(bytes); + MetaData::read_from_buffered(&mut cursor, false).map_err(map_exr)?; + Ok(cursor.position()) +} + +/// Reads the chunks of flat part `index` of a file that also holds deep parts into `reader`. +/// +/// The `exr` crate's image reader validates every header before it reads a chunk and refuses +/// deep headers, so here only the flat headers are validated, the offset tables (one `u64` per +/// chunk, a table per part in header order) are walked from the end of the headers, and only the +/// selected part's chunks are read. Each chunk must start past the last offset table and inside +/// the file, carry the selected part's number, and hold a block no other chunk of the part holds. +fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader) -> Result<()> { + let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; + let multipart = meta.requirements.is_multilayer(); + let mut long_names = false; + for flat in meta.headers.iter().filter(|h| !h.deep) { + flat.validate(multipart, &mut long_names, false).map_err(map_exr)?; + } + let truncated = || malformed("the offset tables run past the end of the file"); + let (mut table, mut tables_end) = (0u64, tables_start(bytes)?); + for (i, h) in meta.headers.iter().enumerate() { + if i == index { + table = tables_end; + } + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)).ok_or_else(truncated)?; + tables_end = tables_end.checked_add(len).filter(|end| *end <= bytes.len() as u64).ok_or_else(truncated)?; + } + let offsets = usize::try_from(table) + .ok() + .and_then(|start| bytes.get(start..start.checked_add(header.chunk_count.checked_mul(8)?)?)) + .ok_or_else(truncated)?; + let mut blocks = std::collections::HashSet::new(); + for &entry in offsets.as_chunks::<8>().0 { + let offset = u64::from_le_bytes(entry); + let chunk = usize::try_from(offset) + .ok() + .filter(|_| offset >= tables_end) + .and_then(|start| bytes.get(start..)) + .filter(|rest| !rest.is_empty()) + .ok_or_else(|| malformed(format!("chunk offset {offset} is outside the chunk area of the file")))?; + if multipart { + let part = chunk.get(..4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes); + let part = part.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?; + if usize::try_from(part).ok() != Some(index) { + return Err(malformed(format!("the chunk at offset {offset} belongs to part {part}, not part {index}"))); + } + } + let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?; + let tile = header.get_block_data_indices(&chunk.compressed_block).map_err(map_exr)?; + if !blocks.insert(tile) { + return Err(malformed(format!("two chunks of part {index} hold block {:?} at level {:?}", tile.tile_index, tile.level_index))); + } + if reader.filter_block(tile) { + let block = UncompressedBlock::decompress_chunk(chunk, meta, false).map_err(map_exr)?; + reader.read_block(header, block).map_err(map_exr)?; + } + } + Ok(()) +} + /// Decodes the channels of flat part `index` that `keep` accepts (by the name as stored), in -/// their stored type. A file with deep parts, or a kept channel that is subsampled, is -/// [`Error::Unsupported`]. +/// their stored type. A deep part, or a kept channel that is subsampled, is +/// [`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 { let meta = read_meta(bytes, limits)?; - if meta.requirements.has_deep_data { + 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())); } - let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?; if matches!(header.compression, ExrCompression::HTJ2K32 | ExrCompression::HTJ2K256) { return Err(Error::Unsupported("HTJ2K compression".into())); } let info = part_info(index, header); let select = Select { index, keep, seen: std::cell::Cell::new(0) }; - let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?; - let planes = image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))?; + let planes = if meta.headers.iter().any(|header| header.deep) { + select.seen.set(index); + let mut reader = select.create_channels_reader(header).map_err(map_exr)?; + read_flat_part_chunks(bytes, &meta, index, &mut reader)?; + reader.into_channels() + } else { + let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?; + image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))? + }; let channels = info.channels.iter().zip(planes).filter_map(|(c, p)| Some(Channel { name: c.name.clone(), samples: p? })).collect(); Ok(Part { info, channels }) } diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index 54c28d9aa..feda8fc28 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -1,4 +1,5 @@ -//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate. +//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate, except the +//! files that mix flat and deep parts, which are built by hand. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use exr::meta::attribute::{Preview, Text}; @@ -238,3 +239,203 @@ fn hostile_files_never_panic() { } assert!(parts(b"not exr", &limits).is_err()); } + +// A multi-part file with a flat scan-line part and a deep scan-line part, built by hand per the +// OpenEXR file layout (the `exr` crate's writer refuses deep headers): version 2 with the deep and +// multi-part flags, the headers and an empty header, one offset table per part, then chunks that +// each start with their part number. Both parts are uncompressed, one scan line per chunk. + +fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { + out.extend_from_slice(name.as_bytes()); + out.push(0); + out.extend_from_slice(ty.as_bytes()); + out.push(0); + out.extend_from_slice(&(payload.len() as i32).to_le_bytes()); + out.extend_from_slice(payload); +} + +/// The version field and the headers of a mixed file whose flat part is `w × h` with float +/// `B`, `G`, `R` channels and whose deep part is 3 × 2 with one float `A` channel. +fn mixed_headers(deep_first: bool, (w, h): (usize, usize)) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for deep in if deep_first { [true, false] } else { [false, true] } { + let (w, h, channels): (usize, usize, &[&str]) = if deep { (3, 2, &["A"]) } else { (w, h, &["B", "G", "R"]) }; + let mut chlist = Vec::new(); + for name in channels { + chlist.extend_from_slice(name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&2i32.to_le_bytes()); // float + chlist.extend_from_slice(&[0u8; 4]); // pLinear and reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + let window: Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + mixed_attr(&mut out, "channels", "chlist", &chlist); + mixed_attr(&mut out, "chunkCount", "int", &(h as i32).to_le_bytes()); + mixed_attr(&mut out, "compression", "compression", &[0]); + mixed_attr(&mut out, "dataWindow", "box2i", &window); + mixed_attr(&mut out, "displayWindow", "box2i", &window); + mixed_attr(&mut out, "lineOrder", "lineOrder", &[0]); + if deep { + mixed_attr(&mut out, "maxSamplesPerPixel", "int", &1i32.to_le_bytes()); + } + mixed_attr(&mut out, "name", "string", if deep { b"deep" } else { b"flat" }); + mixed_attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + mixed_attr(&mut out, "screenWindowCenter", "v2f", &[0u8; 8]); + mixed_attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + mixed_attr(&mut out, "type", "string", if deep { b"deepscanline" } else { b"scanlineimage" }); + if deep { + mixed_attr(&mut out, "version", "int", &1i32.to_le_bytes()); + } + out.push(0); // end of this header + } + out.push(0); // end of the header list + out +} + +/// 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 { + channel + 1.0 + i as f32 +} + +/// A mixed file with a 3 × 2 flat part, and its chunk offsets as `offsets[part][line]`. +fn mixed_file(deep_first: bool) -> (Vec, [[usize; 2]; 2]) { + let (w, h) = (3usize, 2usize); + let mut out = mixed_headers(deep_first, (w, h)); + let chunks: Vec>> = (0..2i32) + .map(|part| { + let deep = (part == 0) == deep_first; + (0..h) + .map(|y| { + let mut c = Vec::new(); + c.extend_from_slice(&part.to_le_bytes()); + c.extend_from_slice(&(y as i32).to_le_bytes()); + if deep { + // Packed count table, packed and unpacked sample data: one sample a pixel. + for size in [w * 4, w * 4, w * 4] { + c.extend_from_slice(&(size as u64).to_le_bytes()); + } + (1..=w as u32).for_each(|n| c.extend_from_slice(&n.to_le_bytes())); + (0..w).for_each(|_| c.extend_from_slice(&0.5f32.to_le_bytes())); + } else { + c.extend_from_slice(&((w * 3 * 4) as i32).to_le_bytes()); + for channel in [100.0, 10.0, 0.0] { + (0..w).for_each(|x| c.extend_from_slice(&mixed_flat(channel, y * w + x).to_le_bytes())); + } + } + c + }) + .collect() + }) + .collect(); + let mut offsets = [[0usize; 2]; 2]; + let mut at = out.len() + 2 * h * 8; + for (part, lines) in chunks.iter().enumerate() { + for (y, c) in lines.iter().enumerate() { + offsets[part][y] = at; + out.extend_from_slice(&(at as u64).to_le_bytes()); + at += c.len(); + } + } + chunks.into_iter().flatten().for_each(|c| out.extend_from_slice(&c)); + (out, offsets) +} + +#[test] +fn a_flat_part_beside_a_deep_part_decodes() { + let limits = Limits::default(); + for deep_first in [false, true] { + let (bytes, _) = mixed_file(deep_first); + let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; + let parts = parts(&bytes, &limits).unwrap(); + assert_eq!((parts[flat].deep, parts[deep].deep, parts[flat].name.as_deref()), (false, true, Some("flat"))); + + let part = decode_part(&bytes, flat, &all, &limits).unwrap(); + for (name, channel) in [("R", 0.0), ("G", 10.0), ("B", 100.0)] { + let expected: Vec = (0..6).map(|i| mixed_flat(channel, i)).collect(); + assert_eq!(part.channel(name).unwrap().samples, Samples::F32(expected), "deep first {deep_first} {name}"); + } + let only_g = decode_part(&bytes, flat, &|n| n == "G", &limits).unwrap(); + assert_eq!(only_g.channels.iter().map(|c| c.name.as_str()).collect::>(), ["G"]); + + let display = decode_display_part(&bytes, flat, &|n| n == "flat.R", &|_| 9.0, &limits).unwrap(); + assert_eq!(display.channels.len(), 1); + assert_eq!((display.channels[0].name.as_str(), display.channels[0].samples.as_slice()), ("flat.R", &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0][..])); + + assert!(matches!(decode_part(&bytes, deep, &all, &limits), Err(Error::Unsupported(_)))); + assert!(matches!(decode_display_part(&bytes, deep, &all, &|_| 0.0, &limits), Err(Error::Unsupported(_)))); + assert!(matches!(decode_part(&bytes, 2, &all, &limits), Err(Error::Malformed(_)))); + let deep_image = crate::deep::decode_deep(&bytes, &limits).unwrap(); + assert_eq!((deep_image.width, deep_image.height), (3, 2)); + } +} + +#[test] +fn a_flat_part_s_chunks_beside_a_deep_part_are_checked_before_they_are_read() { + let limits = Limits::default(); + for deep_first in [false, true] { + let (good, offsets) = mixed_file(deep_first); + let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; + let tables = mixed_headers(deep_first, (3, 2)).len(); + let entry = tables + flat * 2 * 8; + let with_offset = |slot: usize, value: u64| { + let mut b = good.clone(); + b[entry + slot * 8..entry + slot * 8 + 8].copy_from_slice(&value.to_le_bytes()); + b + }; + let refused = |b: &[u8]| { + for r in [decode_part(b, flat, &all, &limits).map(|_| ()), decode_display_part(b, flat, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first}: {r:?}"); + } + }; + // The deep part's chunk, the end of the file and past it, the offset tables, the same + // chunk twice. + refused(&with_offset(0, offsets[deep][0] as u64)); + refused(&with_offset(0, good.len() as u64)); + refused(&with_offset(0, u64::MAX)); + refused(&with_offset(0, (tables + 8) as u64)); + refused(&with_offset(1, offsets[flat][0] as u64)); + // A chunk naming another part, and one declaring more bytes than a block holds. + let mut b = good.clone(); + b[offsets[flat][1]..offsets[flat][1] + 4].copy_from_slice(&(deep as i32).to_le_bytes()); + refused(&b); + let mut b = good.clone(); + b[offsets[flat][0] + 8..offsets[flat][0] + 12].copy_from_slice(&i32::MAX.to_le_bytes()); + refused(&b); + // Offset tables longer than the file. + refused(&good[..tables + 20]); + + for n in 0..good.len() { + let _ = decode_part(&good[..n], flat, &all, &limits); + let _ = decode_display_part(&good[..n], flat, &all, &|_| 0.0, &limits); + } + for i in 0..good.len() { + for v in [0u8, 1, 0x7F, 0xFF] { + let mut b = good.clone(); + b[i] = v; + if let Ok(p) = decode_part(&b, flat, &all, &limits) { + let n = p.info.data_window.width as usize * p.info.data_window.height as usize; + assert!(p.channels.iter().all(|c| c.samples.len() == n)); + } + let _ = decode_display_part(&b, flat, &all, &|_| 0.0, &limits); + } + } + } +} + +#[test] +fn the_flat_parts_of_a_mixed_file_must_fit_the_file() { + // 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a + // ratio of 1 plus the 1 MiB slack, though another part is deep. + let headers = mixed_headers(false, (1024, 1024)); + assert!(parts(&headers, &Limits::default()).is_ok()); + let tight = Limits { max_ratio: 1, ..Limits::default() }; + for r in [parts(&headers, &tight).map(|_| ()), decode_part(&headers, 0, &all, &tight).map(|_| ())] { + assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{r:?}"); + } + // The deep part's samples are not counted, and a small flat part passes the same ratio. + assert!(parts(&mixed_file(false).0, &tight).is_ok()); +} From 53e460515639a404f74d7a5f675dfbac477edc88 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 01:45:31 -0800 Subject: [PATCH 2/5] EXR: a flat part beside a deep part needs a chunk for each of its blocks The chunk reader for a flat part of a file that also holds deep parts refused only a second chunk for the same block. A tiled part's chunk that named a level the part does not have was skipped without decompressing, so the tile it took the place of had no chunk and the part decoded with that tile left at zero. Each chunk now has to hold one of the part's own blocks, taken from the header, that no other chunk holds; anything else is Malformed. Since a part has as many chunks as blocks, every block then has a chunk. The mixed-file tests also run with a tiled flat part, and a new test names a missing level and a repeated tile. --- crates/exr/src/decode.rs | 12 +++-- crates/exr/src/decode_tests.rs | 83 ++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index 31b2bce09..0931ec659 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -747,7 +747,8 @@ fn tables_start(bytes: &[u8]) -> Result { /// deep headers, so here only the flat headers are validated, the offset tables (one `u64` per /// chunk, a table per part in header order) are walked from the end of the headers, and only the /// selected part's chunks are read. Each chunk must start past the last offset table and inside -/// the file, carry the selected part's number, and hold a block no other chunk of the part holds. +/// the file, carry the selected part's number, and hold one of the part's blocks that no other +/// chunk of the part holds, so the part's chunks hold every one of its blocks. fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader) -> Result<()> { let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; let multipart = meta.requirements.is_multilayer(); @@ -768,7 +769,7 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m .ok() .and_then(|start| bytes.get(start..start.checked_add(header.chunk_count.checked_mul(8)?)?)) .ok_or_else(truncated)?; - let mut blocks = std::collections::HashSet::new(); + let mut blocks: std::collections::HashSet = header.blocks_increasing_y_order().map(|block| block.location).collect(); for &entry in offsets.as_chunks::<8>().0 { let offset = u64::from_le_bytes(entry); let chunk = usize::try_from(offset) @@ -786,8 +787,11 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m } let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?; let tile = header.get_block_data_indices(&chunk.compressed_block).map_err(map_exr)?; - if !blocks.insert(tile) { - return Err(malformed(format!("two chunks of part {index} hold block {:?} at level {:?}", tile.tile_index, tile.level_index))); + if !blocks.remove(&tile) { + return Err(malformed(format!( + "part {index} has no free slot for block {:?} at level {:?}: it is outside the part or another chunk holds it", + tile.tile_index, tile.level_index + ))); } if reader.filter_block(tile) { let block = UncompressedBlock::decompress_chunk(chunk, meta, false).map_err(map_exr)?; diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index feda8fc28..2ba556597 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -240,10 +240,11 @@ fn hostile_files_never_panic() { assert!(parts(b"not exr", &limits).is_err()); } -// A multi-part file with a flat scan-line part and a deep scan-line part, built by hand per the -// OpenEXR file layout (the `exr` crate's writer refuses deep headers): version 2 with the deep and -// multi-part flags, the headers and an empty header, one offset table per part, then chunks that -// each start with their part number. Both parts are uncompressed, one scan line per chunk. +// A multi-part file with a flat part and a deep scan-line part, built by hand per the OpenEXR file +// layout (the `exr` crate's writer refuses deep headers): version 2 with the deep and multi-part +// flags, the headers and an empty header, one offset table per part, then chunks that each start +// with their part number. Both parts are uncompressed, two chunks a part: the deep part and a +// scan-line flat part hold one scan line a chunk, and a tiled flat part one 2 × 2 tile a chunk. fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { out.extend_from_slice(name.as_bytes()); @@ -255,8 +256,9 @@ fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { } /// The version field and the headers of a mixed file whose flat part is `w × h` with float -/// `B`, `G`, `R` channels and whose deep part is 3 × 2 with one float `A` channel. -fn mixed_headers(deep_first: bool, (w, h): (usize, usize)) -> Vec { +/// `B`, `G`, `R` channels, in scan lines or (with `tiled`) in one level of 2 × 2 tiles, and whose +/// deep part is 3 × 2 with one float `A` channel. +fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec { let mut out = Vec::new(); out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); @@ -273,8 +275,10 @@ fn mixed_headers(deep_first: bool, (w, h): (usize, usize)) -> Vec { } chlist.push(0); let window: Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + let tiled = tiled && !deep; + let chunks = if tiled { w.div_ceil(2) * h.div_ceil(2) } else { h }; mixed_attr(&mut out, "channels", "chlist", &chlist); - mixed_attr(&mut out, "chunkCount", "int", &(h as i32).to_le_bytes()); + mixed_attr(&mut out, "chunkCount", "int", &(chunks as i32).to_le_bytes()); mixed_attr(&mut out, "compression", "compression", &[0]); mixed_attr(&mut out, "dataWindow", "box2i", &window); mixed_attr(&mut out, "displayWindow", "box2i", &window); @@ -286,7 +290,18 @@ fn mixed_headers(deep_first: bool, (w, h): (usize, usize)) -> Vec { mixed_attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); mixed_attr(&mut out, "screenWindowCenter", "v2f", &[0u8; 8]); mixed_attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); - mixed_attr(&mut out, "type", "string", if deep { b"deepscanline" } else { b"scanlineimage" }); + if tiled { + // 2 × 2 tiles, one level, rounded down. + mixed_attr(&mut out, "tiles", "tiledesc", &[2, 0, 0, 0, 2, 0, 0, 0, 0]); + } + let ty: &[u8] = if deep { + b"deepscanline" + } else if tiled { + b"tiledimage" + } else { + b"scanlineimage" + }; + mixed_attr(&mut out, "type", "string", ty); if deep { mixed_attr(&mut out, "version", "int", &1i32.to_le_bytes()); } @@ -301,10 +316,11 @@ 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][line]`. -fn mixed_file(deep_first: bool) -> (Vec, [[usize; 2]; 2]) { +/// 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, [[usize; 2]; 2]) { let (w, h) = (3usize, 2usize); - let mut out = mixed_headers(deep_first, (w, h)); + let mut out = mixed_headers(deep_first, tiled, (w, h)); let chunks: Vec>> = (0..2i32) .map(|part| { let deep = (part == 0) == deep_first; @@ -312,6 +328,18 @@ fn mixed_file(deep_first: bool) -> (Vec, [[usize; 2]; 2]) { .map(|y| { let mut c = Vec::new(); c.extend_from_slice(&part.to_le_bytes()); + if tiled && !deep { + // Tile (y, 0) at level (0, 0): columns 2y and 2y + 1 that lie inside. + [y as i32, 0, 0, 0].iter().for_each(|v| c.extend_from_slice(&v.to_le_bytes())); + let xs = 2 * y..(2 * y + 2).min(w); + c.extend_from_slice(&((xs.len() * h * 3 * 4) as i32).to_le_bytes()); + for row in 0..h { + for channel in [100.0, 10.0, 0.0] { + xs.clone().for_each(|x| c.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes())); + } + } + return c; + } c.extend_from_slice(&(y as i32).to_le_bytes()); if deep { // Packed count table, packed and unpacked sample data: one sample a pixel. @@ -347,8 +375,8 @@ fn mixed_file(deep_first: bool) -> (Vec, [[usize; 2]; 2]) { #[test] fn a_flat_part_beside_a_deep_part_decodes() { let limits = Limits::default(); - for deep_first in [false, true] { - let (bytes, _) = mixed_file(deep_first); + for (deep_first, tiled) in [(false, false), (true, false), (false, true), (true, true)] { + let (bytes, _) = mixed_file(deep_first, tiled); let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; let parts = parts(&bytes, &limits).unwrap(); assert_eq!((parts[flat].deep, parts[deep].deep, parts[flat].name.as_deref()), (false, true, Some("flat"))); @@ -356,7 +384,7 @@ fn a_flat_part_beside_a_deep_part_decodes() { let part = decode_part(&bytes, flat, &all, &limits).unwrap(); for (name, channel) in [("R", 0.0), ("G", 10.0), ("B", 100.0)] { let expected: Vec = (0..6).map(|i| mixed_flat(channel, i)).collect(); - assert_eq!(part.channel(name).unwrap().samples, Samples::F32(expected), "deep first {deep_first} {name}"); + assert_eq!(part.channel(name).unwrap().samples, Samples::F32(expected), "deep first {deep_first} tiled {tiled} {name}"); } let only_g = decode_part(&bytes, flat, &|n| n == "G", &limits).unwrap(); assert_eq!(only_g.channels.iter().map(|c| c.name.as_str()).collect::>(), ["G"]); @@ -377,9 +405,9 @@ fn a_flat_part_beside_a_deep_part_decodes() { fn a_flat_part_s_chunks_beside_a_deep_part_are_checked_before_they_are_read() { let limits = Limits::default(); for deep_first in [false, true] { - let (good, offsets) = mixed_file(deep_first); + let (good, offsets) = mixed_file(deep_first, false); let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; - let tables = mixed_headers(deep_first, (3, 2)).len(); + let tables = mixed_headers(deep_first, false, (3, 2)).len(); let entry = tables + flat * 2 * 8; let with_offset = |slot: usize, value: u64| { let mut b = good.clone(); @@ -426,16 +454,35 @@ fn a_flat_part_s_chunks_beside_a_deep_part_are_checked_before_they_are_read() { } } +#[test] +fn a_tiled_flat_part_beside_a_deep_part_must_hold_each_of_its_tiles() { + let limits = Limits::default(); + for deep_first in [false, true] { + let (good, offsets) = mixed_file(deep_first, true); + let flat = usize::from(deep_first); + // The second chunk names tile (0, 0) at level (1, 1), which a one-level part does not + // have, or tile (0, 0) again, so tile (1, 0) has no chunk. + for coordinates in [[0i32, 0, 1, 1], [0, 0, 0, 0]] { + let mut b = good.clone(); + let at = offsets[flat][1] + 4; + coordinates.iter().enumerate().for_each(|(i, v)| b[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes())); + for r in [decode_part(&b, flat, &all, &limits).map(|_| ()), decode_display_part(&b, flat, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first} {coordinates:?}: {r:?}"); + } + } + } +} + #[test] fn the_flat_parts_of_a_mixed_file_must_fit_the_file() { // 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a // ratio of 1 plus the 1 MiB slack, though another part is deep. - let headers = mixed_headers(false, (1024, 1024)); + let headers = mixed_headers(false, false, (1024, 1024)); assert!(parts(&headers, &Limits::default()).is_ok()); let tight = Limits { max_ratio: 1, ..Limits::default() }; for r in [parts(&headers, &tight).map(|_| ()), decode_part(&headers, 0, &all, &tight).map(|_| ())] { assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{r:?}"); } // The deep part's samples are not counted, and a small flat part passes the same ratio. - assert!(parts(&mixed_file(false).0, &tight).is_ok()); + assert!(parts(&mixed_file(false, false).0, &tight).is_ok()); } From 2114ab37356e27d84ac74a1d407e1f49dc2989f1 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 06:28:32 -0800 Subject: [PATCH 3/5] EXR: a scan-line chunk beside a deep part must start at its block's first line The block index of a scan-line chunk is its y offset divided by the lines a block holds, rounded toward zero. A chunk whose y was not the first line of a block, or lay up to 15 lines above the data window of a ZIP part, still named a block and was decoded into it. Such a chunk is now Malformed. --- crates/exr/src/decode.rs | 17 +++++++++-- crates/exr/src/decode_tests.rs | 53 ++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index 0931ec659..b903db1cc 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -18,7 +18,7 @@ use std::io::Cursor; use exr::block::UncompressedBlock; -use exr::block::chunk::{Chunk, TileCoordinates}; +use exr::block::chunk::{Chunk, CompressedBlock, TileCoordinates}; use exr::image::read::image::ReadImage; use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels}; use exr::meta::MetaData; @@ -748,7 +748,8 @@ fn tables_start(bytes: &[u8]) -> Result { /// chunk, a table per part in header order) are walked from the end of the headers, and only the /// selected part's chunks are read. Each chunk must start past the last offset table and inside /// the file, carry the selected part's number, and hold one of the part's blocks that no other -/// chunk of the part holds, so the part's chunks hold every one of its blocks. +/// chunk of the part holds, so the part's chunks hold every one of its blocks. A scan-line +/// chunk's y must be the first line of its block. fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader) -> Result<()> { let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; let multipart = meta.requirements.is_multilayer(); @@ -787,6 +788,18 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m } let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?; let tile = header.get_block_data_indices(&chunk.compressed_block).map_err(map_exr)?; + if let CompressedBlock::ScanLine(block) = &chunk.compressed_block { + // The block index is rounded toward zero, so a y that is not the first line of a + // block, or lies just above the data window, would still name a block. + let lines = header.compression.scan_lines_per_block() as i64; + let first = i64::from(header.own_attributes.layer_position.y()) + tile.tile_index.y() as i64 * lines; + if i64::from(block.y_coordinate) != first { + return Err(malformed(format!( + "the scan-line chunk at offset {offset} starts at y {}, which is not the first line of a block", + block.y_coordinate + ))); + } + } if !blocks.remove(&tile) { return Err(malformed(format!( "part {index} has no free slot for block {:?} at level {:?}: it is outside the part or another chunk holds it", diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index 2ba556597..e96ee23f0 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -257,8 +257,14 @@ fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { /// The version field and the headers of a mixed file whose flat part is `w × h` with float /// `B`, `G`, `R` channels, in scan lines or (with `tiled`) in one level of 2 × 2 tiles, and whose -/// deep part is 3 × 2 with one float `A` channel. +/// deep part is 3 × 2 with one float `A` channel. Both parts are uncompressed. fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec { + mixed_headers_compressed(deep_first, tiled, (w, h), 0) +} + +/// [`mixed_headers`] with the flat part's compression code: 0 for none, or 3 for ZIP, which +/// holds 16 scan lines a block. +fn mixed_headers_compressed(deep_first: bool, tiled: bool, (w, h): (usize, usize), compression: u8) -> Vec { let mut out = Vec::new(); out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); @@ -276,10 +282,12 @@ fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); let tiled = tiled && !deep; - let chunks = if tiled { w.div_ceil(2) * h.div_ceil(2) } else { h }; + let compression = if deep { 0 } else { compression }; + let lines = if compression == 3 { 16 } else { 1 }; + let chunks = if tiled { w.div_ceil(2) * h.div_ceil(2) } else { h.div_ceil(lines) }; mixed_attr(&mut out, "channels", "chlist", &chlist); mixed_attr(&mut out, "chunkCount", "int", &(chunks as i32).to_le_bytes()); - mixed_attr(&mut out, "compression", "compression", &[0]); + mixed_attr(&mut out, "compression", "compression", &[compression]); mixed_attr(&mut out, "dataWindow", "box2i", &window); mixed_attr(&mut out, "displayWindow", "box2i", &window); mixed_attr(&mut out, "lineOrder", "lineOrder", &[0]); @@ -473,6 +481,45 @@ fn a_tiled_flat_part_beside_a_deep_part_must_hold_each_of_its_tiles() { } } +#[test] +fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() { + // A ZIP flat part (16 lines a block) holds its 3 × 2 pixels in one chunk, stored raw, as a + // block is when compressing it does not make it smaller; the deep part follows. + let limits = Limits::default(); + let (w, h) = (3usize, 2usize); + let (plain, offsets) = mixed_file(false, false); + let deep = &plain[offsets[1][0]..]; + let with_y = |y: i32| { + let mut out = mixed_headers_compressed(false, false, (w, h), 3); + let mut chunk = Vec::new(); + chunk.extend_from_slice(&0i32.to_le_bytes()); + chunk.extend_from_slice(&y.to_le_bytes()); + chunk.extend_from_slice(&((w * h * 3 * 4) as i32).to_le_bytes()); + for row in 0..h { + for channel in [100.0, 10.0, 0.0] { + (0..w).for_each(|x| chunk.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes())); + } + } + let flat_at = out.len() + 3 * 8; + let deep_at = flat_at + chunk.len(); + for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { + out.extend_from_slice(&(at as u64).to_le_bytes()); + } + out.extend_from_slice(&chunk); + out.extend_from_slice(deep); + out + }; + let part = decode_part(&with_y(0), 0, &all, &limits).unwrap(); + assert_eq!(part.channel("R").unwrap().samples, Samples::F32((0..6).map(|i| mixed_flat(0.0, i)).collect())); + // Each of these y values rounds to block 0, but none is that block's first line. + for y in [-15, -1, 1, 15] { + let b = with_y(y); + for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "y {y}: {r:?}"); + } + } +} + #[test] fn the_flat_parts_of_a_mixed_file_must_fit_the_file() { // 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a From f3b4f73121cf1ff535c7bf4190956214385830ed Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 06:36:43 -0800 Subject: [PATCH 4/5] EXR: a block too large to address is LimitExceeded instead of a panic The exr crate bounds a flat chunk by a block's pixels times the bytes a pixel takes, an unchecked product, before it reads the chunk. A 1 x 1 part with five float channels in 10^9 x 10^9 tiles passed every size limit, and decode_part and decode_display_part then panicked with an overflow, for files of flat parts only and for mixed files alike. read_meta now computes the product with checked arithmetic for each flat part and returns LimitExceeded when it does not fit in a usize. --- crates/exr/src/decode.rs | 12 ++++++- crates/exr/src/decode_tests.rs | 58 ++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index b903db1cc..d141accc6 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -249,7 +249,17 @@ fn read_meta(bytes: &[u8], limits: &Limits) -> Result { check_previews(bytes)?; let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(map_exr)?; let (mut total, mut flat, mut dwa) = (0u64, 0u64, 0u64); - for header in meta.headers.iter() { + for (index, header) in meta.headers.iter().enumerate() { + // The crate sizes a flat chunk's bound as a block's pixels times the bytes a pixel takes, + // without checking the product, before it reads the chunk. + let block_pixels = match header.blocks { + exr::meta::BlockDescription::Tiles(tiles) => (tiles.tile_size.0 as u64).checked_mul(tiles.tile_size.1 as u64), + exr::meta::BlockDescription::ScanLines => (header.compression.scan_lines_per_block() as u64).checked_mul(header.layer_size.0 as u64), + }; + let block_bytes = block_pixels.and_then(|n| n.checked_mul(header.channels.bytes_per_pixel as u64)).and_then(|n| usize::try_from(n).ok()); + if !header.deep && block_bytes.is_none() { + return Err(Error::LimitExceeded(format!("a block of part {index} holds more bytes than this platform can address"))); + } let (w, h) = window_size(header.layer_size); if w > limits.max_width || h > limits.max_height { return Err(Error::LimitExceeded(format!("a {w}x{h} part exceeds {}x{}", limits.max_width, limits.max_height))); diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index e96ee23f0..f4842d262 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -259,17 +259,17 @@ fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { /// `B`, `G`, `R` channels, in scan lines or (with `tiled`) in one level of 2 × 2 tiles, and whose /// deep part is 3 × 2 with one float `A` channel. Both parts are uncompressed. fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec { - mixed_headers_compressed(deep_first, tiled, (w, h), 0) + mixed_headers_with(deep_first, tiled, (w, h), 0, &["B", "G", "R"], 2) } -/// [`mixed_headers`] with the flat part's compression code: 0 for none, or 3 for ZIP, which -/// holds 16 scan lines a block. -fn mixed_headers_compressed(deep_first: bool, tiled: bool, (w, h): (usize, usize), compression: u8) -> Vec { +/// [`mixed_headers`] with the flat part's compression code (0 for none, or 3 for ZIP, which +/// holds 16 scan lines a block), its float channels, and the width and height of its tiles. +fn mixed_headers_with(deep_first: bool, tiled: bool, (w, h): (usize, usize), compression: u8, flat_channels: &[&str], tile: u32) -> Vec { let mut out = Vec::new(); out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); for deep in if deep_first { [true, false] } else { [false, true] } { - let (w, h, channels): (usize, usize, &[&str]) = if deep { (3, 2, &["A"]) } else { (w, h, &["B", "G", "R"]) }; + let (w, h, channels): (usize, usize, &[&str]) = if deep { (3, 2, &["A"]) } else { (w, h, flat_channels) }; let mut chlist = Vec::new(); for name in channels { chlist.extend_from_slice(name.as_bytes()); @@ -284,7 +284,7 @@ fn mixed_headers_compressed(deep_first: bool, tiled: bool, (w, h): (usize, usize let tiled = tiled && !deep; let compression = if deep { 0 } else { compression }; let lines = if compression == 3 { 16 } else { 1 }; - let chunks = if tiled { w.div_ceil(2) * h.div_ceil(2) } else { h.div_ceil(lines) }; + let chunks = if tiled { w.div_ceil(tile as usize) * h.div_ceil(tile as usize) } else { h.div_ceil(lines) }; mixed_attr(&mut out, "channels", "chlist", &chlist); mixed_attr(&mut out, "chunkCount", "int", &(chunks as i32).to_le_bytes()); mixed_attr(&mut out, "compression", "compression", &[compression]); @@ -299,8 +299,10 @@ fn mixed_headers_compressed(deep_first: bool, tiled: bool, (w, h): (usize, usize mixed_attr(&mut out, "screenWindowCenter", "v2f", &[0u8; 8]); mixed_attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); if tiled { - // 2 × 2 tiles, one level, rounded down. - mixed_attr(&mut out, "tiles", "tiledesc", &[2, 0, 0, 0, 2, 0, 0, 0, 0]); + // `tile` × `tile` tiles, one level, rounded down. + let mut desc: Vec = [tile, tile].iter().flat_map(|v| v.to_le_bytes()).collect(); + desc.push(0); + mixed_attr(&mut out, "tiles", "tiledesc", &desc); } let ty: &[u8] = if deep { b"deepscanline" @@ -490,7 +492,7 @@ fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() { let (plain, offsets) = mixed_file(false, false); let deep = &plain[offsets[1][0]..]; let with_y = |y: i32| { - let mut out = mixed_headers_compressed(false, false, (w, h), 3); + let mut out = mixed_headers_with(false, false, (w, h), 3, &["B", "G", "R"], 2); let mut chunk = Vec::new(); chunk.extend_from_slice(&0i32.to_le_bytes()); chunk.extend_from_slice(&y.to_le_bytes()); @@ -520,6 +522,44 @@ fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() { } } +#[test] +fn a_tile_too_large_to_address_is_refused_before_a_chunk_is_read() { + // Five float channels in 10^9 × 10^9 tiles: 20 bytes a pixel times 10^18 pixels does not fit + // in a 64-bit size, though the 1 × 1 part passes every size limit. + let limits = Limits::default(); + let channels = ["A", "B", "C", "D", "E"]; + let tile = 1_000_000_000u32; + + let (plain, offsets) = mixed_file(false, false); + let mut mixed = mixed_headers_with(false, true, (1, 1), 0, &channels, tile); + let mut chunk = Vec::new(); + [0i32, 0, 0, 0, 0, 20].iter().for_each(|v| chunk.extend_from_slice(&v.to_le_bytes())); + channels.iter().for_each(|_| chunk.extend_from_slice(&1f32.to_le_bytes())); + let flat_at = mixed.len() + 3 * 8; + let deep_at = flat_at + chunk.len(); + for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { + mixed.extend_from_slice(&(at as u64).to_le_bytes()); + } + mixed.extend_from_slice(&chunk); + mixed.extend_from_slice(&plain[offsets[1][0]..]); + + // A file of flat parts only, read by the crate's own reader, with its 4 × 4 tiles widened. + let mut flat_only = file(vec![layer(None, 1, 1, &channels, true, exr::prelude::Compression::Uncompressed)], (1, 1)); + let at = flat_only.windows(9).position(|w| w == b"tiledesc\0").unwrap() + 9 + 4; + [tile, tile].iter().enumerate().for_each(|(i, v)| flat_only[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes())); + + for (name, b) in [("mixed", &mixed), ("flat only", &flat_only)] { + let results = [ + parts(b, &limits).map(|_| ()), + decode_part(b, 0, &all, &limits).map(|_| ()), + decode_display_part(b, 0, &all, &|_| 0.0, &limits).map(|_| ()), + ]; + for r in results { + assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{name}: {r:?}"); + } + } +} + #[test] fn the_flat_parts_of_a_mixed_file_must_fit_the_file() { // 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a From 2c555128169a2137c60ad2373aacf061bc6e38e9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 06:42:38 -0800 Subject: [PATCH 5/5] EXR: a chunk beside a deep part must fit the rest of the file The exr crate allocates a chunk's declared payload, up to the size of a block, before it reads it. In a mixed file whose flat part has large tiles, a chunk declaring i32::MAX bytes made a 2 GiB allocation from a file of a few hundred bytes. read_flat_part_chunks now reads the declared length first and returns Malformed when it is longer than the bytes left in the file. --- crates/exr/src/decode.rs | 14 ++++++++++- crates/exr/src/decode_tests.rs | 46 ++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index d141accc6..315b54989 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -759,7 +759,8 @@ fn tables_start(bytes: &[u8]) -> Result { /// selected part's chunks are read. Each chunk must start past the last offset table and inside /// the file, carry the selected part's number, and hold one of the part's blocks that no other /// chunk of the part holds, so the part's chunks hold every one of its blocks. A scan-line -/// chunk's y must be the first line of its block. +/// chunk's y must be the first line of its block, and no chunk may declare more bytes than the +/// file has left after it. fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader) -> Result<()> { let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; let multipart = meta.requirements.is_multilayer(); @@ -796,6 +797,17 @@ fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &m return Err(malformed(format!("the chunk at offset {offset} belongs to part {part}, not part {index}"))); } } + // The crate allocates a chunk's declared payload before it reads it, so the payload must + // fit in the rest of the file. The length follows the part number and the block's y, or + // its four tile coordinates. + let fields = if matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)) { 16 } else { 4 }; + let size_at = if multipart { 4 } else { 0 } + fields; + let size = chunk.get(size_at..size_at + 4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes); + let size = size.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?; + let left = chunk.len() - (size_at + 4); + if usize::try_from(size).is_ok_and(|size| size > left) { + return Err(malformed(format!("the chunk at offset {offset} declares {size} bytes, but {left} remain in the file"))); + } let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?; let tile = header.get_block_data_indices(&chunk.compressed_block).map_err(map_exr)?; if let CompressedBlock::ScanLine(block) = &chunk.compressed_block { diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index f4842d262..71896814f 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -522,6 +522,38 @@ fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() { } } +/// A mixed file whose flat part, first, is one pixel of these float channels (each 1.0) in one +/// `tile` × `tile` tile, held by a chunk that declares `size` payload bytes. +fn mixed_one_tile(channels: &[&str], tile: u32, size: i32) -> Vec { + let (plain, offsets) = mixed_file(false, false); + let mut out = mixed_headers_with(false, true, (1, 1), 0, channels, tile); + let mut chunk = Vec::new(); + [0i32, 0, 0, 0, 0, size].iter().for_each(|v| chunk.extend_from_slice(&v.to_le_bytes())); + channels.iter().for_each(|_| chunk.extend_from_slice(&1f32.to_le_bytes())); + let flat_at = out.len() + 3 * 8; + let deep_at = flat_at + chunk.len(); + for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { + out.extend_from_slice(&(at as u64).to_le_bytes()); + } + out.extend_from_slice(&chunk); + out.extend_from_slice(&plain[offsets[1][0]..]); + out +} + +#[test] +fn a_chunk_beside_a_deep_part_must_fit_the_rest_of_the_file() { + // 2^15 × 2^15 tiles of three floats let a chunk hold up to 12 GiB, so the crate would + // allocate the 2 GiB this chunk declares before it found the file too short. + let limits = Limits::default(); + let channels = ["B", "G", "R"]; + let good = mixed_one_tile(&channels, 1 << 15, 12); + assert_eq!(decode_part(&good, 0, &all, &limits).unwrap().channel("R").unwrap().samples, Samples::F32(vec![1.0])); + let b = mixed_one_tile(&channels, 1 << 15, i32::MAX); + for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(m)) if m.contains("remain in the file")), "{r:?}"); + } +} + #[test] fn a_tile_too_large_to_address_is_refused_before_a_chunk_is_read() { // Five float channels in 10^9 × 10^9 tiles: 20 bytes a pixel times 10^18 pixels does not fit @@ -529,19 +561,7 @@ fn a_tile_too_large_to_address_is_refused_before_a_chunk_is_read() { let limits = Limits::default(); let channels = ["A", "B", "C", "D", "E"]; let tile = 1_000_000_000u32; - - let (plain, offsets) = mixed_file(false, false); - let mut mixed = mixed_headers_with(false, true, (1, 1), 0, &channels, tile); - let mut chunk = Vec::new(); - [0i32, 0, 0, 0, 0, 20].iter().for_each(|v| chunk.extend_from_slice(&v.to_le_bytes())); - channels.iter().for_each(|_| chunk.extend_from_slice(&1f32.to_le_bytes())); - let flat_at = mixed.len() + 3 * 8; - let deep_at = flat_at + chunk.len(); - for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { - mixed.extend_from_slice(&(at as u64).to_le_bytes()); - } - mixed.extend_from_slice(&chunk); - mixed.extend_from_slice(&plain[offsets[1][0]..]); + let mixed = mixed_one_tile(&channels, tile, 20); // A file of flat parts only, read by the crate's own reader, with its 4 × 4 tiles widened. let mut flat_only = file(vec![layer(None, 1, 1, &channels, true, exr::prelude::Compression::Uncompressed)], (1, 1));