]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fuzzing and benchmarking for dns_name_fromwire()
authorTony Finch <fanf@isc.org>
Mon, 7 Nov 2022 16:22:48 +0000 (16:22 +0000)
committerTony Finch <dot@dotat.at>
Thu, 17 Nov 2022 08:45:17 +0000 (08:45 +0000)
Since this is very sensitive code which has often had security
problems in many DNS implementations, it needs a decent amount of
validation. This fuzzer ensures that the new code has the same output
as the old code, and that it doesn't take longer than a second.

The benchmark uses the fuzzer's copy of the old dns_name_fromwire()
code to compare a number of scenarios: many compression pointers, many
labels, long labels, random data, with/without downcasing.

55 files changed:
fuzz/.gitignore
fuzz/Makefile.am
fuzz/dns_name_fromwire.c [new file with mode: 0644]
fuzz/dns_name_fromwire.in/00b28ff06b788b9b67c6b259800f404f9f3761fd [new file with mode: 0644]
fuzz/dns_name_fromwire.in/02440c9e09b9ccb3c36d1ae3b1dc395d39dd2e61 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/02a01a03cc76d29a0d3f819e9f6e95266f4ee4eb [new file with mode: 0644]
fuzz/dns_name_fromwire.in/03de4865464d9fee5cf614c2e8196cb0246b60f3 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/0c932286228aafdb4bbcf9d39f4554d814e7c1cb [new file with mode: 0644]
fuzz/dns_name_fromwire.in/12248b2bb3063f4f6aca8ec227aa26f24299e53b [new file with mode: 0644]
fuzz/dns_name_fromwire.in/2c17e4e73e0ee81d3ed7279f3fbc51d35e8837a1 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/302f0fe41e58a99c91f22062cda18d4683dc702f [new file with mode: 0644]
fuzz/dns_name_fromwire.in/43f90bb183e988637abbcbc50be4a89ebff60d9a [new file with mode: 0644]
fuzz/dns_name_fromwire.in/448daf1db0846c67ecb7a533361647cb41e5dce2 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/477cdb78a23d07e2fc8103c4c79c9a545bcec06b [new file with mode: 0644]
fuzz/dns_name_fromwire.in/4b345f6450981c1ed938081331eeabd03598cb20 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/5821576d8de31746675cc0ea7f9e652438070dfe [new file with mode: 0644]
fuzz/dns_name_fromwire.in/608fa82ab128dfb43c9a064bfab677d9b5f3f72b [new file with mode: 0644]
fuzz/dns_name_fromwire.in/61c70af9c1b0588393243703eb472271290f88eb [new file with mode: 0644]
fuzz/dns_name_fromwire.in/680357a1752af34a7f882cced6b1e5fd8ecc4bcc [new file with mode: 0644]
fuzz/dns_name_fromwire.in/69abe6811b3437087b53d4c4e9157d4ba7a0890b [new file with mode: 0644]
fuzz/dns_name_fromwire.in/69d86c5a071a2d76679d54da225e955c35c40db1 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/6cd63cd014fa252d9f5e62129e05dc6cbd75a7e3 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/7c20e8ac688f92b55099a7f63ff2a4f4bcd7704a [new file with mode: 0644]
fuzz/dns_name_fromwire.in/7d8a23740ae15d7d3dfde25ad1538ae9b9d52265 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/88188ee25030661f21a4dacca02caeb7eff34235 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/93dccf807dc4df3bfc1711ef3ad3bbd65a7d8c67 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/955cad73af85bdac5a701aea20a086c75585ca74 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/9a279b27d765ca2eccc67e1fc8bafb0fa8702208 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/9d30975bf630a3203ed71154257ebcfc19d04067 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/9feb4af1c2bb4444e9417099b21c6b25f7221dee [new file with mode: 0644]
fuzz/dns_name_fromwire.in/a3f75fec735667f1c1218f53e073f25586dc0813 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/b473a861dafebb3cd6ca6b92609da9e40eeea68e [new file with mode: 0644]
fuzz/dns_name_fromwire.in/b8811cd9f785b9fb458236875fda90d048383689 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/bf546f247bb7a3672a04bb4c9e3b5049f981d435 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/bf7b68e9cc0467f7bbbfe1dc0dfd7c3124b3695c [new file with mode: 0644]
fuzz/dns_name_fromwire.in/bfb829dd666f7fb4c6d9ae55f58503967cf1b53b [new file with mode: 0644]
fuzz/dns_name_fromwire.in/c351ad42ccc7485a0ad6e1ae8cc07d66ee9b045a [new file with mode: 0644]
fuzz/dns_name_fromwire.in/cc6cba815d062e0e250463aad2113fdcafd48571 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/db6cca49e957261d7a262fbf6dafcac01353f1e4 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/de4e7188e5540bd15eb83ddb5a784c6dc35d8d94 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/df58248c414f342c81e056b40bee12d17a08bf61 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/df7467bb9625092ba9d23c0d8e4f29c7f4e23dd1 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/e05d045edfa8ebe33c73b6459e7d704e0c958c74 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/e23b6fdeadc0379b095bc1ffb8a1d2101ebe76b1 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/e333716351a0ded128bbb67ba46b5017a16ae6bf [new file with mode: 0644]
fuzz/dns_name_fromwire.in/e52912805d2e9212d53a46426e160813124be117 [new file with mode: 0644]
fuzz/dns_name_fromwire.in/f4a59b7257dbd0777e57e6e25689d374f4d771bc [new file with mode: 0644]
fuzz/dns_name_fromwire.in/fc712b19cbed4a8b4db3226dfbfdf3fb7be7690a [new file with mode: 0644]
fuzz/dns_name_fromwire.in/fda8ce1c09f3e385a5c0798f9ee158a4ca7d449f [new file with mode: 0644]
fuzz/dns_name_fromwire.in/fe38c27050137b9ce57189c681beb2b59a539a98 [new file with mode: 0644]
fuzz/old.c [new file with mode: 0644]
fuzz/old.h [new file with mode: 0644]
tests/bench/.gitignore
tests/bench/Makefile.am
tests/bench/dns_name_fromwire.c [new file with mode: 0644]

index 7d03848c63827ca3218c103c95f75fa4eb868a06..fe9ca76c5dee3a50b703cf3051104fe41c79b492 100644 (file)
@@ -4,6 +4,7 @@
 /dns_message_checksig
 /dns_message_parse
 /dns_name_fromtext_target
+/dns_name_fromwire
 /dns_rdata_fromtext
 /dns_rdata_fromwire_text
 /isc_lex_getmastertoken
index eb4a2f4ad642fc29b3af69c54147d31f4799a2e8..d265c6c05f8ba04cc48aaaf25636bdab30c6c546 100644 (file)
@@ -26,6 +26,7 @@ check_PROGRAMS =                      \
        dns_message_checksig            \
        dns_message_parse               \
        dns_name_fromtext_target        \
+       dns_name_fromwire               \
        dns_rdata_fromtext              \
        dns_rdata_fromwire_text         \
        isc_lex_getmastertoken          \
@@ -36,11 +37,17 @@ EXTRA_DIST =                                \
        dns_message_checksig.in         \
        dns_message_parse.in            \
        dns_name_fromtext_target.in     \
+       dns_name_fromwire.in            \
        dns_rdata_fromtext.in           \
        dns_rdata_fromwire_text.in      \
        isc_lex_getmastertoken.in       \
        isc_lex_gettoken.in
 
+dns_name_fromwire_SOURCES =            \
+       dns_name_fromwire.c             \
+       old.c                           \
+       old.h
+
 TESTS = $(check_PROGRAMS)
 
 if HAVE_FUZZ_LOG_COMPILER
diff --git a/fuzz/dns_name_fromwire.c b/fuzz/dns_name_fromwire.c
new file mode 100644 (file)
index 0000000..ec9e0d2
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <isc/ascii.h>
+#include <isc/buffer.h>
+#include <isc/util.h>
+
+#include <dns/compress.h>
+#include <dns/fixedname.h>
+#include <dns/name.h>
+
+#include "fuzz.h"
+#include "old.h"
+
+bool debug = false;
+
+int
+LLVMFuzzerInitialize(int *argc __attribute__((unused)),
+                    char ***argv __attribute__((unused))) {
+       return (0);
+}
+
+int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+       isc_result_t new_result;
+       isc_result_t old_result;
+       dns_fixedname_t new_fixed;
+       dns_fixedname_t old_fixed;
+       dns_name_t *new_name = dns_fixedname_initname(&new_fixed);
+       dns_name_t *old_name = dns_fixedname_initname(&old_fixed);
+       uint8_t *new_offsets;
+       uint8_t *old_offsets;
+       dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
+       isc_buffer_t new_buf;
+       isc_buffer_t old_buf;
+
+       /*
+        * Output buffers may be partially used or undersized.
+        */
+       if (size > 0) {
+               uint8_t add = *data++;
+               size--;
+               isc_buffer_add(&new_fixed.buffer, add);
+               isc_buffer_add(&old_fixed.buffer, add);
+       }
+
+       /*
+        * timeout faster if we hit a pointer loop
+        */
+       alarm(1);
+
+       /*
+        * We shift forward by half the input data to make an area
+        * that pointers can refer back to.
+        */
+
+       isc_buffer_constinit(&new_buf, data, size);
+       isc_buffer_add(&new_buf, size);
+       isc_buffer_setactive(&new_buf, size);
+       isc_buffer_forward(&new_buf, size / 2);
+       new_result = dns_name_fromwire(new_name, &new_buf, dctx, 0, NULL);
+
+       isc_buffer_constinit(&old_buf, data, size);
+       isc_buffer_add(&old_buf, size);
+       isc_buffer_setactive(&old_buf, size);
+       isc_buffer_forward(&old_buf, size / 2);
+       old_result = old_name_fromwire(old_name, &old_buf, dctx, 0, NULL);
+
+       REQUIRE(new_result == old_result);
+       REQUIRE(dns_name_equal(new_name, old_name));
+       REQUIRE(new_name->labels == old_name->labels);
+
+       new_offsets = new_name->offsets;
+       old_offsets = old_name->offsets;
+       REQUIRE(new_offsets != NULL && old_offsets != NULL);
+       REQUIRE(memcmp(new_offsets, old_offsets, old_name->labels) == 0);
+
+       REQUIRE(new_fixed.buffer.current == old_fixed.buffer.current);
+       REQUIRE(new_fixed.buffer.active == old_fixed.buffer.active);
+       REQUIRE(new_fixed.buffer.used == old_fixed.buffer.used);
+       REQUIRE(new_fixed.buffer.length == old_fixed.buffer.length);
+
+       REQUIRE(new_buf.base == old_buf.base);
+       REQUIRE(new_buf.current == old_buf.current);
+       REQUIRE(new_buf.active == old_buf.active);
+       REQUIRE(new_buf.used == old_buf.used);
+       REQUIRE(new_buf.length == old_buf.length);
+
+       return (0);
+}
diff --git a/fuzz/dns_name_fromwire.in/00b28ff06b788b9b67c6b259800f404f9f3761fd b/fuzz/dns_name_fromwire.in/00b28ff06b788b9b67c6b259800f404f9f3761fd
new file mode 100644 (file)
index 0000000..59f144e
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/00b28ff06b788b9b67c6b259800f404f9f3761fd differ
diff --git a/fuzz/dns_name_fromwire.in/02440c9e09b9ccb3c36d1ae3b1dc395d39dd2e61 b/fuzz/dns_name_fromwire.in/02440c9e09b9ccb3c36d1ae3b1dc395d39dd2e61
new file mode 100644 (file)
index 0000000..fd53418
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/02440c9e09b9ccb3c36d1ae3b1dc395d39dd2e61 differ
diff --git a/fuzz/dns_name_fromwire.in/02a01a03cc76d29a0d3f819e9f6e95266f4ee4eb b/fuzz/dns_name_fromwire.in/02a01a03cc76d29a0d3f819e9f6e95266f4ee4eb
new file mode 100644 (file)
index 0000000..93a3f27
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/02a01a03cc76d29a0d3f819e9f6e95266f4ee4eb differ
diff --git a/fuzz/dns_name_fromwire.in/03de4865464d9fee5cf614c2e8196cb0246b60f3 b/fuzz/dns_name_fromwire.in/03de4865464d9fee5cf614c2e8196cb0246b60f3
new file mode 100644 (file)
index 0000000..0231663
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/03de4865464d9fee5cf614c2e8196cb0246b60f3 differ
diff --git a/fuzz/dns_name_fromwire.in/0c932286228aafdb4bbcf9d39f4554d814e7c1cb b/fuzz/dns_name_fromwire.in/0c932286228aafdb4bbcf9d39f4554d814e7c1cb
new file mode 100644 (file)
index 0000000..b40dc8d
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/0c932286228aafdb4bbcf9d39f4554d814e7c1cb differ
diff --git a/fuzz/dns_name_fromwire.in/12248b2bb3063f4f6aca8ec227aa26f24299e53b b/fuzz/dns_name_fromwire.in/12248b2bb3063f4f6aca8ec227aa26f24299e53b
new file mode 100644 (file)
index 0000000..a8fd239
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/12248b2bb3063f4f6aca8ec227aa26f24299e53b differ
diff --git a/fuzz/dns_name_fromwire.in/2c17e4e73e0ee81d3ed7279f3fbc51d35e8837a1 b/fuzz/dns_name_fromwire.in/2c17e4e73e0ee81d3ed7279f3fbc51d35e8837a1
new file mode 100644 (file)
index 0000000..7fe02ee
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/2c17e4e73e0ee81d3ed7279f3fbc51d35e8837a1 differ
diff --git a/fuzz/dns_name_fromwire.in/302f0fe41e58a99c91f22062cda18d4683dc702f b/fuzz/dns_name_fromwire.in/302f0fe41e58a99c91f22062cda18d4683dc702f
new file mode 100644 (file)
index 0000000..0eba245
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/302f0fe41e58a99c91f22062cda18d4683dc702f differ
diff --git a/fuzz/dns_name_fromwire.in/43f90bb183e988637abbcbc50be4a89ebff60d9a b/fuzz/dns_name_fromwire.in/43f90bb183e988637abbcbc50be4a89ebff60d9a
new file mode 100644 (file)
index 0000000..1b7df00
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/43f90bb183e988637abbcbc50be4a89ebff60d9a differ
diff --git a/fuzz/dns_name_fromwire.in/448daf1db0846c67ecb7a533361647cb41e5dce2 b/fuzz/dns_name_fromwire.in/448daf1db0846c67ecb7a533361647cb41e5dce2
new file mode 100644 (file)
index 0000000..b3acc83
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/448daf1db0846c67ecb7a533361647cb41e5dce2 differ
diff --git a/fuzz/dns_name_fromwire.in/477cdb78a23d07e2fc8103c4c79c9a545bcec06b b/fuzz/dns_name_fromwire.in/477cdb78a23d07e2fc8103c4c79c9a545bcec06b
new file mode 100644 (file)
index 0000000..3c48f31
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/477cdb78a23d07e2fc8103c4c79c9a545bcec06b differ
diff --git a/fuzz/dns_name_fromwire.in/4b345f6450981c1ed938081331eeabd03598cb20 b/fuzz/dns_name_fromwire.in/4b345f6450981c1ed938081331eeabd03598cb20
new file mode 100644 (file)
index 0000000..863a376
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/4b345f6450981c1ed938081331eeabd03598cb20 differ
diff --git a/fuzz/dns_name_fromwire.in/5821576d8de31746675cc0ea7f9e652438070dfe b/fuzz/dns_name_fromwire.in/5821576d8de31746675cc0ea7f9e652438070dfe
new file mode 100644 (file)
index 0000000..6a0ea0d
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/5821576d8de31746675cc0ea7f9e652438070dfe differ
diff --git a/fuzz/dns_name_fromwire.in/608fa82ab128dfb43c9a064bfab677d9b5f3f72b b/fuzz/dns_name_fromwire.in/608fa82ab128dfb43c9a064bfab677d9b5f3f72b
new file mode 100644 (file)
index 0000000..f52278d
--- /dev/null
@@ -0,0 +1 @@
+\8a0
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/61c70af9c1b0588393243703eb472271290f88eb b/fuzz/dns_name_fromwire.in/61c70af9c1b0588393243703eb472271290f88eb
new file mode 100644 (file)
index 0000000..84016db
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/61c70af9c1b0588393243703eb472271290f88eb differ
diff --git a/fuzz/dns_name_fromwire.in/680357a1752af34a7f882cced6b1e5fd8ecc4bcc b/fuzz/dns_name_fromwire.in/680357a1752af34a7f882cced6b1e5fd8ecc4bcc
new file mode 100644 (file)
index 0000000..c102377
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/680357a1752af34a7f882cced6b1e5fd8ecc4bcc differ
diff --git a/fuzz/dns_name_fromwire.in/69abe6811b3437087b53d4c4e9157d4ba7a0890b b/fuzz/dns_name_fromwire.in/69abe6811b3437087b53d4c4e9157d4ba7a0890b
new file mode 100644 (file)
index 0000000..84f94d7
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/69abe6811b3437087b53d4c4e9157d4ba7a0890b differ
diff --git a/fuzz/dns_name_fromwire.in/69d86c5a071a2d76679d54da225e955c35c40db1 b/fuzz/dns_name_fromwire.in/69d86c5a071a2d76679d54da225e955c35c40db1
new file mode 100644 (file)
index 0000000..f5c9b43
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/69d86c5a071a2d76679d54da225e955c35c40db1 differ
diff --git a/fuzz/dns_name_fromwire.in/6cd63cd014fa252d9f5e62129e05dc6cbd75a7e3 b/fuzz/dns_name_fromwire.in/6cd63cd014fa252d9f5e62129e05dc6cbd75a7e3
new file mode 100644 (file)
index 0000000..3c3d30a
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/6cd63cd014fa252d9f5e62129e05dc6cbd75a7e3 differ
diff --git a/fuzz/dns_name_fromwire.in/7c20e8ac688f92b55099a7f63ff2a4f4bcd7704a b/fuzz/dns_name_fromwire.in/7c20e8ac688f92b55099a7f63ff2a4f4bcd7704a
new file mode 100644 (file)
index 0000000..89c5f62
--- /dev/null
@@ -0,0 +1 @@
+î0õw
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/7d8a23740ae15d7d3dfde25ad1538ae9b9d52265 b/fuzz/dns_name_fromwire.in/7d8a23740ae15d7d3dfde25ad1538ae9b9d52265
new file mode 100644 (file)
index 0000000..7bd539e
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/7d8a23740ae15d7d3dfde25ad1538ae9b9d52265 differ
diff --git a/fuzz/dns_name_fromwire.in/88188ee25030661f21a4dacca02caeb7eff34235 b/fuzz/dns_name_fromwire.in/88188ee25030661f21a4dacca02caeb7eff34235
new file mode 100644 (file)
index 0000000..3caa4b6
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/88188ee25030661f21a4dacca02caeb7eff34235 differ
diff --git a/fuzz/dns_name_fromwire.in/93dccf807dc4df3bfc1711ef3ad3bbd65a7d8c67 b/fuzz/dns_name_fromwire.in/93dccf807dc4df3bfc1711ef3ad3bbd65a7d8c67
new file mode 100644 (file)
index 0000000..bf69983
--- /dev/null
@@ -0,0 +1 @@
+0\8f
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/955cad73af85bdac5a701aea20a086c75585ca74 b/fuzz/dns_name_fromwire.in/955cad73af85bdac5a701aea20a086c75585ca74
new file mode 100644 (file)
index 0000000..1b30f72
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/955cad73af85bdac5a701aea20a086c75585ca74 differ
diff --git a/fuzz/dns_name_fromwire.in/9a279b27d765ca2eccc67e1fc8bafb0fa8702208 b/fuzz/dns_name_fromwire.in/9a279b27d765ca2eccc67e1fc8bafb0fa8702208
new file mode 100644 (file)
index 0000000..5f45b63
--- /dev/null
@@ -0,0 +1,2 @@
+\84ÿ
+\8b
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/9d30975bf630a3203ed71154257ebcfc19d04067 b/fuzz/dns_name_fromwire.in/9d30975bf630a3203ed71154257ebcfc19d04067
new file mode 100644 (file)
index 0000000..4c93dba
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/9d30975bf630a3203ed71154257ebcfc19d04067 differ
diff --git a/fuzz/dns_name_fromwire.in/9feb4af1c2bb4444e9417099b21c6b25f7221dee b/fuzz/dns_name_fromwire.in/9feb4af1c2bb4444e9417099b21c6b25f7221dee
new file mode 100644 (file)
index 0000000..dd011fc
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/9feb4af1c2bb4444e9417099b21c6b25f7221dee differ
diff --git a/fuzz/dns_name_fromwire.in/a3f75fec735667f1c1218f53e073f25586dc0813 b/fuzz/dns_name_fromwire.in/a3f75fec735667f1c1218f53e073f25586dc0813
new file mode 100644 (file)
index 0000000..620b966
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/a3f75fec735667f1c1218f53e073f25586dc0813 differ
diff --git a/fuzz/dns_name_fromwire.in/b473a861dafebb3cd6ca6b92609da9e40eeea68e b/fuzz/dns_name_fromwire.in/b473a861dafebb3cd6ca6b92609da9e40eeea68e
new file mode 100644 (file)
index 0000000..ced1f37
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/b473a861dafebb3cd6ca6b92609da9e40eeea68e differ
diff --git a/fuzz/dns_name_fromwire.in/b8811cd9f785b9fb458236875fda90d048383689 b/fuzz/dns_name_fromwire.in/b8811cd9f785b9fb458236875fda90d048383689
new file mode 100644 (file)
index 0000000..018d57f
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/b8811cd9f785b9fb458236875fda90d048383689 differ
diff --git a/fuzz/dns_name_fromwire.in/bf546f247bb7a3672a04bb4c9e3b5049f981d435 b/fuzz/dns_name_fromwire.in/bf546f247bb7a3672a04bb4c9e3b5049f981d435
new file mode 100644 (file)
index 0000000..c2b5898
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/bf546f247bb7a3672a04bb4c9e3b5049f981d435 differ
diff --git a/fuzz/dns_name_fromwire.in/bf7b68e9cc0467f7bbbfe1dc0dfd7c3124b3695c b/fuzz/dns_name_fromwire.in/bf7b68e9cc0467f7bbbfe1dc0dfd7c3124b3695c
new file mode 100644 (file)
index 0000000..081d7f1
--- /dev/null
@@ -0,0 +1 @@
+\8dõ
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/bfb829dd666f7fb4c6d9ae55f58503967cf1b53b b/fuzz/dns_name_fromwire.in/bfb829dd666f7fb4c6d9ae55f58503967cf1b53b
new file mode 100644 (file)
index 0000000..c2b8c7f
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/bfb829dd666f7fb4c6d9ae55f58503967cf1b53b differ
diff --git a/fuzz/dns_name_fromwire.in/c351ad42ccc7485a0ad6e1ae8cc07d66ee9b045a b/fuzz/dns_name_fromwire.in/c351ad42ccc7485a0ad6e1ae8cc07d66ee9b045a
new file mode 100644 (file)
index 0000000..82628ab
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/c351ad42ccc7485a0ad6e1ae8cc07d66ee9b045a differ
diff --git a/fuzz/dns_name_fromwire.in/cc6cba815d062e0e250463aad2113fdcafd48571 b/fuzz/dns_name_fromwire.in/cc6cba815d062e0e250463aad2113fdcafd48571
new file mode 100644 (file)
index 0000000..3fe9467
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/cc6cba815d062e0e250463aad2113fdcafd48571 differ
diff --git a/fuzz/dns_name_fromwire.in/db6cca49e957261d7a262fbf6dafcac01353f1e4 b/fuzz/dns_name_fromwire.in/db6cca49e957261d7a262fbf6dafcac01353f1e4
new file mode 100644 (file)
index 0000000..ee55ada
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/db6cca49e957261d7a262fbf6dafcac01353f1e4 differ
diff --git a/fuzz/dns_name_fromwire.in/de4e7188e5540bd15eb83ddb5a784c6dc35d8d94 b/fuzz/dns_name_fromwire.in/de4e7188e5540bd15eb83ddb5a784c6dc35d8d94
new file mode 100644 (file)
index 0000000..8a8fa78
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/de4e7188e5540bd15eb83ddb5a784c6dc35d8d94 differ
diff --git a/fuzz/dns_name_fromwire.in/df58248c414f342c81e056b40bee12d17a08bf61 b/fuzz/dns_name_fromwire.in/df58248c414f342c81e056b40bee12d17a08bf61
new file mode 100644 (file)
index 0000000..f59ec20
--- /dev/null
@@ -0,0 +1 @@
+*
\ No newline at end of file
diff --git a/fuzz/dns_name_fromwire.in/df7467bb9625092ba9d23c0d8e4f29c7f4e23dd1 b/fuzz/dns_name_fromwire.in/df7467bb9625092ba9d23c0d8e4f29c7f4e23dd1
new file mode 100644 (file)
index 0000000..25daf0d
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/df7467bb9625092ba9d23c0d8e4f29c7f4e23dd1 differ
diff --git a/fuzz/dns_name_fromwire.in/e05d045edfa8ebe33c73b6459e7d704e0c958c74 b/fuzz/dns_name_fromwire.in/e05d045edfa8ebe33c73b6459e7d704e0c958c74
new file mode 100644 (file)
index 0000000..977463b
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/e05d045edfa8ebe33c73b6459e7d704e0c958c74 differ
diff --git a/fuzz/dns_name_fromwire.in/e23b6fdeadc0379b095bc1ffb8a1d2101ebe76b1 b/fuzz/dns_name_fromwire.in/e23b6fdeadc0379b095bc1ffb8a1d2101ebe76b1
new file mode 100644 (file)
index 0000000..d07176a
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/e23b6fdeadc0379b095bc1ffb8a1d2101ebe76b1 differ
diff --git a/fuzz/dns_name_fromwire.in/e333716351a0ded128bbb67ba46b5017a16ae6bf b/fuzz/dns_name_fromwire.in/e333716351a0ded128bbb67ba46b5017a16ae6bf
new file mode 100644 (file)
index 0000000..fc5aa04
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/e333716351a0ded128bbb67ba46b5017a16ae6bf differ
diff --git a/fuzz/dns_name_fromwire.in/e52912805d2e9212d53a46426e160813124be117 b/fuzz/dns_name_fromwire.in/e52912805d2e9212d53a46426e160813124be117
new file mode 100644 (file)
index 0000000..f4930c5
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/e52912805d2e9212d53a46426e160813124be117 differ
diff --git a/fuzz/dns_name_fromwire.in/f4a59b7257dbd0777e57e6e25689d374f4d771bc b/fuzz/dns_name_fromwire.in/f4a59b7257dbd0777e57e6e25689d374f4d771bc
new file mode 100644 (file)
index 0000000..dff8904
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/f4a59b7257dbd0777e57e6e25689d374f4d771bc differ
diff --git a/fuzz/dns_name_fromwire.in/fc712b19cbed4a8b4db3226dfbfdf3fb7be7690a b/fuzz/dns_name_fromwire.in/fc712b19cbed4a8b4db3226dfbfdf3fb7be7690a
new file mode 100644 (file)
index 0000000..45701d0
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/fc712b19cbed4a8b4db3226dfbfdf3fb7be7690a differ
diff --git a/fuzz/dns_name_fromwire.in/fda8ce1c09f3e385a5c0798f9ee158a4ca7d449f b/fuzz/dns_name_fromwire.in/fda8ce1c09f3e385a5c0798f9ee158a4ca7d449f
new file mode 100644 (file)
index 0000000..9774a89
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/fda8ce1c09f3e385a5c0798f9ee158a4ca7d449f differ
diff --git a/fuzz/dns_name_fromwire.in/fe38c27050137b9ce57189c681beb2b59a539a98 b/fuzz/dns_name_fromwire.in/fe38c27050137b9ce57189c681beb2b59a539a98
new file mode 100644 (file)
index 0000000..2cf643e
Binary files /dev/null and b/fuzz/dns_name_fromwire.in/fe38c27050137b9ce57189c681beb2b59a539a98 differ
diff --git a/fuzz/old.c b/fuzz/old.c
new file mode 100644 (file)
index 0000000..124e5c6
--- /dev/null
@@ -0,0 +1,223 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#include <isc/ascii.h>
+#include <isc/buffer.h>
+#include <isc/result.h>
+#include <isc/types.h>
+#include <isc/util.h>
+
+#include <dns/compress.h>
+#include <dns/types.h>
+
+/*
+ */
+
+#include "old.h"
+
+/*
+ * code copied from lib/dns/name.c as of commit
+ * 6967973568fe80b03e1729259f8907ce8792be34
+ */
+
+typedef enum { fw_start = 0, fw_ordinary, fw_newcurrent } fw_state;
+
+#define VALID_NAME(n) ISC_MAGIC_VALID(n, DNS_NAME_MAGIC)
+
+#define INIT_OFFSETS(name, var, default_offsets) \
+       if ((name)->offsets != NULL)             \
+               var = (name)->offsets;           \
+       else                                     \
+               var = (default_offsets);
+
+#define MAKE_EMPTY(name)                           \
+       do {                                       \
+               name->ndata = NULL;                \
+               name->length = 0;                  \
+               name->labels = 0;                  \
+               name->attributes.absolute = false; \
+       } while (0)
+
+#define BINDABLE(name) (!name->attributes.readonly && !name->attributes.dynamic)
+
+isc_result_t
+old_name_fromwire(dns_name_t *name, isc_buffer_t *source, dns_decompress_t dctx,
+                 unsigned int options, isc_buffer_t *target) {
+       unsigned char *cdata, *ndata;
+       unsigned int cused; /* Bytes of compressed name data used */
+       unsigned int nused, labels, n, nmax;
+       unsigned int current, new_current, biggest_pointer;
+       bool done;
+       fw_state state = fw_start;
+       unsigned int c;
+       unsigned char *offsets;
+       dns_offsets_t odata;
+       bool downcase;
+       bool seen_pointer;
+
+       /*
+        * Copy the possibly-compressed name at source into target,
+        * decompressing it.  Loop prevention is performed by checking
+        * the new pointer against biggest_pointer.
+        */
+
+       REQUIRE(VALID_NAME(name));
+       REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) ||
+               (target == NULL && ISC_BUFFER_VALID(name->buffer)));
+
+       downcase = ((options & DNS_NAME_DOWNCASE) != 0);
+
+       if (target == NULL && name->buffer != NULL) {
+               target = name->buffer;
+               isc_buffer_clear(target);
+       }
+
+       REQUIRE(BINDABLE(name));
+
+       INIT_OFFSETS(name, offsets, odata);
+
+       /*
+        * Make 'name' empty in case of failure.
+        */
+       MAKE_EMPTY(name);
+
+       /*
+        * Initialize things to make the compiler happy; they're not required.
+        */
+       n = 0;
+       new_current = 0;
+
+       /*
+        * Set up.
+        */
+       labels = 0;
+       done = false;
+
+       ndata = isc_buffer_used(target);
+       nused = 0;
+       seen_pointer = false;
+
+       /*
+        * Find the maximum number of uncompressed target name
+        * bytes we are willing to generate.  This is the smaller
+        * of the available target buffer length and the
+        * maximum legal domain name length (255).
+        */
+       nmax = isc_buffer_availablelength(target);
+       if (nmax > DNS_NAME_MAXWIRE) {
+               nmax = DNS_NAME_MAXWIRE;
+       }
+
+       cdata = isc_buffer_current(source);
+       cused = 0;
+
+       current = source->current;
+       biggest_pointer = current;
+
+       /*
+        * Note:  The following code is not optimized for speed, but
+        * rather for correctness.  Speed will be addressed in the future.
+        */
+
+       while (current < source->active && !done) {
+               c = *cdata++;
+               current++;
+               if (!seen_pointer) {
+                       cused++;
+               }
+
+               switch (state) {
+               case fw_start:
+                       if (c < 64) {
+                               offsets[labels] = nused;
+                               labels++;
+                               if (nused + c + 1 > nmax) {
+                                       goto full;
+                               }
+                               nused += c + 1;
+                               *ndata++ = c;
+                               if (c == 0) {
+                                       done = true;
+                               }
+                               n = c;
+                               state = fw_ordinary;
+                       } else if (c >= 192) {
+                               /*
+                                * 14-bit compression pointer
+                                */
+                               if (!dns_decompress_getpermitted(dctx)) {
+                                       return (DNS_R_DISALLOWED);
+                               }
+                               new_current = c & 0x3F;
+                               state = fw_newcurrent;
+                       } else {
+                               return (DNS_R_BADLABELTYPE);
+                       }
+                       break;
+               case fw_ordinary:
+                       if (downcase) {
+                               c = isc_ascii_tolower(c);
+                       }
+                       *ndata++ = c;
+                       n--;
+                       if (n == 0) {
+                               state = fw_start;
+                       }
+                       break;
+               case fw_newcurrent:
+                       new_current *= 256;
+                       new_current += c;
+                       if (new_current >= biggest_pointer) {
+                               return (DNS_R_BADPOINTER);
+                       }
+                       biggest_pointer = new_current;
+                       current = new_current;
+                       cdata = (unsigned char *)source->base + current;
+                       seen_pointer = true;
+                       state = fw_start;
+                       break;
+               default:
+                       FATAL_ERROR("Unknown state %d", state);
+                       /* Does not return. */
+               }
+       }
+
+       if (!done) {
+               return (ISC_R_UNEXPECTEDEND);
+       }
+
+       name->ndata = (unsigned char *)target->base + target->used;
+       name->labels = labels;
+       name->length = nused;
+       name->attributes.absolute = true;
+
+       isc_buffer_forward(source, cused);
+       isc_buffer_add(target, name->length);
+
+       return (ISC_R_SUCCESS);
+
+full:
+       if (nmax == DNS_NAME_MAXWIRE) {
+               /*
+                * The name did not fit even though we had a buffer
+                * big enough to fit a maximum-length name.
+                */
+               return (DNS_R_NAMETOOLONG);
+       } else {
+               /*
+                * The name might fit if only the caller could give us a
+                * big enough buffer.
+                */
+               return (ISC_R_NOSPACE);
+       }
+}
diff --git a/fuzz/old.h b/fuzz/old.h
new file mode 100644 (file)
index 0000000..5c5f82b
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#pragma once
+
+/*%
+ * For verifying no functional change in the rewrite of dns_name_fromwire()
+ */
+isc_result_t
+old_name_fromwire(dns_name_t *name, isc_buffer_t *source, dns_decompress_t dctx,
+                 unsigned int options, isc_buffer_t *target);
index 494d04e29fe9f64eae0c8b54b6b9a75cd1a8b7bb..b166d01436a42181a81f80ef65776d73402e7148 100644 (file)
@@ -1,3 +1,4 @@
 /ascii
 /compress
+/dns_name_fromwire
 /siphash
index 9483c7323666d13352b30676ea1617a0636c0680..c5be25c8ec075b90eb18bc456bb26a165d84565a 100644 (file)
@@ -2,7 +2,8 @@ include $(top_srcdir)/Makefile.top
 
 AM_CPPFLAGS +=                 \
        $(LIBISC_CFLAGS)        \
-       $(LIBDNS_CFLAGS)
+       $(LIBDNS_CFLAGS)        \
+       -I$(top_srcdir)/fuzz
 
 LDADD +=                       \
        $(LIBISC_LIBS)          \
@@ -11,4 +12,10 @@ LDADD +=                     \
 noinst_PROGRAMS =              \
        ascii                   \
        compress                \
+       dns_name_fromwire       \
        siphash
+
+dns_name_fromwire_SOURCES =            \
+       $(top_builddir)/fuzz/old.c      \
+       $(top_builddir)/fuzz/old.h      \
+       dns_name_fromwire.c
diff --git a/tests/bench/dns_name_fromwire.c b/tests/bench/dns_name_fromwire.c
new file mode 100644 (file)
index 0000000..6092ac2
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <isc/ascii.h>
+#include <isc/buffer.h>
+#include <isc/random.h>
+#include <isc/time.h>
+#include <isc/util.h>
+
+#include <dns/compress.h>
+#include <dns/fixedname.h>
+#include <dns/name.h>
+
+#include "old.h"
+
+static uint32_t
+old_bench(const uint8_t *data, size_t size) {
+       isc_result_t result;
+       dns_fixedname_t fixed;
+       dns_name_t *name = dns_fixedname_initname(&fixed);
+       dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
+       isc_buffer_t buf;
+       uint32_t count = 0;
+
+       isc_buffer_constinit(&buf, data, size);
+       isc_buffer_add(&buf, size);
+       isc_buffer_setactive(&buf, size);
+
+       while (isc_buffer_consumedlength(&buf) < size) {
+               result = old_name_fromwire(name, &buf, dctx, 0, NULL);
+               if (result != ISC_R_SUCCESS) {
+                       isc_buffer_forward(&buf, 1);
+               }
+               count++;
+       }
+       return (count);
+}
+
+static uint32_t
+new_bench(const uint8_t *data, size_t size) {
+       isc_result_t result;
+       dns_fixedname_t fixed;
+       dns_name_t *name = dns_fixedname_initname(&fixed);
+       dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
+       isc_buffer_t buf;
+       uint32_t count = 0;
+
+       isc_buffer_constinit(&buf, data, size);
+       isc_buffer_add(&buf, size);
+       isc_buffer_setactive(&buf, size);
+
+       while (isc_buffer_consumedlength(&buf) < size) {
+               result = dns_name_fromwire(name, &buf, dctx, 0, NULL);
+               if (result != ISC_R_SUCCESS) {
+                       isc_buffer_forward(&buf, 1);
+               }
+               count++;
+       }
+       return (count);
+}
+
+static void
+oldnew_bench(const uint8_t *data, size_t size) {
+       isc_time_t t0;
+       isc_time_now_hires(&t0);
+       uint32_t n1 = old_bench(data, size);
+       isc_time_t t1;
+       isc_time_now_hires(&t1);
+       uint32_t n2 = new_bench(data, size);
+       isc_time_t t2;
+       isc_time_now_hires(&t2);
+
+       double t01 = (double)isc_time_microdiff(&t1, &t0);
+       double t12 = (double)isc_time_microdiff(&t2, &t1);
+       printf("  old %u / %f ms; %f / us\n", n1, t01 / 1000.0, n1 / t01);
+       printf("  new %u / %f ms; %f / us\n", n2, t12 / 1000.0, n2 / t12);
+       printf("  old/new %f or %f\n", t01 / t12, t12 / t01);
+}
+
+#define NAMES 1000
+static uint8_t buf[1024 * NAMES];
+
+int
+main(void) {
+       unsigned int p;
+
+       printf("random buffer\n");
+       isc_random_buf(buf, sizeof(buf));
+       oldnew_bench(buf, sizeof(buf));
+
+       p = 0;
+       for (unsigned int name = 0; name < NAMES; name++) {
+               unsigned int start = p;
+               unsigned int prev = p;
+               buf[p++] = 0;
+               for (unsigned int label = 0; label < 127; label++) {
+                       unsigned int ptr = prev - start;
+                       prev = p;
+                       buf[p++] = 1;
+                       buf[p++] = 'a';
+                       buf[p++] = 0xC0 | (ptr >> 8);
+                       buf[p++] = 0xFF & ptr;
+               }
+       }
+       printf("127 compression pointers\n");
+       oldnew_bench(buf, p);
+
+       p = 0;
+       for (unsigned int name = 0; name < NAMES; name++) {
+               for (unsigned int label = 0; label < 127; label++) {
+                       buf[p++] = 1;
+                       buf[p++] = 'a';
+               }
+               buf[p++] = 0;
+       }
+       printf("127 sequential labels\n");
+       oldnew_bench(buf, p);
+
+       p = 0;
+       for (unsigned int name = 0; name < NAMES; name++) {
+               for (unsigned int label = 0; label < 4; label++) {
+                       buf[p++] = 62;
+                       for (unsigned int c = 0; c < 62; c++) {
+                               buf[p++] = 'a';
+                       }
+               }
+               buf[p++] = 0;
+       }
+       printf("4 long sequential labels\n");
+       oldnew_bench(buf, p);
+}