Skip to content

Commit

Permalink
mkv: Return error instead of panicing when reading the wrong EBML ele…
Browse files Browse the repository at this point in the history
…ment.

The SeekHeadElement may contain an invalid element type and position
pair. We can't assume what we are reading at the the position is
the stated element type. Since this was an assertion a malicious or
broken file could trigger it. Return an error instead.

Fixes #201.
  • Loading branch information
pdeljanov committed Feb 22, 2024
1 parent 8f89962 commit 37cf4d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions symphonia-format-mkv/src/demuxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ impl FormatReader for MkvReader {

for (etype, pos) in seek_positions {
it.seek(pos)?;

// Safety: The element type or position may be incorrect. The element iterator will
// validate the type (as declared in the header) of the element at the seeked
// position against the element type asked to be read.
match etype {
ElementType::Tracks => {
segment_tracks = Some(it.read_element::<TracksElement>()?);
Expand Down
10 changes: 5 additions & 5 deletions symphonia-format-mkv/src/ebml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ impl<R: ReadBytes> ElementIterator<R> {
/// [Self::read_header] or [Self::read_child_header].
pub(crate) fn read_element_data<E: Element>(&mut self) -> Result<E> {
let header = self.current.expect("EBML header must be read before calling this function");
assert_eq!(
header.etype,
E::ID,
"EBML element type must be checked before calling this function"
);

// Ensure the EBML element header has the same element type as the one being read.
if header.etype != E::ID {
return decode_error("mkv: unexpected EBML element");
}

let element = E::read(&mut self.reader, header)?;
// Update position to match the position element reader finished at
Expand Down

0 comments on commit 37cf4d8

Please sign in to comment.