fix oob read in UTF32 LA from byte-scaled bounds check - #241
Open
zayeem06 wants to merge 1 commit into
Open
Conversation
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.
Out-of-bounds read in the UTF-32 LA routines
The lookahead bounds check in antlr3UTF32LA, antlr3UTF32LALE and antlr3UTF32LABE compares a byte pointer advanced by
la - 1, but the value it returns reads a 4-byte UTF-32 unit at indexla - 1. The limit moves one byte per lookahead while the read moves four, so anyLA(2)or greater near the end of a UTF-32 stream (a file opened with a UTF-32 BOM, or a UCS4 in-place stream) passes the check and reads up to four bytes past the input buffer. I scaled the check by the element size in all three functions so the whole unit atla - 1must sit inside the buffer; the 8-bit and EBCDIC paths are single-byte and were left as-is.I confirmed it with an ASAN reproducer built from the exact check and read against a one-code-point heap buffer:
LA(2)reports a heap-buffer-overflow read of size 4 before the patch and returns EOF after it. Worth a reviewer eye on the negative-lapath, which these routines still don't lower-bound, but that's pre-existing and separate from this fix.