Skip to content

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

Hardening: hostile MP4 edit lists and cyclic subclip chains (#66)

Brandon Thomas0dd66d8aa33e
diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rs
index d21497615..2fedf3be1 100644
--- a/crates/codecs/src/mp4.rs
+++ b/crates/codecs/src/mp4.rs
@@ -389,7 +389,9 @@ impl MediaSource for Mp4Source {
// Map the requested window to source samples (edit list offset applied: presentation = pts + edit_offset).
let ratio = src_rate as f64 / sample_rate as f64;
let p0 = (start as f64 * ratio).floor() as i64;
- let s0 = p0 - self.audio_offset * src_rate as i64 / track.timescale.max(1) as i64;
+ // (in i128: a hostile edit list can make the offset large enough to overflow i64)
+ let offset = i128::from(self.audio_offset) * i128::from(src_rate) / i128::from(track.timescale.max(1));
+ let s0 = i64::try_from(i128::from(p0) - offset).unwrap_or(if offset > 0 { i64::MIN } else { i64::MAX });
let need = (frames as f64 * ratio).ceil() as i64 + 2;
// Samples-per-unit: the track timescale is usually the sample rate for audio.
let unit = src_rate as f64 / track.timescale.max(1) as f64;
@@ -399,14 +401,14 @@ impl MediaSource for Mp4Source {
let mut i = first;
while i < track.samples.len() {
let pk_start = (self.audio_starts[i] as f64 * unit) as i64;
- if pk_start >= s0 + need {
+ if pk_start >= s0.saturating_add(need) {
break;
}
let pk = self.audio_packet(&mut st, i)?;
for (c, dst) in src.iter_mut().enumerate() {
let Some(chan) = pk.get(c.min(pk.len().saturating_sub(1))) else { continue };
for (k, v) in chan.iter().enumerate() {
- let pos = pk_start + k as i64 - s0;
+ let pos = pk_start.saturating_add(k as i64).saturating_sub(s0);
if pos >= 0 && (pos as usize) < dst.len() {
dst[pos as usize] = *v;
}
@@ -836,4 +838,30 @@ mod he_aac_tests {
let crossings = x.windows(2).filter(|w| w[0] < 0.0 && w[1] >= 0.0).count();
assert!((995..=1005).contains(&crossings), "{crossings} Hz");
}
+
+ /// A hostile edit list (#66): an AAC track whose edit puts it absurdly far off, read at another
+ /// rate, scaled the offset to the source rate in i64 and overflowed. It reads as silence now.
+ #[test]
+ fn hostile_audio_edit_offset_never_overflows() {
+ use filmcraft_isobmff::Edit;
+ let rate = 32_000u32;
+ let tone: Vec<f32> = (0..rate as usize / 2).map(|i| 0.25 * (i as f32 * 0.05).sin()).collect();
+ let mut enc = filmcraft_aac::Encoder::new(filmcraft_aac::EncoderConfig::cbr(rate, 1, 64_000)).unwrap();
+ let mut aus = enc.encode(&[&tone]);
+ aus.extend(enc.flush());
+ // AOT 2 (AAC-LC), 32 kHz (index 5), mono
+ let asc = vec![0x12, 0x88];
+ for media_time in [i64::MAX, i64::MAX / 3, i64::MIN / 3] {
+ let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).unwrap();
+ let mut cfg = TrackConfig::new(SampleEntry::aac(asc.clone(), 1, rate), rate);
+ cfg.edits = vec![Edit { segment_duration: 500, media_time, media_rate: 0x10000 }];
+ let t = mux.add_track(cfg).unwrap();
+ for au in &aus {
+ mux.write_sample(t, WriteSample { data: au, duration: 1024, composition_offset: 0, is_sync: true }).unwrap();
+ }
+ let src = super::Mp4Source::open("hostile.mp4", mux.finish().unwrap().into_inner().into()).unwrap();
+ let buf = src.audio(0, 4800, 48_000).unwrap();
+ assert!(buf.channels.iter().flatten().all(|v| v.is_finite() && v.abs() <= 1.0), "media_time {media_time}");
+ }
+ }
}
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rs
index a006647c8..4a5031772 100644
--- a/crates/engine/src/clip_ops_tests.rs
+++ b/crates/engine/src/clip_ops_tests.rs
@@ -837,3 +837,17 @@ fn subclip_source_monitor_trims_and_markers() {
s.execute("edit.undo", json!({})).unwrap();
assert!(matches!(s.project.item(sub).unwrap().kind, ItemKind::Subclip { restrict_trims: false, .. }));
}
+
+/// A damaged project whose subclip is its own parent (#66): `media_duration` followed the chain
+/// without a bound and overflowed the stack. It gives up after a few hops now.
+#[test]
+fn a_cyclic_subclip_chain_has_no_media_duration() {
+ let mut s = demo();
+ let ocean = item_named(&s, "Ocean_Sunset.mp4");
+ s.execute("source.open", json!({"item": ocean.0})).unwrap();
+ let sub = ItemId(s.execute("clip.makeSubclip", json!({})).unwrap()["item"].as_u64().unwrap());
+ s.execute("clip.editSubclip", json!({"item": sub.0, "restrictTrims": false})).unwrap();
+ let Some(ItemKind::Subclip { parent, .. }) = std::sync::Arc::make_mut(&mut s.project).item_mut(sub).map(|i| &mut i.kind) else { panic!() };
+ *parent = sub;
+ assert_eq!(crate::media_duration(&s.project, &s.media, sub), None);
+}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index c50ca5c08..95ad5d00f 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -947,20 +947,32 @@ pub fn media_start(p: &Project, id: ItemId) -> Tick {
/// Media duration of an item (None for stills/adjustment layers = unlimited handles).
pub fn media_duration(p: &Project, _pool: &MediaPool, id: ItemId) -> Option<Tick> {
- let it = p.item(id)?;
- match &it.kind {
- filmcraft_project::ItemKind::Media(m) => match m.info.kind {
- filmcraft_media::MediaKind::Still | filmcraft_media::MediaKind::Synthetic => None,
- _ => Some(m.info.duration),
- },
- filmcraft_project::ItemKind::Sequence(s) => Some(s.duration()),
- // a subclip that restricts trims ends at its Out point; otherwise its parent's media is the limit
- filmcraft_project::ItemKind::Subclip { range, restrict_trims: true, .. } => Some(range.end()),
- filmcraft_project::ItemKind::Subclip { parent, .. } => media_duration(p, _pool, *parent),
- filmcraft_project::ItemKind::AdjustmentLayer { .. } | filmcraft_project::ItemKind::Graphic { .. } => None,
+ // a subclip's limit is its parent's: follow the chain, bounded (a damaged project file can make
+ // it cyclic or arbitrarily deep)
+ let mut id = id;
+ for _ in 0..=MAX_SUBCLIP_CHAIN {
+ let it = p.item(id)?;
+ return match &it.kind {
+ filmcraft_project::ItemKind::Media(m) => match m.info.kind {
+ filmcraft_media::MediaKind::Still | filmcraft_media::MediaKind::Synthetic => None,
+ _ => Some(m.info.duration),
+ },
+ filmcraft_project::ItemKind::Sequence(s) => Some(s.duration()),
+ // a subclip that restricts trims ends at its Out point; otherwise its parent's media is the limit
+ filmcraft_project::ItemKind::Subclip { range, restrict_trims: true, .. } => Some(range.end()),
+ filmcraft_project::ItemKind::Subclip { parent, .. } => {
+ id = *parent;
+ continue;
+ }
+ filmcraft_project::ItemKind::AdjustmentLayer { .. } | filmcraft_project::ItemKind::Graphic { .. } => None,
+ };
}
+ None
}
+/// Subclips of subclips followed before giving up (real projects nest one or two deep).
+const MAX_SUBCLIP_CHAIN: usize = 16;
+
#[cfg(test)]
mod aaf_omf_tests;
#[cfg(test)]