From 84b1f339aab2d9928e30440164a98a56f0b58709 Mon Sep 17 00:00:00 2001 From: Will Stott Date: Sun, 21 Jul 2024 10:56:17 +0100 Subject: [PATCH] Add test to reproduce #56 --- src/dns_parser/error.rs | 2 ++ src/dns_parser/name.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/dns_parser/error.rs b/src/dns_parser/error.rs index be73606..e6ef322 100644 --- a/src/dns_parser/error.rs +++ b/src/dns_parser/error.rs @@ -4,6 +4,8 @@ use thiserror::Error; #[derive(Debug, Error)] #[allow(dead_code)] pub enum Error { + #[error("invalid compression pointer")] + BadPointer, #[error("packet is smaller than header size")] HeaderTooShort, #[error("packet is has incomplete data")] diff --git a/src/dns_parser/name.rs b/src/dns_parser/name.rs index fd5f6bc..9ad8674 100644 --- a/src/dns_parser/name.rs +++ b/src/dns_parser/name.rs @@ -186,3 +186,31 @@ impl<'a> PartialEq for Name<'a> { } impl<'a> Eq for Name<'a> {} + +#[cfg(test)] +mod test { + use super::Error; + use super::Name; + + #[test] + fn parse_badpointer_same_offset() { + // A buffer where an offset points to itself, + // which is a bad compression pointer. + let same_offset = vec![192, 2, 192, 2]; + let is_match = matches!(Name::scan(&same_offset, &same_offset), + Err(Error::BadPointer)); + + assert!(is_match); + } + + #[test] + fn parse_badpointer_forward_offset() { + // A buffer where the offsets points back to each other which causes + // infinite recursion if never checked, a bad compression pointer. + let forwards_offset = vec![192, 2, 192, 4, 192, 2]; + let is_match = matches!(Name::scan(&forwards_offset, &forwards_offset), + Err(Error::BadPointer)); + + assert!(is_match); + } +} \ No newline at end of file