Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
66 changes: 49 additions & 17 deletions crates/core/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) -> 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!(
Expand All @@ -256,13 +260,10 @@ fn record_bounds(pack: &[u8], offset: u64, max: Option<usize>) -> 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))
}

Expand All @@ -281,9 +282,12 @@ fn decompress_and_decode(payload: &[u8], id: &ObjectId, max: Option<usize>) -> 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 ({})",
Expand Down Expand Up @@ -431,8 +435,12 @@ pub fn parse_pack_reader_max<R: Read>(
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.
Expand Down Expand Up @@ -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());
Expand Down
9 changes: 9 additions & 0 deletions crates/repo/src/http_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> {
Expand Down
13 changes: 10 additions & 3 deletions crates/repo/src/serve_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ struct TokenFile {
tokens: Vec<TokenEntry>,
}

/// 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<Vec<TokenEntry>> {
Ok(toml::from_str::<TokenFile>(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<Vec<TokenEntry>> {
match std::fs::read_to_string(layout.serve_tokens_path()) {
Ok(s) => Ok(toml::from_str::<TokenFile>(&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)),
}
Expand Down
12 changes: 8 additions & 4 deletions crates/repo/src/tls_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ fn parse_line(line: &str) -> Option<PinEntry> {
})
}

/// 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<PinEntry> {
text.lines().filter_map(parse_line).collect()
}

fn read_pin_file(file: &Path) -> Result<String> {
match std::fs::read_to_string(file) {
Ok(t) => Ok(t),
Expand All @@ -365,10 +372,7 @@ fn read_pin_file(file: &Path) -> Result<String> {
/// 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<Vec<PinEntry>> {
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
Expand Down
6 changes: 5 additions & 1 deletion fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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
Binary file added fuzz/corpus/pack_decode/raw_pack
Binary file not shown.
Binary file added fuzz/corpus/pack_decode/valid_record
Binary file not shown.
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/http_opening.rs
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/pack_decode.rs
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/pack_stream.rs
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/text_files.rs
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 7 additions & 0 deletions fuzz/fuzz_targets/walfmt_decode.rs
Original file line number Diff line number Diff line change
@@ -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));
Loading
Loading