From fcb7844022311353e56fade700858225e120a9e5 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:20:33 +0300 Subject: [PATCH] fix(demux): the mp4 door gets the resolution bound the Matroska one now has The previous commit bounded `PixelWidth`/`PixelHeight` and left the mp4 arm reading its dimensions unchecked. The mp4 crate types a track's width and height as `u16`, which is a smaller claim than Matroska's EBML integer but still eight times past `MAX_RESOLUTION`: a visual sample entry of `ff ff ff ff` is a legal 65535x65535, and it reaches the same `display_width * height` allocation in `decode.rs`. Both doors now refuse a stated picture past the bound, by the same `UnsupportedDisplaySize`. Witness, with non-vacuity proven: the test patches the first visual sample entry's two 16-bit dimension fields of a copy of `assets/test_baseline.mp4` (offsets 32 and 34 into the entry) to `ff ff` and asserts the open is refused by name. With the bound removed from the source the test fails ("a 65535x65535 mp4 is refused by name, never opened"), so it bites. --- crates/engine/src/demux.rs | 9 +++++ crates/engine/tests/untrusted_header.rs | 46 ++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/demux.rs b/crates/engine/src/demux.rs index ff5ca88..df43017 100644 --- a/crates/engine/src/demux.rs +++ b/crates/engine/src/demux.rs @@ -1008,6 +1008,15 @@ impl Mp4Demuxer { None => Rotation::None, }; let (coded_w, coded_h) = (u32::from(track.width()), u32::from(track.height())); + // The same bound the Matroska door applies, on the same values: the + // mp4 crate types a track's dimensions as `u16`, which is a smaller + // claim than Matroska's but still eight times past the 8K ceiling -- + // 65535x65535 is a legal `tkhd`, and it sizes the same buffers. + for stated in [coded_w, coded_h] { + if stated > crate::MAX_RESOLUTION { + return Err(unsupported_display_size(u64::from(stated))); + } + } // The ratio one coded pixel is drawn at, off the sample entry's `pasp` // box: absent for the square-pixel file, which is the common case. let pixel_aspect = pasp_of(path, track_id); diff --git a/crates/engine/tests/untrusted_header.rs b/crates/engine/tests/untrusted_header.rs index c24c407..c897925 100644 --- a/crates/engine/tests/untrusted_header.rs +++ b/crates/engine/tests/untrusted_header.rs @@ -160,4 +160,48 @@ fn a_hostile_pasp_decodes_the_coded_picture_instead_of_aborting() { coded, "the coded picture, drawn square: a wrapped claim resizes nothing" ); -} \ No newline at end of file +} +/// The same bound, on the other door. An mp4 states its picture in a visual +/// sample entry's two `u16` fields -- a smaller claim than Matroska's EBML +/// integer, but still eight times past the 8K ceiling, and it reaches the same +/// `decode.rs` allocation. +#[test] +fn a_mp4_may_not_state_a_picture_past_the_resolution_bound() { + let scratch = Scratch::new("mp4dims"); + let into = scratch.path("mp4_huge_dims.mp4"); + patch_sample_entry_size(&asset("test_baseline.mp4"), &into, 65535, 65535); + + let Err(why) = Demuxer::open(&into) else { + panic!("a 65535x65535 mp4 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" + ); +} + +/// A copy of `source` with the width and height of its first visual sample +/// entry set to `w`/`h` (ISO/IEC 14496-12 ยง12.1.3: two 16-bit fields, 24 +/// bytes into the entry, after the 6 reserved bytes, the 2-byte data +/// reference index and the 16 bytes of pre-defined and reserved fields). +fn patch_sample_entry_size(source: &Path, into: &Path, w: u16, h: u16) { + let mut bytes = std::fs::read(source).expect("read the source fixture"); + let stsd = find(&bytes, b"stsd").expect("the fixture has a sample description"); + // 'stsd' + version/flags + entry_count, then the first entry. + let entry = stsd + 12; + assert_eq!(&bytes[entry + 4..entry + 8], b"avc1", "the first entry is the video one"); + bytes[entry + 32..entry + 34].copy_from_slice(&w.to_be_bytes()); + bytes[entry + 34..entry + 36].copy_from_slice(&h.to_be_bytes()); + std::fs::write(into, bytes).expect("write the patched fixture"); +} + +fn find(haystack: &[u8], needle: &[u8]) -> Option { + haystack + .windows(needle.len()) + .position(|window| window == needle) +}