Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate high order two bits of first dns label octet #494

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,23 @@ void DNS::skip_to_dname_end(InputMemoryStream& stream) const {
break;
}
else {
if ((value & 0xc0)) {
const uint8_t offset_discriminator = value & 0xc0;
if (offset_discriminator == 0xc0) {
// This is an offset label, skip the second byte and we're done
stream.skip(1);
break;
}
else {
else if (offset_discriminator == 0) {
// This is an offset label, skip the second byte and we're done
stream.skip(1);
break;
}
else if ((value & 0x3f) == value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh I think the diff was bad, this is duplicated now. Sorry!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries; all looks ok now I think...

// This is an actual label, skip its contents
stream.skip(value);
} else {
// high order two bits of the first octet of a label must be either 11 or 00
throw malformed_packet();
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions tests/src/dns_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ TEST_F(DNSTest, BadLabelSize) {

// add bad length
const size_t bad_label_len{0x80};
const size_t label_offset = payload_sz;
payload[payload_sz++] = bad_label_len;

// fill label for incorrect length and terminate
Expand All @@ -590,13 +591,18 @@ TEST_F(DNSTest, BadLabelSize) {
payload + payload_sz);
payload_sz += sizeof(type_class);

// SUCCEED moves from dns_decompression_pointer_out_of_bounds to malformed_packet after fix
const DNS packet(payload, payload_sz);
EXPECT_EQ(packet.questions_count(), 1);
// invalid high two bits of label first octest is detected early now
try {
const auto queries{packet.queries()};
const DNS packet(payload, payload_sz);
FAIL();
} catch (dns_decompression_pointer_out_of_bounds& oob) {
} catch (malformed_packet& mp) {
SUCCEED();
}

// check the other invalid value of high two bits in label size
payload[label_offset] = 0x10;
try {
const DNS packet(payload, payload_sz);
FAIL();
} catch (malformed_packet& mp) {
SUCCEED();
Expand Down