Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

security(vp8): bound frame allocation (#2929)

Earl Co34848b39649c
diff --git a/crates/vp8/src/decoder.rs b/crates/vp8/src/decoder.rs
index 0c030c740..50d8c517d 100644
--- a/crates/vp8/src/decoder.rs
+++ b/crates/vp8/src/decoder.rs
@@ -15,6 +15,9 @@ use crate::transform;
/// Largest frame dimension in macroblocks (14-bit sizes).
const MAX_MBS: usize = 16384 / 16 + 1;
+/// Maximum padded luma samples retained by one VP8 reference picture (about 8K UHD).
+/// 2^26 luma samples: 8192x8192, above every VP8 frame a camera or encoder writes (8192x4320 is 35M).
+const MAX_LUMA_SAMPLES: usize = 1 << 26;
/// A reconstructed frame: planes sized to whole macroblocks.
#[derive(Clone)]
@@ -162,14 +165,18 @@ impl Decoder {
/// (`show_frame`), `None` for a hidden frame (an altref).
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Option<Picture>> {
let tag = FrameTag::parse(data)?;
+ ensure!(tag.first_part_size > 0, "empty first partition");
if tag.key_frame {
ensure!(tag.width > 0 && tag.height > 0, "key frame of size {}x{}", tag.width, tag.height);
let (w, h) = (usize::from(tag.width), usize::from(tag.height));
+ let (mb_w, mb_h) = (w.div_ceil(16), h.div_ceil(16));
+ let samples = mb_w.checked_mul(16).and_then(|w| w.checked_mul(mb_h)).and_then(|h| h.checked_mul(16));
+ ensure!(samples.is_some_and(|n| n <= MAX_LUMA_SAMPLES), "frame size {}x{} exceeds the decoder memory limit", tag.width, tag.height);
if w != self.width || h != self.height {
self.width = w;
self.height = h;
- self.mb_w = w.div_ceil(16);
- self.mb_h = h.div_ceil(16);
+ self.mb_w = mb_w;
+ self.mb_h = mb_h;
self.seg_map = vec![0; self.mb_w * self.mb_h];
}
// key frame defaults (9.3, 9.4, 9.7, 13.4, 16.1, 17.2)
diff --git a/crates/vp8/tests/robustness.rs b/crates/vp8/tests/robustness.rs
index 7dfc4f897..a4027f15d 100644
--- a/crates/vp8/tests/robustness.rs
+++ b/crates/vp8/tests/robustness.rs
@@ -78,7 +78,15 @@ fn random_bytes_and_tiny_frames_do_not_panic() {
key[3..6].copy_from_slice(&[0x9d, 0x01, 0x2a]);
key[6..8].copy_from_slice(&1920u16.to_le_bytes());
key[8..10].copy_from_slice(&1080u16.to_le_bytes());
- let _ = dec.decode(&key, 0);
+ assert!(dec.decode(&key, 0).is_err(), "empty first partition must not reconstruct a frame");
+
+ // A header can declare nearly 16K square dimensions with just ten bytes. It must be rejected
+ // before the macroblock map and reference planes are allocated.
+ key[0] = 0x30; // one-byte first partition
+ key[10] = 0;
+ key[6..8].copy_from_slice(&16_383u16.to_le_bytes());
+ key[8..10].copy_from_slice(&16_383u16.to_le_bytes());
+ assert!(dec.decode(&key, 0).is_err(), "oversized key frame must be rejected");
}
#[test]