forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns_response_unittest.cc
1686 lines (1495 loc) · 63.7 KB
/
dns_response_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/dns_response.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include "base/big_endian.h"
#include "base/check.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "base/time/time.h"
#include "net/base/io_buffer.h"
#include "net/dns/dns_query.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/dns_util.h"
#include "net/dns/public/dns_protocol.h"
#include "net/dns/record_rdata.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace net {
namespace {
TEST(DnsRecordParserTest, Constructor) {
const char data[] = { 0 };
EXPECT_FALSE(DnsRecordParser().IsValid());
EXPECT_TRUE(DnsRecordParser(data, 1, 0, 0).IsValid());
EXPECT_TRUE(DnsRecordParser(data, 1, 1, 0).IsValid());
EXPECT_FALSE(DnsRecordParser(data, 1, 0, 0).AtEnd());
EXPECT_TRUE(DnsRecordParser(data, 1, 1, 0).AtEnd());
}
TEST(DnsRecordParserTest, ReadName) {
const uint8_t data[] = {
// all labels "foo.example.com"
0x03, 'f', 'o', 'o', 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c',
'o', 'm',
// byte 0x10
0x00,
// byte 0x11
// part label, part pointer, "bar.example.com"
0x03, 'b', 'a', 'r', 0xc0, 0x04,
// byte 0x17
// all pointer to "bar.example.com", 2 jumps
0xc0, 0x11,
// byte 0x1a
};
std::string out;
DnsRecordParser parser(data, sizeof(data), 0, /*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
EXPECT_EQ(0x11u, parser.ReadName(data + 0x00, &out));
EXPECT_EQ("foo.example.com", out);
// Check that the last "." is never stored.
out.clear();
EXPECT_EQ(0x1u, parser.ReadName(data + 0x10, &out));
EXPECT_EQ("", out);
out.clear();
EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, &out));
EXPECT_EQ("bar.example.com", out);
out.clear();
EXPECT_EQ(0x2u, parser.ReadName(data + 0x17, &out));
EXPECT_EQ("bar.example.com", out);
// Parse name without storing it.
EXPECT_EQ(0x11u, parser.ReadName(data + 0x00, nullptr));
EXPECT_EQ(0x1u, parser.ReadName(data + 0x10, nullptr));
EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, nullptr));
EXPECT_EQ(0x2u, parser.ReadName(data + 0x17, nullptr));
// Check that it works even if initial position is different.
parser = DnsRecordParser(data, sizeof(data), 0x12, /*num_records=*/0);
EXPECT_EQ(0x6u, parser.ReadName(data + 0x11, nullptr));
}
TEST(DnsRecordParserTest, ReadNameFail) {
const uint8_t data[] = {
// label length beyond packet
0x30, 'x', 'x', 0x00,
// pointer offset beyond packet
0xc0, 0x20,
// pointer loop
0xc0, 0x08, 0xc0, 0x06,
// incorrect label type (currently supports only direct and pointer)
0x80, 0x00,
// truncated name (missing root label)
0x02, 'x', 'x',
};
DnsRecordParser parser(data, sizeof(data), 0, /*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(data + 0x00, &out));
EXPECT_EQ(0u, parser.ReadName(data + 0x04, &out));
EXPECT_EQ(0u, parser.ReadName(data + 0x08, &out));
EXPECT_EQ(0u, parser.ReadName(data + 0x0a, &out));
EXPECT_EQ(0u, parser.ReadName(data + 0x0c, &out));
EXPECT_EQ(0u, parser.ReadName(data + 0x0e, &out));
}
// Returns an RFC 1034 style domain name with a length of |name_len|.
// Also writes the expected dotted string representation into |dotted_str|,
// which must be non-null.
std::vector<uint8_t> BuildRfc1034Name(const size_t name_len,
std::string* dotted_str) {
// Impossible length. If length not zero, need at least 2 to allow label
// length and label contents.
CHECK_NE(name_len, 1u);
CHECK(dotted_str != nullptr);
auto ChoosePrintableCharLambda = [](uint8_t n) { return n % 26 + 'A'; };
const size_t max_label_len = 63;
std::vector<uint8_t> data;
dotted_str->clear();
while (data.size() < name_len) {
// Compute the size of the next label.
//
// No need to account for next label length because the final zero length is
// not considered included in overall length.
size_t label_len = std::min(name_len - data.size() - 1, max_label_len);
// Need to ensure the remainder is not 1 because that would leave room for a
// label length but not a label.
if (name_len - data.size() - label_len - 1 == 1) {
CHECK_GT(label_len, 1u);
label_len -= 1;
}
// Write the length octet
data.push_back(label_len);
// Write |label_len| bytes of label data
const size_t size_with_label = data.size() + label_len;
while (data.size() < size_with_label) {
const uint8_t chr = ChoosePrintableCharLambda(data.size());
data.push_back(chr);
dotted_str->push_back(chr);
CHECK(data.size() <= name_len);
}
// Write a trailing dot after every label
dotted_str->push_back('.');
}
// Omit the final dot
if (!dotted_str->empty())
dotted_str->pop_back();
CHECK(data.size() == name_len);
// Final zero-length label (not considered included in overall length).
data.push_back(0);
return data;
}
TEST(DnsRecordParserTest, ReadNameGoodLength) {
const size_t name_len_cases[] = {2, 10, 40, 250, 254, 255};
for (auto name_len : name_len_cases) {
std::string expected_name;
const std::vector<uint8_t> data_vector =
BuildRfc1034Name(name_len, &expected_name);
ASSERT_EQ(data_vector.size(), name_len + 1);
const uint8_t* data = data_vector.data();
DnsRecordParser parser(data, data_vector.size(), 0, /*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(data_vector.size(), parser.ReadName(data, &out));
EXPECT_EQ(expected_name, out);
}
}
// Tests against incorrect name length validation, which is anti-pattern #3 from
// the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, ReadNameTooLongFail) {
const size_t name_len_cases[] = {256, 257, 258, 300, 10000};
for (auto name_len : name_len_cases) {
std::string expected_name;
const std::vector<uint8_t> data_vector =
BuildRfc1034Name(name_len, &expected_name);
ASSERT_EQ(data_vector.size(), name_len + 1);
const uint8_t* data = data_vector.data();
DnsRecordParser parser(data, data_vector.size(), 0, /*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(data, &out));
}
}
// Tests against incorrect name compression pointer validation, which is anti-
// pattern #6 from the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectsNamesWithLoops) {
const char kData[] =
"\003www\007example\300\031" // www.example with pointer to byte 25
"aaaaaaaaaaa" // Garbage data to spread things out.
"\003foo\300\004"; // foo with pointer to byte 4.
DnsRecordParser parser(kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(kData, &out));
}
// Tests against incorrect name compression pointer validation, which is anti-
// pattern #6 from the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectsNamesPointingOutsideData) {
const char kData[] =
"\003www\007example\300\031"; // www.example with pointer to byte 25
DnsRecordParser parser(kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(kData, &out));
}
TEST(DnsRecordParserTest, ParsesValidPointer) {
const char kData[] =
"\003www\007example\300\022" // www.example with pointer to byte 25.
"aaaa" // Garbage data to spread things out.
"\004test\000"; // .test
DnsRecordParser parser(kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(14u, parser.ReadName(kData, &out));
EXPECT_EQ(out, "www.example.test");
}
// Per RFC 1035, section 4.1.4, the first 2 bits of a DNS name label determine
// if it is a length label (if the bytes are 00) or a pointer label (if the
// bytes are 11). It is a common DNS parsing bug to treat 01 or 10 as pointer
// labels, but these are reserved and invalid. Such labels should always result
// in DnsRecordParser rejecting the name.
//
// Tests against incorrect name compression pointer validation, which is anti-
// pattern #6 from the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectsNamesWithInvalidLabelTypeAsPointer) {
const char kData[] =
"\003www\007example\200\022" // www.example with invalid label as pointer
"aaaa" // Garbage data to spread things out.
"\004test\000"; // .test
DnsRecordParser parser(kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(kData, &out));
}
// Per RFC 1035, section 4.1.4, the first 2 bits of a DNS name label determine
// if it is a length label (if the bytes are 00) or a pointer label (if the
// bytes are 11). Such labels should always result in DnsRecordParser rejecting
// the name.
//
// Tests against incorrect name compression pointer validation, which is anti-
// pattern #6 from the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectsNamesWithInvalidLabelTypeAsLength) {
const char kData[] =
"\003www\007example\104" // www.example with invalid label as length
"test\000"; // test. (in case \104 is interpreted as length=4)
// Append a bunch of zeroes to the buffer in case \104 is interpreted as a
// long length.
std::string data(kData, sizeof(kData) - 1);
data.append(256, '\000');
DnsRecordParser parser(data.data(), data.size(), /*offset=*/0,
/*num_records=*/0);
ASSERT_TRUE(parser.IsValid());
std::string out;
EXPECT_EQ(0u, parser.ReadName(data.data(), &out));
}
TEST(DnsRecordParserTest, ReadRecord) {
const uint8_t data[] = {
// Type CNAME record.
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00, 0x00,
0x05, // TYPE is CNAME.
0x00, 0x01, // CLASS is IN.
0x00, 0x01, 0x24, 0x74, // TTL is 0x00012474.
0x00, 0x06, // RDLENGTH is 6 bytes.
0x03, 'f', 'o', 'o', // compressed name in record
0xc0, 0x00,
// Type A record.
0x03, 'b', 'a', 'r', // compressed owner name
0xc0, 0x00, 0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x20, 0x13, 0x55, // TTL is 0x00201355.
0x00, 0x04, // RDLENGTH is 4 bytes.
0x7f, 0x02, 0x04, 0x01, // IP is 127.2.4.1
};
std::string out;
DnsRecordParser parser(data, sizeof(data), 0, /*num_records=*/2);
DnsResourceRecord record;
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_EQ("example.com", record.name);
EXPECT_EQ(dns_protocol::kTypeCNAME, record.type);
EXPECT_EQ(dns_protocol::kClassIN, record.klass);
EXPECT_EQ(0x00012474u, record.ttl);
EXPECT_EQ(6u, record.rdata.length());
EXPECT_EQ(6u, parser.ReadName(record.rdata.data(), &out));
EXPECT_EQ("foo.example.com", out);
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_EQ("bar.example.com", record.name);
EXPECT_EQ(dns_protocol::kTypeA, record.type);
EXPECT_EQ(dns_protocol::kClassIN, record.klass);
EXPECT_EQ(0x00201355u, record.ttl);
EXPECT_EQ(4u, record.rdata.length());
EXPECT_EQ(base::StringPiece("\x7f\x02\x04\x01"), record.rdata);
EXPECT_TRUE(parser.AtEnd());
// Test truncated record.
parser = DnsRecordParser(data, sizeof(data) - 2, 0, /*num_records=*/2);
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_FALSE(parser.AtEnd());
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsRecordParserTest, ReadsRecordWithLongName) {
std::string dotted_name;
const std::vector<uint8_t> dns_name =
BuildRfc1034Name(dns_protocol::kMaxNameLength, &dotted_name);
std::string data(reinterpret_cast<const char*>(dns_name.data()),
dns_name.size());
data.append(
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x01", // 192.168.0.1
14);
DnsRecordParser parser(data.data(), data.size(), 0, /*num_records=*/1);
DnsResourceRecord record;
EXPECT_TRUE(parser.ReadRecord(&record));
}
// Tests against incorrect name length validation, which is anti-pattern #3 from
// the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectRecordWithTooLongName) {
std::string dotted_name;
const std::vector<uint8_t> dns_name =
BuildRfc1034Name(dns_protocol::kMaxNameLength + 1, &dotted_name);
std::string data(reinterpret_cast<const char*>(dns_name.data()),
dns_name.size());
data.append(
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x01", // 192.168.0.1
14);
DnsRecordParser parser(data.data(), data.size(), 0, /*num_records=*/1);
DnsResourceRecord record;
EXPECT_FALSE(parser.ReadRecord(&record));
}
// Test that a record cannot be parsed with a name extending past the end of the
// data.
// Tests against incorrect name length validation, which is anti-pattern #3 from
// the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectRecordWithNonendedName) {
const char kNonendedName[] = "\003www\006google\006www";
DnsRecordParser parser(kNonendedName, sizeof(kNonendedName) - 1, 0,
/*num_records=*/1);
DnsResourceRecord record;
EXPECT_FALSE(parser.ReadRecord(&record));
}
// Test that a record cannot be parsed with a name without final null
// termination. Parsing should assume the name has not ended and find the first
// byte of the TYPE field instead, making the remainder of the record
// unparsable.
// Tests against incorrect name null termination, which is anti-pattern #4 from
// the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsRecordParserTest, RejectRecordNameMissingNullTermination) {
const char kData[] =
"\003www\006google\004test" // Name without termination.
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x01"; // 192.168.0.1
DnsRecordParser parser(kData, sizeof(kData) - 1, 0, /*num_records=*/1);
DnsResourceRecord record;
EXPECT_FALSE(parser.ReadRecord(&record));
}
// Test that no more records can be parsed once the claimed number of records
// have been parsed.
TEST(DnsRecordParserTest, RejectReadingTooManyRecords) {
const char kData[] =
"\003www\006google\004test\000"
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x01" // 192.168.0.1
"\003www\010chromium\004test\000"
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x02"; // 192.168.0.2
DnsRecordParser parser(
kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/1); // Claim 1 record despite there being 2 in `kData`.
DnsResourceRecord record1;
EXPECT_TRUE(parser.ReadRecord(&record1));
// Expect second record cannot be parsed because only 1 was expected.
DnsResourceRecord record2;
EXPECT_FALSE(parser.ReadRecord(&record2));
}
// Test that no more records can be parsed once the end of the buffer is
// reached, even if more records are claimed.
TEST(DnsRecordParserTest, RejectReadingPastEnd) {
const char kData[] =
"\003www\006google\004test\000"
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x01" // 192.168.0.1
"\003www\010chromium\004test\000"
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x01\x51\x80" // TTL=1 day
"\x00\x04" // RDLENGTH=4 bytes
"\xc0\xa8\x00\x02"; // 192.168.0.2
DnsRecordParser parser(
kData, /*length=*/sizeof(kData) - 1, /*offset=*/0,
/*num_records=*/3); // Claim 3 record despite there being 2 in `kData`.
DnsResourceRecord record;
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsResponseTest, InitParse) {
// This includes \0 at the end.
const char qname_data[] = "\x0A""codereview""\x08""chromium""\x03""org";
const base::StringPiece qname(qname_data, sizeof(qname_data));
// Compilers want to copy when binding temporary to const &, so must use heap.
std::unique_ptr<DnsQuery> query(
new DnsQuery(0xcafe, qname, dns_protocol::kTypeA));
const uint8_t response_data[] = {
// Header
0xca, 0xfe, // ID
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x01, // 1 question
0x00, 0x02, // 2 RRs (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x01, // 1 additional RRs
// Question
// This part is echoed back from the respective query.
0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h',
'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00,
0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
// Answer 1
0xc0, 0x0c, // NAME is a pointer to name in Question section.
0x00, 0x05, // TYPE is CNAME.
0x00, 0x01, // CLASS is IN.
0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
0x24, 0x74, 0x00, 0x12, // RDLENGTH is 18 bytes.
// ghs.l.google.com in DNS format.
0x03, 'g', 'h', 's', 0x01, 'l', 0x06, 'g', 'o', 'o', 'g', 'l', 'e', 0x03,
'c', 'o', 'm', 0x00,
// Answer 2
0xc0, 0x35, // NAME is a pointer to name in Answer 1.
0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, // TTL (4 bytes) is 53 seconds.
0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes.
0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
0x5f, 0x79,
// Additional 1
0x00, // NAME is empty (root domain).
0x00, 0x29, // TYPE is OPT.
0x10, 0x00, // CLASS is max UDP payload size (4096).
0x00, 0x00, 0x00, 0x00, // TTL (4 bytes) is rcode, version and flags.
0x00, 0x08, // RDLENGTH
0x00, 0xFF, // OPT code
0x00, 0x04, // OPT data size
0xDE, 0xAD, 0xBE, 0xEF // OPT data
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_FALSE(resp.id());
// Reject too short.
EXPECT_FALSE(resp.InitParse(query->io_buffer()->size() - 1, *query));
EXPECT_FALSE(resp.IsValid());
EXPECT_FALSE(resp.id());
// Reject wrong id.
std::unique_ptr<DnsQuery> other_query = query->CloneWithNewId(0xbeef);
EXPECT_FALSE(resp.InitParse(sizeof(response_data), *other_query));
EXPECT_FALSE(resp.IsValid());
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
// Reject wrong question.
std::unique_ptr<DnsQuery> wrong_query(
new DnsQuery(0xcafe, qname, dns_protocol::kTypeCNAME));
EXPECT_FALSE(resp.InitParse(sizeof(response_data), *wrong_query));
EXPECT_FALSE(resp.IsValid());
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
// Accept matching question.
EXPECT_TRUE(resp.InitParse(sizeof(response_data), *query));
EXPECT_TRUE(resp.IsValid());
// Check header access.
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
EXPECT_EQ(0x8180, resp.flags());
EXPECT_EQ(0x0, resp.rcode());
EXPECT_EQ(2u, resp.answer_count());
EXPECT_EQ(1u, resp.additional_answer_count());
// Check question access.
std::string response_qname;
ASSERT_TRUE(DNSDomainFromDot(resp.GetSingleDottedName(), &response_qname));
EXPECT_EQ(query->qname(), response_qname);
EXPECT_EQ(query->qtype(), resp.GetSingleQType());
EXPECT_EQ("codereview.chromium.org", resp.GetSingleDottedName());
DnsResourceRecord record;
DnsRecordParser parser = resp.Parser();
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_TRUE(parser.AtEnd());
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsResponseTest, InitParseInvalidFlags) {
// This includes \0 at the end.
const char qname_data[] =
"\x0A"
"codereview"
"\x08"
"chromium"
"\x03"
"org";
const base::StringPiece qname(qname_data, sizeof(qname_data));
// Compilers want to copy when binding temporary to const &, so must use heap.
std::unique_ptr<DnsQuery> query(
new DnsQuery(0xcafe, qname, dns_protocol::kTypeA));
const uint8_t response_data[] = {
// Header
0xca, 0xfe, // ID
0x01, 0x80, // RA, no error. Note the absence of the required QR bit.
0x00, 0x01, // 1 question
0x00, 0x01, // 1 RRs (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// Question
// This part is echoed back from the respective query.
0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h',
'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00,
0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
// Answer 1
0xc0, 0x0c, // NAME is a pointer to name in Question section.
0x00, 0x05, // TYPE is CNAME.
0x00, 0x01, // CLASS is IN.
0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
0x24, 0x74, 0x00, 0x12, // RDLENGTH is 18 bytes.
// ghs.l.google.com in DNS format.
0x03, 'g', 'h', 's', 0x01, 'l', 0x06, 'g', 'o', 'o', 'g', 'l', 'e', 0x03,
'c', 'o', 'm', 0x00,
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_FALSE(resp.InitParse(sizeof(response_data), *query));
EXPECT_FALSE(resp.IsValid());
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
}
TEST(DnsResponseTest, InitParseRejectsResponseWithoutQuestions) {
const char kResponse[] =
"\x02\x45" // ID=581
"\x81\x80" // Standard query response, RA, no error
"\x00\x00" // 0 questions
"\x00\x01" // 1 answers
"\x00\x00" // 0 authority records
"\x00\x00" // 0 additional records
"\003www\006google\004test\000" // www.google.test
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\x00\x00\x2a\x30" // TTL=3 hours
"\x00\x04" // RDLENGTH=4 bytes
"\xa0\xa0\xa0\xa0"; // 10.10.10.10
DnsResponse resp;
memcpy(resp.io_buffer()->data(), kResponse, sizeof(kResponse) - 1);
// Validate that the response is fine if not matching against a query.
ASSERT_TRUE(resp.InitParseWithoutQuery(sizeof(kResponse) - 1));
const char kQueryName[] = "\003www\006google\004test\000";
DnsQuery query(
/*id=*/581, base::StringPiece(kQueryName, sizeof(kQueryName) - 1),
dns_protocol::kTypeA);
EXPECT_FALSE(resp.InitParse(sizeof(kResponse) - 1, query));
}
TEST(DnsResponseTest, InitParseRejectsResponseWithTooManyQuestions) {
const char kResponse[] =
"\x02\x46" // ID=582
"\x81\x80" // Standard query response, RA, no error
"\x00\x02" // 2 questions
"\x00\x00" // 0 answers
"\x00\x00" // 0 authority records
"\x00\x00" // 0 additional records
"\003www\006google\004test\000" // www.google.test
"\x00\x01" // TYPE=A
"\x00\x01" // CLASS=IN
"\003www\010chromium\004test\000" // www.chromium.test
"\x00\x01" // TYPE=A
"\x00\x01"; // CLASS=IN
DnsResponse resp;
memcpy(resp.io_buffer()->data(), kResponse, sizeof(kResponse) - 1);
// Validate that the response is fine if not matching against a query.
ASSERT_TRUE(resp.InitParseWithoutQuery(sizeof(kResponse) - 1));
const char kQueryName[] = "\003www\006google\004test\000";
DnsQuery query(
/*id=*/582, base::StringPiece(kQueryName, sizeof(kQueryName) - 1),
dns_protocol::kTypeA);
EXPECT_FALSE(resp.InitParse(sizeof(kResponse) - 1, query));
}
TEST(DnsResponseTest, InitParseWithoutQuery) {
DnsResponse resp;
memcpy(resp.io_buffer()->data(), kT0ResponseDatagram,
sizeof(kT0ResponseDatagram));
// Accept matching question.
EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(kT0ResponseDatagram)));
EXPECT_TRUE(resp.IsValid());
// Check header access.
EXPECT_EQ(0x8180, resp.flags());
EXPECT_EQ(0x0, resp.rcode());
EXPECT_EQ(kT0RecordCount, resp.answer_count());
// Check question access.
EXPECT_EQ(kT0Qtype, resp.GetSingleQType());
EXPECT_EQ(kT0HostName, resp.GetSingleDottedName());
DnsResourceRecord record;
DnsRecordParser parser = resp.Parser();
for (unsigned i = 0; i < kT0RecordCount; i ++) {
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
}
EXPECT_TRUE(parser.AtEnd());
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsResponseTest, InitParseWithoutQueryNoQuestions) {
const uint8_t response_data[] = {
// Header
0xca, 0xfe, // ID
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No question
0x00, 0x01, // 2 RRs (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// Answer 1
0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h',
'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00,
0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, // TTL (4 bytes) is 53 seconds.
0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes.
0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
0x5f, 0x79,
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data)));
// Check header access.
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
EXPECT_EQ(0x8180, resp.flags());
EXPECT_EQ(0x0, resp.rcode());
EXPECT_EQ(0u, resp.question_count());
EXPECT_EQ(0x1u, resp.answer_count());
EXPECT_THAT(resp.dotted_qnames(), testing::IsEmpty());
EXPECT_THAT(resp.qtypes(), testing::IsEmpty());
DnsResourceRecord record;
DnsRecordParser parser = resp.Parser();
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_EQ("codereview.chromium.org", record.name);
EXPECT_EQ(0x00000035u, record.ttl);
EXPECT_EQ(dns_protocol::kTypeA, record.type);
EXPECT_TRUE(parser.AtEnd());
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsResponseTest, InitParseWithoutQueryInvalidFlags) {
const uint8_t response_data[] = {
// Header
0xca, 0xfe, // ID
0x01, 0x80, // RA, no error. Note the absence of the required QR bit.
0x00, 0x00, // No question
0x00, 0x01, // 2 RRs (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// Answer 1
0x0a, 'c', 'o', 'd', 'e', 'r', 'e', 'v', 'i', 'e', 'w', 0x08, 'c', 'h',
'r', 'o', 'm', 'i', 'u', 'm', 0x03, 'o', 'r', 'g', 0x00, 0x00,
0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, // TTL (4 bytes) is 53 seconds.
0x00, 0x35, 0x00, 0x04, // RDLENGTH is 4 bytes.
0x4a, 0x7d, // RDATA is the IP: 74.125.95.121
0x5f, 0x79,
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_FALSE(resp.InitParseWithoutQuery(sizeof(response_data)));
EXPECT_THAT(resp.id(), testing::Optional(0xcafe));
}
TEST(DnsResponseTest, InitParseWithoutQueryTwoQuestions) {
const uint8_t response_data[] = {
// Header
0xca,
0xfe, // ID
0x81,
0x80, // Standard query response, RA, no error
0x00,
0x02, // 2 questions
0x00,
0x01, // 2 RRs (answers)
0x00,
0x00, // 0 authority RRs
0x00,
0x00, // 0 additional RRs
// Question 1
0x0a,
'c',
'o',
'd',
'e',
'r',
'e',
'v',
'i',
'e',
'w',
0x08,
'c',
'h',
'r',
'o',
'm',
'i',
'u',
'm',
0x03,
'o',
'r',
'g',
0x00,
0x00,
0x01, // TYPE is A.
0x00,
0x01, // CLASS is IN.
// Question 2
0x0b,
'c',
'o',
'd',
'e',
'r',
'e',
'v',
'i',
'e',
'w',
'2',
0xc0,
0x17, // pointer to "chromium.org"
0x00,
0x01, // TYPE is A.
0x00,
0x01, // CLASS is IN.
// Answer 1
0xc0,
0x0c, // NAME is a pointer to name in Question section.
0x00,
0x01, // TYPE is A.
0x00,
0x01, // CLASS is IN.
0x00,
0x00, // TTL (4 bytes) is 53 seconds.
0x00,
0x35,
0x00,
0x04, // RDLENGTH is 4 bytes.
0x4a,
0x7d, // RDATA is the IP: 74.125.95.121
0x5f,
0x79,
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_TRUE(resp.InitParseWithoutQuery(sizeof(response_data)));
// Check header access.
EXPECT_EQ(0x8180, resp.flags());
EXPECT_EQ(0x0, resp.rcode());
EXPECT_EQ(2u, resp.question_count());
EXPECT_EQ(0x01u, resp.answer_count());
EXPECT_THAT(resp.dotted_qnames(),
testing::ElementsAre("codereview.chromium.org",
"codereview2.chromium.org"));
EXPECT_THAT(resp.qtypes(),
testing::ElementsAre(dns_protocol::kTypeA, dns_protocol::kTypeA));
DnsResourceRecord record;
DnsRecordParser parser = resp.Parser();
EXPECT_FALSE(parser.AtEnd());
EXPECT_TRUE(parser.ReadRecord(&record));
EXPECT_EQ("codereview.chromium.org", record.name);
EXPECT_EQ(0x35u, record.ttl);
EXPECT_EQ(dns_protocol::kTypeA, record.type);
EXPECT_TRUE(parser.AtEnd());
EXPECT_FALSE(parser.ReadRecord(&record));
}
TEST(DnsResponseTest, InitParseWithoutQueryPacketTooShort) {
const uint8_t response_data[] = {
// Header
0xca, 0xfe, // ID
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No question
};
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data, sizeof(response_data));
EXPECT_FALSE(resp.InitParseWithoutQuery(sizeof(response_data)));
}
TEST(DnsResponseTest, InitParseAllowsQuestionWithLongName) {
const char kResponseHeader[] =
"\x02\x45" // ID=581
"\x81\x80" // Standard query response, RA, no error
"\x00\x01" // 1 question
"\x00\x00" // 0 answers
"\x00\x00" // 0 authority records
"\x00\x00"; // 0 additional records
std::string dotted_name;
const std::vector<uint8_t> dns_name =
BuildRfc1034Name(dns_protocol::kMaxNameLength, &dotted_name);
std::string response_data(kResponseHeader, sizeof(kResponseHeader) - 1);
response_data.append(reinterpret_cast<const char*>(dns_name.data()),
dns_name.size());
response_data.append(
"\x00\x01" // TYPE=A
"\x00\x01", // CLASS=IN)
4);
DnsResponse resp1;
memcpy(resp1.io_buffer()->data(), response_data.data(), response_data.size());
EXPECT_TRUE(resp1.InitParseWithoutQuery(response_data.size()));
DnsQuery query(
581,
base::StringPiece(reinterpret_cast<const char*>(dns_name.data()),
dns_name.size()),
dns_protocol::kTypeA);
DnsResponse resp2(resp1.io_buffer(), response_data.size());
EXPECT_TRUE(resp2.InitParse(response_data.size(), query));
}
// Tests against incorrect name length validation, which is anti-pattern #3 from
// the "NAME:WRECK" report:
// https://www.forescout.com/company/resources/namewreck-breaking-and-fixing-dns-implementations/
TEST(DnsResponseTest, InitParseRejectsQuestionWithTooLongName) {
const char kResponseHeader[] =
"\x02\x45" // ID=581
"\x81\x80" // Standard query response, RA, no error
"\x00\x01" // 1 question
"\x00\x00" // 0 answers
"\x00\x00" // 0 authority records
"\x00\x00"; // 0 additional records
std::string dotted_name;
const std::vector<uint8_t> dns_name =
BuildRfc1034Name(dns_protocol::kMaxNameLength + 1, &dotted_name);
std::string response_data(kResponseHeader, sizeof(kResponseHeader) - 1);
response_data.append(reinterpret_cast<const char*>(dns_name.data()),
dns_name.size());
response_data.append(
"\x00\x01" // TYPE=A
"\x00\x01", // CLASS=IN)
4);
DnsResponse resp;
memcpy(resp.io_buffer()->data(), response_data.data(), response_data.size());