From 48ad076ac3a8d073ebde785f4a59cc50ae854db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 16:10:31 +0300 Subject: [PATCH] fix(demux): a container may not state a picture past the resolution bound The class sweep for lane H, and it found one more instance of the same defect one layer down from the `pasp` one: `mkv_tracks` read `PixelWidth` and `PixelHeight` with a bare `ebml_uint(...) as u32`, so a Matroska header could state any picture at all and the engine opened it. `crate::MAX_RESOLUTION` (8K) already existed for exactly this, and was applied to a keystroke (`PlaybackSession::set_resolution`) and to a `.edith` `resolution` line -- but never to a container header. So 65535x65535 opened cleanly, and `decode.rs:1324` then sized its stretched planes as `display_width * height`: 6_442_254_337 bytes for one frame's luma and chroma, before a single pixel is read. The `as u32` narrowing made it worse in the other direction too -- `PixelWidth` of 2^32+1440 truncates to 1440. Both elements are now refused by name, `UnsupportedDisplaySize`, matching the existing `NoVideoTrack` / `UnsupportedRotation` convention (Display impl, Error impl, `is_it`), so the open fails with a message naming the size claimed and the maximum, and a caller can match the type rather than the prose. Witness RED: the patched fixture opened and reported `65535x65535` coded, with the test naming the 6.4 GB allocation `decode.rs` would have sized from it. A companion test holds the other half of the contract -- the ordinary fixture is inside the bound and still opens -- so the bound cannot quietly become a refusal of the library. --- crates/engine/src/demux.rs | 62 ++++++++++++++++++++++++++++- crates/engine/tests/display_unit.rs | 51 +++++++++++++++++++++++- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index 8d7819b..ff5ca88 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -250,6 +250,40 @@ fn mp4_codec(fourcc: &[u8; 4]) -> Option { #[derive(Debug)] pub struct NoVideoTrack(String); +/// A container that states a picture larger than this engine will compose: +/// past [`crate::MAX_RESOLUTION`], where a per-frame buffer stops being a +/// sane thing to allocate. Refused by name, like the other named refusals, +/// because "this file is too big to open" is something a user can act on and +/// a capacity overflow one layer down is not. +#[derive(Debug)] +pub struct UnsupportedDisplaySize(u64); + +impl std::fmt::Display for UnsupportedDisplaySize { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "this file states a {} pixel picture, past the {} pixel maximum", + self.0, + crate::MAX_RESOLUTION + ) + } +} + +impl std::error::Error for UnsupportedDisplaySize {} + +fn unsupported_display_size(stated: u64) -> crate::Error { + UnsupportedDisplaySize(stated).into() +} + +impl UnsupportedDisplaySize { + /// Whether an `error` is one of these, however deep in a `Box` it is -- + /// [`NoVideoTrack::is_it`]'s and [`UnsupportedRotation::is_it`]'s twin, + /// for a caller that shows the refusal differently from the others. + pub fn is_it(error: &crate::Error) -> bool { + error.downcast_ref::().is_some() + } +} + impl std::fmt::Display for NoVideoTrack { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) @@ -2609,8 +2643,32 @@ fn mkv_track_entry( let mut at = body; while let Some(e) = ebml_element(file, at, stop)? { match e.0 { - PIXEL_WIDTH => width = ebml_uint(file, e.1, e.2)? as u32, - PIXEL_HEIGHT => height = ebml_uint(file, e.1, e.2)? as u32, + // A container may state any size at all, and these + // two reach a per-frame allocation directly + // (`decode.rs` sizes its planes as + // `display_width * height`). The engine already has + // the bound for this -- [`crate::MAX_RESOLUTION`], + // 8K -- and it was applied to a keystroke and to a + // `.edith` line but never to a header, so a + // 65535x65535 track opened and asked for a + // gigabyte-scale buffer. A size past the bound is + // refused by name here, where the file can be + // refused, rather than discovered as a capacity + // overflow one layer down. + PIXEL_WIDTH => { + let stated = ebml_uint(file, e.1, e.2)?; + if stated > u64::from(crate::MAX_RESOLUTION) { + return Err(unsupported_display_size(stated)); + } + width = stated as u32; + } + PIXEL_HEIGHT => { + let stated = ebml_uint(file, e.1, e.2)?; + if stated > u64::from(crate::MAX_RESOLUTION) { + return Err(unsupported_display_size(stated)); + } + height = stated as u32; + } DISPLAY_WIDTH => display_width = ebml_uint(file, e.1, e.2)?, DISPLAY_HEIGHT => display_height = ebml_uint(file, e.1, e.2)?, DISPLAY_UNIT => display_unit = ebml_uint(file, e.1, e.2)?, diff --git a/crates/engine/tests/display_unit.rs b/crates/engine/tests/display_unit.rs index 0bedce1..e90133f 100644 --- a/crates/engine/tests/display_unit.rs +++ b/crates/engine/tests/display_unit.rs @@ -215,4 +215,53 @@ fn a_physical_display_unit_is_read_as_square() { meta.pixel_aspect ); } -} \ No newline at end of file +} +/// The coded size is untrusted too, and it reaches an allocation directly: +/// `decode.rs` sizes its stretched planes as `display_width * height`. The +/// engine already has a bound for exactly this -- [`engine::MAX_RESOLUTION`], +/// 8K, the size a per-frame buffer stops being sane at -- and the demux +/// readers never applied it to a container-stated width or height. +/// +/// The fixture patches the `PixelWidth`/`PixelHeight` payloads to 65535 each, +/// which is a legal two-byte EBML integer, so the element keeps its exact +/// length and the copy stays seekable. +#[test] +fn a_coded_size_past_the_resolution_bound_is_refused_not_allocated() { + let scratch = Scratch::new("huge"); + let into = scratch.path("huge_dims.mkv"); + with_video_element( + &asset("test_anamorphic.mkv"), + &into, + &VIDEO_UNIT_3 + .replacen("b08205a0", "b082ffff", 1) + .replacen("ba820438", "ba82ffff", 1), + ); + + // Refused, and refused by name: the open fails with a message that says + // what the file claimed and what the maximum is, rather than succeeding + // and handing a caller a 65535x65535 to allocate. + let Err(why) = Demuxer::open(&into) else { + panic!("a 65535x65535 header is refused by name, never opened"); + }; + let said = why.to_string(); + assert!( + said.contains("65535") && said.contains(&engine::MAX_RESOLUTION.to_string()), + "the refusal names the size claimed and the maximum: {said}" + ); + assert!( + engine::demux::UnsupportedDisplaySize::is_it(&why), + "and it is the named refusal a caller can match on, not a string" + ); +} + +/// The bound is a ceiling on a real picture, not a floor on a file: the +/// largest size a real source states still opens, untouched. +#[test] +fn a_real_size_up_to_the_bound_still_opens() { + let (meta, _) = Demuxer::open(&asset("test_anamorphic.mkv")).expect("open the fixture"); + assert!( + engine::is_resolution(meta.coded_width, meta.coded_height), + "the ordinary fixture is inside the bound, which is what keeps the \ + bound from refusing the library" + ); +}