Skip to content

Add opt-in limit on Huffman table rebuilds (fixes #83) - #84

Closed
scadastrangelove wants to merge 1 commit into
image-rs:mainfrom
scadastrangelove:fix-huffman-table-rebuild-dos
Closed

scadastrangelove wants to merge 1 commit into
image-rs:mainfrom
scadastrangelove:fix-huffman-table-rebuild-dos

Conversation

@scadastrangelove

Copy link
Copy Markdown

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

  • No existing signatures change. The new counter lives on Decompressor as opt-in state, defaulting to unlimited — zero behavior change for existing callers (png, or anything using decompress_to_vec/decompress_to_vec_bounded).
  • Mirrors the crate's existing ignore_adler32() opt-in-setter pattern, and the shape of the already-accepted miniz_oxide fix for the same bug class in a different DEFLATE implementation.
  • New DecompressionError::HuffmanTableRebuildLimitExceeded variant so callers can distinguish "hit your own budget" from "corrupt data."
  • Rejected alternative: extending the existing fixed-table caching to dynamic blocks — unsafe in general, since consecutive dynamic blocks can legitimately use different Huffman trees; detecting "unchanged" would itself require an O(table-size) equality check, for comparatively little benefit over the simpler opt-in-limit approach.

Testing

  • Own PoC (hand-crafted chain of minimal BTYPE=10 blocks, 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.
  • Full test suite: 14→20 tests, all passing, both debug and --release.
  • Fix verified: with set_max_huffman_table_rebuilds(100), the 1,000,000-block payload is rejected in ~372µs instead of ~2.9-3s.
  • Correctness/perf regression check: patched a local build into a real png v0.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

  1. Adding a new variant to DecompressionError (not #[non_exhaustive]) is semver-sensitive for any downstream crate that exhaustively matches it — confirmed png (the primary consumer) doesn't, but can't rule out other downstream crates.
  2. The free functions decompress_to_vec/decompress_to_vec_bounded still have no way to opt into this limit — only the lower-level Decompressor API does, mirroring ignore_adler32's existing precedent, but worth confirming that's the desired ergonomics.
  3. Did not separately bound the (much cheaper) code-length-alphabet table rebuild in 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

…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>
@scadastrangelove

Copy link
Copy Markdown
Author

Closing this proactively — caught a problem on our own before it caused confusion. This branch was cut from c365c7e (2024-12-05), not from current main, which has since merged #73 ("Try fixed Huffman codes and uncompressed blocks") — a substantial refactor of the exact Huffman table-rebuild path this fix touches. The diff here is stale against a ~1.5-year-old base and no longer applies cleanly (same class of process failure as an unrefreshed-clone problem we found and got called out for elsewhere this week).

The underlying finding may still be valid on current main — re-verifying and, if so, rebuilding the fix against current main properly before refiling. Sorry for the stale PR sitting open; closing rather than leaving it to bit-rot further.

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.

Algorithmic-complexity DoS: dynamic-Huffman table rebuild cost decoupled from output size (same class as the fixed miniz_oxide init_tree bug)

1 participant