|
| 1 | +/* |
| 2 | + * This file is part of PowerDNS or dnsdist. |
| 3 | + * Copyright -- PowerDNS.COM B.V. and its contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of version 2 of the GNU General Public License as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * In addition, for the avoidance of any doubt, permission is granted to |
| 10 | + * link this program with OpenSSL and to (re)distribute the binaries |
| 11 | + * produced as the result of such linking. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <array> |
| 24 | + |
| 25 | +#include "dnsparser.hh" |
| 26 | +#include "dnsrecords.hh" |
| 27 | +#include "dnswriter.hh" |
| 28 | +#include "dnsname.hh" |
| 29 | +#include "ednsoptions.hh" |
| 30 | +#include "qtype.hh" |
| 31 | +#include "statbag.hh" |
| 32 | + |
| 33 | +// NOLINTNEXTLINE(readability-identifier-length,bugprone-throwing-static-initialization) |
| 34 | +StatBag S; |
| 35 | + |
| 36 | +bool g_slogStructured{false}; |
| 37 | + |
| 38 | +static void init() |
| 39 | +{ |
| 40 | + reportAllTypes(); |
| 41 | +} |
| 42 | + |
| 43 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
| 44 | + |
| 45 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
| 46 | +{ |
| 47 | + static bool initialized = false; |
| 48 | + |
| 49 | + if (!initialized) { |
| 50 | + init(); |
| 51 | + initialized = true; |
| 52 | + } |
| 53 | + |
| 54 | + if (size > std::numeric_limits<uint16_t>::max()) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 59 | + const char* chars = reinterpret_cast<const char*>(data); |
| 60 | + |
| 61 | + try { |
| 62 | + // 1) Feed the raw, untrusted bytes straight to the EDNS OPT option parsers: |
| 63 | + // the option TLV decoder in ednsoptions.cc is otherwise unreached. |
| 64 | + EDNSOptionViewMap viewOptions; |
| 65 | + (void)getEDNSOptions(chars, size, viewOptions); |
| 66 | + |
| 67 | + std::vector<std::pair<uint16_t, std::string>> options; |
| 68 | + (void)getEDNSOptionsFromContent(std::string(chars, size), options); |
| 69 | + |
| 70 | + // 2) Drive DNSPacketWriter with attacker-controlled structures (several |
| 71 | + // records, attacker rdata, name compression, EDNS OPT writing), the |
| 72 | + // full-packet writer paths that the parse/record fuzzers never reach. |
| 73 | + size_t pos = 0; |
| 74 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 75 | + const auto getU8 = [&]() -> uint8_t { return pos < size ? data[pos++] : 0; }; |
| 76 | + const auto getU16 = [&]() -> uint16_t { |
| 77 | + const uint16_t highV = getU8(); |
| 78 | + return static_cast<uint16_t>((highV << 8) | getU8()); |
| 79 | + }; |
| 80 | + |
| 81 | + const uint16_t qtype = getU16(); |
| 82 | + const DNSName qname("."); |
| 83 | + std::vector<uint8_t> packet; |
| 84 | + DNSPacketWriter writer(packet, qname, qtype); |
| 85 | + |
| 86 | + static const std::array<const char*, 4> suffixes{".", "example.com.", "a.example.com.", "powerdns.com."}; |
| 87 | + |
| 88 | + const unsigned int records = getU8() % 8U; |
| 89 | + for (unsigned int i = 0; i < records && pos < size; ++i) { |
| 90 | + // A valid name sharing one of a few suffixes (so the name-compression |
| 91 | + // paths fire) with an attacker-controlled first label. |
| 92 | + const uint8_t labelLen = getU8() % 32U; |
| 93 | + std::string label; |
| 94 | + for (uint8_t j = 0; j < labelLen && pos < size; ++j) { |
| 95 | + label.push_back(static_cast<char>('a' + (getU8() % 26))); |
| 96 | + } |
| 97 | + const std::string suffix = suffixes.at(getU8() % suffixes.size()); |
| 98 | + DNSName name{}; |
| 99 | + try { |
| 100 | + if (label.empty()) { |
| 101 | + name = DNSName(suffix); |
| 102 | + } |
| 103 | + else { |
| 104 | + label.append("."); |
| 105 | + label.append(suffix); |
| 106 | + name = DNSName(label); |
| 107 | + } |
| 108 | + } |
| 109 | + catch (...) { |
| 110 | + name = qname; |
| 111 | + } |
| 112 | + |
| 113 | + const uint16_t rtype = getU16(); |
| 114 | + const uint16_t rdlen = getU16(); |
| 115 | + std::string rdata; |
| 116 | + for (uint16_t j = 0; j < rdlen && pos < size; ++j) { |
| 117 | + rdata.push_back(static_cast<char>(getU8())); |
| 118 | + } |
| 119 | + |
| 120 | + writer.startRecord(name, rtype); |
| 121 | + writer.xfrBlob(rdata); |
| 122 | + } |
| 123 | + |
| 124 | + // EDNS OPT writing fed with the attacker-parsed option vector. |
| 125 | + if (!options.empty()) { |
| 126 | + DNSPacketWriter::optvect_t optvect; |
| 127 | + for (const auto& option : options) { |
| 128 | + optvect.emplace_back(option.first, option.second); |
| 129 | + } |
| 130 | + writer.addOpt(1232, 0, 0, optvect); |
| 131 | + } |
| 132 | + writer.commit(); |
| 133 | + |
| 134 | + // Re-parse the writer's own output. |
| 135 | + if (!packet.empty()) { |
| 136 | + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 137 | + MOADNSParser reparse(false, reinterpret_cast<const char*>(packet.data()), packet.size()); |
| 138 | + } |
| 139 | + } |
| 140 | + // NOLINTNEXTLINE(bugprone-empty-catch) |
| 141 | + catch (const std::exception& e) { |
| 142 | + } |
| 143 | + // NOLINTNEXTLINE(bugprone-empty-catch) |
| 144 | + catch (const PDNSException& e) { |
| 145 | + } |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
0 commit comments