encoding/wkb: prevent panic on overflowing point count - #179
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
unmarshalPoints validated the buffer size with len(data) < int(num*16), but num is a uint32 taken straight from the input so num*16 is computed in uint32 and can wrap to a small value. A linestring (or polygon ring, or ewkb geometry) header claiming e.g. 0x10000001 points made the product wrap to 16, so an undersized buffer passed the guard and the read loop then indexed past the end of the slice, panicking. Do the multiplication in 64-bit space so the bounds check is accurate, and add a regression test.
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
Unmarshalpanics with an out-of-range slice index on some malformed WKB input. The header carries a 32-bit point count that is read straight from the bytes, and the length check that is supposed to reject truncated buffers overflows, so an undersized buffer slips through and the read loop indexes past the end of the slice.In
wkbcommon.unmarshalPoints:numis auint32, sonum * 16is evaluated inuint32and wraps modulo 2^32. A header claiming0x10000001points makes the product wrap to16, so a 16-byte buffer passes the guard; the loop then runsint(num)(~268M) iterations and readsdata[16*i:], panicking as soon asiwalks off the end.Reproducer (a 25-byte input):
This is reachable from the documented
wkb.Unmarshal/ewkb.Unmarshalentry points (linestrings and polygon rings both go throughunmarshalPoints), so parsing untrusted WKB can crash the caller.Fix
Do the multiplication in 64-bit space so the bounds check is accurate:
Valid input is unaffected — for any count that legitimately fits the buffer the comparison is identical. Truncated input now returns
ErrNotWKBinstead of panicking. Added a regression test, andgo test ./...passes.