Skip to content

Decoding robustness: track numbers, audio size, bounded reading, FFV1 - #558

Merged
JeromeMartinez merged 4 commits into
MediaArea:mainfrom
JeromeMartinez:Decoding_Robustness
Nov 23, 2026
Merged

JeromeMartinez merged 4 commits into
MediaArea:mainfrom
JeromeMartinez:Decoding_Robustness

Conversation

@JeromeMartinez

Copy link
Copy Markdown
Member

Fixes found during the work on the Photoshop layers, independent from it.

  • Audio: missing or extra audio blocks in the compressed file (e.g. lost during a remux) gave a shorter or longer WAV/AIFF without error, --check included when the file was encoded without --hash. The data size declared in the header (data chunk, ds64 for RF64, SSND chunk) is now compared to the size of the decoded audio data.
  • Matroska: the track number of a block was read on 1 byte (tracks after the 127th not decodable) and used as the position of the track entry (blocks given to the wrong tracks when the track numbers are not in the order of the track entries, e.g. video decoded as FLAC). It is now read as an EBML variable-size integer and mapped to the track entry declaring this TrackNumber.
  • Reversibility data and attachments: the zlib content was inflated into a buffer of the declared size without limit (uncaught std::bad_alloc, gigabytes allocated, truncation in 32-bit builds) and a content not inflated correctly was used partially. The size is now bounded by the maximal deflate ratio, memory errors are caught, and unreadable content is empty. New file size slots are zeroed, and no null pointer is given to memcpy()/memset()/memcmp() with a size of 0.
  • FFV1: slices allocated when the frame size is known and the slice grid is checked (an unchecked grid could allocate gigabytes), memory errors end the frame with an error, 64-bit slice boundaries, slices with an invalid header or without decoding states not decoded instead of using a null pointer.

…header

Missing or extra audio blocks in the compressed file (e.g. blocks lost
during a remux) gave a shorter or longer WAV/AIFF file without error,
--check included when the file was encoded without --hash.
The data size declared in the header of the file (data chunk, ds64 for
RF64, SSND chunk) is now compared to the size of the decoded audio data
at the end of the track.
The zlib content of the reversibility data and of the attachments was
inflated into a buffer of the size declared in the file, without limit:
a crafted size could abort with an uncaught std::bad_alloc or allocate
gigabytes, a size above 4 GiB was truncated in 32-bit builds, and a
content not inflated correctly was used partially (e.g. as the header
of a restored file).
The declared size is now refused above the maximal deflate ratio or
above the addressable size, memory errors are caught, and the content
is empty when it is not readable. The attachments use the same reader.

Also:
- new slots of the file sizes are zeroed (frames without file size),
- no null pointer is given to memcpy()/memset()/memcmp() with a size of
  0 (empty buffers, empty file names).
The track number of a block was read as a 1-byte value, so the tracks
after the 127th could not be decoded, and it was used as the position
of the track entry, so a file with track numbers not in the order of
the track entries (e.g. remuxed) had its blocks given to the wrong
tracks (e.g. video decoded as FLAC).
The track number of a block is now read as an EBML variable-size
integer and mapped to the track entry declaring this TrackNumber.
The RawCookedTrack elements still describe the track entries by
position.
- The slices are allocated with the first frame of each slot, when the
  frame size is known and the slice grid is checked against it, instead
  of with the configuration record (an unchecked grid could allocate
  gigabytes or overflow the count of slices).
- Memory errors end the frame with an error instead of terminating.
- The slice boundaries are computed with 64-bit products (overflow with
  big frames or many slices).
- A slice with an invalid header is not decoded (its position is
  unknown), and a slice without decoding states (error in its keyframe)
  is not decoded instead of using a null pointer.
@JeromeMartinez
JeromeMartinez merged commit 0b4be29 into MediaArea:main Nov 23, 2026
3 checks passed
@JeromeMartinez
JeromeMartinez deleted the Decoding_Robustness branch November 23, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant