Files
2026-11-22 14:21:54 +01:00

74 lines
3.0 KiB
Rust

//! Malformed / hostile input must never panic or hang the server (stable-Rust fuzzing).
#![allow(clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::*;
use dns_core::client::{QueryOptions, build_query};
use dns_core::proto::rr::RecordType;
/// xorshift PRNG (deterministic, no extra deps)
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
}
#[tokio::test(flavor = "multi_thread")]
async fn random_and_mutated_packets_do_not_panic() {
let dir = tmpdir("fuzz");
write(
&dir,
"z.zone",
"$TTL 60\n@ IN SOA ns hm 1 3600 600 86400 60\n@ IN NS ns\nns IN A 127.0.0.1\nwww IN A 192.0.2.1\n*.w IN TXT \"x\"\n",
);
let yaml = "server: { listen: [\"127.0.0.1:0\"], data_dir: data }\napi: { enabled: false }\nrate_limit: { enabled: false }\nzones: [ { name: example.test, type: primary, file: z.zone, transfer_acl: [\"127.0.0.1/32\"] } ]\n";
let h = start(&dir, yaml).await;
let shared = &h.shared;
let peer: std::net::SocketAddr = "127.0.0.1:5555".parse().unwrap();
let mut rng = Rng(0x9E37_79B9_7F4A_7C15);
let seeds: Vec<Vec<u8>> = ["www.example.test.", "x.w.example.test.", "example.test."]
.iter()
.flat_map(|n| {
[RecordType::A, RecordType::AXFR, RecordType::IXFR, RecordType::ANY]
.map(|t| build_query(&n.parse().unwrap(), t, &QueryOptions::default()).to_vec().unwrap())
})
.collect();
for i in 0..30_000u32 {
let pkt: Vec<u8> = if i % 3 == 0 {
let len = (rng.next() % 600) as usize;
(0..len).map(|_| rng.next() as u8).collect()
} else {
let mut p = seeds[(rng.next() as usize) % seeds.len()].clone();
for _ in 0..(1 + rng.next() % 6) {
let idx = (rng.next() as usize) % p.len();
p[idx] = rng.next() as u8;
}
if rng.next().is_multiple_of(5) {
p.truncate((rng.next() as usize) % p.len());
}
p
};
let _ = dns_core::proto::op::Message::from_vec(&pkt);
for t in [dns_server::handler::Transport::Udp, dns_server::handler::Transport::Tcp] {
let _ = tokio::time::timeout(std::time::Duration::from_secs(2), dns_server::handler::handle(shared, &pkt, peer, t))
.await
.expect("handler hung");
}
}
// zone file parser and config parser with garbage
for _ in 0..2000 {
let len = (rng.next() % 300) as usize;
let s: String = (0..len).map(|_| (32 + (rng.next() % 95) as u8) as char).collect();
let _ = dns_core::zonefile::parse_str(&s, &"example.test.".parse().unwrap(), None);
let _ = dns_config::Config::from_str_any(&s, "fuzz");
}
// server still answers
let m = q(h.bound.dns[0], "www.example.test.", RecordType::A).await;
assert_eq!(answers(&m), vec!["A 192.0.2.1"]);
h.shutdown().await;
}