Skip to content

Commit 72f2073

Browse files
bors[bot]Alexandra Sandulescu
andauthored
Merge #562
562: fuzz: DHCP header parser r=Dirbaio a=alexandrasandulescu Hi. This fuzz target tests the [DHCPRepr](https://docs.rs/smoltcp/0.7.5/smoltcp/wire/struct.DhcpRepr.html) `parse` and `emit` functions ([context](#305 (comment))). The fuzz target requires a small change in the `DHCPOption` parser because the option type `OPT_DOMAIN_NAME_SERVER ` requires the payload representing the DNS server list to have a length multiple of 4. Otherwise the `chunks` call places the remainder in the last chunk (length < 4). This panics when the last chunk is parsed into an `IPv4Address`. Do you have a better suggestion for handling this situation? I ran the fuzzer for a couple of days but it didn't find any crash yet. To increase the chances, I used the oss-fuzz seed corpora of `dnsmasq` DHCP fuzzer and `systemd` DHCP server fuzzer. Please share if you have suggestions on how to improve coverage. Co-authored-by: Alexandra Sandulescu <aesa@google.com>
2 parents c90d54a + 858968b commit 72f2073

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

fuzz/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ name = "tcp_headers"
2828
path = "fuzz_targets/tcp_headers.rs"
2929
test = false
3030
doc = false
31+
32+
[[bin]]
33+
name = "dhcp_header"
34+
path = "fuzz_targets/dhcp_header.rs"
35+
test = false
36+
doc = false

fuzz/fuzz_targets/dhcp_header.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
use smoltcp::wire::{DhcpPacket, DhcpRepr};
4+
5+
fuzz_target!(|data: &[u8]| {
6+
let _ = match DhcpPacket::new_checked(data) {
7+
Ok(ref packet) => match DhcpRepr::parse(packet) {
8+
Ok(dhcp_repr) => {
9+
let mut dhcp_payload = vec![0; dhcp_repr.buffer_len()];
10+
match DhcpPacket::new_checked(&mut dhcp_payload[..]) {
11+
Ok(mut dhcp_packet) => Some(dhcp_repr.emit(&mut dhcp_packet)),
12+
Err(_) => None,
13+
}
14+
}
15+
Err(_) => None,
16+
},
17+
Err(_) => None,
18+
};
19+
});

src/wire/dhcpv4.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,11 @@ impl<'a> Repr<'a> {
824824
data,
825825
} => {
826826
let mut servers = [None; MAX_DNS_SERVER_COUNT];
827-
for (server, chunk) in servers.iter_mut().zip(data.chunks(4)) {
827+
let chunk_size = 4;
828+
for (server, chunk) in servers.iter_mut().zip(data.chunks(chunk_size)) {
829+
if chunk.len() != chunk_size {
830+
return Err(Error::Malformed);
831+
}
828832
*server = Some(Ipv4Address::from_bytes(chunk));
829833
}
830834
dns_servers = Some(servers);

0 commit comments

Comments
 (0)