Skip to content

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

security(codecs): validate VP9 empty tiles and AV1 film grain scaling points (#3081)

Dylan M. Taylorb4901c5bce27
diff --git a/crates/av1/src/grain.rs b/crates/av1/src/grain.rs
index d4baad621..677243644 100644
--- a/crates/av1/src/grain.rs
+++ b/crates/av1/src/grain.rs
@@ -133,6 +133,10 @@ pub(crate) fn apply(pic: &mut Picture, g: &FilmGrainParams, seq: &SequenceHeader
for i in 0..n - 1 {
let dy = ys[i + 1] as i32 - ys[i] as i32;
let dx = xs[i + 1] as i32 - xs[i] as i32;
+ // The header parser rejects non-increasing points; never divide by zero regardless.
+ if dx <= 0 {
+ continue;
+ }
let delta = dy * ((65536 + (dx >> 1)) / dx);
for x in 0..dx {
l[xs[i] as usize + x as usize] = ys[i] as i32 + ((x * delta + 32768) >> 16);
@@ -274,3 +278,34 @@ pub(crate) fn apply(pic: &mut Picture, g: &FilmGrainParams, seq: &SequenceHeader
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // Regression: two equal scaling points divided by zero building the scaling lookup.
+ #[test]
+ fn equal_scaling_points_do_not_panic() {
+ let mut g = FilmGrainParams { apply_grain: true, num_y_points: 2, ..Default::default() };
+ g.point_y_value[..2].copy_from_slice(&[64, 64]);
+ g.point_y_scaling[..2].copy_from_slice(&[20, 40]);
+ let (w, h) = (16usize, 16usize);
+ let mut pic = Picture {
+ width: w as u32,
+ height: h as u32,
+ bit_depth: 10,
+ subsampling_x: 1,
+ subsampling_y: 1,
+ mono_chrome: false,
+ planes: [vec![512; w * h], vec![512; w * h / 4], vec![512; w * h / 4]],
+ color_primaries: 1,
+ transfer_characteristics: 1,
+ matrix_coefficients: 1,
+ full_range: false,
+ pts: 0,
+ draft: false,
+ };
+ apply(&mut pic, &g, &SequenceHeader::default());
+ assert_eq!(pic.planes[0].len(), w * h);
+ }
+}
diff --git a/crates/av1/src/header.rs b/crates/av1/src/header.rs
index 73d7ec437..c4e5f8a16 100644
--- a/crates/av1/src/header.rs
+++ b/crates/av1/src/header.rs
@@ -1251,6 +1251,9 @@ impl FrameHeader {
g.point_y_value[i] = r.f(8)? as u8;
g.point_y_scaling[i] = r.f(8)? as u8;
}
+ if !strictly_increasing(&g.point_y_value[..g.num_y_points]) {
+ return Err(Error::Invalid("point_y_value"));
+ }
g.chroma_scaling_from_luma = if seq.color.mono_chrome { false } else { r.flag()? };
if seq.color.mono_chrome || g.chroma_scaling_from_luma || (seq.color.subsampling_x == 1 && seq.color.subsampling_y == 1 && g.num_y_points == 0) {
g.num_cb_points = 0;
@@ -1264,6 +1267,9 @@ impl FrameHeader {
g.point_cb_value[i] = r.f(8)? as u8;
g.point_cb_scaling[i] = r.f(8)? as u8;
}
+ if !strictly_increasing(&g.point_cb_value[..g.num_cb_points]) {
+ return Err(Error::Invalid("point_cb_value"));
+ }
g.num_cr_points = r.f(4)? as usize;
if g.num_cr_points > 10 {
return Err(Error::Invalid("num_cr_points"));
@@ -1272,6 +1278,9 @@ impl FrameHeader {
g.point_cr_value[i] = r.f(8)? as u8;
g.point_cr_scaling[i] = r.f(8)? as u8;
}
+ if !strictly_increasing(&g.point_cr_value[..g.num_cr_points]) {
+ return Err(Error::Invalid("point_cr_value"));
+ }
}
g.grain_scaling_minus_8 = r.f(2)?;
g.ar_coeff_lag = r.f(2)?;
@@ -1322,6 +1331,12 @@ pub(crate) fn default_gm_params() -> [[i32; 6]; 8] {
p
}
+/// Film grain scaling points must strictly increase (6.8.20); equal points would divide by zero when
+/// the scaling lookup is interpolated.
+fn strictly_increasing(values: &[u8]) -> bool {
+ values.windows(2).all(|w| w.first() < w.get(1))
+}
+
fn inverse_recenter(r: i32, v: i32) -> i32 {
if v > 2 * r {
v
@@ -1359,3 +1374,17 @@ fn decode_subexp(r: &mut BitReader, num_syms: i32) -> Result<i32> {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::strictly_increasing;
+
+ #[test]
+ fn film_grain_points_must_strictly_increase() {
+ assert!(strictly_increasing(&[]));
+ assert!(strictly_increasing(&[7]));
+ assert!(strictly_increasing(&[0, 64, 255]));
+ assert!(!strictly_increasing(&[0, 64, 64]));
+ assert!(!strictly_increasing(&[128, 64]));
+ }
+}
diff --git a/crates/vp9/src/decoder.rs b/crates/vp9/src/decoder.rs
index e220a7f62..619d1b5c2 100644
--- a/crates/vp9/src/decoder.rs
+++ b/crates/vp9/src/decoder.rs
@@ -352,6 +352,7 @@ impl Decoder {
ensure!(s <= data.len() - pos, "tile size {s} beyond frame data");
s
};
+ ensure!(sz > 0, "empty tile payload");
td.push(&data[pos..pos + sz]);
pos += sz;
}