From 0623ada24df1da99c72bb8cd4959b2cb0e64ccc2 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Tue, 1 Feb 2022 15:44:43 -0600 Subject: [PATCH] dns: better error handling when parsing names The DNS name parser will error out with an error even if the error is incomplete. Instead of manually generating errors, use '?' to let the nom error ripple up the error handling chain. The reason this wasn't done in the first place is this code predates the ? operator, or we were not aware of it at the time. This prevents the case where probing fails when there is enough data to parse the header, but not enough to complete name parser. In such a case a parse error is returned (instead of incomplete) resulting in the payload not being detected as DNS. Ticket #5034 --- rust/src/dns/parser.rs | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/rust/src/dns/parser.rs b/rust/src/dns/parser.rs index 2b1a30582b..6dd6466c0d 100644 --- a/rust/src/dns/parser.rs +++ b/rust/src/dns/parser.rs @@ -67,33 +67,21 @@ pub fn dns_parse_name<'a, 'b>(start: &'b [u8], message: &'b [u8]) -> IResult<&'b pos = &pos[1..]; break; } else if len & 0b1100_0000 == 0 { - match length_data(be_u8)(pos) as IResult<&[u8], _> { - Ok((rem, label)) => { - if name.len() > 0 { - name.push('.' as u8); - } - name.extend(label); - pos = rem; - } - _ => { - return Err(Err::Error(error_position!(pos, ErrorKind::OctDigit))); - } + let (rem, label) = length_data(be_u8)(pos)?; + if name.len() > 0 { + name.push('.' as u8); } + name.extend(label); + pos = rem; } else if len & 0b1100_0000 == 0b1100_0000 { - match be_u16(pos) as IResult<&[u8], _> { - Ok((rem, leader)) => { - let offset = usize::from(leader) & 0x3fff; - if offset > message.len() { - return Err(Err::Error(error_position!(pos, ErrorKind::OctDigit))); - } - pos = &message[offset..]; - if pivot == start { - pivot = rem; - } - } - _ => { - return Err(Err::Error(error_position!(pos, ErrorKind::OctDigit))); - } + let (rem, leader) = be_u16(pos)?; + let offset = usize::from(leader) & 0x3fff; + if offset > message.len() { + return Err(Err::Error(error_position!(pos, ErrorKind::OctDigit))); + } + pos = &message[offset..]; + if pivot == start { + pivot = rem; } } else { return Err(Err::Error(error_position!(pos, ErrorKind::OctDigit))); -- 2.47.3