avoid out-of-bounds opcode read in JS_ReadFunctionBytecode#519
Open
uwezkhan 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.
JS_ReadFunctionBytecode walks the serialized opcode stream and, for the atom formats, reads a 4-byte operand at get_u32(bc_buf + pos + 1) and rewrites it with put_u32. The loop only checks pos < bc_len, not that the whole opcode fits, so a function whose byte_code ends in a truncated atom opcode reads and relocates up to four bytes past the bytecode allocation. JS_ReadObject with JS_READ_OBJ_BYTECODE reaches this; ASan reports a heap-buffer-overflow READ of size 4 on a 17-byte blob whose 1-byte byte_code is a single OP_push_atom_value. bc_byte_swap has the same unchecked walk on big-endian targets.
Before, the reader trusted the opcode size to stay within byte_code_len and stepped pos past the end only after touching the operand. After, each iteration rejects an opcode that would extend past bc_len before any operand is read, and rewinds b->byte_code_len to the validated prefix so atom cleanup frees only what was already relocated, matching the existing truncated-atom error path. The check sits in the reader because the operand bytes come straight from the input buffer and no caller can make a short opcode whole. Tradeoff: a deliberately truncated final opcode now fails the load with a SyntaxError instead of being silently tolerated.