Repository navigation
Shorten: avoid signed integer overflow in getRiceGolombCode - #1496
Merged
Merged
Conversation
VariableLengthInput::getRiceGolombCode accumulated the Rice-Golomb quotient in a signed int32_t and then shifted it left. The quotient comes from the unary prefix of the bitstream, which is attacker- controlled, so a long prefix makes the value large and the left shift overflows a signed int (undefined behaviour). Accumulate the quotient in an unsigned uint32_t so the shifts are well-defined, and convert back to int32_t on return. Behaviour is unchanged for valid input. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Contributor
|
Thanks! |
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.
Fixes the signed-integer overflow reported in #1495.
VariableLengthInput::getRiceGolombCodebuilds the Rice-Golomb quotient in a signedint32_t(result) and then left-shifts it (result << k). The quotient comes from the unary prefix of the bitstream, which is attacker-controlled, so a long prefix makesresultlarge and the left shift overflows a signedint— undefined behaviour (UBSan aborts atshortenfile.cpp:135). It is reachable from the publicFileRefAPI on an untrusted file.This changes
resultto an unsigneduint32_tso the shifts are well-defined (unsigned overflow is defined in C++), and casts back toint32_ton return. Behaviour is unchanged for valid input; only the undefined behaviour on malformed input is removed.Verification
58499cbwith-fsanitize=address,undefined. The reproducer from Shorten: signed integer overflow in getRiceGolombCode (attacker-controlled input) #1495 aborts atshortenfile.cpp:135before this change and runs clean after it.tests/data: identical results before and after (0 regressions).Found via local libFuzzer + ASan/UBSan fuzzing.