From 4c29159d9b8f603496a96213ea44ca6af0cf7f8d Mon Sep 17 00:00:00 2001 From: kernalix7 Date: Thu, 13 Aug 2026 13:36:06 +0900 Subject: [PATCH] fix(kernel): reject forward compression pointers in DNS name decoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audit of `dns.rs` ahead of wiring it into the UDP receive path. The parser was already hardened against the two classic DNS name-decompression hangs: both `decode_domain_name` and `skip_name` cap pointer indirection at `MAX_POINTERS` (16), all offset arithmetic is saturating, label length is bounded at 63, and `rdlength`/output copies are bounds-checked. Pointer cycles and self-pointers are therefore already terminated by the hop cap. What was missing is the RFC 1035 §4.1.4 prior-occurrence rule. A compression pointer names a name that appeared *earlier* in the message, so its target must be strictly before the pointer. `decode_domain_name` only checked `ptr < data.len()`, never `ptr < cur`, so a pointer could point forward at bytes that were never labels and reconstruct an attacker-controlled "name". RFC 9267 calls out exactly this omission, and it is the BIND/tcpdump CVE-2017-12995 fix. Enforce `ptr < cur`, comparing against the position of the pointer just read so chained hops must each move strictly backward. This makes cycles and self-pointers unreachable by construction instead of relying on the hop cap, and rejects forward pointers the cap never counted. `skip_name` returned `cur + 2` for a pointer without checking the second byte exists; a pointer at the final byte yielded an offset one past the message end. Guard it. Five regression tests. Two fail without the fix (`rejects_forward_pointer`, `rejects_truncated_pointer`); the self-pointer, cycle, and legal-backward-chain-over-the-cap cases already passed via the hop cap and are kept as guards so a future relaxation of the backward rule turns them red. `test_decode_domain_name_pointer` (offset 13 → 0, a legitimate backward pointer) still passes, confirming valid packets are unaffected. Suite: 4601 passed, 0 skipped. --- crates/kernel/src/dns.rs | 96 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/crates/kernel/src/dns.rs b/crates/kernel/src/dns.rs index f317f46..f55d761 100644 --- a/crates/kernel/src/dns.rs +++ b/crates/kernel/src/dns.rs @@ -344,6 +344,15 @@ pub fn decode_domain_name(data: &[u8], offset: usize, out: &mut [u8]) -> Result< if ptr >= data.len() { return Err(Error::InvalidArgument); } + // SECURITY (RFC 1035 §4.1.4, RFC 9267): a compression pointer names + // a PRIOR occurrence, so it must point strictly backward. `cur` is + // the position of the pointer just read, so on a chain each hop + // must target a strictly earlier offset. This rejects forward and + // self pointers outright and makes cycles unreachable, rather than + // relying on the hop cap to terminate them. + if ptr >= cur { + return Err(Error::InvalidArgument); + } cur = ptr; jumped = true; pointer_count = pointer_count.saturating_add(1); @@ -490,7 +499,12 @@ fn skip_name(data: &[u8], offset: usize) -> Result { } let b = data[cur]; if (b & 0xC0) == 0xC0 { - // Pointer: 2 bytes total, then we are done. + // Pointer: 2 bytes total, then we are done. The second byte must + // actually be present; a pointer at the final byte would otherwise + // return an offset one past the end of the message. + if cur.saturating_add(2) > data.len() { + return Err(Error::InvalidArgument); + } return Ok(cur.saturating_add(2)); } if b == 0 { @@ -959,6 +973,86 @@ mod tests { assert_eq!(&out[..name_len], b"example.com"); } + // SECURITY (RFC 1035 §4.1.4, RFC 9267): a compression pointer must name a + // PRIOR occurrence, i.e. point strictly backward. A forward pointer reads + // bytes that were never labels and reconstructs an attacker-controlled + // "name". Rejecting `ptr >= cur` is the BIND/tcpdump (CVE-2017-12995) rule. + #[test] + fn test_decode_domain_name_rejects_forward_pointer() { + // At offset 0 sits a pointer that points FORWARD to offset 6, where a + // label actually lives. Without a backward-only rule this resolves. + let mut data = [0u8; 16]; + data[0] = 0xC0; + data[1] = 0x06; // forward pointer to offset 6 + // A label "evil" at offset 6 (after a gap of attacker padding). + data[6] = 4; + data[7..11].copy_from_slice(b"evil"); + data[11] = 0; + + let mut out = [0u8; 256]; + let r = decode_domain_name(&data, 0, &mut out); + assert!( + r.is_err(), + "forward pointer must be rejected (RFC 1035 §4.1.4 prior-occurrence rule)" + ); + } + + // SECURITY: a self-referential pointer (ptr == its own position) is a cycle + // of length one. The hop cap bounds it, but it must be rejected outright. + #[test] + fn test_decode_domain_name_rejects_self_pointer() { + let mut data = [0u8; 8]; + data[2] = 0xC0; + data[3] = 0x02; // pointer at 2 -> targets 2 (itself) + let mut out = [0u8; 256]; + assert!(decode_domain_name(&data, 2, &mut out).is_err()); + } + + // SECURITY: a pointer cycle across two offsets must terminate with an + // error, never hang. ptr at 4 -> 6, ptr at 6 -> 4. + #[test] + fn test_decode_domain_name_rejects_pointer_cycle() { + let mut data = [0u8; 8]; + data[4] = 0xC0; + data[5] = 0x06; // 4 -> 6 + data[6] = 0xC0; + data[7] = 0x04; // 6 -> 4 + let mut out = [0u8; 256]; + assert!(decode_domain_name(&data, 4, &mut out).is_err()); + } + + // A strictly backward pointer chain longer than MAX_POINTERS must error, + // proving the hop bound holds even when every individual hop is legal. + #[test] + fn test_decode_domain_name_hop_cap_on_legal_backward_chain() { + // Lay out N positions, each a pointer to the previous, all backward. + // data[i*2] = 0xC0, data[i*2+1] = (i-1)*2. Starting at the last. + let mut data = [0u8; 256]; + let hops = 20; // > MAX_POINTERS (16) + for i in 1..=hops { + data[i * 2] = 0xC0; + data[i * 2 + 1] = ((i - 1) * 2) as u8; + } + data[0] = 0; // root terminator at the very start + let mut out = [0u8; 256]; + assert!( + decode_domain_name(&data, hops * 2, &mut out).is_err(), + "chain exceeding MAX_POINTERS must error even when all hops are backward" + ); + } + + // skip_name returns the offset after a compression pointer without checking + // the pointer's second byte is present, yielding an offset past the message. + #[test] + fn test_skip_name_rejects_truncated_pointer() { + // A single pointer byte at the very end: no second byte follows. + let data = [0xC0u8]; + assert!( + skip_name(&data, 0).is_err(), + "pointer at the final byte has no second byte; must not return offset 2" + ); + } + #[test] fn test_build_query() { let mut buf = [0u8; 512];