Fix out-of-bounds access in EXI bitstream bounds check#129
Open
lippit wants to merge 2 commits into
Open
Conversation
exi_bitstream_has_overflow only advanced and validated byte_pos when bit_count reached a full byte (EXI_BITSTREAM_MAX_BIT_COUNT). It never checked that the byte about to be accessed was within the stream before exi_bitstream_read_bit and exi_bitstream_write_bit dereference data[byte_pos]. This allowed two out-of-bounds accesses: - Empty input (data_size == 0): the first bit access dereferences data[0] on a zero-length buffer. Every message is decoded by first reading the 8-bit EXI header, so all decoders are reachable with a zero-byte input. - End-of-buffer off-by-one: when bit_count rolls over at the last valid byte, byte_pos is advanced to data_size (one past the end) and the next access touches data[data_size]. Signed-off-by: Georg Lipič <georg.lippitsch@gmx.at>
Up to standards ✅🟢 Issues
|
When left-shifting uint8_t bytes from stream_data, each operand is implicitly promoted to signed int. Shifting a value into the sign bit (e.g. stream_data[4] << 24) is undefined behavior in C, which can lead to incorrect payload length and payload id values depending on the compiler. Cast the operands to uint16_t / uint32_t before shifting so the operations are performed on unsigned types of the intended width. Signed-off-by: Georg Lipič <georg.lippitsch@gmx.at>
Contributor
|
Good catch. I will take a look and do some testing. |
Author
|
Happy to also submit a PR to the main project with regenerate files. |
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.
Fix out-of-bounds access in EXI bitstream bounds check and undefined behavior in V2GTP header parsing
Fix 1:
exi_bitstream_has_overflow only advanced and validated byte_pos when bit_count reached a full byte (EXI_BITSTREAM_MAX_BIT_COUNT). It never checked that the byte about to be accessed was within the stream before exi_bitstream_read_bit and exi_bitstream_write_bit dereference data[byte_pos]. This allowed two out-of-bounds accesses:
Fix 2:
When left-shifting uint8_t bytes from stream_data, each operand is implicitly promoted to signed int. Shifting a value into the sign bit (e.g. stream_data[4] << 24) is undefined behavior in C, which can lead to incorrect payload length and payload id values depending on the compiler.
Describe your changes
Add a bounds check after the byte-advance block that rejects when byte_pos >= data_size. It is placed after the advance so it validates the position actually about to be dereferenced, and it runs even when bit_count is 0, so empty input and a bad init offset are caught as well. The now-redundant else-return is removed, since the new check covers it; the increment stays guarded so byte_pos is not pushed past the end on the overflow path.
Cast the operands to uint16_t / uint32_t before shifting so the operations are performed on unsigned types of the intended width.
Checklist before requesting a review