EncodedS2ShapeIndex: OOM (16 GiB alloc) from unchecked num_edges/num_clipped
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:
- Single-shape path —
int num_edges = header >> 3;whereheaderisuint64_t. A large header makesheader >> 3overflowint(→ negative), which sign-extends to0xFFFFFFFFwhen passed toS2ClippedShape::Init, which doesedges_ = new int32_t[0xFFFFFFFF]= 17179869180 bytes. - Multi-shape path —
num_clipped = header >> 3is unbounded, soadd_shapes()allocates memory proportional to an attacker-controlled count. - Multi-shape path —
num_edges = (header >> 3) + 1is 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).
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.