Repository navigation
feat(kernel): validate DNS messages arriving on the DNS port - #184
Merged
Merged
Conversation
Wire the now-hardened DNS parser into the UDP receive path as a structural admission check, mirroring how `handle_udp_packet` already delegates to `crate::udp` and re-checks bounds at the call site. `dns.rs` gains `validate_message`, a framing-only validator that walks the header and every declared section (questions, then answer/authority/additional records) using the pointer-hardened `decode_domain_name`, bounds-checks the fixed fields and each `RDLENGTH`, treats RDATA as opaque, and requires the message to end exactly at the last record. Unlike `parse_response` it accepts both queries and responses, imposes no answer cap, and covers the authority/additional sections, so it is a sound gate regardless of the QR bit or response code. It deliberately reuses `decode_domain_name`, not `skip_name`, so the forward/self-pointer rejection applies to every name. `handle_udp_packet` now keeps the parsed UDP header and, for datagrams whose source or destination port is 53, validates the payload after the checksum is verified. Malformed DNS is dropped silently via the existing `Ok(0)` convention rather than surfaced as an error; no resolver state, cache, or query transmission is introduced — that plumbing is a separate concern. Tests: `validate_message` accepts a `build_query` output and rejects a forward-pointer question, an RDLENGTH overrun, and trailing bytes; at the net layer a valid DNS query on port 53 is consumed (`Ok(0)`) and a valid-checksum-but-malformed datagram is dropped rather than erroring. Suite: 4607 passed, 0 skipped. QEMU boot passes on all three architectures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #183, which hardened the DNS parser. This wires it into the UDP
receive path as a structural admission check, per the
crates/kernel/AGENTS.md"wire one parser per PR, mirror
handle_udp_packet" guidance.What changes
dns.rs— newvalidate_message. A framing-only validator that walks theheader and every declared section (questions, then answer/authority/additional
records) using the pointer-hardened
decode_domain_name, bounds-checks thefixed fields and each
RDLENGTH, treats RDATA as opaque, and requires themessage to end exactly at the last declared record (
pos == data.len()).It deliberately differs from
parse_response, which is unsuitable as anadmission gate:
parse_responserejects valid non-zero-RCODE responses(NXDOMAIN/SERVFAIL), caps answers at
MAX_ANSWERS, and never looks at theauthority or additional sections.
validate_messageaccepts both queries andresponses, imposes no answer cap, and covers all four sections — a sound gate
for any DNS datagram regardless of the QR bit.
It reuses
decode_domain_name, notskip_name, so the forward- andself-pointer rejection added in #183 applies to every name in every section.
RDATA is opaque because record-type-specific names (CNAME/MX/SOA) are semantic
parsing, out of scope for framing.
net.rs— port-53 hook inhandle_udp_packet. The handler now keeps theparsed UDP header and, when source or destination port is
DNS_PORT, validatesthe payload after the checksum is verified. Malformed DNS is dropped silently
via the existing
Ok(0)convention rather than surfaced as an error. Noresolver state, cache, or query transmission is introduced.
udp.rs—UDP_HEADER_LENbecomespub(crate)so the DNS payload offsetuses the named constant rather than a magic
8.Tests
validate_message:Net-layer integration (both return
Ok(0)but with opposite meaning, which thetests assert and document):
Verification
cargo fmt --all -- --check·cargo clippy --workspace -- -D warnings·cargo build --workspacecargo build -p oncrix-kernel --bin oncrix-kernel --target aarch64-unknown-none/riscv64gc-unknown-none-elfbash scripts/run-tests.sh— 4607 passed, 0 skipped (kernel 367 → 373)bash scripts/run-qemu-test.sh— boots clean on x86_64, aarch64, riscv64Scope
Framing validation only. A resolver path (transaction-ID correlation, query TX,
cache, tick pump) is intentionally separate and is not introduced here.
Note
oncrix-syscallintermittently fails inside the sequentialrun-tests.shrunner (~1 in 3 full runs) but passes standalone (3605/3605) and on re-run. It
is a separate crate untouched by this change; flagging as a runner flake to
investigate independently, not a regression here.