File tree Expand file tree Collapse file tree 2 files changed +18
-8
lines changed Expand file tree Collapse file tree 2 files changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -86,14 +86,18 @@ void DNS::skip_to_dname_end(InputMemoryStream& stream) const {
86
86
break ;
87
87
}
88
88
else {
89
- if ((value & 0xc0 )) {
90
- // This is an offset label, skip the second byte and we're done
89
+ const uint8_t offset_discriminator = value & 0xc0 ;
90
+ if (offset_discriminator == 0xc0 ) {
91
+ // This is an offset pointer, skip the second byte and we're done
91
92
stream.skip (1 );
92
93
break ;
93
94
}
94
- else {
95
+ else if (offset_discriminator == 0 ) {
95
96
// This is an actual label, skip its contents
96
97
stream.skip (value);
98
+ } else {
99
+ // high order two bits of the first octet of a label must be either 11 or 00
100
+ throw malformed_packet ();
97
101
}
98
102
}
99
103
}
Original file line number Diff line number Diff line change @@ -571,6 +571,7 @@ TEST_F(DNSTest, BadLabelSize) {
571
571
572
572
// add bad length
573
573
const size_t bad_label_len{0x80 };
574
+ const size_t label_offset = payload_sz;
574
575
payload[payload_sz++] = bad_label_len;
575
576
576
577
// fill label for incorrect length and terminate
@@ -590,13 +591,18 @@ TEST_F(DNSTest, BadLabelSize) {
590
591
payload + payload_sz);
591
592
payload_sz += sizeof (type_class);
592
593
593
- // SUCCEED moves from dns_decompression_pointer_out_of_bounds to malformed_packet after fix
594
- const DNS packet (payload, payload_sz);
595
- EXPECT_EQ (packet.questions_count (), 1 );
594
+ // invalid high two bits of label first octest is detected early now
596
595
try {
597
- const auto queries{ packet. queries ()} ;
596
+ const DNS packet (payload, payload_sz) ;
598
597
FAIL ();
599
- } catch (dns_decompression_pointer_out_of_bounds& oob) {
598
+ } catch (malformed_packet& mp) {
599
+ SUCCEED ();
600
+ }
601
+
602
+ // check the other invalid value of high two bits in label size
603
+ payload[label_offset] = 0x10 ;
604
+ try {
605
+ const DNS packet (payload, payload_sz);
600
606
FAIL ();
601
607
} catch (malformed_packet& mp) {
602
608
SUCCEED ();
You can’t perform that action at this time.
0 commit comments