Skip to content

fix: shared limits for hostile input - #154

Open
Chimi6 wants to merge 10 commits into
mainfrom
limits
Open

Chimi6 wants to merge 10 commits into
mainfrom
limits

Conversation

@Chimi6

@Chimi6 Chimi6 commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

One shared mechanism for hostile input, as audits 7a (2.3) and 7b (2b P1-P3, CI1-CI2) proposed, applied at the most reachable crash sites of 1a, 1b, 1c, 1d, 7a and 7b.

The mechanism: src/limits.rs (zero dependencies)

  • Depth: enter/leave, refuses past NESTING = 256. The audit's wasm table had every shape surviving 2,000 levels, so 256 keeps a wide margin.
  • Budget: units left (bytes unless the owner says otherwise), 2 GiB by default (the hub's old MAX_PIXELS cap, now limits::BUDGET).
  • count/clamp and bytes: counts read from files, and count * size as a checked usize (correct for 32-bit wasm).
  • Dimensions: no side zero or past 2^20, row and total within the budget, computed in u64. Image::dimensions_fit goes through it, so every image reader shares one meaning of the cap.
  • The guards live in each reader's own struct (or are made per stream), not in Context: the readers that need them never see the context. Recorded in STATE.md.

Fixes, by audit id (test: tests/hostile.rs runs each fixture named after its id in tests/fixtures/hostile, unless noted)

Nesting:

  • 1d C5, 1b C4: JSON (tree sink and JSONL copy), YAML, TOML (headers, dotted keys, arrays, inline tables together), XML refuse past 256 (generated 100,000-deep inputs in generated_inputs_end_cleanly).
  • 1d C6: Markdown block quotes and lists past 256 stay text; emphasis past 256 is flattened (generated inputs).
  • 1b C2, C3: DOCX tables and text boxes, ODT blocks, spans and lists past 256 are skipped (1b-c2-*, 1b-c3-*).
  • 1b C5: protobuf groups skipped with a stack (unit test deep_groups_are_skipped_without_recursion, 1b-c5-*).
  • 1d C1: RTF \itap capped at 64. 1d C2: self-referencing footnotes in the RTF and ODT writers.
  • 1a C2, C3: PDF lookups through Document::get stop at 16 levels. 1a C15, C16: Indexed over Indexed refused.
  • 1c C2: HEIC grid tiles must be coded pictures.

Counts used as sizes:

  • 1b C6, C7, C8: DOCX w:gridSpan/w:gridBefore (63), list levels past 8 skipped, chart indices (32,000; placed by index, no longer a quadratic find).
  • 1b C9, C10, C11: XLSX columns and <dimension> (XFD), rows (1,048,576), undefined style indices (Excel to Numbers panicked).
  • 1b C12: ODS repeats past 134M cells refused. 1b C13, C14, 7a C4: ODT text:s, spans (1,024) and a copy budget for repeated rows and cells.
  • 1b C19: Pages tables (2^20 cells) and Numbers sheets (2^27 cells). 7a C9: Numbers format widths (panicked).
  • 1b C17: ZIP64 sizes no longer reserved up front. 1b H1: RTF list levels past 9 (panicked).
  • 7a C2, C3: quote depth (16), letter and roman list labels past 780 and 3,999 in decimal.
  • 1a C1, C4, C5, C6, C17: PDF offsets past the end, /N, /W, predictor sizes, image sizes.
  • 7b C2, C3, 1c C15, C9, C11, C13: image sides and totals through Dimensions (PNG row buffers, 32-bit wraps, tall PBM to ICO), JPEG coefficients, WebP canvas, TIFF strips and tiles.

Budgets:

  • 1a C7: every PDF filter stage counts toward 512 MiB per stream (Flate, LZW, RunLength). inflate gets a one-line hook: it never asks for output past its limit (the double Flate bomb peaked at 2.1 GB, now 540 MB). LZW reserves no more than its input can expand to.
  • 1b C18: ZIP entries together stop at 2 GiB per package; iWork streams at 512 MiB of Snappy output.
  • 1d C3: YAML aliases and merges stop after 128 MiB of copied nodes. 1d C4, 7a C1: HTML filler cells and Markdown table padding stop at 524,288 cells.

Stack: the command line, each hop of a chain and each batch worker now run on an 8 MiB thread (limits::STACK). Windows gives the main thread 1 MiB, where the debug build overflowed before the 256-level limit (the first CI run of tests/hostile.rs on Windows); release and the wasm module were fine.

Wasm (7b C1): sublime.js re-instantiates the module after a trap and returns failed with crashed: true; checked against the unfixed module (after a 3,000-deep trap, later conversions work) and in wasm/hostile.mjs with a forced trap.

CI (7b CI1, CI2): tests/hostile.rs (subprocess, 60 s limit, fails on a signal or a changed outcome; also catches overflow panics in the debug build) and wasm/hostile.mjs, now run by scripts/check-wasm.sh.

Not done (STATE.md Tech Debt)

Shared content per reference (7a C5-C8, 1b C16), PDF text-path amplification and fan-out (1a C7 /Contents and ToUnicode, C9, and the quadratic ones), the 7b P1 checked sink and BMP (7b M1, M2), HTML depth, the table normaliser, and the non-limit panics 1b C1, 1b C15, 1c C1-C8, C10, C12, C14.

Behaviour changes for large legitimate files

Images over 2 GiB as 8-bit RGBA or 2^20 pixels on a side are refused by every reader (Netpbm, QOI, TGA and TIFF allowed 2^31 pixels before); a PDF stream decoded whole stops at 512 MiB; a progressive JPEG whose coefficients pass 2 GiB is refused.

Checks

  • cargo test --all-targets: all pass (45 suites, including the new hostile).
  • cargo fmt --check: clean.
  • cargo clippy --all-targets -- -D warnings fails on CI and locally with clippy 0.1.100 (stable since main's last green run): 15 findings, all pre-existing and identical on origin/main (hevc/filter.rs:805, hevc/params.rs:108-731, pages/writer.rs:3148, rtf/reader.rs:3034-3062, planner/chain.rs:36-37). None is in a line this branch changes; left for a separate fix so the parallel branches do not conflict. Wasm clippy clean.
  • cargo doc, check-formats-doc.sh, check-versions.sh, check-deps.sh: pass.
  • Size: CI's Linux release binary is 5,773,048 bytes against the 5,780,000 budget (6,952 left; about +14 KB for this branch). On macOS arm64 the code grew 7.9 KB (__text). The budget is unchanged. Wasm module 3,097,237 bytes (+8,692; budget 3,140,000).
  • CI on the last commit: tests pass on Linux, macOS and Windows; size, wasm module (with hostile.mjs), formats doc and dependency ledger pass; only clippy fails, as above.

src/limits.rs holds the guards every reader takes from one place: Depth
(256 levels, safe for the browser module's stack), Budget (bytes or items
left, 2 GiB by default, the hub's cap), count and bytes for numbers read
from files, and Dimensions for pictures (no side past 2^20, a row and the
whole image within the budget, checked in u64 so 32-bit usize cannot
wrap). Image::dimensions_fit and the value tree's sink use them.
JSON, YAML, TOML and XML refuse nesting past 256 levels (audit 1d C5, 1b
C4); Markdown keeps block markers past 256 as text and flattens emphasis
past 256 (1d C6). YAML aliases and merges stop after 128 MiB of copied
nodes (1d C3). Markdown table padding and HTML span filler cells stop at
cmark-gfm's 524,288 invented cells (7a C1, 1d C4).
Word tables and text boxes and OpenDocument blocks and spans past 256
levels are skipped (1b C2, C3); protobuf groups are skipped with a stack,
not recursion (1b C5); RTF \itap is capped at 64 (1d C1); a footnote that
refers to itself is written once in RTF and ODT (1d C2). Counts from files
are clamped: w:gridSpan and w:gridBefore, list levels, chart indices (1b
C6-C8), Excel columns, rows and style indices (1b C9-C11), ODS repeats as
a product (1b C12), ODT spaces, spans and repeated cells (1b C13, C14, 7a
C4), Pages and Numbers table sizes and format widths (1b C19, 7a C9), list
labels and quote depth (7a C2, C3), RTF list levels (1b H1). ZIP64 sizes
are no longer reserved up front (1b C17).
A lookup that leads back to itself stops after 16 levels (1a C2, C3), an
offset past the end parses as nothing (1a C1), /N and /W are checked (1a
C4, C5), predictor sizes use checked arithmetic (1a C6), every filter
stage of a stream counts toward 512 MiB (1a C7: Flate, LZW, RunLength),
Indexed over Indexed is refused (1a C15, C16), and image sizes go through
Dimensions (1a C17). inflate never asks for output past its limit, and
LZW reserves no more than its input can expand to.
A package's entries together stop at 2 GiB inflated, and an iWork stream
at 512 MiB of Snappy output (1b C18).
PNG, BMP, JPEG, Netpbm, QOI, TGA and TIFF refuse a side past 2^20 or an
image past 2 GiB as 8-bit RGBA before allocating (7b C2, C3; 1c C15:
Netpbm, QOI, TGA and TIFF counted pixels, not bytes). A WebP animation
canvas gets the same check (1c C11), progressive JPEG coefficients count
their two bytes (1c C9), a TIFF strip or tile must fit the budget (1c
C13), and a HEIC grid's tiles must be coded pictures (1c C2).
A conversion that traps no longer breaks the module for later calls
(7b C1): sublime.js drops the instance without calling into it, makes a
new one from the compiled module (asynchronously where the browser
requires it; ready() waits), and returns failed with crashed: true.
Addresses past 2 GiB are read as unsigned.
tests/fixtures/hostile holds the audit's small repro files and cases.txt
says how each must end; tests/hostile.rs runs each through the built
binary as a subprocess (a stack overflow cannot be caught in process) and
fails on a signal, a hang, or a different outcome, and generates the
large deep-nesting and ragged-table inputs. wasm/hostile.mjs runs the
same corpus through the module in the wasm job (7b CI1, CI2).
Windows gives the main thread 1 MiB, where the debug build overflowed
before the 256-level nesting limit (the hostile tests on CI's Windows
runner). The command line, each hop of a chain, and each batch worker now
run on a thread with the stack Linux and macOS give the main thread.
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