diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index 16add36e..aa2f7ff9 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,25 @@ 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 + 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 @@ -42,16 +59,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 ${{ matrix.args }} - 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/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()); diff --git a/crates/repo/src/http_transport.rs b/crates/repo/src/http_transport.rs index dbe941b8..14be1d12 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/.gitignore b/fuzz/.gitignore index ab0eaa1a..03356eff 100644 --- a/fuzz/.gitignore +++ b/fuzz/.gitignore @@ -1,5 +1,9 @@ target/ -corpus/ +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/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/corpus/pack_decode/raw_pack b/fuzz/corpus/pack_decode/raw_pack new file mode 100644 index 00000000..d4db33a6 Binary files /dev/null and b/fuzz/corpus/pack_decode/raw_pack differ diff --git a/fuzz/corpus/pack_decode/valid_record b/fuzz/corpus/pack_decode/valid_record new file mode 100644 index 00000000..587be507 Binary files /dev/null and b/fuzz/corpus/pack_decode/valid_record differ 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..1b5119ba --- /dev/null +++ b/fuzz/src/lib.rs @@ -0,0 +1,116 @@ +//! 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`). +/// +/// Input layout for the record reader: `[offset: u64 LE][expected id: 32][pack +/// bytes]`, so the fuzzer controls the offset and the id the hash check +/// compares against. The index and streaming parsers get the whole input. +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(())); + if let Some((head, pack)) = data.split_at_checked(40) { + let offset = u64::from_le_bytes(head[..8].try_into().unwrap()); + let id = scl_core::ObjectId::from_bytes(head[8..].try_into().unwrap()); + let _ = scl_core::pack::read_object_at_bounded(pack, offset, &id, CAP); + } +} + +/// The streamed-pack frame loop (`wire::read_pack_stream`). The byte budget is +/// below libFuzzer's default 4096-byte input cap so the over-budget path is +/// reachable. +pub fn pack_stream(data: &[u8]) { + let mut sink = Vec::new(); + let _ = scl_repo::wire::read_pack_stream(&mut Cursor::new(data), &mut sink, 1024); +} + +/// 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", + ]; + + /// The committed corpus seed must decode successfully, so the record + /// reader's success path stays reachable from the corpus. + #[test] + fn pack_decode_seed_reaches_successful_decode() { + let seed = include_bytes!("../corpus/pack_decode/valid_record"); + let offset = u64::from_le_bytes(seed[..8].try_into().unwrap()); + let id = scl_core::ObjectId::from_bytes(seed[8..40].try_into().unwrap()); + scl_core::pack::read_object_at_bounded(&seed[40..], offset, &id, CAP).unwrap(); + pack(seed); + } + + /// Generator for the seed above; run with `--ignored` to rewrite it. + #[test] + #[ignore] + fn regenerate_pack_decode_seed() { + let canonical = scl_core::Object::blob(b"seed".to_vec()).encode(); + let id = scl_core::ObjectId::of(&canonical); + let (pack, idx) = scl_core::pack::build_pack(&[(id, canonical)]).unwrap(); + let offset = scl_core::pack::parse_index(&idx).unwrap()[0].offset; + let mut out = offset.to_le_bytes().to_vec(); + out.extend_from_slice(id.as_bytes()); + out.extend_from_slice(&pack); + std::fs::create_dir_all(concat!(env!("CARGO_MANIFEST_DIR"), "/corpus/pack_decode")) + .unwrap(); + std::fs::write( + concat!( + env!("CARGO_MANIFEST_DIR"), + "/corpus/pack_decode/valid_record" + ), + out, + ) + .unwrap(); + } + + #[test] + fn harnesses_are_total_on_seeds() { + for s in SEEDS { + pack(s); + pack_stream(s); + walfmt(s); + http_opening(s); + text_files(s); + } + } +}