S2ShapeIndexCell::Decode: Bound untrusted counts - #682
Open
sushant-me wants to merge 1 commit into
Open
sushant-me wants to merge 1 commit into
sushant-me wants to merge 1 commit into
Conversation
Decode() trusted three counts taken straight from the input: - Single-shape path: `int num_edges = header >> 3` overflowed int for a large header. The count was widened back to uint32_t in S2ClippedShape::Init(), which requested up to 16 GiB (observed as std::bad_alloc for a header whose num_edges is 0xFFFFFFFF); for num_edges == 2**31 it wrapped to zero, so a malformed cell was accepted with a bogus edge count. - Multi-shape path: `num_clipped` was passed straight to add_shapes(), which resized the cell's shape array to an attacker-controlled size. - Multi-shape path: an accumulated shape id was never compared against num_shape_ids, so a cell could hold a shape id that later indexed the shapes out of bounds (the first branch checked against INT_MAX only). Reject all three. There is deliberately no bound on the edge count against the remaining input: edges are run-length encoded (see EncodeEdges), so a count of 8 or more is followed by a varint holding the rest of the run length and a few bytes can legitimately describe many edges. DecodeEdges() already fails cleanly when the input is exhausted. The tests decode the malformed cells directly, so they do not touch the little-endian-only encoded point vector format and are valid on the big-endian CI job.
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.
Split out of #675 at jmr's request, so the parts that were agreed on can be reviewed and submitted separately. This is the
src/s2/s2shape_index.cchalf only: theloop_startsvalidation is #681, and the "return an empty cell instead of nullptr" change is not included here.What the malformed input does before this change
S2ShapeIndexCell::Decode()uses three counts taken straight from the input, none of which is checked against the index or against the type it is stored in. All three are reachable from a cell of a few bytes.int num_edges = header >> 3;overflowsintfor a large header. Fornum_edges == 0xFFFFFFFFthe value becomes-1, whichS2ClippedShape::Init()receives asuint32_t num_edgesand turns intonew int32_t[0xFFFFFFFF]. Under AddressSanitizer:That is a 16 GiB allocation requested by a 5-byte cell header; where it cannot be satisfied the
std::bad_allocescapesDecode(). Fornum_edges == 2**31the count truncates to0in the 31-bitnum_edges_field instead, andDecode()returnstruewith a clipped shape whose edge count is wrong.Multi-shape path:
num_clipped = header >> 3;is passed toadd_shapes(), which resizes the cell's clipped-shape array to that attacker-controlled count before any further input is read.Multi-shape path: an accumulated
shape_idis compared againstINT_MAXbut never againstnum_shape_ids, so a cell can encode a clipped shape withshape_id >= num_shape_ids, and later code indexes the shapes with it.The change
Reject
num_edges > INT32_MAX,num_clipped > num_shape_ids, andshape_id >= num_shape_ids(in each of the three branches that accumulate a shape id).There is deliberately no bound on
num_edgesagainst the remaining input, and the comment in the code records why: edges are run-length encoded, so a count of 8 or more is followed by a varint holding the rest of the run length, and a few bytes can legitimately describe many edges.DecodeEdges()already fails cleanly when the input is exhausted.Tests
src/s2/s2shape_index_test.cc(which already carried a TODO asking forS2ShapeIndexCellcoverage) gains three tests:S2ShapeIndexCell.DecodeRejectsEdgeCountOverflowS2ShapeIndexCell.DecodeRejectsTooManyClippedShapesS2ShapeIndexCell.DecodeRejectsShapeIdPastNumShapeIds(one input per accumulation branch)Each builds a malformed cell byte string and calls
S2ShapeIndexCell::Decode(num_shape_ids, &decoder)directly. They never construct or iterate anEncodedS2ShapeIndex, so they do not touch the encoded point vector format, which is little-endian only (encoded_s2point_vector.cclogsFATAL: Needs architecture with 64-bit little-endian unaligned loads). That keeps them valid on thecmake-big-endianjob.Verification
The whole binary also passes under ASan (
-fsanitize=address -fno-omit-frame-pointer -g), and the new checks do not reject valid cells:s2shape_index_test5/5,mutable_s2shape_index_test29/29,encoded_s2shape_index_test9/9.