Skip to content

feat(kernel): validate DNS messages arriving on the DNS port - #184

Merged
kernalix7 merged 1 commit into
mainfrom
feat/wire-dns-validation
Aug 13, 2026
Merged

kernalix7 merged 1 commit into
mainfrom
feat/wire-dns-validation

Conversation

@kernalix7

Copy link
Copy Markdown
Owner

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 — new 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 declared record (pos == data.len()).

It deliberately differs from parse_response, which is unsuitable as an
admission gate: parse_response rejects valid non-zero-RCODE responses
(NXDOMAIN/SERVFAIL), caps answers at MAX_ANSWERS, and never looks at the
authority or additional sections. validate_message accepts both queries and
responses, 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, not skip_name, so the forward- and
self-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 in handle_udp_packet. The handler now keeps the
parsed UDP header and, when source or destination port is DNS_PORT, 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.

udp.rs — UDP_HEADER_LEN becomes pub(crate) so the DNS payload offset
uses the named constant rather than a magic 8.

Tests

validate_message:

test_validate_message_accepts_built_query                  ok
test_validate_message_rejects_forward_pointer_in_question  ok
test_validate_message_rejects_rdlength_overrun             ok
test_validate_message_rejects_trailing_bytes               ok

Net-layer integration (both return Ok(0) but with opposite meaning, which the
tests assert and document):

dns_valid_query_on_port53_accepted                  ok  (valid DNS admitted)
dns_forward_pointer_on_port53_dropped_not_error     ok  (malformed dropped, not Err)

Verification

  • cargo fmt --all -- --check · cargo clippy --workspace -- -D warnings · cargo build --workspace
  • cargo build -p oncrix-kernel --bin oncrix-kernel --target aarch64-unknown-none / riscv64gc-unknown-none-elf
  • bash scripts/run-tests.sh — 4607 passed, 0 skipped (kernel 367 → 373)
  • bash scripts/run-qemu-test.sh — boots clean on x86_64, aarch64, riscv64

Scope

Framing validation only. A resolver path (transaction-ID correlation, query TX,
cache, tick pump) is intentionally separate and is not introduced here.

Note

oncrix-syscall intermittently fails inside the sequential run-tests.sh
runner (~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.

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.
@kernalix7
kernalix7 merged commit 78d2725 into main Aug 13, 2026
3 checks passed
@kernalix7
kernalix7 deleted the feat/wire-dns-validation branch August 13, 2026 05:08
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