Skip to content

dec: core security hardening against memory validation and resource flaws#1497

Open
owvr27 wants to merge 2 commits into
google:masterfrom
owvr27:defensive-security-patches
Open

dec: core security hardening against memory validation and resource flaws#1497
owvr27 wants to merge 2 commits into
google:masterfrom
owvr27:defensive-security-patches

Conversation

@owvr27

@owvr27 owvr27 commented Jun 20, 2026

Copy link
Copy Markdown

Summary of Hardening Changes

This unified patch introduces production-grade defensive validations across the decoder core (c/dec/decode.c, c/dec/huffman.c, and c/dec/state.c) to eliminate five distinct resilience and memory boundaries gaps surfaced via dense stream state anomalies:

  1. Huffman Table Budget Safeguard: Introduces an explicit table_budget constraint parameter inside BrotliBuildHuffmanTable to halt multi-level accumulation before breaching array structures, accompanied by a strict trailing slab_end range-check inside HuffmanTreeGroupDecode to insulate against heap OOB writes.
  2. RingBuffer Integer Overflow Mitigation: Shifts BrotliCalculateRingBufferSize calculations to precise unsigned arithmetic to completely eliminate undefined behavior (UB) on large window configurations (e.g., window_bits = 30), forcing saturation thresholds at window_size to block unconstrained 1 GiB OOM allocation logic vectors.
  3. Cold-Start Heap Disclosure Prevention: Enforces strict execution limits on speculative 16-byte copy macro activities inside the LZ77 backward-copy operations, introducing a precise exact-length loop fallback when rb_roundtrips == 0 to safeguard uninitialized heap buffers from flushing to the output context.
  4. State Machine Desynchronisation Isolation: Explicitly resets s->loop_counter upon transitioning into BROTLI_STATE_METABLOCK_DONE states, cutting off intermediate state leakages during streaming boundary suspensions, backed by a defensive invariant verification BROTLI_DCHECK at function entries.
  5. Resource-Asymmetry DoS Mitigation: Restricts maximum incoming num_literal_htrees values against logically reachable context mapping indices, and deploys global multiplication overflow validation protections prior to executing internal memory allocation macros within BrotliDecoderHuffmanTreeGroupInit.

Verification

All modifications maintain full API integration limits and seamlessly route violations into existing error blocks (BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE, etc.) without introducing custom public API disruptions. Structural verification confirms no degradation against standard high-performance decoding hot paths (s->rb_roundtrips > 0).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant