Skip to content

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

security(mpeg2v): bound access-unit output (#2935)

Earl Cof9a2d66db1e5
diff --git a/crates/mpeg2v/src/lib.rs b/crates/mpeg2v/src/lib.rs
index 55c1ab9ad..6448207cd 100644
--- a/crates/mpeg2v/src/lib.rs
+++ b/crates/mpeg2v/src/lib.rs
@@ -44,6 +44,9 @@ use headers::{EXTENSION, GROUP_START, PICTURE_START, SEQUENCE_END, SEQUENCE_HEAD
use mc::Frame;
use slice::{PicParams, Target, decode_slice, slice_row};
+/// One access unit must not retain an unbounded number of cropped decoded pictures.
+const MAX_OUTPUT_PICTURES_PER_ACCESS_UNIT: usize = 8;
+
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid MPEG video: {0}")]
@@ -423,6 +426,7 @@ pub struct Decoder {
gray: Option<Arc<Frame>>,
pending: Option<Pending>,
out: Vec<Picture>,
+ output_overflow: bool,
errors: usize,
last_error: Option<&'static str>,
threads: bool,
@@ -447,6 +451,7 @@ impl Decoder {
gray: None,
pending: None,
out: Vec::new(),
+ output_overflow: false,
errors: 0,
last_error: None,
threads: cfg!(feature = "threads"),
@@ -476,6 +481,7 @@ impl Decoder {
/// Decode a chunk of the elementary stream holding whole pictures (an access unit) tagged
/// with `pts`. Returns the pictures that became displayable, in display order.
pub fn decode(&mut self, data: &[u8], pts: i64) -> Result<Vec<Picture>> {
+ self.output_overflow = false;
let codes = start_codes(data);
let mut err = None;
for (k, &(pos, code)) in codes.iter().enumerate() {
@@ -554,6 +560,10 @@ impl Decoder {
}
self.apply_sequence();
self.finish_picture(data, pts);
+ if self.output_overflow {
+ self.out.clear();
+ return Err(Error::Invalid("access unit produces too many decoded pictures".into()));
+ }
let out = std::mem::take(&mut self.out);
match err {
Some(e) if out.is_empty() && self.seq.is_none() => Err(e),
@@ -571,7 +581,7 @@ impl Decoder {
{
a.shown = true;
let p = to_picture(&a.frame, &a.meta);
- self.out.push(p);
+ self.push_output(p);
}
std::mem::take(&mut self.out)
}
@@ -584,6 +594,7 @@ impl Decoder {
self.building = None;
self.pending = None;
self.out.clear();
+ self.output_overflow = false;
self.closed_gop = false;
}
@@ -607,6 +618,11 @@ impl Decoder {
fn finish_picture(&mut self, data: &[u8], pts: i64) {
let Some(pd) = self.pending.take() else { return };
+ if pd.slices.is_empty() {
+ self.errors += 1;
+ self.last_error = Some("picture has no slices");
+ return;
+ }
let Some(seq) = self.seq.clone() else {
self.errors += 1;
return;
@@ -706,13 +722,21 @@ impl Decoder {
if let Some(mut prev) = self.bwd.take() {
if !prev.shown {
prev.shown = true;
- self.out.push(to_picture(&prev.frame, &prev.meta));
+ self.push_output(to_picture(&prev.frame, &prev.meta));
}
self.fwd = Some(prev);
}
self.bwd = Some(Anchor { frame: Arc::new(frame), meta, shown: false });
} else {
- self.out.push(to_picture(&frame, &meta));
+ self.push_output(to_picture(&frame, &meta));
+ }
+ }
+
+ fn push_output(&mut self, picture: Picture) {
+ if self.out.len() >= MAX_OUTPUT_PICTURES_PER_ACCESS_UNIT {
+ self.output_overflow = true;
+ } else {
+ self.out.push(picture);
}
}
}