From d1ffecb132c762c72cdadd308c23600aef43553b Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sun, 10 May 2015 12:19:58 -0700 Subject: [PATCH] Soften DNS parsing rules. Fixes #74. --- src/dns.cpp | 2 +- tests/src/dns.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/dns.cpp b/src/dns.cpp index ed81e9e..3c830da 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -328,7 +328,7 @@ const uint8_t* DNS::compose_name(const uint8_t *ptr, char *out_ptr) const { std::memcpy(&index, ptr, sizeof(uint16_t)); index = Endian::be_to_host(index) & 0x3fff; // Check that the offset is neither too low or too high - if(index < 0x0c || (&records_data[0] + (index - 0x0c)) >= ptr) + if(index < 0x0c || (&records_data[0] + (index - 0x0c)) >= end) throw malformed_packet(); // We've probably found the end of the original domain name. Save it. if(end_ptr == 0) diff --git a/tests/src/dns.cpp b/tests/src/dns.cpp index 4c72a3c..ade5f5d 100644 --- a/tests/src/dns.cpp +++ b/tests/src/dns.cpp @@ -12,7 +12,8 @@ using namespace Tins; class DNSTest : public testing::Test { public: - static const uint8_t expected_packet[], dns_response1[]; + static const uint8_t expected_packet[], dns_response1[], + dns_packet1[]; void test_equals(const DNS &dns1, const DNS &dns2); void test_equals(const DNS::Query &q1, const DNS::Query &q2); @@ -27,7 +28,19 @@ const uint8_t DNSTest::expected_packet[] = { }; const uint8_t DNSTest::dns_response1[] = { -174, 73, 129, 128, 0, 1, 0, 5, 0, 0, 0, 0, 6, 103, 111, 111, 103, 108, 101, 3, 99, 111, 109, 0, 0, 15, 0, 1, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 17, 0, 50, 4, 97, 108, 116, 52, 5, 97, 115, 112, 109, 120, 1, 108, 192, 12, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 40, 4, 97, 108, 116, 51, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 20, 4, 97, 108, 116, 49, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 4, 0, 10, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 30, 4, 97, 108, 116, 50, 192, 47 + 174, 73, 129, 128, 0, 1, 0, 5, 0, 0, 0, 0, 6, 103, 111, 111, 103, 108, + 101, 3, 99, 111, 109, 0, 0, 15, 0, 1, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, + 0, 17, 0, 50, 4, 97, 108, 116, 52, 5, 97, 115, 112, 109, 120, 1, 108, + 192, 12, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 40, 4, 97, 108, + 116, 51, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 20, 4, + 97, 108, 116, 49, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 4, + 0, 10, 192, 47, 192, 12, 0, 15, 0, 1, 0, 0, 2, 88, 0, 9, 0, 30, 4, 97, + 108, 116, 50, 192, 47 +}; + +const uint8_t DNSTest::dns_packet1[] = { + 2, 225, 1, 32, 0, 1, 0, 0, 0, 0, 0, 0, 7, 118, 101, 114, 115, 105, + 111, 110, 4, 98, 105, 110, 100, 192, 27, 0, 16, 0, 3 }; @@ -129,6 +142,14 @@ TEST_F(DNSTest, ConstructorFromBuffer2) { } } +TEST_F(DNSTest, ConstructorFromBuffer3) { + DNS dns(dns_packet1, sizeof(dns_packet1)); + EXPECT_EQ(dns.questions_count(), 1); + DNS::queries_type queries = dns.queries(); + ASSERT_EQ(1, queries.size()); + EXPECT_EQ("version.bind", queries.front().dname()); +} + TEST_F(DNSTest, NoRecords) { DNS dns; EXPECT_TRUE(dns.queries().empty());