Repository navigation
Conversation
Both S2Polygon decoders keep the per-loop depth exactly as encoded. GetParent() assumes the first loop has depth 0 and that depths never skip a level, so a crafted depth makes it return -1 and IsNormalized() then reads loops_[-1]. Reject the depths that FindValidationError() already treats as invalid while decoding, after empty loops have been skipped.
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.
S2Polygon::DecodeUncompressed and S2Polygon::DecodeCompressed take each loop's depth straight from the encoding (a raw 32-bit value in the uncompressed format, a varint in the compressed one) and keep it without any check. GetParent() relies on the first loop having depth 0 and on depths never skipping a level, so for an encoded polygon whose first loop has depth 1, or any loop with a negative depth, it returns -1 and IsNormalized() goes on to dereference loop(-1). I found it while reading the decoders next to the S2PolygonDecodeTest fuzz cases, which decode arbitrary bytes with s2debug disabled and expect that to be safe; in an ASan build, calling IsNormalized() on such a decoded polygon reports a heap-buffer-overflow 8 bytes before the loops_ allocation. Release builds skip the IsValid() check that InitIndex() runs under --s2debug, so nothing sits between the encoded depth and that lookup. Both decoders now reject the depths FindValidationError() already treats as invalid (negative, or more than one level deeper than the previous loop), checked after empty loops are dropped so existing encodings keep decoding. The new DecodeRejectsInvalidLoopDepth test covers both encodings; it fails on master and passes with the change. The rest of the suite is unchanged, apart from S2PointIndex.EmptyData and ExactFloatTest.frexp, which fail identically on master in my Windows/clang-cl build.