diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/BUILD.bazel b/src/stirling/source_connectors/socket_tracer/protocols/dns/BUILD.bazel index ace3fda2260..ff9126bce2e 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/BUILD.bazel +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/BUILD.bazel @@ -39,6 +39,12 @@ pl_cc_test( deps = [":cc_library"], ) +pl_cc_test( + name = "dns_message_test", + srcs = ["dns_message_test.cc"], + deps = [":cc_library"], +) + pl_cc_test( name = "parse_test", srcs = ["parse_test.cc"], diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.cc new file mode 100644 index 00000000000..9c67e32c994 --- /dev/null +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.cc @@ -0,0 +1,211 @@ +/* + * Copyright 2018- The Pixie Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h" + +#include + +namespace px { +namespace stirling { +namespace protocols { +namespace dns { + +namespace { + +constexpr size_t kHeaderSize = 12; +constexpr size_t kQuestionFixedSize = 4; +constexpr size_t kRecordFixedSize = 10; +constexpr size_t kMaxNameLength = 255; +constexpr int kMaxPointerHops = 64; + +uint16_t ReadU16(std::string_view buf, size_t pos) { + return static_cast((static_cast(buf[pos]) << 8) | + static_cast(buf[pos + 1])); +} + +bool ReadName(std::string_view buf, size_t pos, size_t* in_place, std::string* out) { + size_t p = pos; + bool jumped = false; + int hops = 0; + size_t decoded_len = 0; + if (out != nullptr) { + out->clear(); + } + while (true) { + if (p >= buf.size()) { + return false; + } + const uint8_t c = static_cast(buf[p]); + if ((c & 0xC0) == 0xC0) { + if (p + 1 >= buf.size()) { + return false; + } + if (!jumped) { + *in_place = p + 2 - pos; + jumped = true; + } + if (++hops > kMaxPointerHops) { + return false; + } + const size_t target = (static_cast(c & 0x3F) << 8) | static_cast(buf[p + 1]); + if (target >= buf.size()) { + return false; + } + p = target; + continue; + } + if ((c & 0xC0) != 0) { + return false; + } + if (c == 0) { + if (!jumped) { + *in_place = p + 1 - pos; + } + return true; + } + if (p + 1 + c > buf.size()) { + return false; + } + decoded_len += c + 1; + if (decoded_len > kMaxNameLength) { + return false; + } + if (out != nullptr) { + if (!out->empty()) { + out->push_back('.'); + } + out->append(buf.substr(p + 1, c)); + } + p += 1 + c; + } +} + +} // namespace + +bool WalkDNSMessage(std::string_view buf, DNSMessageInfo* info) { + *info = DNSMessageInfo{}; + if (buf.size() < kHeaderSize) { + return false; + } + info->txid = ReadU16(buf, 0); + info->flags = ReadU16(buf, 2); + const uint16_t num_questions = ReadU16(buf, 4); + const uint32_t num_records = + static_cast(ReadU16(buf, 6)) + ReadU16(buf, 8) + ReadU16(buf, 10); + const uint16_t num_answers = ReadU16(buf, 6); + + size_t pos = kHeaderSize; + for (uint16_t i = 0; i < num_questions; ++i) { + DNSQuestion q; + size_t in_place = 0; + if (!ReadName(buf, pos, &in_place, &q.name)) { + return false; + } + pos += in_place; + if (pos + kQuestionFixedSize > buf.size()) { + return false; + } + q.qtype = ReadU16(buf, pos); + pos += kQuestionFixedSize; + info->questions.push_back(std::move(q)); + } + + for (uint32_t i = 0; i < num_records; ++i) { + const size_t owner_pos = pos; + size_t in_place = 0; + if (!ReadName(buf, pos, &in_place, nullptr)) { + return false; + } + pos += in_place; + if (pos + kRecordFixedSize > buf.size()) { + return false; + } + const uint16_t type = ReadU16(buf, pos); + const uint16_t rdlength = ReadU16(buf, pos + 8); + const size_t rdata_pos = pos + kRecordFixedSize; + if (rdata_pos + rdlength > buf.size()) { + return false; + } + if (i < num_answers) { + DNSAnswer a; + a.type = type; + size_t unused = 0; + bool ok = ReadName(buf, owner_pos, &unused, &a.name); + switch (type) { + case kDNSTypeA: + case kDNSTypeAAAA: + ok = ok && rdlength == (type == kDNSTypeA ? 4 : 16); + if (ok) { + a.addr = std::string(buf.substr(rdata_pos, rdlength)); + } + break; + case kDNSTypeCNAME: + case kDNSTypePTR: + ok = ok && ReadName(buf, rdata_pos, &unused, &a.target); + break; + default: + ok = false; + } + if (ok) { + info->answers.push_back(std::move(a)); + } + } + pos = rdata_pos + rdlength; + } + + info->length = pos; + return true; +} + +std::string DNSTypeName(uint16_t type) { + switch (type) { + case 1: + return "A"; + case 2: + return "NS"; + case 5: + return "CNAME"; + case 6: + return "SOA"; + case 12: + return "PTR"; + case 15: + return "MX"; + case 16: + return "TXT"; + case 28: + return "AAAA"; + case 33: + return "SRV"; + case 35: + return "NAPTR"; + case 64: + return "SVCB"; + case 65: + return "HTTPS"; + case 255: + return "ANY"; + default: + return "TYPE" + std::to_string(type); + } +} + +} // namespace dns +} // namespace protocols +} // namespace stirling +} // namespace px diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h new file mode 100644 index 00000000000..23f95a96ae8 --- /dev/null +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h @@ -0,0 +1,64 @@ +/* + * Copyright 2018- The Pixie Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace px { +namespace stirling { +namespace protocols { +namespace dns { + +struct DNSQuestion { + std::string name; + uint16_t qtype = 0; +}; + +struct DNSAnswer { + std::string name; + uint16_t type = 0; + std::string addr; + std::string target; +}; + +constexpr uint16_t kDNSTypeA = 1; +constexpr uint16_t kDNSTypeCNAME = 5; +constexpr uint16_t kDNSTypePTR = 12; +constexpr uint16_t kDNSTypeAAAA = 28; + +struct DNSMessageInfo { + size_t length = 0; + uint16_t txid = 0; + uint16_t flags = 0; + std::vector questions; + std::vector answers; +}; + +bool WalkDNSMessage(std::string_view buf, DNSMessageInfo* info); + +std::string DNSTypeName(uint16_t type); + +} // namespace dns +} // namespace protocols +} // namespace stirling +} // namespace px diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_test.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_test.cc new file mode 100644 index 00000000000..7d83c28f0fd --- /dev/null +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_test.cc @@ -0,0 +1,112 @@ +/* + * Copyright 2018- The Pixie Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h" + +#include + +#include +#include + +#include "src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_testdata.h" + +namespace px { +namespace stirling { +namespace protocols { +namespace dns { + +using testdata::Bytes; + +TEST(WalkDNSMessageTest, PTRQuery) { + const std::string buf = Bytes(testdata::kPTRQueryHex); + DNSMessageInfo info; + ASSERT_TRUE(WalkDNSMessage(buf, &info)); + EXPECT_EQ(info.length, buf.size()); + ASSERT_EQ(info.questions.size(), 1); + EXPECT_EQ(info.questions[0].name, "12.0.42.10.in-addr.arpa"); + EXPECT_EQ(info.questions[0].qtype, 12); + EXPECT_TRUE(info.answers.empty()); +} + +TEST(WalkDNSMessageTest, PTRResponse) { + const std::string buf = Bytes(testdata::kPTRRespHex); + DNSMessageInfo info; + ASSERT_TRUE(WalkDNSMessage(buf, &info)); + EXPECT_EQ(info.length, buf.size()); + EXPECT_EQ(info.txid, 0x1234); + ASSERT_EQ(info.answers.size(), 1); + EXPECT_EQ(info.answers[0].type, kDNSTypePTR); + EXPECT_EQ(info.answers[0].name, "12.0.42.10.in-addr.arpa"); + EXPECT_EQ(info.answers[0].target, "10-42-0-12.kube-dns.kube-system.svc.cluster.local"); +} + +TEST(WalkDNSMessageTest, CompressedNamesAndAdditionalRecord) { + const std::string buf = Bytes(testdata::kCompressedRespHex); + DNSMessageInfo info; + ASSERT_TRUE(WalkDNSMessage(buf, &info)); + EXPECT_EQ(info.length, buf.size()); + ASSERT_EQ(info.questions.size(), 1); + EXPECT_EQ(info.questions[0].name, "intellij-experiments.appspot.com"); + EXPECT_EQ(info.questions[0].qtype, 1); + ASSERT_EQ(info.answers.size(), 1); + EXPECT_EQ(info.answers[0].name, "intellij-experiments.appspot.com"); + EXPECT_EQ(info.answers[0].type, kDNSTypeA); + EXPECT_EQ(info.answers[0].addr, std::string("\xd8\x3a\xc2\xb4", 4)); +} + +TEST(WalkDNSMessageTest, FindsBoundaryOfBackToBackMessages) { + const std::string first = Bytes(testdata::kPTRQueryHex); + const std::string second = Bytes(testdata::kAQueryHex); + const std::string buf = first + second + first; + DNSMessageInfo info; + ASSERT_TRUE(WalkDNSMessage(buf, &info)); + EXPECT_EQ(info.length, first.size()); + + std::string_view rest(buf); + rest.remove_prefix(info.length); + ASSERT_TRUE(WalkDNSMessage(rest, &info)); + EXPECT_EQ(info.length, second.size()); + EXPECT_EQ(info.questions[0].name, "kube-dns.kube-system.svc.cluster.local"); +} + +TEST(WalkDNSMessageTest, RejectsTruncatedMessages) { + const std::string buf = Bytes(testdata::kPTRRespHex); + DNSMessageInfo info; + for (size_t cut = 1; cut < buf.size(); ++cut) { + EXPECT_FALSE(WalkDNSMessage(std::string_view(buf).substr(0, buf.size() - cut), &info)) + << "cut=" << cut; + } +} + +TEST(WalkDNSMessageTest, RejectsPointerLoop) { + const std::string buf("\x00\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\xc0\x0c\x00\x01\x00\x01", + 18); + DNSMessageInfo info; + EXPECT_FALSE(WalkDNSMessage(buf, &info)); +} + +TEST(WalkDNSMessageTest, TypeNames) { + EXPECT_EQ(DNSTypeName(12), "PTR"); + EXPECT_EQ(DNSTypeName(16), "TXT"); + EXPECT_EQ(DNSTypeName(4242), "TYPE4242"); +} + +} // namespace dns +} // namespace protocols +} // namespace stirling +} // namespace px diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_testdata.h b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_testdata.h new file mode 100644 index 00000000000..5c260467c5a --- /dev/null +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_testdata.h @@ -0,0 +1,98 @@ +/* + * Copyright 2018- The Pixie Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +namespace px { +namespace stirling { +namespace protocols { +namespace dns { +namespace testdata { + +constexpr std::string_view kPTRQueryHex = + "123401000001000000000000023132013002343202313007696e2d61646472046172706100000c0001"; + +constexpr std::string_view kPTRRespHex = + "123485000001000100000000023132013002343202313007696e2d61646472046172706100000c0001023132" + "013002343202313007696e2d61646472046172706100000c00010000000500330a31302d34322d302d313208" + "6b7562652d646e730b6b7562652d73797374656d0373766307636c7573746572056c6f63616c00"; + +constexpr std::string_view kAQueryHex = + "234501000001000000000000086b7562652d646e730b6b7562652d73797374656d0373766307636c75737465" + "72056c6f63616c0000010001"; + +constexpr std::string_view kARespHex = + "234585000001000100000000086b7562652d646e730b6b7562652d73797374656d0373766307636c75737465" + "72056c6f63616c0000010001086b7562652d646e730b6b7562652d73797374656d0373766307636c75737465" + "72056c6f63616c00000100010000000500040a2b000a"; + +constexpr std::string_view kCompressedRespHex = + "c6fa8180000100010000000114696e74656c6c696a2d6578706572696d656e74730761707073706f7403636f" + "6d0000010001c00c00010001000001240004d83ac2b40000290200000000000000"; + +inline std::string U16(uint16_t v) { + return std::string{static_cast(v >> 8), static_cast(v & 0xff)}; +} + +inline std::string Name(std::string_view dotted) { + std::string out; + size_t start = 0; + while (start < dotted.size()) { + size_t dot = dotted.find('.', start); + if (dot == std::string_view::npos) dot = dotted.size(); + out.push_back(static_cast(dot - start)); + out.append(dotted.substr(start, dot - start)); + start = dot + 1; + } + out.push_back('\0'); + return out; +} + +inline std::string Header(uint16_t txid, uint16_t flags, uint16_t qd, uint16_t an, uint16_t ns = 0, + uint16_t ar = 0) { + return U16(txid) + U16(flags) + U16(qd) + U16(an) + U16(ns) + U16(ar); +} + +inline std::string RR(std::string_view owner_wire, uint16_t type, std::string_view rdata) { + return std::string(owner_wire) + U16(type) + U16(1) + U16(0) + U16(5) + + U16(static_cast(rdata.size())) + std::string(rdata); +} + +inline std::string Bytes(std::string_view hex) { + std::string out; + out.reserve(hex.size() / 2); + auto nibble = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + return c - 'A' + 10; + }; + for (size_t i = 0; i + 1 < hex.size(); i += 2) { + out.push_back(static_cast((nibble(hex[i]) << 4) | nibble(hex[i + 1]))); + } + return out; +} + +} // namespace testdata +} // namespace dns +} // namespace protocols +} // namespace stirling +} // namespace px diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/parse.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/parse.cc index d664174d502..06cb5131704 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/parse.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/parse.cc @@ -20,13 +20,16 @@ #include #include +#include #include #include #include +#include #include "src/common/base/byte_utils.h" #include "src/common/base/inet_utils.h" #include "src/common/base/types.h" +#include "src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message.h" #include "src/stirling/source_connectors/socket_tracer/protocols/dns/types.h" #include "src/stirling/utils/binary_decoder.h" #include "src/stirling/utils/parse_state.h" @@ -39,21 +42,96 @@ namespace dns { class PxDnsParserListener : public DnsParserListener { public: void onDnsRec(std::string name, in_addr addr) override { - records_.emplace_back(DNSRecord{std::move(name), "", {InetAddrFamily::kIPv4, addr}}); + records_.emplace_back(DNSRecord{std::move(name), "", {InetAddrFamily::kIPv4, addr}, "", ""}); } void onDnsRec(std::string name, in6_addr addr) override { - records_.emplace_back(DNSRecord{std::move(name), "", {InetAddrFamily::kIPv6, addr}}); + records_.emplace_back(DNSRecord{std::move(name), "", {InetAddrFamily::kIPv6, addr}, "", ""}); } void onDnsRec(std::string name, std::string cname) override { - records_.emplace_back(DNSRecord{std::move(name), std::move(cname), {}}); + records_.emplace_back(DNSRecord{std::move(name), std::move(cname), {}, "", ""}); } std::vector records_; }; +namespace { + +constexpr uint16_t kOpcodeMask = ((1 << kOpcodeWidth) - 1) << kOpcodePos; + +void ExtractHeader(std::string_view msg, DNSHeader* header) { + BinaryDecoder decoder(msg); + header->txid = decoder.ExtractBEInt().ValueOr(0xffff); + header->flags = decoder.ExtractBEInt().ValueOr(0xffff); + header->num_queries = decoder.ExtractBEInt().ValueOr(0xffff); + header->num_answers = decoder.ExtractBEInt().ValueOr(0xffff); + header->num_auth = decoder.ExtractBEInt().ValueOr(0xffff); + header->num_addl = decoder.ExtractBEInt().ValueOr(0xffff); +} + +std::vector RecordsFromWalk(DNSMessageInfo* info) { + std::vector records; + const bool is_response = EXTRACT_DNS_FLAG(info->flags, kQRPos, kQRWidth) == 1; + if (!is_response) { + for (auto& q : info->questions) { + DNSRecord r; + r.name = std::move(q.name); + if (q.qtype == kDNSTypeA) { + r.addr = InetAddr{InetAddrFamily::kIPv4, in_addr{}}; + } else if (q.qtype == kDNSTypeAAAA) { + r.addr = InetAddr{InetAddrFamily::kIPv6, in6_addr{}}; + } else { + r.type = DNSTypeName(q.qtype); + } + records.push_back(std::move(r)); + } + return records; + } + for (auto& a : info->answers) { + DNSRecord r; + r.name = std::move(a.name); + switch (a.type) { + case kDNSTypeA: { + in_addr addr; + memcpy(&addr, a.addr.data(), sizeof(addr)); + r.addr = InetAddr{InetAddrFamily::kIPv4, addr}; + } break; + case kDNSTypeAAAA: { + in6_addr addr; + memcpy(&addr, a.addr.data(), sizeof(addr)); + r.addr = InetAddr{InetAddrFamily::kIPv6, addr}; + } break; + case kDNSTypeCNAME: + r.cname = std::move(a.target); + break; + case kDNSTypePTR: + r.type = "PTR"; + r.ptr = std::move(a.target); + break; + default: + continue; + } + records.push_back(std::move(r)); + } + return records; +} + +} // namespace + ParseState ParseFrame(message_type_t type, std::string_view* buf, Frame* result) { PX_UNUSED(type); + DNSMessageInfo info; + if (WalkDNSMessage(*buf, &info)) { + const std::string_view msg = buf->substr(0, info.length); + buf->remove_prefix(info.length); + if ((info.flags & kOpcodeMask) != 0) { + return ParseState::kIgnored; + } + ExtractHeader(msg, &result->header); + result->AddRecords(RecordsFromWalk(&info)); + return ParseState::kSuccess; + } + PxDnsParserListener response_handler; std::unique_ptr parser = DnsParserNew(&response_handler); int retval = parser->parse(buf->data(), buf->length()); @@ -63,13 +141,7 @@ ParseState ParseFrame(message_type_t type, std::string_view* buf, Frame* result) // DnsParser ensures there is a complete header. CTX_DCHECK_GT(buf->size(), sizeof(DNSHeader)); - BinaryDecoder decoder(*buf); - result->header.txid = decoder.ExtractBEInt().ValueOr(0xffff); - result->header.flags = decoder.ExtractBEInt().ValueOr(0xffff); - result->header.num_queries = decoder.ExtractBEInt().ValueOr(0xffff); - result->header.num_answers = decoder.ExtractBEInt().ValueOr(0xffff); - result->header.num_auth = decoder.ExtractBEInt().ValueOr(0xffff); - result->header.num_addl = decoder.ExtractBEInt().ValueOr(0xffff); + ExtractHeader(*buf, &result->header); // The ValueOr(0xffff) conditions should never trigger, since there are enough bytes. CTX_DCHECK_NE(result->header.flags, 0xffff); diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/parse_test.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/parse_test.cc index e5379ec7fa3..d5191f2cecc 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/parse_test.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/parse_test.cc @@ -20,6 +20,9 @@ #include #include +#include + +#include "src/stirling/source_connectors/socket_tracer/protocols/dns/dns_message_testdata.h" #include "src/stirling/source_connectors/socket_tracer/protocols/dns/parse.h" namespace px { @@ -461,6 +464,137 @@ TEST_F(DNSParserTest, PartialRecords) { } } +TEST_F(DNSParserTest, BackToBackRequests) { + const std::string buf = testdata::Bytes(testdata::kPTRQueryHex) + + testdata::Bytes(testdata::kAQueryHex) + + testdata::Bytes(testdata::kPTRQueryHex); + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kRequest, std::string_view(buf), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + stream_id_t only_key = frames.begin()->first; + ASSERT_EQ(frames[only_key].size(), 3); + + EXPECT_EQ(frames[only_key][0].header.txid, 0x1234); + ASSERT_EQ(frames[only_key][0].records().size(), 1); + EXPECT_EQ(frames[only_key][0].records()[0].name, "12.0.42.10.in-addr.arpa"); + EXPECT_EQ(frames[only_key][0].records()[0].type, "PTR"); + + EXPECT_EQ(frames[only_key][1].header.txid, 0x2345); + ASSERT_EQ(frames[only_key][1].records().size(), 1); + EXPECT_EQ(frames[only_key][1].records()[0].name, "kube-dns.kube-system.svc.cluster.local"); + EXPECT_EQ(frames[only_key][1].records()[0].type, ""); + EXPECT_EQ(frames[only_key][1].records()[0].addr.family, InetAddrFamily::kIPv4); + + EXPECT_EQ(frames[only_key][2].header.txid, 0x1234); +} + +TEST_F(DNSParserTest, BackToBackResponsesWithPTR) { + const std::string buf = + testdata::Bytes(testdata::kARespHex) + testdata::Bytes(testdata::kPTRRespHex); + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kResponse, std::string_view(buf), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + stream_id_t only_key = frames.begin()->first; + ASSERT_EQ(frames[only_key].size(), 2); + + EXPECT_EQ(frames[only_key][0].header.txid, 0x2345); + ASSERT_EQ(frames[only_key][0].records().size(), 1); + EXPECT_EQ(frames[only_key][0].records()[0].addr.AddrStr(), "10.43.0.10"); + + EXPECT_EQ(frames[only_key][1].header.txid, 0x1234); + ASSERT_EQ(frames[only_key][1].records().size(), 1); + EXPECT_EQ(frames[only_key][1].records()[0].name, "12.0.42.10.in-addr.arpa"); + EXPECT_EQ(frames[only_key][1].records()[0].type, "PTR"); + EXPECT_EQ(frames[only_key][1].records()[0].ptr, + "10-42-0-12.kube-dns.kube-system.svc.cluster.local"); +} + +TEST_F(DNSParserTest, ManyBackToBackRequests) { + std::string buf; + for (int i = 0; i < 255; ++i) { + buf += testdata::Bytes(testdata::kPTRQueryHex); + } + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kRequest, std::string_view(buf), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + EXPECT_EQ(frames.begin()->second.size(), 255); +} + +TEST_F(DNSParserTest, ResponseWithManyAnswers) { + using testdata::Header; + using testdata::Name; + using testdata::RR; + std::string msg = Header(0x3456, 0x8180, 1, 30) + Name("web.shop.svc.cluster.local") + + testdata::U16(1) + testdata::U16(1); + for (int i = 0; i < 30; ++i) { + const std::string ip{10, 0, 0, static_cast(i + 1)}; + msg += RR(std::string("\xc0\x0c", 2), 1, ip); + } + const std::string buf = msg + testdata::Bytes(testdata::kARespHex); + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kResponse, std::string_view(buf), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + auto& f = frames.begin()->second; + ASSERT_EQ(f.size(), 2); + ASSERT_EQ(f[0].records().size(), 30); + EXPECT_EQ(f[0].records()[0].name, "web.shop.svc.cluster.local"); + EXPECT_EQ(f[0].records()[0].addr.AddrStr(), "10.0.0.1"); + EXPECT_EQ(f[0].records()[29].addr.AddrStr(), "10.0.0.30"); + EXPECT_EQ(f[1].header.txid, 0x2345); +} + +TEST_F(DNSParserTest, UncompressedCNAME) { + using testdata::Header; + using testdata::Name; + using testdata::RR; + const std::string msg = Header(0x4567, 0x8180, 1, 2) + Name("www.example.com") + + testdata::U16(1) + testdata::U16(1) + + RR(Name("www.example.com"), 5, Name("web.example.net")) + + RR(Name("web.example.net"), 1, std::string{93, (char)184, (char)216, 34}); + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kResponse, std::string_view(msg), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + auto& f = frames.begin()->second; + ASSERT_EQ(f.size(), 1); + ASSERT_EQ(f[0].records().size(), 2); + EXPECT_EQ(f[0].records()[0].name, "www.example.com"); + EXPECT_EQ(f[0].records()[0].cname, "web.example.net"); + EXPECT_EQ(f[0].records()[1].name, "web.example.net"); + EXPECT_EQ(f[0].records()[1].addr.AddrStr(), "93.184.216.34"); +} + +TEST_F(DNSParserTest, NonQueryOpcodeSkippedAlone) { + using testdata::Header; + using testdata::Name; + const std::string notify = + Header(0x5678, 0x2000, 1, 0) + Name("example.com") + testdata::U16(6) + testdata::U16(1); + const std::string buf = notify + testdata::Bytes(testdata::kAQueryHex); + + absl::flat_hash_map> frames; + ParseResult parse_result = + ParseFramesLoop(message_type_t::kRequest, std::string_view(buf), &frames); + + ASSERT_EQ(parse_result.state, ParseState::kSuccess); + auto& f = frames.begin()->second; + ASSERT_EQ(f.size(), 1); + EXPECT_EQ(f[0].header.txid, 0x2345); +} + } // namespace dns } // namespace protocols } // namespace stirling diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher.cc index 733bb4a52ba..65bf93baceb 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher.cc @@ -110,7 +110,8 @@ void ProcessReq(const Frame& req_frame, Request* req) { rapidjson::Value queries(rapidjson::kArrayType); for (const auto& r : req_frame.records()) { const std::string& name = r.name; - std::string_view type_name = DNSRecordTypeName(r.addr.family); + std::string_view type_name = + r.type.empty() ? DNSRecordTypeName(r.addr.family) : std::string_view(r.type); rapidjson::Value query(rapidjson::kObjectType); query.AddMember("name", rapidjson::StringRef(name.data(), name.size()), d.GetAllocator()); @@ -141,7 +142,12 @@ void ProcessResp(const Frame& resp_frame, Response* resp) { rapidjson::Value answer(rapidjson::kObjectType); answer.AddMember("name", rapidjson::StringRef(name.data(), name.size()), d.GetAllocator()); - if (!r.cname.empty()) { + if (!r.ptr.empty()) { + std::string_view type_name = "PTR"; + answer.AddMember("type", rapidjson::StringRef(type_name.data(), type_name.size()), + d.GetAllocator()); + answer.AddMember("ptr", rapidjson::StringRef(r.ptr.data(), r.ptr.size()), d.GetAllocator()); + } else if (!r.cname.empty()) { std::string_view type_name = "CNAME"; answer.AddMember("type", rapidjson::StringRef(type_name.data(), type_name.size()), d.GetAllocator()); diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher_test.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher_test.cc index 02b69677fe1..db0735dd7f7 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher_test.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/stitcher_test.cc @@ -82,7 +82,7 @@ TEST(DnsStitcherTest, RecordOutput) { ip_addr.addr = addr_tmp; std::vector dns_records; - dns_records.push_back(DNSRecord{"pixie.ai", "", ip_addr}); + dns_records.push_back(DNSRecord{"pixie.ai", "", ip_addr, "", ""}); int t = 0; Frame req0_frame = CreateReqFrame(++t, 0); diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/types.h b/src/stirling/source_connectors/socket_tracer/protocols/dns/types.h index f4408899f80..20e33d551dd 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/types.h +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/types.h @@ -91,6 +91,10 @@ struct DNSRecord { // TODO(oazizi): Consider using std::variant. std::string cname; InetAddr addr; + + std::string type; + + std::string ptr; }; struct Frame : public FrameBase { @@ -100,7 +104,8 @@ struct Frame : public FrameBase { void AddRecords(std::vector&& records) { for (const auto& r : records) { - records_size_ += r.name.size() + r.cname.size() + sizeof(r.addr); + records_size_ += + r.name.size() + r.cname.size() + sizeof(r.addr) + r.type.size() + r.ptr.size(); } records_ = std::move(records); } diff --git a/src/stirling/source_connectors/socket_tracer/protocols/dns/types_test.cc b/src/stirling/source_connectors/socket_tracer/protocols/dns/types_test.cc index fe6d4659f0f..b08b16d0976 100644 --- a/src/stirling/source_connectors/socket_tracer/protocols/dns/types_test.cc +++ b/src/stirling/source_connectors/socket_tracer/protocols/dns/types_test.cc @@ -29,8 +29,8 @@ namespace dns { TEST(DNSTypesTest, ByteSize) { Frame f = {}; - DNSRecord r1{"px.dev", "px_cname", {}}; - DNSRecord r2{"aloooooooooooooooooooooooongname", "", {}}; + DNSRecord r1{"px.dev", "px_cname", {}, "", ""}; + DNSRecord r2{"aloooooooooooooooooooooooongname", "", {}, "", ""}; f.AddRecords({r1, r2}); auto s = std::string_view("px.dev").size() + std::string_view("px_cname").size() +