s2geometry icon indicating copy to clipboard operation
s2geometry copied to clipboard

EncodedS2ShapeIndex: OOM (16 GiB alloc) from unchecked num_edges/num_clipped

Open sushant-me opened this issue 2 months ago • 1 comments

OOM (16 GiB allocation) from unchecked size fields in S2ShapeIndexCell::Decode

A 117-byte malformed input causes S2ShapeIndexCell::Decode() to attempt a single operator new(17179869180) (~16 GiB), crashing with OOM.

Root cause

Three values decoded from the untrusted header are used for allocation without bounds checks:

  1. Single-shape path — int num_edges = header >> 3; where header is uint64_t. A large header makes header >> 3 overflow int (→ negative), which sign-extends to 0xFFFFFFFF when passed to S2ClippedShape::Init, which does edges_ = new int32_t[0xFFFFFFFF] = 17179869180 bytes.
  2. Multi-shape path — num_clipped = header >> 3 is unbounded, so add_shapes() allocates memory proportional to an attacker-controlled count.
  3. Multi-shape path — num_edges = (header >> 3) + 1 is unbounded.

Reproduction

117-byte input (base64):

CAAIFAAIAWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsi4uLi4uLi4uLbGxsdQAAACePCAAIABoAGgAgAAATJP////////8HjwAAAAAAAAAAAAAAAABAAAAAADoAEwAAEwgFOs8A

libFuzzer:

ERROR: libFuzzer: out-of-memory (malloc(17179869180))
  #9  S2ShapeIndexCell::Decode(int, Decoder*)   <- operator new(16 GiB)
  #10 EncodedS2ShapeIndex::GetCell(int) const

Impact

Denial of service: a 117-byte input forces a ~16 GiB allocation (CWE-400 uncontrolled resource consumption), crashing the process.

Fix

Bound each size field: num_edges against int32 max / remaining input bytes, and num_clipped against num_shape_ids. See PR #675.

Related to (distinct from) #674 (null deref) and #676 (OOB shape_id).

sushant-me avatar Sep 15 '26 18:09 sushant-me

Related framing: #686 documents the trusted byte stream assumption on EncodedS2ShapeIndex::Init — that a true return does not mean the encoding is sound, because cells are decoded lazily and a malformed one is not reached until later.

The behaviour reported here is unchanged; the note only states the contract a caller is operating under when it hands the decoder an encoding that does not satisfy it. Filing it as documentation rather than a fix follows jmr's reading on #674.

sushant-me avatar Oct 05 '26 10:10 sushant-me