A container may not state a picture past the resolution bound
Browse files at this commit48ad076ac3a8d073ebde785f4a59cc50ae854db7Parent: d4dc736- fix
- a container may not state a picture past the resolution bound
2 changed files +110 -3
crates/engine/src/demux.rsmodified+60 -2
| @@ -250,6 +250,40 @@ fn mp4_codec(fourcc: &[u8; 4]) -> Option<Codec> { | |||
| 250 | #[derive(Debug)] | 250 | #[derive(Debug)] |
| 251 | pub struct NoVideoTrack(String); | 251 | pub struct NoVideoTrack(String); |
| 252 | 252 | ||
| 253 | +/// A container that states a picture larger than this engine will compose: | ||
| 254 | +/// past [`crate::MAX_RESOLUTION`], where a per-frame buffer stops being a | ||
| 255 | +/// sane thing to allocate. Refused by name, like the other named refusals, | ||
| 256 | +/// because "this file is too big to open" is something a user can act on and | ||
| 257 | +/// a capacity overflow one layer down is not. | ||
| 258 | +#[derive(Debug)] | ||
| 259 | +pub struct UnsupportedDisplaySize(u64); | ||
| 260 | + | ||
| 261 | +impl std::fmt::Display for UnsupportedDisplaySize { | ||
| 262 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 263 | + write!( | ||
| 264 | + f, | ||
| 265 | + "this file states a {} pixel picture, past the {} pixel maximum", | ||
| 266 | + self.0, | ||
| 267 | + crate::MAX_RESOLUTION | ||
| 268 | + ) | ||
| 269 | + } | ||
| 270 | +} | ||
| 271 | + | ||
| 272 | +impl std::error::Error for UnsupportedDisplaySize {} | ||
| 273 | + | ||
| 274 | +fn unsupported_display_size(stated: u64) -> crate::Error { | ||
| 275 | + UnsupportedDisplaySize(stated).into() | ||
| 276 | +} | ||
| 277 | + | ||
| 278 | +impl UnsupportedDisplaySize { | ||
| 279 | + /// Whether an `error` is one of these, however deep in a `Box` it is -- | ||
| 280 | + /// [`NoVideoTrack::is_it`]'s and [`UnsupportedRotation::is_it`]'s twin, | ||
| 281 | + /// for a caller that shows the refusal differently from the others. | ||
| 282 | + pub fn is_it(error: &crate::Error) -> bool { | ||
| 283 | + error.downcast_ref::<Self>().is_some() | ||
| 284 | + } | ||
| 285 | +} | ||
| 286 | + | ||
| 253 | impl std::fmt::Display for NoVideoTrack { | 287 | impl std::fmt::Display for NoVideoTrack { |
| 254 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 288 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 255 | f.write_str(&self.0) | 289 | f.write_str(&self.0) |
| @@ -2609,8 +2643,32 @@ fn mkv_track_entry( | |||
| 2609 | let mut at = body; | 2643 | let mut at = body; |
| 2610 | while let Some(e) = ebml_element(file, at, stop)? { | 2644 | while let Some(e) = ebml_element(file, at, stop)? { |
| 2611 | match e.0 { | 2645 | match e.0 { |
| 2612 | - PIXEL_WIDTH => width = ebml_uint(file, e.1, e.2)? as u32, | 2646 | + // A container may state any size at all, and these |
| 2613 | - PIXEL_HEIGHT => height = ebml_uint(file, e.1, e.2)? as u32, | 2647 | + // two reach a per-frame allocation directly |
| 2648 | + // (`decode.rs` sizes its planes as | ||
| 2649 | + // `display_width * height`). The engine already has | ||
| 2650 | + // the bound for this -- [`crate::MAX_RESOLUTION`], | ||
| 2651 | + // 8K -- and it was applied to a keystroke and to a | ||
| 2652 | + // `.edith` line but never to a header, so a | ||
| 2653 | + // 65535x65535 track opened and asked for a | ||
| 2654 | + // gigabyte-scale buffer. A size past the bound is | ||
| 2655 | + // refused by name here, where the file can be | ||
| 2656 | + // refused, rather than discovered as a capacity | ||
| 2657 | + // overflow one layer down. | ||
| 2658 | + PIXEL_WIDTH => { | ||
| 2659 | + let stated = ebml_uint(file, e.1, e.2)?; | ||
| 2660 | + if stated > u64::from(crate::MAX_RESOLUTION) { | ||
| 2661 | + return Err(unsupported_display_size(stated)); | ||
| 2662 | + } | ||
| 2663 | + width = stated as u32; | ||
| 2664 | + } | ||
| 2665 | + PIXEL_HEIGHT => { | ||
| 2666 | + let stated = ebml_uint(file, e.1, e.2)?; | ||
| 2667 | + if stated > u64::from(crate::MAX_RESOLUTION) { | ||
| 2668 | + return Err(unsupported_display_size(stated)); | ||
| 2669 | + } | ||
| 2670 | + height = stated as u32; | ||
| 2671 | + } | ||
| 2614 | DISPLAY_WIDTH => display_width = ebml_uint(file, e.1, e.2)?, | 2672 | DISPLAY_WIDTH => display_width = ebml_uint(file, e.1, e.2)?, |
| 2615 | DISPLAY_HEIGHT => display_height = ebml_uint(file, e.1, e.2)?, | 2673 | DISPLAY_HEIGHT => display_height = ebml_uint(file, e.1, e.2)?, |
| 2616 | DISPLAY_UNIT => display_unit = ebml_uint(file, e.1, e.2)?, | 2674 | DISPLAY_UNIT => display_unit = ebml_uint(file, e.1, e.2)?, |
crates/engine/tests/display_unit.rsmodified+50 -1
| @@ -215,4 +215,53 @@ fn a_physical_display_unit_is_read_as_square() { | |||
| 215 | meta.pixel_aspect | 215 | meta.pixel_aspect |
| 216 | ); | 216 | ); |
| 217 | } | 217 | } |
| 218 | -} | ||
| \ No newline at end of file | \ No newline at end of file | ||
| 218 | +} | ||
| 219 | +/// The coded size is untrusted too, and it reaches an allocation directly: | ||
| 220 | +/// `decode.rs` sizes its stretched planes as `display_width * height`. The | ||
| 221 | +/// engine already has a bound for exactly this -- [`engine::MAX_RESOLUTION`], | ||
| 222 | +/// 8K, the size a per-frame buffer stops being sane at -- and the demux | ||
| 223 | +/// readers never applied it to a container-stated width or height. | ||
| 224 | +/// | ||
| 225 | +/// The fixture patches the `PixelWidth`/`PixelHeight` payloads to 65535 each, | ||
| 226 | +/// which is a legal two-byte EBML integer, so the element keeps its exact | ||
| 227 | +/// length and the copy stays seekable. | ||
| 228 | +#[test] | ||
| 229 | +fn a_coded_size_past_the_resolution_bound_is_refused_not_allocated() { | ||
| 230 | + let scratch = Scratch::new("huge"); | ||
| 231 | + let into = scratch.path("huge_dims.mkv"); | ||
| 232 | + with_video_element( | ||
| 233 | + &asset("test_anamorphic.mkv"), | ||
| 234 | + &into, | ||
| 235 | + &VIDEO_UNIT_3 | ||
| 236 | + .replacen("b08205a0", "b082ffff", 1) | ||
| 237 | + .replacen("ba820438", "ba82ffff", 1), | ||
| 238 | + ); | ||
| 239 | + | ||
| 240 | + // Refused, and refused by name: the open fails with a message that says | ||
| 241 | + // what the file claimed and what the maximum is, rather than succeeding | ||
| 242 | + // and handing a caller a 65535x65535 to allocate. | ||
| 243 | + let Err(why) = Demuxer::open(&into) else { | ||
| 244 | + panic!("a 65535x65535 header is refused by name, never opened"); | ||
| 245 | + }; | ||
| 246 | + let said = why.to_string(); | ||
| 247 | + assert!( | ||
| 248 | + said.contains("65535") && said.contains(&engine::MAX_RESOLUTION.to_string()), | ||
| 249 | + "the refusal names the size claimed and the maximum: {said}" | ||
| 250 | + ); | ||
| 251 | + assert!( | ||
| 252 | + engine::demux::UnsupportedDisplaySize::is_it(&why), | ||
| 253 | + "and it is the named refusal a caller can match on, not a string" | ||
| 254 | + ); | ||
| 255 | +} | ||
| 256 | + | ||
| 257 | +/// The bound is a ceiling on a real picture, not a floor on a file: the | ||
| 258 | +/// largest size a real source states still opens, untouched. | ||
| 259 | +#[test] | ||
| 260 | +fn a_real_size_up_to_the_bound_still_opens() { | ||
| 261 | + let (meta, _) = Demuxer::open(&asset("test_anamorphic.mkv")).expect("open the fixture"); | ||
| 262 | + assert!( | ||
| 263 | + engine::is_resolution(meta.coded_width, meta.coded_height), | ||
| 264 | + "the ordinary fixture is inside the bound, which is what keeps the \ | ||
| 265 | + bound from refusing the library" | ||
| 266 | + ); | ||
| 267 | +} | ||