FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
MP4 rtmd: bound the header read and reject damaged timecode fields
Max Murphy83fb73b9b1e6
diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rsindex dcb23c019..2c1b4c590 100644--- a/crates/codecs/src/mp4.rs+++ b/crates/codecs/src/mp4.rs@@ -9,7 +9,7 @@ use std::sync::{Arc, Mutex};use filmcraft_color::ColorInfo;use filmcraft_frame::{AudioBuffer, Region, VideoFrame};-use filmcraft_isobmff::{CleanAperture, CodecConfig, Mp4File, TrackKind};+use filmcraft_isobmff::{ByteSource, CleanAperture, CodecConfig, Mp4File, TrackKind};use filmcraft_media::{AudioStreamInfo, FrameRequest, MediaError, MediaInfo, MediaKind, MediaSource, SharedSource, VideoStreamInfo};use filmcraft_time::{FrameRate, Tick};@@ -162,22 +162,38 @@ fn color_from(entry: &filmcraft_isobmff::SampleEntry, w: u32, h: u32) -> ColorIncrate::stream_color::resolve(w, h, &sources)}-/// Sony XAVC `rtmd` (real-time metadata) start timecode and drop-frame flag, read from the first-/// sample. Sony stores the fields as raw decimal bytes in the sample's fixed header: hours,-/// minutes, seconds, a drop-frame flag, then frames. The layout is proprietary and undocumented,-/// so it is reproduced here from real footage; it is the same block ffmpeg reports as the-/// `timecode` tag of an `rtmd` stream.-fn rtmd_fields(sample: &[u8]) -> Option<(i64, i64, i64, i64, bool)> {+/// First bytes of a Sony `rtmd` sample needed for the start-timecode block (`frames` is last).+const RTMD_HEADER_LEN: usize = 0x12;++/// Sony XAVC `rtmd` (real-time metadata) start timecode fields, read from the first sample: hours,+/// minutes, seconds, a drop-frame flag and frames. Sony stores them as raw decimal bytes in the+/// sample's fixed header. The layout is proprietary and undocumented, so it is reproduced here from+/// real footage; it is the same block ffmpeg reports as the `timecode` tag of an `rtmd` stream.+///+/// Fields outside SMPTE bounds are rejected (no fabricated start time from damaged metadata);+/// `base` is the timecode frame base (24, 25, 30, 60…), so a frame number must be below it.+fn rtmd_fields(sample: &[u8], base: i64) -> Option<(i64, i64, i64, i64, bool)> {let f = sample.get(0x0d..0x12)?;- Some((f[0] as i64, f[1] as i64, f[2] as i64, f[4] as i64, f[3] != 0))+ let (h, m, s) = (f[0] as i64, f[1] as i64, f[2] as i64);+ let frames = f[4] as i64;+ if h > 23 || m > 59 || s > 59 || frames >= base.max(1) {+ return None;+ }+ Some((h, m, s, frames, f[3] != 0))}/// Start timecode from a Sony `rtmd` track as a frame count at `rate` (issue #460), used when the/// file has no `tmcd` track. `None` if there is no `rtmd` track or it has no usable first sample.+/// Only the fixed-size header is read (a damaged `stsz` size can't drive a huge allocation).fn rtmd_start_timecode(file: &Mp4File, bytes: &crate::Src, rate: FrameRate) -> Option<i64> {- let ti = file.tracks.iter().position(|t| t.entries.first().is_some_and(|e| e.format.0 == *b"rtmd"))?;- let sample = file.read_sample(bytes, ti, 0).ok()?;- let (h, m, s, f, drop) = rtmd_fields(&sample)?;+ let t = file.tracks.iter().find(|t| t.entries.first().is_some_and(|e| e.format.0 == *b"rtmd"))?;+ let s = t.samples.first()?;+ if s.size < RTMD_HEADER_LEN as u32 {+ return None;+ }+ let mut buf = [0u8; RTMD_HEADER_LEN];+ bytes.read_at(s.offset, &mut buf).ok()?;+ let (h, m, s, f, drop) = rtmd_fields(&buf, rate.timecode_base())?;Some(filmcraft_time::fields_to_frames(h, m, s, f, rate, drop))}@@ -938,15 +954,21 @@ mod tests {}#[test]- fn rtmd_fields_parse_and_ignore_short_samples() {+ fn rtmd_fields_parse_and_reject_damaged_metadata() {// 03:37:12:34, non-drop (a Sony FX3/a6400 XAVC header).let mut s = [0u8; 24];s[0x0d..0x12].copy_from_slice(&[3, 37, 12, 0, 34]);- assert_eq!(rtmd_fields(&s), Some((3, 37, 12, 34, false)));+ assert_eq!(rtmd_fields(&s, 60), Some((3, 37, 12, 34, false)));s[0x10] = 1;- assert_eq!(rtmd_fields(&s), Some((3, 37, 12, 34, true)));+ assert_eq!(rtmd_fields(&s, 60), Some((3, 37, 12, 34, true)));// Too short to hold the block: no fields, no panic.- assert_eq!(rtmd_fields(&[0u8; 6]), None);+ assert_eq!(rtmd_fields(&[0u8; 6], 60), None);+ // Damaged fields are rejected rather than normalized into a plausible time.+ for bad in [[3, 37, 12, 0, 60], [3, 60, 0, 0, 0], [24, 0, 0, 0, 0], [0, 0, 60, 0, 0]] {+ let mut b = [0u8; 24];+ b[0x0d..0x12].copy_from_slice(&bad);+ assert_eq!(rtmd_fields(&b, 60), None, "{bad:?}");+ }}}diff --git a/crates/codecs/tests/mp4_rtmd.rs b/crates/codecs/tests/mp4_rtmd.rsindex 34b8e18b6..ae937d79f 100644--- a/crates/codecs/tests/mp4_rtmd.rs+++ b/crates/codecs/tests/mp4_rtmd.rs@@ -66,6 +66,12 @@ fn video_entry(b: &mut B) {/// A video track at 59.94 fps, plus a Sony `rtmd` track whose first sample is `rtmd_sample`.fn file_with_rtmd(rtmd_sample: &[u8]) -> Vec<u8> {+ file_with_rtmd_claimed(rtmd_sample, rtmd_sample.len() as u32)+}++/// As [`file_with_rtmd`] but the `rtmd` sample's `stsz` size is `claimed` (which may exceed the+/// bytes actually present, as a corrupt file can).+fn file_with_rtmd_claimed(rtmd_sample: &[u8], claimed: u32) -> Vec<u8> {let mut b = B::default();b.leaf(b"ftyp", b"isom\x00\x00\x00\x00isom");let mdat_start = b.0.len();@@ -179,7 +185,7 @@ fn file_with_rtmd(rtmd_sample: &[u8]) -> Vec<u8> {b.full(b"stsz", |b| {b.u32(0);b.u32(1);- b.u32(rtmd_sample.len() as u32);+ b.u32(claimed);});b.full(b"stco", |b| {b.u32(1);@@ -223,3 +229,20 @@ fn rtmd_without_usable_sample_is_ignored() {let src = filmcraft_codecs::open_bytes("short.mp4", Arc::from(file.into_boxed_slice())).unwrap();assert_eq!(src.info().start_timecode, None);}++#[test]+fn huge_claimed_rtmd_sample_does_not_exhaust_memory() {+ // The stsz size claims 16 MiB but only the header bytes exist: only the fixed-size header is+ // read, so a corrupt size cannot drive a huge allocation during open (issue #460 review).+ let file = file_with_rtmd_claimed(&rtmd_sample(1, 2, 3, 4, 0), 16 * 1024 * 1024);+ let src = filmcraft_codecs::open_bytes("big.mp4", Arc::from(file.into_boxed_slice())).unwrap();+ assert_eq!(src.info().start_timecode, Some(223_384)); // 01:02:03:04 at 59.94+}++#[test]+fn damaged_timecode_fields_are_not_published() {+ // Frame 60 at 59.94 fps (valid range is 0–59): unusable metadata, not a fabricated time.+ let file = file_with_rtmd(&rtmd_sample(3, 37, 12, 60, 0));+ let src = filmcraft_codecs::open_bytes("bad.mp4", Arc::from(file.into_boxed_slice())).unwrap();+ assert_eq!(src.info().start_timecode, None);+}