Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/moov/trak/mdia/minf/stbl/sgpd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ impl AtomExt for Sgpd {
None
};
let entry_count = u32::decode(buf)?;
// 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);
Comment on lines +71 to +72

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow valid zero-byte version 0 sample group entries

For an HEVC track using a version 0 sgpd with one tsas or stsa entry, this check rejects a valid box: these sample group entries have empty payloads, as defined in ISO/IEC 14496-15, sections 8.4.6.2.2 and 8.4.7.2.2. Such a box has entry_count == 1 and zero remaining bytes; previously, AnySampleGroupEntry::decode preserved it as an empty unknown entry, but now decoding returns OverDecode(sgpd), also preventing the containing track or fragment from decoding. Account for legitimate zero-byte grouping types rather than applying the one-byte minimum universally.

Useful? React with 👍 / 👎.

}
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
Expand Down Expand Up @@ -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 {
Expand Down
Loading