From d4390b9b6ea985c910ed59836cebd778ab9c27d9 Mon Sep 17 00:00:00 2001 From: Matthew Gregan Date: Wed, 25 Nov 2026 10:28:09 +1300 Subject: [PATCH] Limit ES descriptor nesting depth --- mp4parse/src/lib.rs | 18 +++++++++++++++--- mp4parse/src/tests.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/mp4parse/src/lib.rs b/mp4parse/src/lib.rs index 33dda4c0..21e751cd 100644 --- a/mp4parse/src/lib.rs +++ b/mp4parse/src/lib.rs @@ -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; @@ -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()?; @@ -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(()) => {} @@ -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()?; @@ -5735,6 +5740,7 @@ fn read_dc_descriptor( &data[des.position().try_into()?..data.len()], esds, strictness, + remaining_depth, )?; } @@ -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); @@ -5788,6 +5795,7 @@ fn read_es_descriptor( &data[des.position().try_into()?..data.len()], esds, strictness, + remaining_depth, )?; } @@ -5796,12 +5804,16 @@ fn read_es_descriptor( /// See MP4 (ISO 14496-14:2020) ยง 6.7.2 fn read_esds(src: &mut BMFFBox, strictness: ParseStrictness) -> Result { + // 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; diff --git a/mp4parse/src/tests.rs b/mp4parse/src/tests.rs index d9fdcdfc..880afe6b 100644 --- a/mp4parse/src/tests.rs +++ b/mp4parse/src/tests.rs @@ -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.