Repository navigation
Add opt-in limit on Huffman table rebuilds (fixes #83) - #84
scadastrangelove wants to merge 1 commit into
Conversation
…builds Every dynamic-Huffman (BTYPE=10) block forces build_tables (and thus huffman::build_table's ~4096-entry primary lit/len table rebuild) regardless of how many output bytes the block actually produces. Unlike fixed-Huffman blocks, which cache their table across consecutive blocks via `fixed_table`, there was no equivalent safeguard for dynamic blocks. A stream consisting of a chain of minimal, near-zero-output dynamic-Huffman blocks (an EOB-only 2-symbol code) can therefore force an amount of CPU work that scales with block count while output stays at zero bytes, so output-size-based limits (e.g. decompress_to_vec_bounded) never engage. This is the same architectural bug class already disclosed and fixed (via an opt-in rebuild budget) in miniz_oxide. A hand-crafted PoC (examples/poc_dynamic_block_dos.rs) confirms the cost scales roughly linearly with block count and is decoupled from output size: 1,000,000 minimal blocks (~41 MB of input, 0 bytes of output) took several seconds to reject. Fix: add an opt-in `Decompressor::set_max_huffman_table_rebuilds` budget, mirroring the existing `ignore_adler32` opt-in-setter pattern. Unlimited by default (zero behavior change for existing callers such as `png`, verified by patching a local build into the `png` crate's own decode path). Once configured, exceeding the budget returns the new `DecompressionError::HuffmanTableRebuildLimitExceeded` before the (max+1)-th expensive rebuild is paid for, cutting rejection time from seconds to microseconds independent of the attacker-chosen block count. Adds regression tests covering: the limit rejecting an over-budget chain, an at-or-under-budget chain still succeeding, an off-by-one boundary check, the default (unset) case remaining fully unlimited, and legitimate miniz_oxide-encoded dynamic-Huffman data decoding byte-identically with a generous limit configured. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Closing this proactively — caught a problem on our own before it caused confusion. This branch was cut from The underlying finding may still be valid on current |
Fixes #83.
Summary
Adds an opt-in
Decompressor::set_max_huffman_table_rebuilds(usize)that bounds how many times a single decompression call may rebuild the dynamic-Huffman decode table, closing the algorithmic-complexity DoS described in #83.Design
Decompressoras opt-in state, defaulting to unlimited — zero behavior change for existing callers (png, or anything usingdecompress_to_vec/decompress_to_vec_bounded).ignore_adler32()opt-in-setter pattern, and the shape of the already-acceptedminiz_oxidefix for the same bug class in a different DEFLATE implementation.DecompressionError::HuffmanTableRebuildLimitExceededvariant so callers can distinguish "hit your own budget" from "corrupt data."Testing
BTYPE=10blocks, each producing 0 output bytes): confirmed linear-in-block-count cost (1K blocks ~2.5ms → 1M blocks ~2.9-3s), zero output throughout;decompress_to_vec_bounded's output-size limiting gives zero protection since output never grows.--release.set_max_huffman_table_rebuilds(100), the 1,000,000-block payload is rejected in ~372µs instead of ~2.9-3s.pngv0.17.16 dependency via[patch.crates-io]and successfully decoded real PNG files end-to-end; an interleaved A/B microbenchmark decompressing 500 KB of real miniz_oxide-encoded dynamic-Huffman data showed no measurable performance difference (before≈918µs/after≈908µs, within the environment's run-to-run noise).Open questions for maintainer judgment
DecompressionError(not#[non_exhaustive]) is semver-sensitive for any downstream crate that exhaustively matches it — confirmedpng(the primary consumer) doesn't, but can't rule out other downstream crates.decompress_to_vec/decompress_to_vec_boundedstill have no way to opt into this limit — only the lower-levelDecompressorAPI does, mirroringignore_adler32's existing precedent, but worth confirming that's the desired ergonomics.read_code_length_codes— it's implicitly bounded by the same block-count budget and is a small fraction of total cost, but flagging for a defense-in-depth review.Discovered by the rust-in-peace security pipeline.
🤖 Generated with Claude Code