Make FieldMaskTree walks over deep mask trees stack safe - #30447
Open
ManoharPaturi wants to merge 1 commit into
Open
ManoharPaturi wants to merge 1 commit into
ManoharPaturi wants to merge 1 commit into
Conversation
Nothing bounds the depth of a FieldMask path, so a hostile mask such as one hundred thousand dotted segments over a recursive field drives unbounded recursion in FieldMaskTree::MergeMessage and AddRequiredFieldPath and overflows the stack. Convert both walks to explicit work stacks, mirroring what ClearChildren() and ForEachLeaf() in this file already do to stay stack safe on deep trees, and add a regression test that crashed with SIGSEGV before the change.
This branch has not been deployed
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.
Nothing bounds the depth of a FieldMask path. AddPath() builds the tree iteratively and accepts arbitrarily deep dotted paths, and ClearChildren(), ForEachLeaf() and the node destructor in field_mask_util.cc were already written iteratively with comments noting they must stay stack safe on very deep trees. Two walks did not follow that pattern and recursed once per tree level: FieldMaskTree::MergeMessage and FieldMaskTree::AddRequiredFieldPath.
Because AddPath() performs no schema validation, a hostile mask over any message with a recursive optional field is enough to drive the recursion. For example, one hundred thousand dotted "a" segments against proto2_unittest::TestRecursiveMessage makes FieldMaskUtil::MergeMessageTo overflow the stack, since MergeMessage descends one frame per tree level and materializes a submessage at each step. AddRequiredFieldPath, reached through FieldMaskUtil::TrimMessage with keep_required_fields, recurses the same way over the tree even when the message itself is shallow.
Both walks now use an explicit work stack, mirroring the style of ClearChildren() and ForEachLeaf() in the same file. Processing order within a node is unchanged; only the order in which independent subtrees are visited differs, and each visited field of a given message is distinct, so the merge and trim results are identical.
Testing: added FieldMaskUtilTest.StackSafeDeepMaskPath, which merges a 100000 segment mask into arena allocated TestRecursiveMessage instances and then trims with keep_required_fields set. Before the change the test process dies with SIGSEGV (exit 139) from stack exhaustion inside MergeMessage; with the change the full FieldMaskUtilTest suite passes, 22 tests locally. The arena matters only for teardown of the very deep destination chain that the merge materializes.