edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | # fuzzimage: cargo-fuzz targets for ec-image |
| 2 | |
| 3 | Goal: DONE.md shared gate 2, at least one cargo-fuzz target per untrusted entry point in |
| 4 | ec-image (whole-file decode, limit-bounded decode, header info, animation, and each format module). |
| 5 | Base sha: 6718bc03 (main at worktree creation). |
| 6 | |
| 7 | ## What changed |
| 8 | - `crates/ec-image/fuzz/`: standalone cargo-fuzz crate (`ec-image-fuzz`, own `[workspace]`, path dep |
| 9 | on ec-image). Same layout as `crates/ec-ogg/fuzz/`. Ten targets: |
| 10 | - `decode`: `ec_image::decode` (format guessed from the signature), default limits. |
| 11 | - `decode_with_limits`: same, under `Limits { max_pixels: 4096*4096, max_alloc: 64 MiB, |
| 12 | max_total_alloc: 128 MiB }`. |
| 13 | - `info`: `ec_image::info`. |
| 14 | - `decode_animation`: `ec_image::decode_animation` (GIF and animated WebP frames). |
| 15 | - `bmp`, `jpeg`, `png`, `tiff`: each module's `info` and `decode` under the small limits. |
| 16 | - `gif`: `info`, `decode`, `decode_animation`. `webp`: `info`, `decode`, `decode_frames`. |
| 17 | - `seed.sh`: every file in `fixtures/stills` (94 files) seeds the four whole-file targets; each |
| 18 | format target gets only its own format's files (bmp 17, gif 6, jpeg 11, png 31, tiff 17, webp 12). |
| 19 | - `.gitignore`: target, corpus, artifacts, coverage. |
| 20 | - `crates/ec-image/src/webp/vp8.rs`: the inverse DCT runs in i64. Found by fuzzing (see below). |
| 21 | - `crates/ec-image/src/png.rs`: the 16-bit expansion buffer is budgeted with `Limits::check_bytes` |
| 22 | before it is allocated (it was unchecked; it can be twice the raw scanlines the existing check saw). |
| 23 | - `crates/ec-image/src/bmp.rs`: the output buffer (width x height x channels) is budgeted with |
| 24 | `Limits::check_bytes` (it was unchecked). The RLE index buffer is `width * height`, covered by |
| 25 | the same bound. |
| 26 | - `crates/ec-image/src/jpeg.rs`: |
| 27 | - a scan that names no components is `Error::corrupt` (it indexed an empty vector). Found by fuzzing. |
| 28 | - coefficient storage is budgeted across all components with one `AllocBudget`, not one buffer at |
| 29 | a time; three 4:4:4 planes could each pass the per-buffer cap and add up to far past it. |
| 30 | - the RGB output buffer is budgeted with `check_bytes`; `reconstruct` now takes `Limits`. |
| 31 | - a single-component frame with no plane returns `Error::corrupt` instead of `expect`. |
| 32 | - `crates/ec-image/tests/fuzz_regressions.rs`: five regression tests. Two inline the minimised |
| 33 | fuzzer inputs (a 650-byte WebP that drives the VP8 IDCT overflow, and a 90-byte BMP whose embedded |
| 34 | JPEG has an empty scan). Three pin the new budget refusals (PNG expansion, BMP output, JPEG |
| 35 | coefficients across components). |
| 36 | - Lint cleanup needed for the gate. The workspace toolchain (rustc/clippy 1.98) flagged 22 |
| 37 | pre-existing lints in `ec-image` (`chunks_exact` with a constant size, a needless lifetime, manual |
| 38 | range patterns, a collapsible `if`, a manual checked division). They are fixed in place, with no |
| 39 | behaviour change: `chunks_exact(N)` became `as_chunks::<N>().0.iter()`, and the rest are the |
| 40 | mechanical rewrites clippy suggests. Two sites in `tests/differential.rs` got the same change. |
| 41 | |
| 42 | ## Bugs found and fixed |
| 43 | Fuzz pass 1 (120 s each, `-max_total_time=120 -rss_limit_mb=2048 -timeout=10`): |
| 44 | |
| 45 | | target | exit | finding | |
| 46 | |---|---|---| |
| 47 | | decode | 77 (crash) | VP8 IDCT i32 overflow in `webp/vp8.rs` (panic in debug / fuzz builds) | |
| 48 | | decode_with_limits | 77 | same | |
| 49 | | decode_animation | 77 | same | |
| 50 | | webp | 77 | same | |
| 51 | | bmp | 77 | `jpeg.rs` index out of bounds: scan with zero components (embedded JPEG) | |
| 52 | | info, gif, jpeg, png, tiff | 0 | no crash | |
| 53 | |
| 54 | - VP8 IDCT: the second pass multiplies intermediate values that a hostile block can drive past |
| 55 | i32. The fix widens the arithmetic to i64 and truncates the result to i32 at the end. Valid |
| 56 | streams never leave i32, so their output is unchanged. In a release build the old code wrapped |
| 57 | silently rather than panicking, so the regression test only catches the bug in a debug build. |
| 58 | - Minimised inputs (the `cargo fuzz tmin` runs were done on a detached worktree of main, so the crash |
| 59 | reproduced on the old code): the 650-byte WebP for the VP8 bug and the 90-byte BMP for the JPEG bug. |
| 60 | - The unchecked-allocation fixes (PNG, BMP, JPEG output, JPEG coefficient budget) were found by reading |
| 61 | the decoders, not by fuzzing. Each has a test that fails on main and passes after the fix. |
| 62 | |
| 63 | ## Verify commands and results |
| 64 | All cargo commands ran through `scripts/remote-run.sh fuzzimage` on host a or b. |
| 65 | |
| 66 | - `cargo +nightly fuzz build --fuzz-dir crates/ec-image/fuzz`: exit 0, all ten targets built. |
| 67 | - Pass 1 (above): five crashes, all fixed. |
| 68 | - Pass 2, after the fixes, 120 s each, same flags: |
| 69 | |
| 70 | | target | exit | executions | crash/timeout/OOM | |
| 71 | |---|---|---:|---| |
| 72 | | decode | 0 | 13,798 | none | |
| 73 | | decode_with_limits | 0 | 17,124 | none | |
| 74 | | info | 0 | 3,683,798 | none | |
| 75 | | decode_animation | 0 | 15,300 | none | |
| 76 | | bmp | 0 | 70,771 | none | |
| 77 | | gif | 0 | 28,748 | none | |
| 78 | | jpeg | 0 | 8,365 | none | |
| 79 | | png | 0 | 394,575 | none | |
| 80 | | tiff | 0 | 290,053 | none | |
| 81 | | webp | 0 | 9,862 | none | |
| 82 | |
| 83 | - Regression tests on main (pre-fix, `fuzz_regressions.rs` copied into a detached worktree): |
| 84 | `cargo test -p ec-image --test fuzz_regressions`: exit 101, 5 of 5 failed, as intended (the |
| 85 | JPEG and VP8 tests panicked on overflow or indexing; the three budget tests failed their assertions). |
| 86 | - `cargo clippy -p ec-image --all-targets -- -D warnings`: exit 0 (clean). |
| 87 | - `cargo test -p ec-image` (debug, default threads, `EC_MEMGUARD_MAX=6G`): exit 0. Unit 27 passed, |
| 88 | differential 19 passed, fuzz 18 passed (about 500 s), fuzz_regressions 5 passed, real_library 2 |
| 89 | passed, doctests 0. |
| 90 | - `cargo test -p ec-image --release -- --test-threads=1` (`EC_MEMGUARD_MAX=6G`): exit 0. Unit 27, |
| 91 | differential 19, fuzz 18 (79 s), fuzz_regressions 5, real_library 2. |
| 92 | - `cargo test -p ec-image --release` with default threads: exit 101. The `fuzz` binary is SIGKILLed |
| 93 | by the memory guard (`a_gif_with_many_tiny_frames_over_a_huge_canvas_is_refused_by_the_total_budget` |
| 94 | holds several GiB of canvases while the other tests run in parallel). This is pre-existing: the same |
| 95 | GIT test was killed on main under the default 3500M guard too. Under the default threads, a 6G cap |
| 96 | does not help. The single-threaded run passes. |
| 97 | |
| 98 | ## Left open |
| 99 | - The gate asks for 1 CPU-hour per target. Only 120 s smoke runs were done. The gate is not met. |
| 100 | - The corpus is seeded from fixtures only. No minimised corpus and no coverage measured. |
| 101 | - `fuzz_regressions` pins the budgets with hand-built containers. The fuzzer does not reach the |
| 102 | budget refusals, because the seeds are small real files. |
| 103 | - `cargo test -p ec-image --release` with default threads needs more memory than the lane's 3500M |
| 104 | guard. A fix belongs in `tests/fuzz.rs` (lower the GIT budget test's canvas or serialise it), which |
| 105 | is outside this lane's scope, so it is not changed here. |
| 106 | - `crates/ec-image/fuzz/Cargo.lock` was generated on the server and is not in this commit. |