1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/dns/dns_query.h"
6
7 #include <utility>
8
9 #include "base/big_endian.h"
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "base/sys_byteorder.h"
14 #include "net/base/io_buffer.h"
15 #include "net/dns/dns_names_util.h"
16 #include "net/dns/opt_record_rdata.h"
17 #include "net/dns/public/dns_protocol.h"
18 #include "net/dns/record_rdata.h"
19 #include "third_party/abseil-cpp/absl/types/optional.h"
20
21 namespace net {
22
23 namespace {
24
25 const size_t kHeaderSize = sizeof(dns_protocol::Header);
26
27 // Size of the fixed part of an OPT RR:
28 // https://tools.ietf.org/html/rfc6891#section-6.1.2
29 static const size_t kOptRRFixedSize = 11;
30
31 // https://tools.ietf.org/html/rfc6891#section-6.2.5
32 // TODO(robpercival): Determine a good value for this programmatically.
33 const uint16_t kMaxUdpPayloadSize = 4096;
34
QuestionSize(size_t qname_size)35 size_t QuestionSize(size_t qname_size) {
36 // QNAME + QTYPE + QCLASS
37 return qname_size + sizeof(uint16_t) + sizeof(uint16_t);
38 }
39
40 // Buffer size of Opt record for |rdata| (does not include Opt record or RData
41 // added for padding).
OptRecordSize(const OptRecordRdata * rdata)42 size_t OptRecordSize(const OptRecordRdata* rdata) {
43 return rdata == nullptr ? 0 : kOptRRFixedSize + rdata->buf().size();
44 }
45
46 // Padding size includes Opt header for the padding. Does not include OptRecord
47 // header (kOptRRFixedSize) even when added just for padding.
DeterminePaddingSize(size_t unpadded_size,DnsQuery::PaddingStrategy padding_strategy)48 size_t DeterminePaddingSize(size_t unpadded_size,
49 DnsQuery::PaddingStrategy padding_strategy) {
50 switch (padding_strategy) {
51 case DnsQuery::PaddingStrategy::NONE:
52 return 0;
53 case DnsQuery::PaddingStrategy::BLOCK_LENGTH_128:
54 size_t padding_size = OptRecordRdata::Opt::kHeaderSize;
55 size_t remainder = (padding_size + unpadded_size) % 128;
56 padding_size += (128 - remainder) % 128;
57 DCHECK_EQ((unpadded_size + padding_size) % 128, 0u);
58 return padding_size;
59 }
60 }
61
AddPaddingIfNecessary(const OptRecordRdata * opt_rdata,DnsQuery::PaddingStrategy padding_strategy,size_t no_opt_buffer_size)62 std::unique_ptr<OptRecordRdata> AddPaddingIfNecessary(
63 const OptRecordRdata* opt_rdata,
64 DnsQuery::PaddingStrategy padding_strategy,
65 size_t no_opt_buffer_size) {
66 // If no input OPT record rdata and no padding, no OPT record rdata needed.
67 if (!opt_rdata && padding_strategy == DnsQuery::PaddingStrategy::NONE)
68 return nullptr;
69
70 std::unique_ptr<OptRecordRdata> merged_opt_rdata;
71 if (opt_rdata) {
72 merged_opt_rdata = OptRecordRdata::Create(
73 base::StringPiece(opt_rdata->buf().data(), opt_rdata->buf().size()));
74 } else {
75 merged_opt_rdata = std::make_unique<OptRecordRdata>();
76 }
77 DCHECK(merged_opt_rdata);
78
79 size_t unpadded_size =
80 no_opt_buffer_size + OptRecordSize(merged_opt_rdata.get());
81 size_t padding_size = DeterminePaddingSize(unpadded_size, padding_strategy);
82
83 if (padding_size > 0) {
84 // |opt_rdata| must not already contain padding if DnsQuery is to add
85 // padding.
86 DCHECK(!merged_opt_rdata->ContainsOptCode(dns_protocol::kEdnsPadding));
87 // OPT header is the minimum amount of padding.
88 DCHECK(padding_size >= OptRecordRdata::Opt::kHeaderSize);
89
90 merged_opt_rdata->AddOpt(std::make_unique<OptRecordRdata::PaddingOpt>(
91 padding_size - OptRecordRdata::Opt::kHeaderSize));
92 }
93
94 return merged_opt_rdata;
95 }
96
97 } // namespace
98
99 // DNS query consists of a 12-byte header followed by a question section.
100 // For details, see RFC 1035 section 4.1.1. This header template sets RD
101 // bit, which directs the name server to pursue query recursively, and sets
102 // the QDCOUNT to 1, meaning the question section has a single entry.
DnsQuery(uint16_t id,base::span<const uint8_t> qname,uint16_t qtype,const OptRecordRdata * opt_rdata,PaddingStrategy padding_strategy)103 DnsQuery::DnsQuery(uint16_t id,
104 base::span<const uint8_t> qname,
105 uint16_t qtype,
106 const OptRecordRdata* opt_rdata,
107 PaddingStrategy padding_strategy)
108 : qname_size_(qname.size()) {
109 #if DCHECK_IS_ON()
110 absl::optional<std::string> dotted_name =
111 dns_names_util::NetworkToDottedName(qname);
112 DCHECK(dotted_name && !dotted_name.value().empty());
113 #endif // DCHECK_IS_ON()
114
115 size_t buffer_size = kHeaderSize + QuestionSize(qname_size_);
116 std::unique_ptr<OptRecordRdata> merged_opt_rdata =
117 AddPaddingIfNecessary(opt_rdata, padding_strategy, buffer_size);
118 if (merged_opt_rdata)
119 buffer_size += OptRecordSize(merged_opt_rdata.get());
120
121 io_buffer_ = base::MakeRefCounted<IOBufferWithSize>(buffer_size);
122
123 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
124 *header_ = {};
125 header_->id = base::HostToNet16(id);
126 header_->flags = base::HostToNet16(dns_protocol::kFlagRD);
127 header_->qdcount = base::HostToNet16(1);
128
129 // Write question section after the header.
130 base::BigEndianWriter writer(io_buffer_->data() + kHeaderSize,
131 io_buffer_->size() - kHeaderSize);
132 writer.WriteBytes(qname.data(), qname.size());
133 writer.WriteU16(qtype);
134 writer.WriteU16(dns_protocol::kClassIN);
135
136 if (merged_opt_rdata) {
137 DCHECK_NE(merged_opt_rdata->OptCount(), 0u);
138
139 header_->arcount = base::HostToNet16(1);
140 // Write OPT pseudo-resource record.
141 writer.WriteU8(0); // empty domain name (root domain)
142 writer.WriteU16(OptRecordRdata::kType); // type
143 writer.WriteU16(kMaxUdpPayloadSize); // class
144 // ttl (next 3 fields)
145 writer.WriteU8(0); // rcode does not apply to requests
146 writer.WriteU8(0); // version
147 // TODO(robpercival): Set "DNSSEC OK" flag if/when DNSSEC is supported:
148 // https://tools.ietf.org/html/rfc3225#section-3
149 writer.WriteU16(0); // flags
150
151 // rdata
152 writer.WriteU16(merged_opt_rdata->buf().size()); // rdata length
153 writer.WriteBytes(merged_opt_rdata->buf().data(),
154 merged_opt_rdata->buf().size());
155 }
156 }
157
DnsQuery(scoped_refptr<IOBufferWithSize> buffer)158 DnsQuery::DnsQuery(scoped_refptr<IOBufferWithSize> buffer)
159 : io_buffer_(std::move(buffer)) {}
160
DnsQuery(const DnsQuery & query)161 DnsQuery::DnsQuery(const DnsQuery& query) {
162 CopyFrom(query);
163 }
164
operator =(const DnsQuery & query)165 DnsQuery& DnsQuery::operator=(const DnsQuery& query) {
166 CopyFrom(query);
167 return *this;
168 }
169
170 DnsQuery::~DnsQuery() = default;
171
CloneWithNewId(uint16_t id) const172 std::unique_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const {
173 return base::WrapUnique(new DnsQuery(*this, id));
174 }
175
Parse(size_t valid_bytes)176 bool DnsQuery::Parse(size_t valid_bytes) {
177 if (io_buffer_ == nullptr || io_buffer_->data() == nullptr) {
178 return false;
179 }
180 CHECK(valid_bytes <= base::checked_cast<size_t>(io_buffer_->size()));
181 // We should only parse the query once if the query is constructed from a raw
182 // buffer. If we have constructed the query from data or the query is already
183 // parsed after constructed from a raw buffer, |header_| is not null.
184 DCHECK(header_ == nullptr);
185 base::BigEndianReader reader(
186 reinterpret_cast<const uint8_t*>(io_buffer_->data()), valid_bytes);
187 dns_protocol::Header header;
188 if (!ReadHeader(&reader, &header)) {
189 return false;
190 }
191 if (header.flags & dns_protocol::kFlagResponse) {
192 return false;
193 }
194 if (header.qdcount != 1) {
195 VLOG(1) << "Not supporting parsing a DNS query with multiple (or zero) "
196 "questions.";
197 return false;
198 }
199 std::string qname;
200 if (!ReadName(&reader, &qname)) {
201 return false;
202 }
203 uint16_t qtype;
204 uint16_t qclass;
205 if (!reader.ReadU16(&qtype) || !reader.ReadU16(&qclass) ||
206 qclass != dns_protocol::kClassIN) {
207 return false;
208 }
209 // |io_buffer_| now contains the raw packet of a valid DNS query, we just
210 // need to properly initialize |qname_size_| and |header_|.
211 qname_size_ = qname.size();
212 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
213 return true;
214 }
215
id() const216 uint16_t DnsQuery::id() const {
217 return base::NetToHost16(header_->id);
218 }
219
qname() const220 base::span<const uint8_t> DnsQuery::qname() const {
221 return base::span<const uint8_t>(
222 reinterpret_cast<const uint8_t*>(io_buffer_->data() + kHeaderSize),
223 qname_size_);
224 }
225
qtype() const226 uint16_t DnsQuery::qtype() const {
227 uint16_t type;
228 base::ReadBigEndian(reinterpret_cast<const uint8_t*>(
229 io_buffer_->data() + kHeaderSize + qname_size_),
230 &type);
231 return type;
232 }
233
question() const234 base::StringPiece DnsQuery::question() const {
235 return base::StringPiece(io_buffer_->data() + kHeaderSize,
236 QuestionSize(qname_size_));
237 }
238
question_size() const239 size_t DnsQuery::question_size() const {
240 return QuestionSize(qname_size_);
241 }
242
set_flags(uint16_t flags)243 void DnsQuery::set_flags(uint16_t flags) {
244 header_->flags = flags;
245 }
246
DnsQuery(const DnsQuery & orig,uint16_t id)247 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) {
248 CopyFrom(orig);
249 header_->id = base::HostToNet16(id);
250 }
251
CopyFrom(const DnsQuery & orig)252 void DnsQuery::CopyFrom(const DnsQuery& orig) {
253 qname_size_ = orig.qname_size_;
254 io_buffer_ = base::MakeRefCounted<IOBufferWithSize>(orig.io_buffer()->size());
255 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(),
256 io_buffer_.get()->size());
257 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
258 }
259
ReadHeader(base::BigEndianReader * reader,dns_protocol::Header * header)260 bool DnsQuery::ReadHeader(base::BigEndianReader* reader,
261 dns_protocol::Header* header) {
262 return (
263 reader->ReadU16(&header->id) && reader->ReadU16(&header->flags) &&
264 reader->ReadU16(&header->qdcount) && reader->ReadU16(&header->ancount) &&
265 reader->ReadU16(&header->nscount) && reader->ReadU16(&header->arcount));
266 }
267
ReadName(base::BigEndianReader * reader,std::string * out)268 bool DnsQuery::ReadName(base::BigEndianReader* reader, std::string* out) {
269 DCHECK(out != nullptr);
270 out->clear();
271 out->reserve(dns_protocol::kMaxNameLength + 1);
272 uint8_t label_length;
273 if (!reader->ReadU8(&label_length)) {
274 return false;
275 }
276 while (label_length) {
277 if (out->size() + 1 + label_length > dns_protocol::kMaxNameLength) {
278 return false;
279 }
280
281 out->append(reinterpret_cast<char*>(&label_length), 1);
282
283 base::StringPiece label;
284 if (!reader->ReadPiece(&label, label_length)) {
285 return false;
286 }
287 out->append(label.data(), label.size());
288 if (!reader->ReadU8(&label_length)) {
289 return false;
290 }
291 }
292 DCHECK_LE(out->size(), static_cast<size_t>(dns_protocol::kMaxNameLength));
293 out->append(1, '\0');
294 return true;
295 }
296
297 } // namespace net
298