From 5daa478c0a72c88f3413c6819e69ab2f81c73520 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:39:41 +0200 Subject: [PATCH 1/4] test(fuzz): add pack, pack-stream, walfmt, HTTP opening and pin/token parser fuzz targets --- .github/workflows/fuzz.yml | 24 +++++++--- crates/repo/src/http_transport.rs | 9 ++++ crates/repo/src/serve_tokens.rs | 13 ++++-- crates/repo/src/tls_pins.rs | 12 +++-- fuzz/Cargo.toml | 39 ++++++++++++++++ fuzz/fuzz_targets/http_opening.rs | 7 +++ fuzz/fuzz_targets/pack_decode.rs | 7 +++ fuzz/fuzz_targets/pack_stream.rs | 7 +++ fuzz/fuzz_targets/text_files.rs | 7 +++ fuzz/fuzz_targets/walfmt_decode.rs | 7 +++ fuzz/src/lib.rs | 75 ++++++++++++++++++++++++++++++ 11 files changed, 194 insertions(+), 13 deletions(-) create mode 100644 fuzz/fuzz_targets/http_opening.rs create mode 100644 fuzz/fuzz_targets/pack_decode.rs create mode 100644 fuzz/fuzz_targets/pack_stream.rs create mode 100644 fuzz/fuzz_targets/text_files.rs create mode 100644 fuzz/fuzz_targets/walfmt_decode.rs create mode 100644 fuzz/src/lib.rs diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index 16add36e..c1fc33bd 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -2,7 +2,9 @@ name: fuzz # Nightly libFuzzer run on the remote-facing decode surface (OSTIF audit # T-13, G-001): the framed wire protocol `sc serve` parses from untrusted -# peers, its body decoders, and canonical object decoding. Runs on a +# peers, its body decoders, canonical object decoding, pack/index decoding, +# bucket WAL metadata, HTTP openings, and the pin and token file parsers. +# One matrix job per target. Runs on a # schedule, never as a PR gate — nightly + sanitizers don't belong in the # merge path. A crash uploads its reproducer input as an artifact. on: @@ -21,10 +23,20 @@ permissions: contents: read jobs: - wire-decode: - name: cargo fuzz (wire_decode) + fuzz: + name: cargo fuzz (${{ matrix.target }}) runs-on: ubuntu-latest timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + target: + - wire_decode + - pack_decode + - pack_stream + - walfmt_decode + - http_opening + - text_files env: # The repo-root rust-toolchain.toml pins stable 1.96.1 and overrides the # installed rustup default; libFuzzer needs nightly, and this env var @@ -42,16 +54,16 @@ jobs: - name: install cargo-fuzz run: cargo install cargo-fuzz --locked - name: build fuzz target - run: cargo fuzz build wire_decode + run: cargo fuzz build ${{ matrix.target }} - name: fuzz for 10 minutes # rss_limit above MAX_OBJECT_SIZE-sized legitimate allocations so the # documented frame-length pre-allocation (ADR-0039 deferred item) # doesn't false-positive; a real runaway still trips it. - run: cargo fuzz run wire_decode -- -max_total_time=600 -rss_limit_mb=4096 + run: cargo fuzz run ${{ matrix.target }} -- -max_total_time=600 -rss_limit_mb=4096 - name: upload crash reproducers if: failure() uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: fuzz-artifacts + name: fuzz-artifacts-${{ matrix.target }} path: fuzz/artifacts/ if-no-files-found: ignore diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index a6470087..fb5af29c 100644 --- a/crates/repo/src/http_transport.rs +++ b/crates/repo/src/http_transport.rs @@ -169,6 +169,15 @@ pub(crate) fn write_status(w: &mut impl Write, code: u16) -> Result<()> { .map_err(|e| Error::InvalidArgument(format!("HTTP status write failed: {e}"))) } +/// Run the three untrusted-opening parsers over `data`. Public only so the +/// fuzz crate can reach the crate-private readers. +#[doc(hidden)] +pub fn fuzz_openings(data: &[u8]) { + let _ = read_client_opening(&mut &data[..]); + let _ = read_status(&mut &data[..]); + let _ = read_bounded_opening(&mut &data[..]); +} + /// CLIENT: read the status line + headers up to the blank line (bounded). /// Returns the numeric status code. pub(crate) fn read_status(r: &mut impl Read) -> Result { diff --git a/crates/repo/src/serve_tokens.rs b/crates/repo/src/serve_tokens.rs index d8e50959..4667b1e6 100644 --- a/crates/repo/src/serve_tokens.rs +++ b/crates/repo/src/serve_tokens.rs @@ -53,12 +53,19 @@ struct TokenFile { tokens: Vec, } +/// Parse the text of `serve-tokens.toml`. Public only so the fuzz crate can +/// reach the parser without touching disk. +#[doc(hidden)] +pub fn parse_tokens(s: &str) -> Result> { + Ok(toml::from_str::(s) + .map_err(|e| Error::BadConfig(format!("bad serve-tokens.toml: {e}")))? + .tokens) +} + /// Load all tokens (empty when the file is absent — the "no auth" state). pub fn load(layout: &Layout) -> Result> { match std::fs::read_to_string(layout.serve_tokens_path()) { - Ok(s) => Ok(toml::from_str::(&s) - .map_err(|e| Error::BadConfig(format!("bad serve-tokens.toml: {e}")))? - .tokens), + Ok(s) => parse_tokens(&s), Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()), Err(e) => Err(Error::Io(e)), } diff --git a/crates/repo/src/tls_pins.rs b/crates/repo/src/tls_pins.rs index f3aea7b5..abb80d9d 100644 --- a/crates/repo/src/tls_pins.rs +++ b/crates/repo/src/tls_pins.rs @@ -350,6 +350,13 @@ fn parse_line(line: &str) -> Option { }) } +/// Parse pin-file text the way [`list`] does. Public only so the fuzz crate +/// can reach the line parser without touching disk. +#[doc(hidden)] +pub fn parse_pin_text(text: &str) -> Vec { + text.lines().filter_map(parse_line).collect() +} + fn read_pin_file(file: &Path) -> Result { match std::fs::read_to_string(file) { Ok(t) => Ok(t), @@ -365,10 +372,7 @@ fn read_pin_file(file: &Path) -> Result { /// comments and malformed lines are skipped). A host that was pinned twice is /// listed twice: a connection uses the first. pub fn list(file: &Path) -> Result> { - Ok(read_pin_file(file)? - .lines() - .filter_map(parse_line) - .collect()) + Ok(parse_pin_text(&read_pin_file(file)?)) } /// Drop every pin for `host:port` (hostnames compare case-insensitively) and diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 1a509897..bd3c0d8c 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -11,6 +11,10 @@ edition = "2021" [package.metadata] cargo-fuzz = true +[lib] +name = "scl_fuzz" +path = "src/lib.rs" + [dependencies] libfuzzer-sys = "0.4" scl-core = { path = "../crates/core" } @@ -26,5 +30,40 @@ test = false doc = false bench = false +[[bin]] +name = "pack_decode" +path = "fuzz_targets/pack_decode.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "pack_stream" +path = "fuzz_targets/pack_stream.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "walfmt_decode" +path = "fuzz_targets/walfmt_decode.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "http_opening" +path = "fuzz_targets/http_opening.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "text_files" +path = "fuzz_targets/text_files.rs" +test = false +doc = false +bench = false + [profile.release] debug = 1 diff --git a/fuzz/fuzz_targets/http_opening.rs b/fuzz/fuzz_targets/http_opening.rs new file mode 100644 index 00000000..c7c05ef0 --- /dev/null +++ b/fuzz/fuzz_targets/http_opening.rs @@ -0,0 +1,7 @@ +//! Fuzz `scl_fuzz::http_opening` (see fuzz/src/lib.rs for what it reaches). + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|data: &[u8]| scl_fuzz::http_opening(data)); diff --git a/fuzz/fuzz_targets/pack_decode.rs b/fuzz/fuzz_targets/pack_decode.rs new file mode 100644 index 00000000..c34c1acf --- /dev/null +++ b/fuzz/fuzz_targets/pack_decode.rs @@ -0,0 +1,7 @@ +//! Fuzz `scl_fuzz::pack` (see fuzz/src/lib.rs for what it reaches). + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|data: &[u8]| scl_fuzz::pack(data)); diff --git a/fuzz/fuzz_targets/pack_stream.rs b/fuzz/fuzz_targets/pack_stream.rs new file mode 100644 index 00000000..b950fd72 --- /dev/null +++ b/fuzz/fuzz_targets/pack_stream.rs @@ -0,0 +1,7 @@ +//! Fuzz `scl_fuzz::pack_stream` (see fuzz/src/lib.rs for what it reaches). + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|data: &[u8]| scl_fuzz::pack_stream(data)); diff --git a/fuzz/fuzz_targets/text_files.rs b/fuzz/fuzz_targets/text_files.rs new file mode 100644 index 00000000..4d67150e --- /dev/null +++ b/fuzz/fuzz_targets/text_files.rs @@ -0,0 +1,7 @@ +//! Fuzz `scl_fuzz::text_files` (see fuzz/src/lib.rs for what it reaches). + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|data: &[u8]| scl_fuzz::text_files(data)); diff --git a/fuzz/fuzz_targets/walfmt_decode.rs b/fuzz/fuzz_targets/walfmt_decode.rs new file mode 100644 index 00000000..fd3447a8 --- /dev/null +++ b/fuzz/fuzz_targets/walfmt_decode.rs @@ -0,0 +1,7 @@ +//! Fuzz `scl_fuzz::walfmt` (see fuzz/src/lib.rs for what it reaches). + +#![no_main] + +use libfuzzer_sys::fuzz_target; + +fuzz_target!(|data: &[u8]| scl_fuzz::walfmt(data)); diff --git a/fuzz/src/lib.rs b/fuzz/src/lib.rs new file mode 100644 index 00000000..4d2e22b8 --- /dev/null +++ b/fuzz/src/lib.rs @@ -0,0 +1,75 @@ +//! Harness bodies shared by the fuzz targets and the plain `cargo test` replay +//! below, so a broken harness is caught without nightly. Each function feeds +//! arbitrary bytes to an untrusted-input decoder, in memory only; totality +//! (Ok or Err, never a panic or runaway allocation) is the invariant. + +use std::io::Cursor; + +/// Per-object cap for the pack readers: small, so a zstd bomb trips the bound +/// instead of the fuzzer's RSS limit. +const CAP: usize = 1 << 20; + +/// Pack record, index and bounded-zstd decoding (`scl_core::pack`). +pub fn pack(data: &[u8]) { + let _ = scl_core::pack::parse_index(data); + let _ = scl_core::pack::parse_pack_reader_max(Cursor::new(data), CAP, |_, _| Ok(())); + // Offset is attacker-chosen in a hostile index; derive it from the input. + let off = data.first().map_or(0, |&b| u64::from(b)); + let id = scl_core::ObjectId::from_bytes([0u8; 32]); + let _ = scl_core::pack::read_object_at_bounded(data, off, &id, CAP); +} + +/// The streamed-pack frame loop (`wire::read_pack_stream`). +pub fn pack_stream(data: &[u8]) { + let mut sink = Vec::new(); + let _ = scl_repo::wire::read_pack_stream(&mut Cursor::new(data), &mut sink, CAP as u64); +} + +/// Bucket WAL metadata (`walfmt`). +pub fn walfmt(data: &[u8]) { + let _ = scl_repo::walfmt::Manifest::decode(data); + let _ = scl_repo::walfmt::LogEntry::decode(data); + let _ = scl_repo::walfmt::Checkpoint::decode(data); +} + +/// HTTP opening parsers (server request, client status, bounded reader). +pub fn http_opening(data: &[u8]) { + scl_repo::http_transport::fuzz_openings(data); +} + +/// TLS pin file and serve-token file parsers (text, no disk). +pub fn text_files(data: &[u8]) { + let text = String::from_utf8_lossy(data); + let _ = scl_repo::tls_pins::parse_pin_text(&text); + let _ = scl_repo::tls_pins::parse_host_port(&text); + let _ = scl_repo::tls_pins::parse_fingerprint(&text); + let _ = scl_repo::serve_tokens::parse_tokens(&text); +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Hand-made seeds: empty, magic-only, short garbage, valid-looking openings. + const SEEDS: &[&[u8]] = &[ + b"", + b"\0", + b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", + b"SCLP\x01\0\0\0", + b"POST /x HTTP/1.1\r\nAuthorization: Bearer t\r\n\r\n", + b"HTTP/1.1 200 OK\r\n\r\n", + b"example.com:443 sha256:00\n# c\n[::1]:1 zz\n", + b"[[token]]\nlabel=\"a\"\nhash=\"b\"\nscope=\"rw\"\n", + ]; + + #[test] + fn harnesses_are_total_on_seeds() { + for s in SEEDS { + pack(s); + pack_stream(s); + walfmt(s); + http_opening(s); + text_files(s); + } + } +} From 0ac063f4090fcde0b560f41f696eb38c86870740 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:18:04 +0200 Subject: [PATCH 2/4] test(fuzz): full-width offset and id for pack_decode, reachable caps, seed corpus --- .github/workflows/fuzz.yml | 7 +++- fuzz/.gitignore | 5 ++- fuzz/corpus/pack_decode/valid_record | Bin 0 -> 98 bytes fuzz/src/lib.rs | 53 ++++++++++++++++++++++++--- 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 fuzz/corpus/pack_decode/valid_record diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index c1fc33bd..aa2f7ff9 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -37,6 +37,11 @@ jobs: - walfmt_decode - http_opening - text_files + include: + # libFuzzer's default max_len (4096 on an empty corpus) cannot reach + # the 8 KiB opening cap. + - target: http_opening + args: -max_len=8192 env: # The repo-root rust-toolchain.toml pins stable 1.96.1 and overrides the # installed rustup default; libFuzzer needs nightly, and this env var @@ -59,7 +64,7 @@ jobs: # rss_limit above MAX_OBJECT_SIZE-sized legitimate allocations so the # documented frame-length pre-allocation (ADR-0039 deferred item) # doesn't false-positive; a real runaway still trips it. - run: cargo fuzz run ${{ matrix.target }} -- -max_total_time=600 -rss_limit_mb=4096 + run: cargo fuzz run ${{ matrix.target }} -- -max_total_time=600 -rss_limit_mb=4096 ${{ matrix.args }} - name: upload crash reproducers if: failure() uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 diff --git a/fuzz/.gitignore b/fuzz/.gitignore index ab0eaa1a..7725fb30 100644 --- a/fuzz/.gitignore +++ b/fuzz/.gitignore @@ -1,5 +1,8 @@ target/ -corpus/ +corpus/* +!corpus/pack_decode/ +corpus/pack_decode/* +!corpus/pack_decode/valid_record artifacts/ coverage/ Cargo.lock diff --git a/fuzz/corpus/pack_decode/valid_record b/fuzz/corpus/pack_decode/valid_record new file mode 100644 index 0000000000000000000000000000000000000000..587be5073c7b228fb178aa112f75fd009a530e74 GIT binary patch literal 98 zcmdO3fPkPaOa9JOEHhqK%@zLUXX5qd%U{LzFyyD~D-e2qr|E=xuycSnBTy+Jb$mcI R8e8@MDzIt-S;eWTDFC Date: Fri, 20 Nov 2026 10:20:58 +0200 Subject: [PATCH 3/4] fix(core): checked arithmetic for untrusted pack record offsets and lengths --- crates/core/src/pack.rs | 66 ++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/crates/core/src/pack.rs b/crates/core/src/pack.rs index 386a2c04..28b535a7 100644 --- a/crates/core/src/pack.rs +++ b/crates/core/src/pack.rs @@ -243,11 +243,15 @@ pub fn read_object_at_bounded( /// before it's ever used to slice `pack` — shared by [`read_object_at`] /// (`max = None`) and [`read_object_at_bounded`] (`max = Some(cap)`). fn record_bounds(pack: &[u8], offset: u64, max: Option) -> Result<(usize, usize)> { - let off = offset as usize; - if off + 4 > pack.len() { - return Err(Error::PackCorrupt(format!("offset {offset} past end"))); - } - let len = u32::from_le_bytes(pack[off..off + 4].try_into().unwrap()) as usize; + // Offset and length come from an untrusted index or record: no unchecked + // addition on either. + let past_end = || Error::PackCorrupt(format!("offset {offset} past end")); + let off = usize::try_from(offset).map_err(|_| past_end())?; + let start = off + .checked_add(4) + .filter(|&s| s <= pack.len()) + .ok_or_else(past_end)?; + let len = u32::from_le_bytes(pack[off..start].try_into().unwrap()) as usize; if let Some(max) = max { if len > max { return Err(Error::PackCorrupt(format!( @@ -256,13 +260,10 @@ fn record_bounds(pack: &[u8], offset: u64, max: Option) -> Result<(usize, ))); } } - let start = off + 4; - let end = start + len; - if end > pack.len() { - return Err(Error::PackCorrupt(format!( - "record at {offset} runs past end" - ))); - } + let end = start + .checked_add(len) + .filter(|&e| e <= pack.len()) + .ok_or_else(|| Error::PackCorrupt(format!("record at {offset} runs past end")))?; Ok((start, end)) } @@ -281,9 +282,12 @@ fn decompress_and_decode(payload: &[u8], id: &ObjectId, max: Option) -> R Some(max) => { let mut decoder = zstd::stream::read::Decoder::new(std::io::Cursor::new(payload)) .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))?; - let canonical = - crate::read_limited(&mut decoder, max + 1, payload.len().saturating_mul(4)) - .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))?; + let canonical = crate::read_limited( + &mut decoder, + max.saturating_add(1), + payload.len().saturating_mul(4), + ) + .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))?; if canonical.len() > max { return Err(Error::PackCorrupt(format!( "decompressed object exceeds the {max}-byte object size limit ({})", @@ -431,8 +435,12 @@ pub fn parse_pack_reader_max( let canonical = { let mut decoder = zstd::stream::read::Decoder::new(std::io::Cursor::new(&payload)) .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))?; - crate::read_limited(&mut decoder, max + 1, payload.len().saturating_mul(4)) - .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))? + crate::read_limited( + &mut decoder, + max.saturating_add(1), + payload.len().saturating_mul(4), + ) + .map_err(|e| Error::PackCorrupt(format!("zstd decode failed: {e}")))? }; // Release the compressed copy before decoding, so peak per record is // decompressed + decoded (about 2 x cap), not three copies. @@ -464,6 +472,30 @@ mod tests { (o.id(), o.encode()) } + /// A hostile index offset or record length near the integer limits must + /// be a corrupt-pack error, never an overflow panic (debug) or wrap + /// (release). + #[test] + fn record_bounds_rejects_overflowing_offsets_and_lengths() { + let id = crate::id::ObjectId::from_bytes([0u8; 32]); + let pack = [0u8; 48]; + for offset in [u64::MAX, u64::MAX - 3, usize::MAX as u64 - 3, 45, 48] { + for max in [None, Some(1 << 20)] { + let err = record_bounds(&pack, offset, max).unwrap_err(); + assert!(matches!(err, Error::PackCorrupt(_)), "{offset}: {err:?}"); + } + assert!(read_object_at(&pack, offset, &id).is_err()); + assert!(read_object_at_bounded(&pack, offset, &id, 1 << 20).is_err()); + } + // Length prefix claims u32::MAX bytes from a valid offset. + let mut long = vec![0u8; 16]; + long[..4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(matches!( + record_bounds(&long, 0, None).unwrap_err(), + Error::PackCorrupt(_) + )); + } + #[test] fn build_then_read_each_object_back() { let a = Object::blob(b"alpha".to_vec()); From 30d77abe6f28f45f64f1d6e6da8a308d57618b69 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:24:04 +0200 Subject: [PATCH 4/4] test(fuzz): seed pack_decode with a raw pack for the streaming parser --- fuzz/.gitignore | 1 + fuzz/corpus/pack_decode/raw_pack | Bin 0 -> 58 bytes 2 files changed, 1 insertion(+) create mode 100644 fuzz/corpus/pack_decode/raw_pack diff --git a/fuzz/.gitignore b/fuzz/.gitignore index 7725fb30..03356eff 100644 --- a/fuzz/.gitignore +++ b/fuzz/.gitignore @@ -3,6 +3,7 @@ corpus/* !corpus/pack_decode/ corpus/pack_decode/* !corpus/pack_decode/valid_record +!corpus/pack_decode/raw_pack artifacts/ coverage/ Cargo.lock diff --git a/fuzz/corpus/pack_decode/raw_pack b/fuzz/corpus/pack_decode/raw_pack new file mode 100644 index 0000000000000000000000000000000000000000..d4db33a63f6d34350ff9a1ec38577f2fb87f04e2 GIT binary patch literal 58 zcmWG`4)A7VU|