Skip to content

reject invalid loop depths when decoding S2Polygon - #714

Open
sayed0200 wants to merge 1 commit into
google:masterfrom
sayed0200:s2polygon-decode-loop-depth
Open

sayed0200 wants to merge 1 commit into
google:masterfrom
sayed0200:s2polygon-decode-loop-depth

Conversation

@sayed0200

Copy link
Copy Markdown

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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant