Debian Codegraph @

fuzz/dns_name_fromwire.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 #include <isc/ascii.h>
19 #include <isc/buffer.h>
20 #include <isc/util.h>
21
22 #include <dns/compress.h>
23 #include <dns/fixedname.h>
24 #include <dns/name.h>
25
26 #include "fuzz.h"
27 #include "old.h"
28
29 bool debug = false;
30
31 int
32 LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) {
33 return 0;
34 }
35
36 int
37 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
38 isc_result_t new_result;
39 isc_result_t old_result;
40 dns_fixedname_t new_fixed;
41 dns_fixedname_t old_fixed;
42 dns_name_t *new_name = dns_fixedname_initname(&new_fixed);
43 dns_name_t *old_name = dns_fixedname_initname(&old_fixed);
44 uint8_t *new_offsets;
45 uint8_t *old_offsets;
46 dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED;
47 isc_buffer_t new_buf;
48 isc_buffer_t old_buf;
49
50 /*
51 * Output buffers may be partially used or undersized.
52 */
53 if (size > 0) {
54 uint8_t add = *data++;
55 size--;
56 isc_buffer_add(&new_fixed.buffer, add);
57 isc_buffer_add(&old_fixed.buffer, add);
58 }
59
60 /*
61 * timeout faster if we hit a pointer loop
62 */
63 alarm(1);
64
65 /*
66 * We shift forward by half the input data to make an area
67 * that pointers can refer back to.
68 */
69
70 isc_buffer_constinit(&new_buf, data, size);
71 isc_buffer_add(&new_buf, size);
72 isc_buffer_setactive(&new_buf, size);
73 isc_buffer_forward(&new_buf, size / 2);
74 new_result = dns_name_fromwire(new_name, &new_buf, dctx, NULL);
75
76 isc_buffer_constinit(&old_buf, data, size);
77 isc_buffer_add(&old_buf, size);
78 isc_buffer_setactive(&old_buf, size);
79 isc_buffer_forward(&old_buf, size / 2);
80 old_result = old_name_fromwire(old_name, &old_buf, dctx, 0, NULL);
81
82 REQUIRE(new_result == old_result);
83 REQUIRE(dns_name_equal(new_name, old_name));
84 REQUIRE(new_name->labels == old_name->labels);
85
86 new_offsets = new_name->offsets;
87 old_offsets = old_name->offsets;
88 REQUIRE(new_offsets != NULL && old_offsets != NULL);
89 REQUIRE(memcmp(new_offsets, old_offsets, old_name->labels) == 0);
90
91 REQUIRE(new_fixed.buffer.current == old_fixed.buffer.current);
92 REQUIRE(new_fixed.buffer.active == old_fixed.buffer.active);
93 REQUIRE(new_fixed.buffer.used == old_fixed.buffer.used);
94 REQUIRE(new_fixed.buffer.length == old_fixed.buffer.length);
95
96 REQUIRE(new_buf.base == old_buf.base);
97 REQUIRE(new_buf.current == old_buf.current);
98 REQUIRE(new_buf.active == old_buf.active);
99 REQUIRE(new_buf.used == old_buf.used);
100 REQUIRE(new_buf.length == old_buf.length);
101
102 return 0;
103 }