Skip to content

fix: out-of-bounds panic parsing a truncated HINFO record - #499

Merged
keepsimple1 merged 2 commits into
mainfrom
fix-hinfo-oob
Sep 4, 2026
Merged

keepsimple1 merged 2 commits into
mainfrom
fix-hinfo-oob

Conversation

@keepsimple1

Copy link
Copy Markdown
Owner

Problem

read_char_string read its length octet as self.data[self.offset] with no bounds check. An HINFO record whose RDATA ends exactly at the end of the message leaves no length octet to read, so the index runs one byte past the packet and panics.

Fix

read_char_string now checks the bound and returns an error if the length byte is missing.

keepsimple1 and others added 2 commits September 3, 2026 21:38
read_char_string read its length octet as self.data[self.offset] with no
bounds check, unlike its siblings read_string, read_vec, read_ipv4 and
read_ipv6, which all check first.

An HINFO record whose RDATA ends exactly at the end of the message -- for
instance one with RDLENGTH 0 as the last record -- leaves nothing to read
there, so the index runs one byte past the packet and panics. ServiceDaemon
passes socket bytes straight to DnsIncoming::new and handles only Err, not
a panic, so 23 bytes from any host on the link would take down the daemon
thread.

It now returns an error instead. read_rr_records already treats a record
error as "skip this record and re-anchor on RDLENGTH", so the malformed
HINFO is dropped and the rest of the packet is still parsed.

Found by fuzzing the packet parser.

Co-Authored-By: Claude Opus 5 <[email protected]>
@keepsimple1
keepsimple1 merged commit c65dd3c into main Sep 4, 2026
3 checks passed
@keepsimple1
keepsimple1 deleted the fix-hinfo-oob branch September 4, 2026 05:16
keepsimple1 added a commit that referenced this pull request Sep 5, 2026
Adds cargo-fuzz scaffolding under fuzz/ and a first target, parse_packet,
covering DnsIncoming::new. ServiceDaemon hands that function the bytes it
reads off a UDP socket without inspecting them first, so every byte it
touches is reachable by any host on the link.

dns_parser is a private module, so a fuzz target -- a separate crate --
cannot call it. The new unstable-fuzz-api feature exposes src/fuzz_api.rs,
a #[doc(hidden)] module of thin wrappers, rather than widening the
visibility of dns_parser itself. It is not public API and carries no
stability guarantee.

The target checks invariants, not just the absence of panics: a parsed
message must agree with its own header counts, and must be exactly one of
a query or a response. It also renders the error of a rejected packet and
the Debug of an accepted one, since both format slices of the raw packet.

Running it needs no arguments:

    cargo +nightly fuzz run parse_packet

cargo-fuzz supplies the corpus directory and artifact prefix itself. A
dictionary and an explicit -max_len were both tried and dropped: over
30-second cold starts neither moved coverage outside noise. DnsIncoming::new
has no size-dependent branch -- MAX_PKT_ABSOLUTE_IPV4 bounds the encoder,
not the parser -- so libFuzzer's default 4096-byte cap costs nothing here.
A later target that exercises to_packets would want -max_len=8972.

CI gains a fuzz job that builds the targets on nightly without running a
campaign. clippy --all-features already covers src/fuzz_api.rs, but nothing
else built fuzz/Cargo.toml or the targets, so they could break unnoticed.

The first run of this target found a remotely-triggerable panic, an HINFO
record with RDLENGTH 0 at the end of a message, fixed in #499 and covered
there by a regression test that runs on stable.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant