[fuzz] host_http_request and host_pairing targets (head cutter agrees with the parser, query round trip, never Paired without the client key, Failed is terminal)
This commit is contained in:
@@ -211,3 +211,17 @@ path = "fuzz_targets/rtsp_client.rs"
|
|||||||
test = false
|
test = false
|
||||||
doc = false
|
doc = false
|
||||||
bench = false
|
bench = false
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "host_http_request"
|
||||||
|
path = "fuzz_targets/host_http_request.rs"
|
||||||
|
test = false
|
||||||
|
doc = false
|
||||||
|
bench = false
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "host_pairing"
|
||||||
|
path = "fuzz_targets/host_pairing.rs"
|
||||||
|
test = false
|
||||||
|
doc = false
|
||||||
|
bench = false
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
//! Fuzzes the host's HTTP request parsing: the head cutter, the head parser, the target parser and the query parser.
|
||||||
|
//! Asserts invariants, not only "no panic": a parsed head ends exactly where the cutter says, a parsed query
|
||||||
|
//! re-encodes to the same parameters, and every typed value of a parsed request is within its documented range.
|
||||||
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||||
|
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use libfuzzer_sys::fuzz_target;
|
||||||
|
|
||||||
|
use domistream::host::http::{
|
||||||
|
HostRequest, MAX_HEAD_BYTES, MAX_MODE_DIMENSION, MAX_MODE_FPS, MAX_PARAMS, Route, encode_query, find_head_end,
|
||||||
|
parse_query, parse_request_head, parse_target,
|
||||||
|
};
|
||||||
|
use domistream::host::pairing::PairPhase;
|
||||||
|
|
||||||
|
fn check(req: &HostRequest) {
|
||||||
|
assert!(req.other_params.len() <= MAX_PARAMS);
|
||||||
|
if let Some(u) = &req.unique_id {
|
||||||
|
assert!(!u.is_empty() && u.len() <= 64, "uniqueid {u:?}");
|
||||||
|
}
|
||||||
|
let mode_ok = |m: &domistream::host::http::DisplayMode| {
|
||||||
|
(1..=MAX_MODE_DIMENSION).contains(&m.width)
|
||||||
|
&& (1..=MAX_MODE_DIMENSION).contains(&m.height)
|
||||||
|
&& (1..=MAX_MODE_FPS).contains(&m.fps)
|
||||||
|
};
|
||||||
|
match &req.route {
|
||||||
|
Route::Launch(l) => assert!(mode_ok(&l.mode), "{:?}", l.mode),
|
||||||
|
Route::Resume(r) => assert!(r.mode.as_ref().is_none_or(mode_ok)),
|
||||||
|
Route::Pair(p) => {
|
||||||
|
assert_eq!(req.unique_id.as_deref(), Some(p.unique_id.as_str()), "/pair needs uniqueid");
|
||||||
|
match &p.phase {
|
||||||
|
PairPhase::GetServerCert { client_cert_pem, .. } => assert!(!client_cert_pem.is_empty()),
|
||||||
|
PairPhase::ClientChallenge(v) | PairPhase::ServerChallengeResp(v) | PairPhase::ClientPairingSecret(v) => {
|
||||||
|
assert!(!v.is_empty())
|
||||||
|
}
|
||||||
|
PairPhase::PairChallenge => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Route::Unknown { path } => assert!(path.starts_with('/') && path.len() <= 256),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fuzz_target!(|data: &[u8]| {
|
||||||
|
let cut = find_head_end(data);
|
||||||
|
if let Ok(Some(n)) = cut {
|
||||||
|
assert!((4..=MAX_HEAD_BYTES).contains(&n) && data[..n].ends_with(b"\r\n\r\n"));
|
||||||
|
}
|
||||||
|
if let Ok(req) = parse_request_head(data) {
|
||||||
|
assert_eq!(cut, Ok(Some(data.len())), "a parsed head ends where the cutter says");
|
||||||
|
check(&req);
|
||||||
|
}
|
||||||
|
let Ok(text) = core::str::from_utf8(data) else { return };
|
||||||
|
if let Ok(req) = parse_target(text) {
|
||||||
|
check(&req);
|
||||||
|
}
|
||||||
|
if let Ok(params) = parse_query(text) {
|
||||||
|
let list: Vec<(&str, &str)> = params.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
|
||||||
|
match parse_query(&encode_query(&list)) {
|
||||||
|
Ok(again) => assert_eq!(again, params, "parse -> encode -> parse"),
|
||||||
|
// Encoding can triple a value (`+` becomes `%20`), so only a cap may refuse it.
|
||||||
|
Err(e) => assert!(matches!(e, domistream::Error::TooLarge { .. }), "{e:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
//! Fuzzes the host's pairing state machine: a session fed arbitrary sequences of phases, PINs, timer wake-ups and
|
||||||
|
//! cancels, with arbitrary values, transports and client ids.
|
||||||
|
//! Asserts invariants, not only "no panic": a fuzzer that holds no client key never gets the session to Paired,
|
||||||
|
//! Failed is terminal, and the session has a deadline exactly while it is in progress.
|
||||||
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||||
|
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use core::time::Duration;
|
||||||
|
|
||||||
|
use libfuzzer_sys::fuzz_target;
|
||||||
|
|
||||||
|
use domistream::host::pairing::{
|
||||||
|
HostIdentity, HostPairingConfig, HostPairingSession, HostPairingState, HostPairingStep, PairPhase, PairRequest,
|
||||||
|
Transport,
|
||||||
|
};
|
||||||
|
use domistream::pairing::Pin;
|
||||||
|
use domistream::{Error, Instant, RandomSource};
|
||||||
|
|
||||||
|
const VECTORS: &str = include_str!("../../crates/domistream/tests/fixtures/pairing.txt");
|
||||||
|
/// The certificate phase 1 presents: a test-only certificate whose key the fuzzer does not have.
|
||||||
|
const CLIENT_CERT_PEM: &str = include_str!("../../crates/domistream/tests/fixtures/test-only-host-cert.pem");
|
||||||
|
|
||||||
|
fn value(name: &str) -> Vec<u8> {
|
||||||
|
let prefix = format!("{name} = ");
|
||||||
|
let line = VECTORS.lines().find(|l| l.starts_with(&prefix)).expect("fixture value");
|
||||||
|
let hex = &line[prefix.len()..];
|
||||||
|
(0..hex.len()).step_by(2).map(|i| u8::from_str_radix(&hex[i..i + 2], 16).expect("hex")).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Fixed;
|
||||||
|
impl RandomSource for Fixed {
|
||||||
|
fn fill(&mut self, dest: &mut [u8]) {
|
||||||
|
dest.fill(0x5A);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the fuzzer's bytes as a script.
|
||||||
|
struct Script<'a>(&'a [u8]);
|
||||||
|
|
||||||
|
impl Script<'_> {
|
||||||
|
fn byte(&mut self) -> Option<u8> {
|
||||||
|
let (&b, rest) = self.0.split_first()?;
|
||||||
|
self.0 = rest;
|
||||||
|
Some(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bytes(&mut self, n: usize) -> Vec<u8> {
|
||||||
|
let n = n.min(self.0.len());
|
||||||
|
let (head, rest) = self.0.split_at(n);
|
||||||
|
self.0 = rest;
|
||||||
|
head.to_vec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fuzz_target!(|data: &[u8]| {
|
||||||
|
// Any RSA identity serves as the host's: the client fixture's.
|
||||||
|
let Ok(host) = HostIdentity::from_der(&value("client_cert_der"), &value("client_key_pkcs8_der")) else { return };
|
||||||
|
let host_cert_der = value("host_cert_der");
|
||||||
|
let mut s = HostPairingSession::new(&host, HostPairingConfig::default(), Instant::ZERO);
|
||||||
|
let mut now = Instant::ZERO;
|
||||||
|
let mut script = Script(data);
|
||||||
|
let mut steps = 0;
|
||||||
|
while let Some(op) = script.byte() {
|
||||||
|
steps += 1;
|
||||||
|
if steps > 24 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let before = s.state();
|
||||||
|
let flags = script.byte().unwrap_or(0);
|
||||||
|
now = now.saturating_add(Duration::from_millis(u64::from(flags & 0x0F) * 700));
|
||||||
|
let unique_id = if flags & 0x10 != 0 {
|
||||||
|
String::from_utf8_lossy(&script.bytes(usize::from(flags >> 5) * 3)).into_owned()
|
||||||
|
} else {
|
||||||
|
"0123456789ABCDEF".to_string()
|
||||||
|
};
|
||||||
|
let len = usize::from(script.byte().unwrap_or(0));
|
||||||
|
let phase = match op % 8 {
|
||||||
|
0 => {
|
||||||
|
let mut salt = [0u8; 16];
|
||||||
|
let raw = script.bytes(16);
|
||||||
|
salt[..raw.len()].copy_from_slice(&raw);
|
||||||
|
let client_cert_pem =
|
||||||
|
if len % 4 == 0 { script.bytes(len * 8) } else { CLIENT_CERT_PEM.as_bytes().to_vec() };
|
||||||
|
Some(PairPhase::GetServerCert { salt, client_cert_pem })
|
||||||
|
}
|
||||||
|
1 => Some(PairPhase::ClientChallenge(script.bytes(len % 40))),
|
||||||
|
2 => Some(PairPhase::ServerChallengeResp(script.bytes(len % 72))),
|
||||||
|
3 => Some(PairPhase::ClientPairingSecret(script.bytes(len * 2))),
|
||||||
|
4 => Some(PairPhase::PairChallenge),
|
||||||
|
5 => {
|
||||||
|
let digits = format!("{:04}", u16::from_le_bytes([len as u8, flags]) % 10_000);
|
||||||
|
if let Ok(pin) = Pin::from_digits(&digits) {
|
||||||
|
let r = s.supply_pin(&host, pin, now);
|
||||||
|
if r.is_ok() {
|
||||||
|
assert_eq!(before, HostPairingState::AwaitPin);
|
||||||
|
assert_eq!(s.state(), HostPairingState::AwaitClientChallenge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
6 => {
|
||||||
|
let late = now.saturating_add(Duration::from_secs(u64::from(flags)));
|
||||||
|
let fired = s.handle_timeout(late);
|
||||||
|
assert_eq!(fired, matches!(s.state(), HostPairingState::Failed(_)) && !matches!(before, HostPairingState::Failed(_)));
|
||||||
|
None
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
s.cancel();
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(phase) = phase {
|
||||||
|
let device_name = (flags & 0x80 != 0).then(|| "fuzz".to_string());
|
||||||
|
let request = PairRequest { unique_id, device_name, phase };
|
||||||
|
let other_cert;
|
||||||
|
let transport = match len % 3 {
|
||||||
|
0 => Transport::Http,
|
||||||
|
1 => Transport::Https { client_cert_der: &host_cert_der },
|
||||||
|
_ => {
|
||||||
|
other_cert = script.bytes(len);
|
||||||
|
Transport::Https { client_cert_der: &other_cert }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let r = s.handle(&host, &request, transport, now, &mut Fixed);
|
||||||
|
assert!(!matches!(r, Ok(HostPairingStep::Paired(_))), "paired without the client's key");
|
||||||
|
if matches!(before, HostPairingState::Failed(_) | HostPairingState::Paired) {
|
||||||
|
assert!(matches!(r, Err(Error::State { .. })));
|
||||||
|
}
|
||||||
|
if r.is_err() {
|
||||||
|
assert!(matches!(s.state(), HostPairingState::Failed(_)), "an error ends the session: {r:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_ne!(s.state(), HostPairingState::Paired, "paired without the client's key");
|
||||||
|
assert!(s.paired_client().is_none());
|
||||||
|
if let HostPairingState::Failed(_) = before {
|
||||||
|
assert_eq!(s.state(), before, "Failed is terminal");
|
||||||
|
}
|
||||||
|
assert_eq!(s.poll_timeout().is_none(), matches!(s.state(), HostPairingState::Failed(_)), "a deadline while in progress");
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user