From fe98f596caf3b7e2653e59da695f6357aa444c24 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 13:34:47 -0700 Subject: [PATCH 1/2] fix: reject sgpd entry counts past the box A version 0 entry of an unknown grouping type reads to the end of the box, so every later entry decodes empty for free, and a 20-byte sgpd declaring 0xffff0000 entries grew a Vec past 3 GB. Every entry takes at least a byte, so a count above the remaining bytes is now OutOfBounds. Found by moq's media fuzz targets (moq-dev/moq#7313). Co-Authored-By: Claude Opus 5.5 --- src/moov/trak/mdia/minf/stbl/sgpd.rs | 42 +++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/moov/trak/mdia/minf/stbl/sgpd.rs b/src/moov/trak/mdia/minf/stbl/sgpd.rs index fb67b46b..86ade110 100644 --- a/src/moov/trak/mdia/minf/stbl/sgpd.rs +++ b/src/moov/trak/mdia/minf/stbl/sgpd.rs @@ -64,7 +64,14 @@ impl AtomExt for Sgpd { None }; let entry_count = u32::decode(buf)?; - let mut entries = Vec::with_capacity((entry_count as usize).min(1024)); + // Every entry takes at least a byte, so reject counts that cannot fit before + // looping: a version 0 entry of an unknown grouping type reads to the end of the + // box, leaving every later entry empty and free to decode, so a 20-byte box could + // otherwise push billions of them. + if entry_count as u64 > buf.remaining() as u64 { + return Err(Error::OutOfBounds); + } + let mut entries = Vec::with_capacity(entry_count as usize); for _ in 0..entry_count { // Spec states: if version>=1 && default_length==0 // But, default_length.is_some(), if and only if version>=1, so fine to just check for @@ -286,6 +293,39 @@ mod tests { ); } + #[test] + fn sgpd_rejects_entry_count_past_the_box() { + // Version 0 of an unknown grouping type, declaring 0xffff0000 entries in a + // 20-byte box: the first reads nothing, and every later one would be empty. + const HUGE_COUNT: &[u8] = &[ + 0x00, 0x00, 0x00, 0x14, 0x73, 0x67, 0x70, 0x64, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, + ]; + + let err = Sgpd::decode(&mut Cursor::new(HUGE_COUNT)).unwrap_err(); + assert!(matches!(err, Error::OverDecode(_)), "{err:?}"); + } + + #[test] + fn sgpd_version_0_unknown_entries_still_decode() { + // Two version 0 entries of an unknown type: the first reads the rest of the + // box, as before, and the count still fits the bytes left. + const VERSION_0: &[u8] = &[ + 0x00, 0x00, 0x00, 0x18, 0x73, 0x67, 0x70, 0x64, 0x00, 0x00, 0x00, 0x00, 0x72, 0x6F, + 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFE, + ]; + + let sgpd = Sgpd::decode(&mut Cursor::new(VERSION_0)).expect("sgpd should decode"); + assert_eq!(sgpd.entries.len(), 2); + assert_eq!( + sgpd.entries[0].entry, + AnySampleGroupEntry::UnknownGroupingType( + FourCC::from(b"roll"), + vec![0xFF, 0xFF, 0xFF, 0xFE] + ) + ); + } + #[test] fn sgpd_essential_requires_version_3_defaults() { let mut sgpd = Sgpd { From b1231668112261c605ec99da8bd11c2db803b91a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 13:43:14 -0700 Subject: [PATCH 2/2] fix: keep the sgpd entry capacity cap Co-Authored-By: Claude Opus 5.5 --- src/moov/trak/mdia/minf/stbl/sgpd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/moov/trak/mdia/minf/stbl/sgpd.rs b/src/moov/trak/mdia/minf/stbl/sgpd.rs index 86ade110..4de3c607 100644 --- a/src/moov/trak/mdia/minf/stbl/sgpd.rs +++ b/src/moov/trak/mdia/minf/stbl/sgpd.rs @@ -71,7 +71,7 @@ impl AtomExt for Sgpd { if entry_count as u64 > buf.remaining() as u64 { return Err(Error::OutOfBounds); } - let mut entries = Vec::with_capacity(entry_count as usize); + let mut entries = Vec::with_capacity((entry_count as usize).min(1024)); for _ in 0..entry_count { // Spec states: if version>=1 && default_length==0 // But, default_length.is_some(), if and only if version>=1, so fine to just check for