Fix stack overflow in desugarer via depth limit#1318
Open
sharadboni wants to merge 1 commit intogoogle:masterfrom
Open
Fix stack overflow in desugarer via depth limit#1318sharadboni wants to merge 1 commit intogoogle:masterfrom
sharadboni wants to merge 1 commit intogoogle:masterfrom
Conversation
The parser's parseInfix function builds chains of Binary nodes using a while loop without incrementing the parser depth counter. This produces arbitrarily deep AST trees that the desugarer then walks recursively, causing a stack overflow crash on deeply nested expressions. Add a depth counter to the desugar() function that increments on each recursive call and throws a StaticError when the depth exceeds 500. This prevents the stack overflow while still allowing reasonable nesting depths for normal programs. The same deeply nested AST also affects recursive walkers in static_analysis.cpp, vm.cpp (countLeaves, findObject, objectFieldsAux, otherJsonToHeap), but the desugarer runs first and now rejects these inputs before they reach those code paths.
Author
|
@johnbartholomew Could you review this security fix? It adds a depth limit to the desugarer to prevent stack overflow from deeply nested binary expression trees that bypass the parser's depth counter. |
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
The parser's
parseInfixfunction builds chains ofBinarynodes using a while loop without incrementing the parser depth counter. This produces arbitrarily deep AST trees. The desugarer atcore/desugarer.cpp:685then recursively walks this tree viadesugar(), causing a stack overflow crash on crafted inputs with deeply nested binary expressions.Similarly affected recursive walkers include
core/static_analysis.cppand severalvm.cppfunctions (countLeaves,findObject,objectFieldsAux,otherJsonToHeap), but the desugarer runs first and now rejects these inputs.Root Cause
parseInfixuses iteration (a while loop) to parse operator chains, so the parser's own depth limit never triggers. The resulting AST can be thousands of levels deep, but subsequent passes walk it with unbounded recursion.Fix
depthcounter parameter to thedesugar()function (and its helper methodsdesugarParams,desugarFields,makeObject,makeObjectComprehension)StaticError("Maximum expression nesting depth exceeded during desugaring.")when depth exceeds 500desugarFile,stdlibAST) start at depth 0 via the default parameterThe limit of 500 is generous enough for any realistic Jsonnet program while preventing stack exhaustion.
Reproduction
Test plan