FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
security(omf): bound metadata expansion and traversal (#2946)
Earl Coa8d074247a0b
diff --git a/crates/interchange/src/omf/bento.rs b/crates/interchange/src/omf/bento.rsindex d3a0a80d7..2648b81b0 100644--- a/crates/interchange/src/omf/bento.rs+++ b/crates/interchange/src/omf/bento.rs@@ -72,6 +72,10 @@ enum Seg {At(u64, u32),}+/// Bento values are metadata, not essence payloads. Limit reassembled segments so repeated+/// references cannot amplify a small OMF into an enormous allocation.+const MAX_VALUE_BYTES: usize = 16 << 20;+/// Builds a Bento container.pub(crate) struct Writer {data: Vec<u8>,@@ -311,7 +315,18 @@ impl<'a> Container<'a> {/// The bytes of a value.pub fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {+ let total = p.segs.iter().try_fold(0usize, |total, s| {+ let len = match s {+ Seg::Imm(b) => b.len(),+ Seg::At(_, len) => *len as usize,+ };+ total.checked_add(len).ok_or_else(|| "Bento value length overflows".to_string())+ })?;+ if total > MAX_VALUE_BYTES {+ return Err("Bento value exceeds the decoded-value budget".into());+ }let mut out = Vec::new();+ out.try_reserve_exact(total).map_err(|_| "cannot allocate Bento value")?;for s in &p.segs {match s {Seg::Imm(b) => out.extend_from_slice(b),@@ -355,3 +370,15 @@ impl<'a> Container<'a> {key}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn repeated_segments_cannot_expand_a_value_past_its_budget() {+ let p = Prop { prop: 1, ty: 1, segs: vec![Seg::At(0, MAX_VALUE_BYTES as u32), Seg::Imm(vec![0])] };+ let c = Container { data: &[], objects: BTreeMap::new(), names: HashMap::new(), by_name: HashMap::new() };+ assert!(c.value(&p).unwrap_err().contains("budget"));+ }+}diff --git a/crates/interchange/src/omf/read.rs b/crates/interchange/src/omf/read.rsindex 3b2d1b369..b46673602 100644--- a/crates/interchange/src/omf/read.rs+++ b/crates/interchange/src/omf/read.rs@@ -10,6 +10,8 @@ use crate::{Error, Report, Result};type Uid = [u8; 12];+const MAX_REFERENCE_VISITS: usize = 65_536;+struct R<'a, 'b> {c: &'b Container<'a>,report: &'b mut Report,@@ -51,8 +53,10 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {}let cmobs: Vec<u32> = mob_list.iter().copied().filter(|&m| class_of(m) == Some(*b"CMOB")).collect();let mut referenced = HashSet::new();+ let mut visited = HashSet::new();+ let mut visits = 0;for &m in &cmobs {- r.collect_refs(m, &mut referenced, 0);+ r.collect_refs(m, &mut referenced, &mut visited, &mut visits, 0);}let primary: Vec<u32> = r.refs(head, "OMFI:HEAD:PrimaryMobs").into_iter().filter(|m| cmobs.contains(m)).collect();let top: Vec<u32> = if !primary.is_empty() {@@ -122,10 +126,11 @@ impl R<'_, '_> {self.i32(o, "OMFI:CPNT:Length").unwrap_or(0).max(0)}- fn collect_refs(&self, o: u32, out: &mut HashSet<Uid>, depth: usize) {- if depth > 64 {+ fn collect_refs(&self, o: u32, out: &mut HashSet<Uid>, visited: &mut HashSet<u32>, visits: &mut usize, depth: usize) {+ if depth > 64 || !visited.insert(o) || *visits >= MAX_REFERENCE_VISITS {return;}+ *visits += 1;if self.class(o) == *b"SCLP"&& let Some(id) = self.uid(o, "OMFI:SCLP:SourceID"){@@ -133,12 +138,12 @@ impl R<'_, '_> {}for p in ["OMFI:MOBJ:Slots", "OMFI:SEQU:Components", "OMFI:EFFE:EffectSlots"] {for c in self.refs(o, p) {- self.collect_refs(c, out, depth + 1);+ self.collect_refs(c, out, visited, visits, depth + 1);}}for p in ["OMFI:MSLT:Segment", "OMFI:ESLT:ArgValue"] {if let Some(c) = self.r#ref(o, p) {- self.collect_refs(c, out, depth + 1);+ self.collect_refs(c, out, visited, visits, depth + 1);}}}