forge
Explore
Log in

Don't you dare go Hollow

The mp4 door gets the resolution bound the Matroska one now has

Browse files at this commit

fcb7844022311353e56fade700858225e120a9e5Ahmet Kaan GümüşParent: 48ad076
  • fix
    • the mp4 door gets the resolution bound the Matroska one now has
Show full message
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.rsmodified+9 -0
@@ -1008,6 +1008,15 @@ impl Mp4Demuxer {
1008 None => Rotation::None,1008 None => Rotation::None,
1009 };1009 };
1010 let (coded_w, coded_h) = (u32::from(track.width()), u32::from(track.height()));1010 let (coded_w, coded_h) = (u32::from(track.width()), u32::from(track.height()));
1011+ // The same bound the Matroska door applies, on the same values: the
1012+ // mp4 crate types a track's dimensions as `u16`, which is a smaller
1013+ // claim than Matroska's but still eight times past the 8K ceiling --
1014+ // 65535x65535 is a legal `tkhd`, and it sizes the same buffers.
1015+ for stated in [coded_w, coded_h] {
1016+ if stated > crate::MAX_RESOLUTION {
1017+ return Err(unsupported_display_size(u64::from(stated)));
1018+ }
1019+ }
1011 // The ratio one coded pixel is drawn at, off the sample entry's `pasp`1020 // The ratio one coded pixel is drawn at, off the sample entry's `pasp`
1012 // box: absent for the square-pixel file, which is the common case.1021 // box: absent for the square-pixel file, which is the common case.
1013 let pixel_aspect = pasp_of(path, track_id);1022 let pixel_aspect = pasp_of(path, track_id);
crates/engine/tests/untrusted_header.rsmodified+45 -1
@@ -160,4 +160,48 @@ fn a_hostile_pasp_decodes_the_coded_picture_instead_of_aborting() {
160 coded,160 coded,
161 "the coded picture, drawn square: a wrapped claim resizes nothing"161 "the coded picture, drawn square: a wrapped claim resizes nothing"
162 );162 );
163-}
\ No newline at end of file\ No newline at end of file
163+}
164+/// The same bound, on the other door. An mp4 states its picture in a visual
165+/// sample entry's two `u16` fields -- a smaller claim than Matroska's EBML
166+/// integer, but still eight times past the 8K ceiling, and it reaches the same
167+/// `decode.rs` allocation.
168+#[test]
169+fn a_mp4_may_not_state_a_picture_past_the_resolution_bound() {
170+ let scratch = Scratch::new("mp4dims");
171+ let into = scratch.path("mp4_huge_dims.mp4");
172+ patch_sample_entry_size(&asset("test_baseline.mp4"), &into, 65535, 65535);
173+
174+ let Err(why) = Demuxer::open(&into) else {
175+ panic!("a 65535x65535 mp4 is refused by name, never opened");
176+ };
177+ let said = why.to_string();
178+ assert!(
179+ said.contains("65535") && said.contains(&engine::MAX_RESOLUTION.to_string()),
180+ "the refusal names the size claimed and the maximum: {said}"
181+ );
182+ assert!(
183+ engine::demux::UnsupportedDisplaySize::is_it(&why),
184+ "and it is the named refusal a caller can match on, not a string"
185+ );
186+}
187+
188+/// A copy of `source` with the width and height of its first visual sample
189+/// entry set to `w`/`h` (ISO/IEC 14496-12 §12.1.3: two 16-bit fields, 24
190+/// bytes into the entry, after the 6 reserved bytes, the 2-byte data
191+/// reference index and the 16 bytes of pre-defined and reserved fields).
192+fn patch_sample_entry_size(source: &Path, into: &Path, w: u16, h: u16) {
193+ let mut bytes = std::fs::read(source).expect("read the source fixture");
194+ let stsd = find(&bytes, b"stsd").expect("the fixture has a sample description");
195+ // 'stsd' + version/flags + entry_count, then the first entry.
196+ let entry = stsd + 12;
197+ assert_eq!(&bytes[entry + 4..entry + 8], b"avc1", "the first entry is the video one");
198+ bytes[entry + 32..entry + 34].copy_from_slice(&w.to_be_bytes());
199+ bytes[entry + 34..entry + 36].copy_from_slice(&h.to_be_bytes());
200+ std::fs::write(into, bytes).expect("write the patched fixture");
201+}
202+
203+fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
204+ haystack
205+ .windows(needle.len())
206+ .position(|window| window == needle)
207+}