From 422ee2ec2c75433ab8fcd671bb59d4abc34873fd Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 23 Nov 2026 11:03:39 +0700 Subject: [PATCH] Fuzzing: catch the app's own caught panics as the app does libFuzzer's panic hook aborts at once, even inside the app's own catch_unwind (pack::pdf::copies around the PDF reader), so the third run stopped on the replayed PDF case after 8 inputs although the app handles it. The targets' init (fuzzing::init) sets a hook that prints the first ten panics and lets them unwind as in the app; a panic nothing catches still reaches libfuzzer-sys's own catch_unwind, which aborts and saves the input. Checked in a container: the PDF case passes in 8 ms, 60 s a target finds nothing. PLAN lists the two findings. --- docs/PLAN.md | 16 +++++++++++++--- fuzz/fuzz_targets/signed_copy.rs | 5 ++++- fuzz/fuzz_targets/text.rs | 5 ++++- fuzz/fuzz_targets/upload.rs | 5 ++++- src/fuzzing.rs | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/docs/PLAN.md b/docs/PLAN.md index d93d621..95baa04 100644 --- a/docs/PLAN.md +++ b/docs/PLAN.md @@ -748,9 +748,19 @@ cases in the tests. `tests/fuzz_cases//` with its fix, which `cargo test` replays on every push. The multipart reading itself is axum's (multer), so no target of ours. -- [ ] The nightly job runs on runner02 on cnx01 (Alex, Nov 23, 2026), by - its own label `cnx01` (cnx-network-clan PR), at 03:00 and at the lowest - CPU priority (`nice`), so the app keeps first call on the CPU. +- [x] The nightly job runs on runner02 on cnx01 (Alex, Nov 23, 2026), by + its own label `cnx01` (cnx-network-clan PR #170), at 03:00 and at the + lowest CPU priority (`nice`), so the app keeps first call on the CPU. + Its first two runs found two bugs, both fixed and replayed from + `tests/fuzz_cases/`: + - a drawn signature of 798 bytes whose PNG header claimed 300 x + 738,197,594 pixels made the app ask for 885 GB, and the failed + allocation ended the whole app. Pictures are now sized from their + header before krilla decodes them (`papers::picture`, at most 50 + megapixels); + - a broken PDF upload made the PDF reader panic, so that person's pack + failed every time. Such a file now prints as a note page + (`pack::pdf::copies`). ## Open questions diff --git a/fuzz/fuzz_targets/signed_copy.rs b/fuzz/fuzz_targets/signed_copy.rs index 1ea534e..495e630 100644 --- a/fuzz/fuzz_targets/signed_copy.rs +++ b/fuzz/fuzz_targets/signed_copy.rs @@ -1,3 +1,6 @@ #![no_main] -libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::signed_copy(data)); +libfuzzer_sys::fuzz_target!( + init: visa::fuzzing::init(), + |data: &[u8]| visa::fuzzing::signed_copy(data) +); diff --git a/fuzz/fuzz_targets/text.rs b/fuzz/fuzz_targets/text.rs index 534bd68..23461de 100644 --- a/fuzz/fuzz_targets/text.rs +++ b/fuzz/fuzz_targets/text.rs @@ -1,3 +1,6 @@ #![no_main] -libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::text(data)); +libfuzzer_sys::fuzz_target!( + init: visa::fuzzing::init(), + |data: &[u8]| visa::fuzzing::text(data) +); diff --git a/fuzz/fuzz_targets/upload.rs b/fuzz/fuzz_targets/upload.rs index 8c15b57..d04fdd8 100644 --- a/fuzz/fuzz_targets/upload.rs +++ b/fuzz/fuzz_targets/upload.rs @@ -1,3 +1,6 @@ #![no_main] -libfuzzer_sys::fuzz_target!(|data: &[u8]| visa::fuzzing::upload(data)); +libfuzzer_sys::fuzz_target!( + init: visa::fuzzing::init(), + |data: &[u8]| visa::fuzzing::upload(data) +); diff --git a/src/fuzzing.rs b/src/fuzzing.rs index 01ea6bb..ff436d7 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -9,6 +9,25 @@ use chrono::NaiveDate; use crate::{agreements, conflicts, dates, files, gps, legal, mrz, pack, records}; +/// Run once before a fuzz target's first input. libFuzzer's panic hook +/// aborts at once, even inside the app's own `catch_unwind` (around the PDF +/// reader, `pack::pdf::copies`), which is not how the app runs. This hook +/// prints the panic (the first ten; a caught one can recur on every input) +/// and lets it unwind as in the app. A panic nothing catches still reaches +/// libfuzzer-sys, which counts it as a finding and saves its input; the +/// replay test (`tests/fuzz_cases.rs`) then shows its message. +pub fn init() { + use std::sync::atomic::{AtomicUsize, Ordering}; + static SEEN: AtomicUsize = AtomicUsize::new(0); + std::panic::set_hook(Box::new(|info| { + match SEEN.fetch_add(1, Ordering::Relaxed) { + 0..10 => eprintln!("{info}"), + 10 => eprintln!("(further panics not shown)"), + _ => {} + } + })); +} + /// A target's body: bytes in, a panic when something is wrong. pub type Target = fn(&[u8]);