Skip to content

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

security(h264): bound hostile picture allocation (#2915)

Earl Co4328e49a49c5
diff --git a/crates/h264/src/decoder.rs b/crates/h264/src/decoder.rs
index 59070d359..93c96f94d 100644
--- a/crates/h264/src/decoder.rs
+++ b/crates/h264/src/decoder.rs
@@ -18,6 +18,10 @@ use filmcraft_bitstream::{BitReader, annexb_nals, length_prefixed_nals, unescape
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
+/// The decoder supports at most 16 reference pictures, so constructing more
+/// inferred gap pictures cannot improve a usable reference list.
+const MAX_INFERRED_GAP_FRAMES: u32 = 16;
+
/// One slice of a picture job.
struct SliceJob {
sh: SliceHeader,
@@ -425,6 +429,10 @@ impl Decoder {
} else {
let max = sps.max_frame_num();
if sh.frame_num != self.prev_ref_frame_num && sh.frame_num != (self.prev_ref_frame_num + 1) % max {
+ let gap = (sh.frame_num + max - self.prev_ref_frame_num - 1) % max;
+ if gap > MAX_INFERRED_GAP_FRAMES {
+ return Err(Error::Invalid("frame number gap exceeds the supported reference limit".into()));
+ }
// frame_num gap (8.2.5.2): insert "non-existing" frames. Their samples are never used by
// conforming streams; copy the latest decoded frame for concealment.
let meta = Arc::new(output_meta(sps, pts, false));
diff --git a/crates/h264/src/params.rs b/crates/h264/src/params.rs
index 5b92a6055..236b7b928 100644
--- a/crates/h264/src/params.rs
+++ b/crates/h264/src/params.rs
@@ -4,6 +4,10 @@ use crate::error::{Result, ensure, unsupported};
use crate::tables::*;
use filmcraft_bitstream::BitReader;
+/// Maximum supported reconstructed luma area. 8K UHD fits comfortably while
+/// hostile SPS dimensions cannot allocate full pictures and decoder work state.
+const MAX_LUMA_SAMPLES: u64 = 1 << 25;
+
/// Scaling matrices after applying fall-back rules, stored in raster order (weightScale).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScalingMatrices {
@@ -516,6 +520,9 @@ impl Sps {
}
pub fn check_supported(&self) -> Result<()> {
+ if u64::from(self.width()).saturating_mul(u64::from(self.height())) > MAX_LUMA_SAMPLES {
+ return unsupported("picture dimensions exceed the decoder memory limit");
+ }
if !self.frame_mbs_only {
return unsupported("interlaced (field / MBAFF) coding");
}