Repository navigation
[FIX] matroska: accumulate EBML element IDs in an unsigned int - #2407
Open
pranayr710 wants to merge 1 commit into
Open
pranayr710 wants to merge 1 commit into
pranayr710 wants to merge 1 commit into
Conversation
Every EBML parse loop in matroska.c builds the element ID one byte at a
time into a signed int:
int code = 0, code_len = 0;
while (pos + len > get_current_byte(file))
{
code <<= 8;
code += mkv_read_byte(file);
On a well-formed file the switch matches within four bytes and code is
reset, but nothing resets it when no case matches, so the shift walks
past INT_MAX and signed overflow is undefined behaviour.
UBSan on a fuzzed MKV corpus reports it at four of the eight identical
loops:
matroska.c:489:8: runtime error: left shift of 8521680 by 8 places
cannot be represented in type 'int'
matroska.c:1229, matroska.c:1275, matroska.c:1963: same
Make code unsigned in all eight. EBML IDs are at most four bytes, every
case label is a positive constant, and the existing diagnostics already
print it with "0x%x", which expects unsigned. Unsigned wraparound is
well defined, so the accumulator keeps the behaviour the format implies.
Verified against master built from the same source in the same image:
UBSan over a 25-file fuzzed MKV corpus: 3 sites -> 0
Well-formed MKV: byte-identical extracted track, stable over 5 runs
Compiler warnings: 15 before, 15 after (all pre-existing)
Three truncated inputs differ in stdout, but master already differs from
itself on those: its diagnostics print the accumulator, whose value was
undefined precisely because of this bug. Repeated runs of master alone
produce the same set of hashes flagged in the comparison.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reason for this PR:
Sanity check:
What was wrong
Every EBML parse loop in
matroska.caccumulates the element ID a byte at a time into a signedint:On a well-formed file the
switchmatches within four bytes andcodeis reset. Nothing resets it when no case matches, so on malformed or truncated input the shift walks pastINT_MAX— signed overflow, which is undefined behaviour (C11 6.5.7p4).UBSan reports it at four of the eight identical loops:
The change
codebecomesunsigned intin all eight loops. EBML IDs are at most four bytes, everycaselabel is a positive constant, and the existing diagnostics already print it withmprint("... 0x%x ...", code), which expectsunsigned— so this also fixes a latent format mismatch. Unsigned wraparound is well defined.code_lenis left asint; it is only a counter.Reproducing
One gotcha that cost me time: CCExtractor writes each MKV subtitle track to a file next to the input, so with a read-only input directory it stops at
Error: Cannot create output file for subtitle trackbefore reaching the parser loops. The input directory has to be writable to reach this code.Verification
Built master and this branch from identical source in the same image.
a58ba9a536a58ba9a536(stable over 5 runs each)One thing to be straight about: three truncated inputs show differing stdout between the two builds — but master already differs from itself on those. Its "Unknown element 0x%x" diagnostic prints the accumulator, whose value was undefined precisely because of this bug. Running master alone five times produces the same set of hashes that the comparison flagged:
So that is pre-existing nondeterminism on malformed input, not a change in behaviour. Well-formed files are byte-identical.
This is UB a compiler is entitled to exploit rather than an observed misbehaviour — on x86/GCC today the shift does what you would naively expect. Same category as #2353.