[fuzz] enet_host and control_host targets
enet_host: accept on the first datagram or a canned connect (optionally acknowledged), then datagrams, some with our header; asserts outgoing datagrams parse within the MTU and deliveries stay under the cap. control_host: a connected ControlHost fed sealed arbitrary plaintexts, replayed sequence numbers, arbitrary frames in valid commands and raw datagrams; asserts parse_client_message invariants and that no message appears without a sealed frame. 60 s each, 0 crashes.
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 = "enet_host"
|
||||||
|
path = "fuzz_targets/enet_host.rs"
|
||||||
|
test = false
|
||||||
|
doc = false
|
||||||
|
bench = false
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "control_host"
|
||||||
|
path = "fuzz_targets/control_host.rs"
|
||||||
|
test = false
|
||||||
|
doc = false
|
||||||
|
bench = false
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
//! Fuzzes the control host: a connected ControlHost fed a mix of raw datagrams, arbitrary plaintexts sealed with
|
||||||
|
//! the session key (so the decrypted-message path is reached), arbitrary frames inside valid ENet commands, and
|
||||||
|
//! time steps; plus `parse_client_message` on every chunk.
|
||||||
|
//! Asserts invariants, not only "no panic": a client message always splits as a message first; kept bodies and
|
||||||
|
//! reported lengths match the input; outgoing datagrams parse; a message event never outnumbers what was sealed.
|
||||||
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||||
|
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use libfuzzer_sys::fuzz_target;
|
||||||
|
|
||||||
|
use core::time::Duration;
|
||||||
|
|
||||||
|
use domistream::Instant;
|
||||||
|
use domistream::control::{
|
||||||
|
ClientControlMessage, ControlCipher, ControlHost, ControlHostConfig, ControlHostEvent, Direction, parse_client_message,
|
||||||
|
parse_message,
|
||||||
|
};
|
||||||
|
use domistream::enet::EnetHostConfig;
|
||||||
|
use domistream::enet::protocol::{Command, CommandBody, ConnectParams, Datagram, NO_PEER_ID, encode_datagram, parse_datagram};
|
||||||
|
|
||||||
|
const PEER_ID: u16 = 3;
|
||||||
|
const KEY: [u8; 16] = [7; 16];
|
||||||
|
|
||||||
|
fn datagram(peer_id: u16, sent_time: u16, commands: Vec<Command>) -> Option<Vec<u8>> {
|
||||||
|
encode_datagram(&Datagram { peer_id, session_id: 0, sent_time: Some(sent_time), commands }).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connected() -> ControlHost {
|
||||||
|
let params = ConnectParams {
|
||||||
|
outgoing_peer_id: 0,
|
||||||
|
incoming_session_id: 0xFF,
|
||||||
|
outgoing_session_id: 0xFF,
|
||||||
|
mtu: 1400,
|
||||||
|
window_size: 65536,
|
||||||
|
channel_count: 1,
|
||||||
|
incoming_bandwidth: 0,
|
||||||
|
outgoing_bandwidth: 0,
|
||||||
|
packet_throttle_interval: 5000,
|
||||||
|
packet_throttle_acceleration: 2,
|
||||||
|
packet_throttle_deceleration: 2,
|
||||||
|
connect_id: 1,
|
||||||
|
};
|
||||||
|
let connect = Command { acknowledge: true, channel_id: 0xFF, reliable_sequence: 1, body: CommandBody::Connect { params, data: 0 } };
|
||||||
|
let config = ControlHostConfig {
|
||||||
|
enet: EnetHostConfig {
|
||||||
|
peer_id: PEER_ID,
|
||||||
|
channel_limit: 1,
|
||||||
|
mtu: 1400,
|
||||||
|
ping_interval: Duration::from_millis(500),
|
||||||
|
timeout: Duration::from_secs(10),
|
||||||
|
},
|
||||||
|
rikey: KEY,
|
||||||
|
stop_timeout: Some(Duration::from_secs(2)),
|
||||||
|
};
|
||||||
|
let mut h = ControlHost::accept(config, Instant::ZERO, &datagram(NO_PEER_ID, 0, vec![connect]).expect("connect"))
|
||||||
|
.expect("canned connect");
|
||||||
|
while h.poll_transmit(Instant::ZERO).is_some() {}
|
||||||
|
let ack = Command {
|
||||||
|
acknowledge: false,
|
||||||
|
channel_id: 0xFF,
|
||||||
|
reliable_sequence: 1,
|
||||||
|
body: CommandBody::Acknowledge { received_reliable_sequence: 1, received_sent_time: 0 },
|
||||||
|
};
|
||||||
|
h.handle_datagram(Instant::from_micros(1000), &datagram(PEER_ID, 1, vec![ack]).expect("ack")).expect("canned ack");
|
||||||
|
assert_eq!(h.poll_event(), Some(ControlHostEvent::Connected));
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_parse(chunk: &[u8]) {
|
||||||
|
let raw = parse_message(chunk);
|
||||||
|
match parse_client_message(chunk) {
|
||||||
|
Ok(m) => {
|
||||||
|
assert!(raw.is_ok(), "a client message must split as a message first");
|
||||||
|
match m {
|
||||||
|
ClientControlMessage::Unknown { body_len, .. } | ClientControlMessage::InputRejected { body_len, .. } => {
|
||||||
|
assert_eq!(body_len + 4, chunk.len());
|
||||||
|
}
|
||||||
|
ClientControlMessage::LossStats(b) | ClientControlMessage::FrameStats(b) => {
|
||||||
|
assert_eq!(&b[..], &chunk[4..]);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => assert!(raw.is_err(), "only the split can fail"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fuzz_target!(|data: &[u8]| {
|
||||||
|
let mut h = connected();
|
||||||
|
let cipher = ControlCipher::new(KEY);
|
||||||
|
let mut reliable = 0u16;
|
||||||
|
let mut sequence = 0u32;
|
||||||
|
let mut sealed = 0usize;
|
||||||
|
let mut messages = 0usize;
|
||||||
|
let mut ms = 2u64;
|
||||||
|
let mut rest = data;
|
||||||
|
while let Some((&op, tail)) = rest.split_first() {
|
||||||
|
if tail.len() < 2 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let len = usize::from(u16::from_be_bytes([tail[0], tail[1]])).min(tail.len() - 2);
|
||||||
|
let (chunk, t) = tail[2..].split_at(len);
|
||||||
|
rest = t;
|
||||||
|
ms += u64::from(op >> 3) * 13;
|
||||||
|
let now = Instant::from_micros(ms * 1000);
|
||||||
|
check_parse(chunk);
|
||||||
|
let wrap = |frame: Vec<u8>, reliable: &mut u16| {
|
||||||
|
*reliable = reliable.wrapping_add(1);
|
||||||
|
let c = Command { acknowledge: true, channel_id: 0, reliable_sequence: *reliable, body: CommandBody::SendReliable(frame) };
|
||||||
|
datagram(PEER_ID, (ms & 0xFFFF) as u16, vec![c])
|
||||||
|
};
|
||||||
|
let bytes = match op & 0x07 {
|
||||||
|
// An arbitrary plaintext, sealed with the next sequence number.
|
||||||
|
0 | 1 | 2 => cipher.seal(sequence, Direction::ClientToHost, chunk).ok().and_then(|f| {
|
||||||
|
sequence = sequence.wrapping_add(1);
|
||||||
|
sealed += 1;
|
||||||
|
wrap(f, &mut reliable)
|
||||||
|
}),
|
||||||
|
// An arbitrary plaintext sealed with a sequence number from the chunk (replays, gaps).
|
||||||
|
3 => {
|
||||||
|
let seq = chunk.get(..4).map_or(0, |b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
|
||||||
|
cipher.seal(seq, Direction::ClientToHost, chunk).ok().and_then(|f| {
|
||||||
|
sealed += 1;
|
||||||
|
wrap(f, &mut reliable)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// Arbitrary bytes as the frame inside a valid reliable command.
|
||||||
|
4 => wrap(chunk.to_vec(), &mut reliable),
|
||||||
|
// A host message.
|
||||||
|
5 => {
|
||||||
|
let _ = h.send_rumble(now, u16::from(op), 1, 2);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
// Raw datagram.
|
||||||
|
_ => Some(chunk.to_vec()),
|
||||||
|
};
|
||||||
|
if let Some(b) = bytes {
|
||||||
|
let _ = h.handle_datagram(now, &b);
|
||||||
|
}
|
||||||
|
let _ = h.handle_timeout(now);
|
||||||
|
while let Some(out) = h.poll_transmit(now) {
|
||||||
|
parse_datagram(&out).expect("the host's own datagrams parse");
|
||||||
|
}
|
||||||
|
while let Some(e) = h.poll_event() {
|
||||||
|
if let ControlHostEvent::Message(_) = e {
|
||||||
|
messages += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(messages <= sealed, "{messages} messages from {sealed} sealed frames");
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
//! Fuzzes the ENet host-side peer: `accept` on the first datagram (or a canned CONNECT, then the verify
|
||||||
|
//! acknowledged, so the connected state is reached), then a sequence of datagrams, some given our header so they
|
||||||
|
//! reach the command loop.
|
||||||
|
//! Asserts invariants, not only "no panic": delivered packets are within MAX_PACKET_BYTES; outgoing datagrams parse
|
||||||
|
//! and stay within the MTU; a disconnected peer has no deadline.
|
||||||
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
||||||
|
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use libfuzzer_sys::fuzz_target;
|
||||||
|
|
||||||
|
use core::time::Duration;
|
||||||
|
|
||||||
|
use domistream::Instant;
|
||||||
|
use domistream::enet::protocol::{Command, CommandBody, ConnectParams, Datagram, NO_PEER_ID, encode_datagram, parse_datagram};
|
||||||
|
use domistream::enet::{EnetHostConfig, EnetHostEvent, EnetHostPeer, EnetHostState, MAX_PACKET_BYTES};
|
||||||
|
|
||||||
|
const PEER_ID: u16 = 3;
|
||||||
|
|
||||||
|
fn config() -> EnetHostConfig {
|
||||||
|
EnetHostConfig {
|
||||||
|
peer_id: PEER_ID,
|
||||||
|
channel_limit: 4,
|
||||||
|
mtu: 1400,
|
||||||
|
ping_interval: Duration::from_millis(500),
|
||||||
|
timeout: Duration::from_secs(10),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn datagram(peer_id: u16, commands: Vec<Command>) -> Vec<u8> {
|
||||||
|
encode_datagram(&Datagram { peer_id, session_id: 0, sent_time: Some(0), commands }).expect("canned datagram")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn canned_connect() -> Vec<u8> {
|
||||||
|
let params = ConnectParams {
|
||||||
|
outgoing_peer_id: 0,
|
||||||
|
incoming_session_id: 0xFF,
|
||||||
|
outgoing_session_id: 0xFF,
|
||||||
|
mtu: 1400,
|
||||||
|
window_size: 65536,
|
||||||
|
channel_count: 2,
|
||||||
|
incoming_bandwidth: 0,
|
||||||
|
outgoing_bandwidth: 0,
|
||||||
|
packet_throttle_interval: 5000,
|
||||||
|
packet_throttle_acceleration: 2,
|
||||||
|
packet_throttle_deceleration: 2,
|
||||||
|
connect_id: 1,
|
||||||
|
};
|
||||||
|
let c = Command { acknowledge: true, channel_id: 0xFF, reliable_sequence: 1, body: CommandBody::Connect { params, data: 0 } };
|
||||||
|
datagram(NO_PEER_ID, vec![c])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drive(h: &mut EnetHostPeer, now: Instant) {
|
||||||
|
let _ = h.handle_timeout(now);
|
||||||
|
while let Some(out) = h.poll_transmit(now) {
|
||||||
|
assert!(out.len() <= h.mtu());
|
||||||
|
parse_datagram(&out).expect("the host's own datagrams parse");
|
||||||
|
}
|
||||||
|
while let Some(e) = h.poll_event() {
|
||||||
|
if let EnetHostEvent::Received { data, .. } = e {
|
||||||
|
assert!(data.len() <= MAX_PACKET_BYTES);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.state() == EnetHostState::Disconnected {
|
||||||
|
assert_eq!(h.poll_timeout(), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The next datagram: a flag byte, a 2-byte big-endian length, the bytes.
|
||||||
|
fn next(rest: &mut &[u8]) -> Option<(u8, Vec<u8>)> {
|
||||||
|
let (&flags, tail) = rest.split_first()?;
|
||||||
|
if tail.len() < 2 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let len = usize::from(u16::from_be_bytes([tail[0], tail[1]])).min(tail.len() - 2);
|
||||||
|
let (d, t) = tail[2..].split_at(len);
|
||||||
|
*rest = t;
|
||||||
|
Some((flags, d.to_vec()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fuzz_target!(|data: &[u8]| {
|
||||||
|
let Some((&mode, mut rest)) = data.split_first() else { return };
|
||||||
|
|
||||||
|
let mut h = if mode & 1 == 0 {
|
||||||
|
let Some((_, first)) = next(&mut rest) else { return };
|
||||||
|
match EnetHostPeer::accept(config(), Instant::ZERO, &first) {
|
||||||
|
Ok(h) => h,
|
||||||
|
Err(_) => return,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut h = EnetHostPeer::accept(config(), Instant::ZERO, &canned_connect()).expect("canned connect");
|
||||||
|
while h.poll_transmit(Instant::ZERO).is_some() {}
|
||||||
|
if mode & 2 != 0 {
|
||||||
|
let ack = Command {
|
||||||
|
acknowledge: false,
|
||||||
|
channel_id: 0xFF,
|
||||||
|
reliable_sequence: 1,
|
||||||
|
body: CommandBody::Acknowledge { received_reliable_sequence: 1, received_sent_time: 0 },
|
||||||
|
};
|
||||||
|
h.handle_datagram(Instant::from_micros(1000), &datagram(PEER_ID, vec![ack])).expect("canned ack");
|
||||||
|
assert_eq!(h.state(), EnetHostState::Connected);
|
||||||
|
}
|
||||||
|
h
|
||||||
|
};
|
||||||
|
let mut ms = 1u64;
|
||||||
|
while let Some((flags, d)) = next(&mut rest) {
|
||||||
|
ms += u64::from(flags >> 2) * 7;
|
||||||
|
let now = Instant::from_micros(ms * 1000);
|
||||||
|
let bytes = if flags & 1 != 0 {
|
||||||
|
let mut b = (0x8000 | PEER_ID).to_be_bytes().to_vec();
|
||||||
|
b.extend_from_slice(&((ms & 0xFFFF) as u16).to_be_bytes());
|
||||||
|
b.extend_from_slice(&d);
|
||||||
|
b
|
||||||
|
} else {
|
||||||
|
d
|
||||||
|
};
|
||||||
|
let _ = h.handle_datagram(now, &bytes);
|
||||||
|
if flags & 2 != 0 {
|
||||||
|
let _ = h.send(now, flags % 3, domistream::enet::Delivery::Reliable, &bytes);
|
||||||
|
}
|
||||||
|
drive(&mut h, now);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user