diff --git a/Cargo.lock b/Cargo.lock index 310b4c59163..eb8e54ef83c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5379,9 +5379,9 @@ dependencies = [ [[package]] name = "mp4-atom" -version = "0.17.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "718b5227fa90c52ed2e72d4d553560130d9e128acb6217d9fe0ea57312ee7063" +checksum = "327faeb7d11fc3735194fdaeaa82af72167801bf39dd3d6b2684c8fc5cd7fc19" dependencies = [ "bytes", "derive_more 2.1.1", diff --git a/Cargo.toml b/Cargo.toml index f6ffa1c4660..b8edbc49b0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -177,7 +177,7 @@ moq-vaapi = "0.1.0" # that argument, so moq-ffi and moq-c opt NVIDIA and VAAPI back in (OpenH264 # through their own `openh264` feature, which release builds enable). moq-video = { version = "0.1.10", path = "rs/moq-video", default-features = false } -mp4-atom = "0.17.0" +mp4-atom = "0.17.1" nix = { version = "0.31.3", features = ["net", "socket", "uio"] } num_enum = "0.7" objc2 = "0.6.4" diff --git a/quest/m1/fuzz-findings/README.md b/quest/m1/fuzz-findings/README.md index 5663bf83975..9dc7400afcf 100644 --- a/quest/m1/fuzz-findings/README.md +++ b/quest/m1/fuzz-findings/README.md @@ -36,6 +36,4 @@ fixed the MKV block-header panic; its input is ## Required -- [moq-mux on a fixed mp4-atom](/quest/m1/fuzz-findings/mp4-atom-bump.md) - waiting on a 0.17.x release containing kixelated/mp4-atom#264 and #267, then moq-mux bumps to it and commits the `sgpd`, `iref` and `iloc` regressions -- [mp4-atom iloc is bounded](/quest/m1/fuzz-findings/mp4-atom-iloc.md) - zero-size `iloc` extents cannot allocate megabytes from a few bytes; fixed upstream by kixelated/mp4-atom#267, finished by the mp4-atom bump - [A catalog update reuses its encoder](/quest/m1/fuzz-findings/catalog-encoder.md) - republishing a compressed catalog or HLS group stops allocating a fresh DEFLATE encoder (about 200 KB) each time diff --git a/quest/m1/fuzz-findings/mp4-atom-bump.md b/quest/m1/fuzz-findings/mp4-atom-bump.md deleted file mode 100644 index aaf79e1397a..00000000000 --- a/quest/m1/fuzz-findings/mp4-atom-bump.md +++ /dev/null @@ -1,39 +0,0 @@ -# [XS] moq-mux on a fixed mp4-atom - -## Goal - -moq-mux depends on an mp4-atom release with the `sgpd` entry-count bound -from [kixelated/mp4-atom#264](https://github.com/kixelated/mp4-atom/pull/264) -and the `iloc` zero-size extent bound from -[kixelated/mp4-atom#267](https://github.com/kixelated/mp4-atom/pull/267), so -the 22-byte init that grows a Vec past 3 GB and the 31 bytes of `iloc` that -allocate about 6 MB in 0.17.0 fail to decode. The `cmaf` and `fmp4` targets' -`sgpd`, `iref` and `iloc` inputs are committed under -`rs/moq-mux/fuzz/regressions/`. - -## Plan - -#264 merged on 2026-10-30 as -kixelated/mp4-atom@06a532aabe47ea108343557fec4cf6c646c2a980, after 0.17.0 -was published, so 0.17.0 lacks it. #267 merged on 2026-11-03 as -kixelated/mp4-atom@2f1936206579b4c64d1d90538989246044e42ed6; the pending -release PR [kixelated/mp4-atom#260](https://github.com/kixelated/mp4-atom/pull/260) -picks up both. - -moq-mux is already on 0.17.0 -([moq-dev/moq#7308](https://github.com/moq-dev/moq/pull/7308)), which fixes -the `meta/iref.rs` subtract overflow (kixelated/mp4-atom#219), so what remains -is a move within 0.17 and the regression inputs. The `iloc` input is the -31-byte `cmaf` one from -[moq-dev/moq#7313's comment](https://github.com/moq-dev/moq/pull/7313#issuecomment-6520535160) -(`AB0AAAAcaWxvYwAAcwAAACAAyP////8AAAD///8AAA==`), committed as -`rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iloc`; on 0.17.0 the replay -reports 6,291,464 bytes allocated, and with #267 it passes. This quest's PR -also completes the [iloc quest](/quest/m1/fuzz-findings/mp4-atom-iloc.md) and -deletes it. - -Public API: none expected in moq-mux. Wire: none. - -## Waiting - -- Maintainer: cut an mp4-atom 0.17.x release that contains kixelated/mp4-atom@06a532aabe47ea108343557fec4cf6c646c2a980 and kixelated/mp4-atom@2f1936206579b4c64d1d90538989246044e42ed6 (agents do not cut it); check that crates.io's latest mp4-atom is above 0.17.0 and that its tag contains both commits diff --git a/quest/m1/fuzz-findings/mp4-atom-iloc.md b/quest/m1/fuzz-findings/mp4-atom-iloc.md deleted file mode 100644 index 3a00dd0b3ad..00000000000 --- a/quest/m1/fuzz-findings/mp4-atom-iloc.md +++ /dev/null @@ -1,33 +0,0 @@ -# [XS] mp4-atom iloc is bounded - -## Goal - -An `iloc` box costs mp4-atom heap proportional to its size: in the `cmaf` -target, 31 bytes of `iloc` no longer allocate about 6 MB. - -## Plan - -`src/meta/iloc.rs` decoded `extent_count` extents per item. When the -offset, length and index sizes are all 0, each extent consumes no input, so -every 6-byte item could push 65,535 extents (about 1.5 MB), and the item -count repeated that. - -[kixelated/mp4-atom#267](https://github.com/kixelated/mp4-atom/pull/267) -fixed it, merging on 2026-11-03 as -kixelated/mp4-atom@2f1936206579b4c64d1d90538989246044e42ed6: decode fails -with `OutOfBounds` on a zero-size extent after an item's first extent, so a -valid single zero-size extent still decodes (decided 2026-10-30, -maintainer). The pending release PR -[kixelated/mp4-atom#260](https://github.com/kixelated/mp4-atom/pull/260) -picks it up. - -What remains is moq-mux's move to that release and the 31-byte `cmaf` -regression input, both tracked in the -[mp4-atom bump](/quest/m1/fuzz-findings/mp4-atom-bump.md); its PR completes -this quest and deletes this file. - -Public API: none expected in moq-mux. Wire: none. - -## Waiting - -- Maintainer: cut an mp4-atom 0.17.x release that contains kixelated/mp4-atom@2f1936206579b4c64d1d90538989246044e42ed6 (agents do not cut it); check that crates.io's latest mp4-atom is above 0.17.0 and that its tag contains that commit diff --git a/rs/moq-mux/fuzz/regressions/README.md b/rs/moq-mux/fuzz/regressions/README.md index eec5b302c2a..48970ac1f5a 100644 --- a/rs/moq-mux/fuzz/regressions/README.md +++ b/rs/moq-mux/fuzz/regressions/README.md @@ -6,3 +6,16 @@ stable toolchain as part of `just check`, allocation bound included, so a bug fo fuzzing stays fixed without anyone running the fuzzer again. See [../README.md](../README.md) for the workflow. + +## mp4-atom inputs + +`cmaf/mp4-atom-{sgpd,iref,iloc}` preserve the minimized inputs from +[the fuzz findings](https://github.com/moq-dev/moq/pull/7313#issuecomment-6520535160), +including the two-byte init length. Their `fmp4/` counterparts replace that length +with a zero write-size selector and wrap the atoms in a `moov` box: streaming +ingest skips other top-level boxes. Both paths replay the malformed metadata with +the allocation bound enabled. + +mp4-atom 0.17.1 contains the `sgpd` entry-count and zero-size `iloc` extent +bounds; 0.17.0 already fixed the `iref` subtraction overflow. moq-mux keeps its aggregate +sample-group budget and relies on the dependency for per-entry bounds. diff --git a/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iloc b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iloc new file mode 100644 index 00000000000..df3b9b299e8 Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iloc differ diff --git a/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iref b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iref new file mode 100644 index 00000000000..cbb47ced29e Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-iref differ diff --git a/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-sgpd b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-sgpd new file mode 100644 index 00000000000..672c03e3220 Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/cmaf/mp4-atom-sgpd differ diff --git a/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iloc b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iloc new file mode 100644 index 00000000000..ae0ecbe2cd3 Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iloc differ diff --git a/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iref b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iref new file mode 100644 index 00000000000..c94c99d3ec9 Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-iref differ diff --git a/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-sgpd b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-sgpd new file mode 100644 index 00000000000..c5e4f6b270b Binary files /dev/null and b/rs/moq-mux/fuzz/regressions/fmp4/mp4-atom-sgpd differ diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 37f99f3a834..7552c86dcc3 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -768,25 +768,25 @@ impl Import { let (mut config, colr, pasp, spatial) = match codec { mp4_atom::Codec::Avc1(avc1) => ( self.init_h264(false, &avc1.avcc, &avc1.visual, container)?, - avc1.colr.as_ref(), + avc1.colr.as_slice(), avc1.pasp.as_ref(), avc1.spatial.as_deref(), ), mp4_atom::Codec::Avc3(avc3) => ( self.init_h264(true, &avc3.avcc, &avc3.visual, container)?, - avc3.colr.as_ref(), + avc3.colr.as_slice(), avc3.pasp.as_ref(), avc3.spatial.as_deref(), ), mp4_atom::Codec::Hev1(hev1) => ( self.init_h265(true, &hev1.hvcc, &hev1.visual, container)?, - hev1.colr.as_ref(), + hev1.colr.as_slice(), hev1.pasp.as_ref(), hev1.spatial.as_deref(), ), mp4_atom::Codec::Hvc1(hvc1) => ( self.init_h265(false, &hvc1.hvcc, &hvc1.visual, container)?, - hvc1.colr.as_ref(), + hvc1.colr.as_slice(), hvc1.pasp.as_ref(), hvc1.spatial.as_deref(), ), @@ -803,7 +803,12 @@ impl Import { config.coded_width = Some(vp08.visual.width as _); config.coded_height = Some(vp08.visual.height as _); config.container = container; - (config, vp08.colr.as_ref(), vp08.pasp.as_ref(), vp08.spatial.as_deref()) + ( + config, + vp08.colr.as_slice(), + vp08.pasp.as_ref(), + vp08.spatial.as_deref(), + ) } mp4_atom::Codec::Vp09(vp09) => { // https://github.com/gpac/mp4box.js/blob/325741b592d910297bf609bc7c400fc76101077b/src/box-codecs.js#L238 @@ -824,7 +829,12 @@ impl Import { config.coded_width = Some(vp09.visual.width as _); config.coded_height = Some(vp09.visual.height as _); config.container = container; - (config, vp09.colr.as_ref(), vp09.pasp.as_ref(), vp09.spatial.as_deref()) + ( + config, + vp09.colr.as_slice(), + vp09.pasp.as_ref(), + vp09.spatial.as_deref(), + ) } mp4_atom::Codec::Av01(av01) => { let mut config = VideoConfig::new(crate::codec::av1::av1_from_av1c(&av01.av1c)); @@ -832,14 +842,21 @@ impl Import { config.coded_width = Some(av01.visual.width as _); config.coded_height = Some(av01.visual.height as _); config.container = container; - (config, av01.colr.as_ref(), av01.pasp.as_ref(), av01.spatial.as_deref()) + ( + config, + av01.colr.as_slice(), + av01.pasp.as_ref(), + av01.spatial.as_deref(), + ) } mp4_atom::Codec::Unknown(unknown, _) => return Err(Error::UnknownCodec(*unknown).into()), unsupported => return Err(Error::UnsupportedCodec(Box::new(unsupported.clone())).into()), }; // The codec config outranks the colr box a muxer added, which only fills its gaps. - let colr = colr.map(super::colr_color).transpose()?.flatten(); - crate::codec::color::fill(&mut config.color, colr.as_ref(), "colr"); + for entry in colr { + let color = super::colr_color(entry)?; + crate::codec::color::fill(&mut config.color, color.as_ref(), "colr"); + } (config.projection, config.stereo) = super::spatial(spatial)?; if let Some(pasp) = pasp { let width = config.coded_width.ok_or(Error::InvalidAspect("missing width"))?; diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index e5de0c89f42..da326f51d4e 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -3659,6 +3659,23 @@ fn colr_fills_what_the_sps_leaves_unsaid() { assert_eq!(video.color, Some(color(9, 16, 9, true))); } +/// ICC profiles do not hide the CICP colour carried by another colr box. +#[test] +fn colr_after_an_icc_profile_fills_the_catalog() { + let init = avc1_init(crate::codec::h264::fixtures::SPS_IPB, Some(color(9, 16, 9, true))); + let (ftyp, mut moov) = decode_init(&init); + let mp4_atom::Codec::Avc1(avc1) = &mut moov.trak[0].mdia.minf.stbl.stsd.codecs[0] else { + panic!("expected avc1"); + }; + avc1.colr.insert(0, mp4_atom::Colr::Prof { profile: vec![1, 2, 3] }); + let mut init = Vec::new(); + ftyp.encode(&mut init).unwrap(); + moov.encode(&mut init).unwrap(); + let catalog = run_fmp4(&init); + let video = catalog.video.renditions.values().next().unwrap(); + assert_eq!(video.color, Some(color(9, 16, 9, true))); +} + /// A source that signals no colour adds no catalog field and no colr box. #[test] fn unsignalled_color_stays_absent() { @@ -3667,7 +3684,7 @@ fn unsignalled_color_stays_absent() { let mp4_atom::Codec::Avc1(avc1) = &moov.trak[0].mdia.minf.stbl.stsd.codecs[0] else { panic!("expected avc1"); }; - assert_eq!(avc1.colr, None); + assert!(avc1.colr.is_empty()); let catalog = run_fmp4(&init); let video = catalog.video.renditions.values().next().unwrap(); diff --git a/rs/moq-mux/src/container/fmp4/metadata.rs b/rs/moq-mux/src/container/fmp4/metadata.rs index a2ee38014a3..6dee8d2de80 100644 --- a/rs/moq-mux/src/container/fmp4/metadata.rs +++ b/rs/moq-mux/src/container/fmp4/metadata.rs @@ -110,7 +110,6 @@ impl Scan { self.groups += sgpd(data)?; return Ok(()); } - b"iloc" => return iloc(data), b"moov" | b"trak" | b"mdia" | b"minf" | b"stbl" | b"dinf" | b"edts" | b"mvex" | b"moof" | b"traf" | b"mfra" | b"iprp" | b"ipco" => Namespace::Any, b"udta" => Namespace::Udta, @@ -231,8 +230,7 @@ fn take<'a>(data: &mut &'a [u8], size: usize) -> Result<&'a [u8]> { Ok(body) } -// mp4-atom 0.17.0 still accepts zero-byte descriptions. Keep the upstream #264 bound here -// until our dependency includes it: every entry must be paid for by at least one input byte. +// Count descriptions across boxes before allocating the metadata tree. fn sgpd(mut data: &[u8]) -> Result { let version = u32::decode(&mut data)? >> 24; take(&mut data, 4)?; // grouping_type @@ -243,56 +241,9 @@ fn sgpd(mut data: &[u8]) -> Result { take(&mut data, 4)?; // default_group_description_index } let count = u32::decode(&mut data)?; - if count as usize > data.len() { - return Err(mp4_atom::Error::OutOfBounds.into()); - } Ok(count.into()) } -// The same dependency also lacks #267. A single zero-size extent names the whole reference; -// several would allocate without consuming input, and are refused before decoding any item. -fn iloc(mut data: &[u8]) -> Result<()> { - let version = u32::decode(&mut data)? >> 24; - let sizes0 = u8::decode(&mut data)?; - let sizes1 = u8::decode(&mut data)?; - let offset = sizes0 >> 4; - let length = sizes0 & 15; - let base = sizes1 >> 4; - let index = if version == 0 { 0 } else { sizes1 & 15 }; - if version > 2 - || [offset, length, base, index] - .iter() - .any(|size| ![0, 4, 8].contains(size)) - { - return Err(mp4_atom::Error::InvalidSize.into()); - } - let count = if version < 2 { - u32::from(u16::decode(&mut data)?) - } else { - u32::decode(&mut data)? - }; - for _ in 0..count { - take( - &mut data, - if version == 0 { - 4 - } else if version == 1 { - 6 - } else { - 8 - }, - )?; - take(&mut data, base as usize)?; - let extents = u16::decode(&mut data)? as usize; - let width = usize::from(offset + length + index); - if width == 0 && extents > 1 { - return Err(mp4_atom::Error::OutOfBounds.into()); - } - take(&mut data, extents * width)?; - } - Ok(()) -} - #[cfg(test)] pub(crate) mod tests { use super::*; @@ -398,7 +349,7 @@ pub(crate) mod tests { data.extend_from_slice(&0u32.to_be_bytes()); } data.extend_from_slice(&1u32.to_be_bytes()); - assert!(check(&boxed(b"sgpd", &data)).is_err()); + assert!(mp4_atom::Any::decode(&mut boxed(b"sgpd", &data).as_slice()).is_err()); } for version in 0..=2 { for count in [1u16, 2] { @@ -419,7 +370,10 @@ pub(crate) mod tests { } ]); data.extend_from_slice(&count.to_be_bytes()); - assert_eq!(check(&boxed(b"iloc", &data)).is_ok(), count == 1); + assert_eq!( + mp4_atom::Any::decode(&mut boxed(b"iloc", &data).as_slice()).is_ok(), + count == 1 + ); } } } diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 25244c88aac..53d6d7f108b 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -996,7 +996,7 @@ pub(crate) fn synthesize_video_trak( // Codecs that carry an out-of-band config record require `description`. let require_description = || description.ok_or_else(|| Error::MissingVideoDescription(config.codec.to_string())); - let colr = color_colr(config.color.as_ref()); + let colr = color_colr(config.color.as_ref()).into_iter().collect(); let sample_entry = match &config.codec { VideoCodec::H264(h264) => { let mut cursor = std::io::Cursor::new(require_description()?);