Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions mp4parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5420,6 +5420,7 @@ fn find_descriptor(
data: &[u8],
esds: &mut ES_Descriptor,
strictness: ParseStrictness,
remaining_depth: u8,
) -> Result<()> {
// Tags for elementary stream description
const ESDESCR_TAG: u8 = 0x03;
Expand All @@ -5430,6 +5431,9 @@ fn find_descriptor(

// Descriptor length should be more than 2 bytes.
while remains.len() > 2 {
let remaining_depth = remaining_depth
.checked_sub(1)
.ok_or(Status::EsdsBadDescriptor)?;
let des = &mut Cursor::new(remains);
let tag = des.read_u8()?;

Expand Down Expand Up @@ -5460,10 +5464,10 @@ fn find_descriptor(

match tag {
ESDESCR_TAG => {
read_es_descriptor(descriptor, esds, strictness)?;
read_es_descriptor(descriptor, esds, strictness, remaining_depth)?;
}
DECODER_CONFIG_TAG => {
read_dc_descriptor(descriptor, esds, strictness)?;
read_dc_descriptor(descriptor, esds, strictness, remaining_depth)?;
}
DECODER_SPECIFIC_TAG => match read_ds_descriptor(descriptor, esds, strictness) {
Ok(()) => {}
Expand Down Expand Up @@ -5715,6 +5719,7 @@ fn read_dc_descriptor(
data: &[u8],
esds: &mut ES_Descriptor,
strictness: ParseStrictness,
remaining_depth: u8,
) -> Result<()> {
let des = &mut Cursor::new(data);
let object_profile = des.read_u8()?;
Expand All @@ -5735,6 +5740,7 @@ fn read_dc_descriptor(
&data[des.position().try_into()?..data.len()],
esds,
strictness,
remaining_depth,
)?;
}

Expand Down Expand Up @@ -5763,6 +5769,7 @@ fn read_es_descriptor(
data: &[u8],
esds: &mut ES_Descriptor,
strictness: ParseStrictness,
remaining_depth: u8,
) -> Result<()> {
let des = &mut Cursor::new(data);

Expand All @@ -5788,6 +5795,7 @@ fn read_es_descriptor(
&data[des.position().try_into()?..data.len()],
esds,
strictness,
remaining_depth,
)?;
}

Expand All @@ -5796,12 +5804,16 @@ fn read_es_descriptor(

/// See MP4 (ISO 14496-14:2020) § 6.7.2
fn read_esds<T: Read>(src: &mut BMFFBox<T>, strictness: ParseStrictness) -> Result<ES_Descriptor> {
// A typical decoder configuration uses three descriptor levels. Leave room
// for unusual files while bounding native stack usage.
const MAX_DESCRIPTOR_DEPTH: u8 = 16;

let (_, _) = read_fullbox_extra(src)?;

let esds_array = read_buf(src, src.bytes_left())?;

let mut es_data = ES_Descriptor::default();
find_descriptor(&esds_array, &mut es_data, strictness)?;
find_descriptor(&esds_array, &mut es_data, strictness, MAX_DESCRIPTOR_DEPTH)?;

es_data.codec_esds = esds_array;

Expand Down
32 changes: 32 additions & 0 deletions mp4parse/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,38 @@ fn read_esds_invalid_descriptor() {
}
}

#[test]
fn find_descriptor_respects_depth_limit() {
let aac_esds = [
0x03, 0x16, 0x00, 0x01, 0x00, // ES descriptor
0x04, 0x11, 0x40, 0x15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Decoder config
0x05, 0x02, 0x11, 0x90, // Decoder-specific info
];

for strictness in [
ParseStrictness::Permissive,
ParseStrictness::Normal,
ParseStrictness::Strict,
] {
for remaining_depth in [1, 2] {
let result = super::find_descriptor(
&aac_esds,
&mut super::ES_Descriptor::default(),
strictness,
remaining_depth,
);
assert!(matches!(
result,
Err(Error::InvalidData(Status::EsdsBadDescriptor))
));
}

let mut esds = super::ES_Descriptor::default();
super::find_descriptor(&aac_esds, &mut esds, strictness, 3).unwrap();
assert_eq!(esds.audio_codec, super::CodecType::AAC);
}
}

#[test]
fn read_esds_redundant_descriptor() {
// the '2' at the end is redundant data.
Expand Down
Loading