166 lines
6.1 KiB
Rust
166 lines
6.1 KiB
Rust
//! 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");
|
|
}
|
|
});
|