Skip to content

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

Open

Description

@sushant-me

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).

Activity

  1. sushant-me commented on Oct 5, 2026

    @sushant-me
    Author

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions