#1565·brotli

Variant analysis: 2 unfixed sibling safety gaps in brotli

Author: x4evexnolCreated Sep 28, 2026Updated Sep 28, 2026

Summary

Variant analysis of historical fixes in brotli identified 2 code sites where an integer-overflow guard, bounds check, or safe-allocation wrapper exists at one location but is missing at a structurally identical sibling location (same file, same function, or same pattern family) elsewhere in the codebase.

Each finding below is code-confirmed against the current HEAD (verified by cloning the repository fresh and checking the pattern is still present), with the exact file/line locations, the root cause, a description of the trigger path, and — where available — ASan/execution verification output or a proof-of-concept.

Note on origin: these were surfaced by an automated variant-analysis pipeline that diffs historical fix commits against sibling code paths, then has each candidate manually reviewed. I'm posting them together per-project rather than as separate issues to respect maintainer time. Happy to split, close, or reprioritize any of these as you see fit.


Finding 1: Brotli quality-level sibling gap — match buffer bounded constant (q10) vs 4 * num_bytes wrap (q11)

Verified against HEAD: 11017d7812b0502005603cf0f3cce0054e885d72 (2026-09-23)

*- Project: Brotli (github.com/google/brotli.git, master branch)

  • Class: Heap-buffer-overflow write (c/enc/backward_references_hq.c:820-831: quality=11 path computes 4 * num_bytes from attacker-controlled input size, DOUBLE multiplies through BROTLI_ALLOC macro → on 32-bit num_bytes > 1 GB wraps to small allocation → main loop fills with OOB writes; sibling: quality=10 path at line 714 in the SAME file uses compile-time constant 2 * (128 + 64) = 384 — bounded, cannot overflow; same file, same struct, same purpose, two structurally identical allocation paths differentiated only by quality level)
  • Status: Code-confirmed, exec-blocked
  • Sibling of: BROTLI_ALLOC(m, BackwardMatch, 2 * (MAX_NUM_MATCHES_H10 + 64)) at backward_references_hq.c:714 — quality=10 path ZopfliComputeShortestPath. Uses MAX_NUM_MATCHES_H10 = 128 from hash.h:245, resulting in element count = 2 * (128 + 64) = 384 — a compile-time constant that cannot overflow regardless of input size. Same file. Same struct type (BackwardMatch). Same purpose (building backward references for zopfli compression). Different quality level (10 vs 11). The only structural change is that quality=11 replaces the bounded constant with unbounded 4 * num_bytes.*

Summary

Brotli's compression pipeline has two quality levels for its "high-quality zopfli" backward-reference builder. Quality 10 (ZopfliComputeShortestPath) allocates its match buffer with a compile-time constant size — 384 entries, safe. Quality 11 (BrotliCreateHqZopfliBackwardReferences) uses 4 * num_bytes where num_bytes is the full input size — unbounded. On 32-bit platforms, num_bytes > 1 GB causes 4 * num_bytes to overflow size_t, wrapping to a small value. This value then feeds into BROTLI_ALLOC(m, BackwardMatch, matches_size) which performs ANOTHER multiply by sizeof(BackwardMatch) = 8. The double-multiply defeats any check — the OOM guard catches only the case where N = 0 (zero allocation), not the case where N wraps to a small non-zero value.

The gap

c
// ═════════════════════════════════════════════════════════════
// c/enc/backward_references_hq.c:713-714 — QUALITY 10 (SAFE ✅):
// Function: ZopfliComputeShortestPath
BackwardMatch* BROTLI_RESTRICT matches =
    BROTLI_ALLOC(m, BackwardMatch, 2 * (MAX_NUM_MATCHES_H10 + 64));
//                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//    MAX_NUM_MATCHES_H10 = 128 (c/enc/hash.h:245)
//    2 * (128 + 64) = 384 — compile-time constant, CANNOT overflow
//    Allocates exactly 384 * sizeof(BackwardMatch) = 3072 bytes
//    Bounded regardless of input size.

// ═════════════════════════════════════════════════════════════
// c/enc/backward_references_hq.c:820-831 — QUALITY 11 (UNSAFE ❌):
// Function: BrotliCreateHqZopfliBackwardReferences
uint32_t* num_matches = BROTLI_ALLOC(m, uint32_t, num_bytes);
//    num_bytes = ATTACKER-CONTROLLED input size. No validation.

size_t matches_size = 4 * num_bytes;              // LINE 820 — FIRST OVERFLOW
//                     ^^^^^^^^^^^^^
//    On 32-bit: num_bytes = 0x40000001 (1 GB + 1)
//    matches_size = 0x00000004 (wraps to 4)

BackwardMatch* matches =
    BROTLI_ALLOC(m, BackwardMatch, matches_size); // LINE 831 — SECOND MULTIPLY
//              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//    BROTLI_ALLOC computes: matches_size * sizeof(BackwardMatch)
//    = 4 * 8 = 32 bytes allocated

// LINE 836-839 — OOM CHECK BYPASSED:
if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(matches) || BROTLI_IS_NULL(num_matches)) {
  // OOM check catches zero/allocation-failure, NOT small wrapped alloc.
  // 32-byte allocation succeeds → check passes → execution continues
}

// LINE 840+ — MAIN LOOP fills match buffer:
// Loop writes up to ~4 * num_bytes BackwardMatch entries (8 bytes each)
// into the 32-byte buffer → massive OOB write
// Indices reach: cur_match_pos * sizeof(BackwardMatch) >> 32

// ═════════════════════════════════════════════════════════════
// c/enc/memory.h:51-52 — BROTLI_ALLOC DOUBLE-MULTIPLY TRAP:
#define BROTLI_ALLOC(M, T, N) \
  ((N) > 0 ? ((T*)BrotliDefaultAllocFunc(M, (N) * sizeof(T))) : NULL)
//                                          ^^^^^^^^^^^^^^^^^
//    BROTLI_ALLOC itself performs N * sizeof(T) with NO overflow check.
//    When N = matches_size (already wrapped), this multiplies again.
//    Combined: (4 * wrapped_num_bytes) * sizeof(BackwardMatch)
//    = (wrapped_value) * 8 = even smaller allocation

The quality-level sibling gap

Both call sites are in the same file (backward_references_hq.c). Both allocate BackwardMatch arrays for the same purpose (storing zopfli backward-reference matches). Both are called from the same orchestration function (BrotliCreateZopfliBackwardReferences dispatches to q10 path; BrotliCreateHqZopfliBackwardReferences is the q11 path). The ONLY structural difference is the quality level:

Quality 10 Quality 11
Function ZopfliComputeShortestPath BrotliCreateHqZopfliBackwardReferences
Match buffer element count 384 (constant) 4 * num_bytes (unbounded)
Overflow-safe? Yes (compile-time constant) No (wraps on 32-bit)
Same file? Yes Yes
Same struct type? BackwardMatch BackwardMatch

This is a classic quality-level sibling gap: the higher-quality compression path is less safe than the lower-quality one, because it uses unbounded dynamic sizing where the lower-quality path used a bounded compile-time constant.

Attack practicality

  • 32-bit: Input size between 1 GB and 4 GB → 4 * num_bytes wraps → double-multiply through BROTLI_ALLOC produces tiny allocation → main loop writes OOB. Directly exploitable. The input can be any data — there's no magic-number check before reaching this code path. Simply call BrotliEncoderCompress(quality=11, input_size=0x40000001, ...).
  • 64-bit: Requires num_bytes > 2^62 (4 exabytes) for overflow — impractical. But the architectural gap remains: the code assumes size_t arithmetic is safe, and the quality-level differentiation makes this a self-undermining design pattern.

Sibling-gap quality

Excellent — same file, same struct, same purpose, adjacent code paths, differentiated only by quality level. The quality=10 path is bounded (constant). The quality=11 path is unbounded and exploitable. This is the first quality-level sibling gap in the dataset — all previous cases involved different function paths, different API calls, or different format coders. Here the gap is purely: "higher quality = less safe."


Analysis date: 2026-09-22. Code-confirmed, not execution-verified. Domain: compression library (Brotli).


Finding 2: Brotli encode.c overflow guard inconsistency — MaxCompressedSize guarded but command buffer + hash table unguarded in same file

Verified against HEAD: 11017d7812b0502005603cf0f3cce0054e885d72 (2026-09-23)

*- Project: Brotli (github.com/google/brotli.git, master branch)

  • Class: Heap-buffer-overflow write (c/enc/encode.c:1097-1105: command buffer sizing s->num_commands_ + bytes/2 + 1 + bytes/4 + 16 accumulates across streaming input blocks without overflow guard — in streaming API num_commands_ never resets, grows toward SIZE_MAX → addition wrap → BROTLI_ALLOC undersized → OOB write on command population; sibling: BrotliEncoderMaxCompressedSize at line 1269 in the SAME FILE has explicit overflow guard (result < input_size) ? 0 : result — demonstrates developer awareness of the overflow problem; also HashTableSize at line 157-162 left-shifts htsize <<= 1 in while-loop toward SIZE_MAX without guard → infinite loop DoS on 64-bit; same file, same size-arithmetic pattern, opposite guarding)
  • Status: Code-confirmed, exec-blocked
  • Sibling of: BrotliEncoderMaxCompressedSize() at encode.c:1263-1270 — same file, same input-size-driven arithmetic. Line 1269: return (result < input_size) ? 0 : result; — explicit overflow detection for input_size + overhead. The command buffer sizing at lines 1097-1105 computes newsize = num_commands_ + bytes/2 + 1 + bytes/4 + 16 with no equivalent guard. The hash table sizing at lines 157-162 left-shifts htsize <<= 1 toward SIZE_MAX with no guard. All three code paths compute derived sizes from input parameters in the same file. Only one has overflow protection.*

Summary

Brotli's encode.c contains a classic same-file sibling gap: three code paths compute allocation-related sizes from input-derived parameters, but only one (BrotliEncoderMaxCompressedSize) has an explicit overflow guard. The command buffer sizing path accumulates num_commands_ across streaming API blocks without ever resetting it — in streaming mode, the accumulator grows without bound. The hash table sizing path left-shifts htsize in a while-loop until it exceeds input_size — if input_size is near SIZE_MAX, the shift wraps to 0 and loops infinitely.

The overflow guard in MaxCompressedSize proves the developers understood the overflow risk. They just didn't apply the same pattern to the other size-arithmetic paths in the same file.

The gaps

c
// ═════════════════════════════════════════════════════════════
// c/enc/encode.c:1263-1270 — SAFE SIBLING ✅:
size_t BrotliEncoderMaxCompressedSize(size_t input_size) {
  size_t num_large_blocks = input_size >> 14;
  size_t overhead = 2 + (4 * num_large_blocks) + 3 + 1;
  size_t result = input_size + overhead;
  if (input_size == 0) return 2;
  return (result < input_size) ? 0 : result;   // ← OVERFLOW GUARD
  //     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  //    Explicit: if addition wrapped, return 0 (invalid).
  //    Same file, same size-arithmetic pattern. THIS IS THE GUARDED SIBLING.
}

// ═════════════════════════════════════════════════════════════
// c/enc/encode.c:1097-1105 — COMMAND BUFFER EXPANSION (UNSAFE ❌):
size_t newsize = s->num_commands_ + bytes / 2 + 1;
//               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//    s->num_commands_ accumulates across ALL streaming blocks.
//    In the one-shot API: bounded by input / min_block_size.
//    In the STREAMING API: num_commands_ NEVER RESETS.
//    After ~4 GB total input on 32-bit: num_commands_ approaches SIZE_MAX.
//    Then: num_commands_ + bytes/2 + 1 WRAPS → undersized allocation.

if (newsize > s->cmd_alloc_size_) {
    newsize += (bytes / 4) + 16;               // ← SECOND ADDITION, ALSO UNGUARDED
    s->cmd_alloc_size_ = newsize;
    new_commands = BROTLI_ALLOC(m, Command, newsize);
    //             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //    BROTLI_ALLOC does newsize * sizeof(Command) — THIRD MULTIPLY, UNGUARDED
    //    If newsize wrapped → tiny allocation → OOB write on command population
    //    No overflow guard. No (result < original) check.
}

// ═════════════════════════════════════════════════════════════
// c/enc/encode.c:157-162 — HASH TABLE SIZING (UNSAFE ❌):
static size_t HashTableSize(size_t max_table_size, size_t input_size) {
  size_t htsize = 256;
  while (htsize < max_table_size && htsize < input_size) {
    htsize <<= 1;      // ← LEFT SHIFT, NO OVERFLOW GUARD
    //  ^^^^^^^^^^^
    //    On 64-bit: if input_size is near SIZE_MAX:
    //    htsize doubles: 256, 512, ..., 2^62, 2^63
    //    When htsize = SIZE_MAX/2 + 1 ≈ 2^63:
    //    htsize <<= 1 = 0  → overflow to zero
    //    while (0 < max_table_size && 0 < input_size)
    //    → BOTH TRUE → INFINITE LOOP
    //
    //    No equivalent of MaxCompressedSize's (result < input) guard.
  }
  return htsize;
}

The same-file, same-pattern, opposite-guarding gap

All three functions are in c/enc/encode.c. All three take size_t input parameters and compute derived size_t values through arithmetic. The structural patterns are identical:

Function Pattern Overflow guard?
BrotliEncoderMaxCompressedSize (:1269) result = input + overhead YES: (result < input) ? 0 : result
Command buffer sizing (:1097) newsize = num_commands + terms NO — raw addition, no guard
Hash table sizing (:160) htsize <<= 1 in while-loop NO — raw shift, no guard

This is a same-file systemic gap: the developer understood the need for overflow checking (proven by the MaxCompressedSize guard), but didn't apply the pattern to the other arithmetic paths. The fix is straightforward: add a (newsize < s->num_commands_) check to the command buffer sizing, and a (htsize & (SIZE_MAX ^ (SIZE_MAX >> 1))) check to the hash table loop.

Attack practicality

  • Command buffer (32-bit): Streaming mode, ~4 GB total input → num_commands_ approaches SIZE_MAX → num_commands_ + bytes/2 + 1 wraps → undersized BROTLI_ALLOC → OOB write on command insertion. Directly exploitable on 32-bit.
  • Command buffer (64-bit): Requires ~16 EB total input in streaming mode — impractical. But the unbounded accumulator is still a design defect — a future API change that increases block sizes or reduces the per-command cost could bring the overflow threshold within reach.
  • Hash table (64-bit): BrotliEncoderCompress(input_size = SIZE_MAX, ...) → HashTableSize infinite loop → DoS. Trivially triggered on any platform.

Sibling-gap quality

Excellent — same file (encode.c), same arithmetic pattern, one guarded, two unguarded. The overflow guard in BrotliEncoderMaxCompressedSize at line 1269 is unambiguous: (result < input_size) ? 0 : result. It proves the developer was aware of the overflow problem and knew how to check for it. The command buffer sizing at line 1097 and the hash table sizing at line 160 perform the same kind of arithmetic but lack equivalent guards. This is a classic partial-adoption gap: the guard exists in the file, is correct, is tested, but was only applied to one of three structurally identical computation sites.


Analysis date: 2026-09-22. Code-confirmed, not execution-verified. Domain: compression library (Brotli).


Methodology

For each historical vulnerability fix in the codebase, we identified the safe pattern the fix introduced (an overflow guard, a safe allocator wrapper, a bounds check) and searched the rest of the codebase for structurally identical code that predates or postdates the fix but never received it. Each candidate was then manually verified against the current HEAD listed above.

Happy to provide anything else that would help triage — additional PoC inputs, a minimal patch following the existing safe-sibling pattern, or a written reproduction script.