Fix buffer overflow with argument parsing (fixes #1219).#1229
Open
ali-mhamza wants to merge 1 commit into
Open
Fix buffer overflow with argument parsing (fixes #1219).#1229ali-mhamza wants to merge 1 commit into
ali-mhamza wants to merge 1 commit into
Conversation
Author
|
After opening this PR, I found a simpler approach to fix this bug/vulnerability. While the approach in the commit works, it involves multiple moving parts and changes multiple functions. Instead, we can simply place a bounds-check on the instruction used as an index into stackEffects, as follows (added lines marked with ***):
However, this solution introduces an extra conditional check for each instruction emitted within the compiler, which may lead to some (minor) compile-time overhead. |
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.
Summary
Given enough comma-separated arguments (in lists, functions, etc.), a buffer overflow is triggered when
emitOp(called from withinemitShortArgfromcallSignature) is called (eventually).Root Cause
The calls to
validateNumParametersin multiple locations unconditionally increments the signature arity (signature->arity). While this does prevent the function from reporting a "Methods cannot have more than 16 parameters" error more than once for a particular parsing phase, it causes the arity field to increase without bound.In
callSignaturecalled fromsubscript, this arity is then added to the instructionCODE_CALL_0, which has integer equivalent value 24. With enough commas, the arity in the signature can increase to 53, which causes this sum to equal 77. The sum is then used inemitOpcalled fromemitShortArg(called fromcallSignature) as an index into stackEffects, which only contains 77 elements. Thus, any arity value of 53 or higher leads to an out-of-index buffer overflow error.In the Wren test script used in the issue, the arity reaches 54 before the buffer overflow occurs.
Fix
The main issue here stems from the unconditional increment for
signature->arity, even after reporting a maximum-parameter error. Thus,validateNumParametersnow returns a Boolean flag indicating whether or not an error was reported (even if it was from similar errors previously reported in the same parsing phase). If an error had been reported, we set the arity to -1.This has two purposes:
validateNumParametersthat an error has been reported to silence it from reporting further errors in the same parsing phase.Testing