Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 pastNESTING= 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 oldMAX_PIXELScap, nowlimits::BUDGET).count/clampandbytes: counts read from files, andcount * sizeas a checkedusize(correct for 32-bit wasm).Dimensions: no side zero or past 2^20, row and total within the budget, computed inu64.Image::dimensions_fitgoes through it, so every image reader shares one meaning of the cap.Context: the readers that need them never see the context. Recorded in STATE.md.Fixes, by audit id (test:
tests/hostile.rsruns each fixture named after its id intests/fixtures/hostile, unless noted)Nesting:
generated_inputs_end_cleanly).1b-c2-*,1b-c3-*).deep_groups_are_skipped_without_recursion,1b-c5-*).\itapcapped at 64. 1d C2: self-referencing footnotes in the RTF and ODT writers.Document::getstop at 16 levels. 1a C15, C16: Indexed over Indexed refused.Counts used as sizes:
w:gridSpan/w:gridBefore(63), list levels past 8 skipped, chart indices (32,000; placed by index, no longer a quadratic find).<dimension>(XFD), rows (1,048,576), undefined style indices (Excel to Numbers panicked).text:s, spans (1,024) and a copy budget for repeated rows and cells./N,/W, predictor sizes, image sizes.Dimensions(PNG row buffers, 32-bit wraps, tall PBM to ICO), JPEG coefficients, WebP canvas, TIFF strips and tiles.Budgets:
inflategets 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.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 oftests/hostile.rson Windows); release and the wasm module were fine.Wasm (7b C1):
sublime.jsre-instantiates the module after a trap and returnsfailedwithcrashed: true; checked against the unfixed module (after a 3,000-deep trap, later conversions work) and inwasm/hostile.mjswith 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) andwasm/hostile.mjs, now run byscripts/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
/Contentsand 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 newhostile).cargo fmt --check: clean.cargo clippy --all-targets -- -D warningsfails on CI and locally with clippy 0.1.100 (stable since main's last green run): 15 findings, all pre-existing and identical onorigin/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.__text). The budget is unchanged. Wasm module 3,097,237 bytes (+8,692; budget 3,140,000).hostile.mjs), formats doc and dependency ledger pass; only clippy fails, as above.