Skip to content

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

security(mxf): bound recovery and PCM edit units (#2948)

Earl Co21df45601c04
diff --git a/crates/mxf/src/file.rs b/crates/mxf/src/file.rs
index 3394b657c..07cead857 100644
--- a/crates/mxf/src/file.rs
+++ b/crates/mxf/src/file.rs
@@ -327,12 +327,13 @@ fn walk(src: &(impl ByteSource + ?Sized)) -> Result<WalkResult> {
let mut win = Window { src, off: 0, buf: Vec::new() };
let mut pos = run_in;
let mut cur_meta: Option<usize> = None;
+ let mut resync_budget = 64 << 20;
while pos + 17 <= len {
let hdr = win.get(pos, 25)?.to_vec();
let key = Ul(hdr[..16].try_into().unwrap_or([0; 16]));
if !key.is_smpte() {
// Lost sync (corrupt data): look for the next partition pack.
- match resync(src, pos + 1, len)? {
+ match resync(src, pos + 1, len, &mut resync_budget)? {
Some(p) => {
w.warnings.push(format!("lost KLV sync at byte {pos}; resumed at {p}"));
pos = p;
@@ -418,12 +419,14 @@ fn is_essence_item(k: &Ul) -> bool {
matches!(k.0[12], 0x05 | 0x06 | 0x07 | 0x15 | 0x16 | 0x17 | 0x18)
}
-/// The next partition pack key at or after `from` (scans at most 16 MiB).
-fn resync(src: &(impl ByteSource + ?Sized), from: u64, len: u64) -> Result<Option<u64>> {
+/// The next partition pack key at or after `from` (scans at most 16 MiB and shares `budget`
+/// with every other recovery attempt in this file).
+fn resync(src: &(impl ByteSource + ?Sized), from: u64, len: u64, budget: &mut usize) -> Result<Option<u64>> {
let mut at = from;
let end = len.min(from + (16 << 20));
- while at < end {
- let chunk = read_upto(src, at, 1 << 20)?;
+ while at < end && *budget > 0 {
+ let chunk = read_upto(src, at, (1 << 20).min(*budget))?;
+ *budget = budget.saturating_sub(chunk.len());
if chunk.len() < 16 {
break;
}
diff --git a/crates/mxf/src/write/mod.rs b/crates/mxf/src/write/mod.rs
index 6852e1cd1..939cbd77e 100644
--- a/crates/mxf/src/write/mod.rs
+++ b/crates/mxf/src/write/mod.rs
@@ -541,6 +541,16 @@ impl<W: Write + Seek> MxfWriter<W> {
if !cfg.edit_rate.is_valid() {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "MXF edit rate must be positive"));
}
+ // OP1a pads every sound track to a whole edit unit on flush. Bound that unit before a
+ // low but technically positive edit rate can turn padding into a multi-gigabyte Vec.
+ const MAX_PCM_EDIT_UNIT_BYTES: u128 = 64 << 20;
+ for sound in &cfg.sound {
+ let samples = (u128::from(sound.sample_rate) * cfg.edit_rate.den as u128).div_ceil(cfg.edit_rate.num as u128);
+ let bytes = samples.saturating_mul(u128::from(sound.block_align()));
+ if bytes > MAX_PCM_EDIT_UNIT_BYTES {
+ return Err(io::Error::new(io::ErrorKind::InvalidInput, "MXF PCM edit unit exceeds the allocation budget"));
+ }
+ }
let n_essence = cfg.picture.is_some() as usize + cfg.sound.len();
if n_essence == 0 {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "MXF file without essence tracks"));
diff --git a/crates/mxf/src/write/tests.rs b/crates/mxf/src/write/tests.rs
index 8728e86c8..5ce7560a8 100644
--- a/crates/mxf/src/write/tests.rs
+++ b/crates/mxf/src/write/tests.rs
@@ -47,6 +47,15 @@ fn op1a_config(rate: Rational, coding: PictureCoding) -> WriterConfig {
cfg
}
+#[test]
+fn op1a_rejects_an_unbounded_pcm_edit_unit_before_padding() {
+ let mut cfg = WriterConfig::new(Pattern::Op1a, Rational::new(1, 36_000), PackageIds::from_seed("low-rate", "Low Rate"));
+ cfg.picture = Some(PictureDesc::new(PictureCoding::ProRes { profile: 3 }, 64, 36));
+ cfg.sound = vec![SoundDesc { sample_rate: 48_000, channels: 2, bits: 16 }];
+ let err = MxfWriter::new(Cursor::new(Vec::new()), cfg).err().unwrap();
+ assert!(err.to_string().contains("PCM edit unit"));
+}
+
/// 10 ProRes-like pictures at 25 fps with stereo 16-bit and mono 24-bit sound pushed in uneven chunks.
fn build_op1a() -> Vec<u8> {
let cfg = op1a_config(Rational::new(25, 1), PictureCoding::ProRes { profile: 3 });