Skip to content

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

security(vp9): bound frame allocation (#2930)

Earl Co99ec34de3008
diff --git a/crates/vp9/src/decoder.rs b/crates/vp9/src/decoder.rs
index af1f969c7..e220a7f62 100644
--- a/crates/vp9/src/decoder.rs
+++ b/crates/vp9/src/decoder.rs
@@ -15,6 +15,10 @@ use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock};
+/// Maximum luma samples accepted for one decoded reference frame: 8192x8192, above level 6.2's
+/// 8192x4352 (35 651 584 samples).
+const MAX_LUMA_SAMPLES: u64 = 1 << 26;
+
/// Counters describing what the decoder has seen (useful to check test coverage).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DecodeStats {
@@ -257,6 +261,12 @@ impl Decoder {
return Ok(());
}
ensure!(h.width <= 16384 && h.height <= 16384, "frame size {}x{} too large", h.width, h.height);
+ ensure!(
+ u64::from(h.width).checked_mul(u64::from(h.height)).is_some_and(|n| n <= MAX_LUMA_SAMPLES),
+ "frame size {}x{} exceeds the decoder memory limit",
+ h.width,
+ h.height
+ );
let (mi_rows, mi_cols) = (h.mi_rows as usize, h.mi_cols as usize);
// compute_image_size semantics (7.2.6).
let size = (h.width, h.height);
@@ -279,6 +289,7 @@ impl Decoder {
let off = h.uncompressed_size;
let hs = h.header_size_in_bytes as usize;
ensure!(off + hs <= data.len(), "compressed header exceeds frame data");
+ ensure!(off + hs < data.len(), "frame has no tile data");
let mut fc = self.st.contexts[h.frame_context_idx as usize].clone();
parse_compressed(&data[off..off + hs], &mut h, &mut fc)?;
self.count_frame(&h);