Skip to content

Commit 6af668f

Browse files
dns: parser reads into garbage on misreported packet size
1 parent c302e65 commit 6af668f

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/src/dns_test.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,76 @@ TEST_F(DNSTest, BadLabelSize) {
602602
SUCCEED();
603603
}
604604
}
605+
606+
TEST_F(DNSTest, BadPacketLength) {
607+
608+
// valid response packet with RR's in all sections
609+
const uint8_t payload[] = {
610+
0x74,0xa9,0x85,0x80,0x00,0x01,0x00,0x02,0x00,0x01,0x00,0x04,0x08,0x5f,0x73,0x65,0x72,
611+
0x76,0x69,0x63,0x65,0x04,0x5f,0x74,0x63,0x70,0x05,0x77,0x69,0x66,0x69,0x36,0x03,
612+
0x6c,0x61,0x6e,0x00,0x00,0x21,0x00,0x01,0xc0,0x0c,0x00,0x21,0x00,0x01,0x00,0x01,
613+
0x51,0x80,0x00,0x16,0x00,0x00,0x00,0x03,0x00,0x09,0x04,0x66,0x61,0x73,0x74,0x05,
614+
0x77,0x69,0x66,0x69,0x36,0x03,0x6c,0x61,0x6e,0x00,0xc0,0x0c,0x00,0x21,0x00,0x01,
615+
0x00,0x01,0x51,0x80,0x00,0x16,0x00,0x00,0x00,0x01,0x00,0x09,0x04,0x73,0x6c,0x6f,
616+
0x77,0x05,0x77,0x69,0x66,0x69,0x36,0x03,0x6c,0x61,0x6e,0x00,0xc0,0x62,0x00,0x02,
617+
0x00,0x01,0x00,0x01,0x51,0x80,0x00,0x05,0x02,0x70,0x69,0xc0,0x62,0xc0,0x5d,0x00,
618+
0x01,0x00,0x01,0x00,0x01,0x51,0x80,0x00,0x04,0x0a,0x18,0x00,0x02,0xc0,0x3b,0x00,
619+
0x01,0x00,0x01,0x00,0x01,0x51,0x80,0x00,0x04,0x0a,0x18,0x00,0x02,0xc0,0x79,0x00,
620+
0x01,0x00,0x01,0x00,0x01,0x51,0x80,0x00,0x04,0x0a,0x18,0x00,0x02,0x00,0x00,0x29,
621+
0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x00,0x0a,0x00,0x18,0x86,0x1f,0x14,0x0f,
622+
0x41,0xfa,0xf3,0x95,0x48,0x6e,0x79,0x61,0x61,0x78,0x32,0x0f,0x44,0x5d,0x21,0x47,
623+
0x85,0x83,0x9a,0x95
624+
};
625+
626+
// packet verifier for all but additional RR's
627+
auto verify = [](const DNS& packet) {
628+
EXPECT_EQ(packet.questions_count(), 1);
629+
EXPECT_EQ(packet.answers_count(), 2);
630+
EXPECT_EQ(packet.authority_count(), 1);
631+
EXPECT_EQ(packet.additional_count(), 4);
632+
EXPECT_EQ(packet.queries().size(), 1U);
633+
EXPECT_EQ(packet.answers().size(), 2U);
634+
EXPECT_EQ(packet.authority().size(), 1U);
635+
};
636+
637+
// case 1; valid packet, correct size, everything ok
638+
{
639+
// packet parses successfully and expected RR's can be fetched including additional
640+
const DNS packet(payload, sizeof(payload));
641+
642+
// most of the RR's parse ok
643+
verify(packet);
644+
645+
// additional ok too
646+
EXPECT_EQ(packet.additional().size(), 4U);
647+
}
648+
649+
// case 2; valid DNS message but misreported packet size; parser heads into uncharted waters
650+
{
651+
// buffer with space for valid packet plus garbage bytes
652+
constexpr size_t bigsz{512};
653+
uint8_t big_packet[bigsz];
654+
655+
// copy valid packet
656+
std::copy(payload,
657+
payload + sizeof(payload),
658+
big_packet);
659+
660+
// fill additional bytes with junk
661+
std::fill(big_packet + sizeof(payload),
662+
big_packet + bigsz,
663+
0x5A);
664+
665+
// initial packet parse ok
666+
const DNS packet(big_packet, bigsz);
667+
668+
// most of the RR's parse ok
669+
verify(packet);
670+
671+
// but the additional section continues to read into the garbage and throws
672+
// despite sufficient information to stop
673+
EXPECT_THROW(packet.additional().size(), malformed_packet);
674+
}
675+
}
676+
677+

0 commit comments

Comments
 (0)