Debian Codegraph @

fuzz/dns_rdata_fromwire_text.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 <assert.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <string.h>
18
19 #include <isc/buffer.h>
20 #include <isc/lex.h>
21 #include <isc/mem.h>
22 #include <isc/result.h>
23 #include <isc/util.h>
24
25 #include <dns/callbacks.h>
26 #include <dns/compress.h>
27 #include <dns/master.h>
28 #include <dns/rdata.h>
29 #include <dns/rdatatype.h>
30
31 #include "fuzz.h"
32
33 bool debug = false;
34
35 /*
36 * Fuzz input to dns_rdata_fromwire(). Then convert the result
37 * to text, back to wire format, to multiline text, and back to wire
38 * format again, checking for consistency throughout the sequence.
39 */
40
41 static isc_mem_t *mctx = NULL;
42 static isc_lex_t *lex = NULL;
43
44 int
45 LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) {
46 isc_lexspecials_t specials;
47
48 isc_mem_create(&mctx);
49 isc_lex_create(mctx, 64, &lex);
50
51 memset(specials, 0, sizeof(specials));
52 specials[0] = 1;
53 specials['('] = 1;
54 specials[')'] = 1;
55 specials['"'] = 1;
56 isc_lex_setspecials(lex, specials);
57 isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
58
59 return 0;
60 }
61
62 static void
63 nullmsg(dns_rdatacallbacks_t *cb, const char *fmt, ...) {
64 va_list args;
65
66 UNUSED(cb);
67
68 if (debug) {
69 va_start(args, fmt);
70 vfprintf(stderr, fmt, args);
71 fprintf(stderr, "\n");
72 va_end(args);
73 }
74 }
75
76 int
77 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
78 char totext[64 * 1044 * 4];
79 dns_compress_t cctx;
80 dns_rdatatype_t rdtype;
81 dns_rdataclass_t rdclass;
82 dns_rdatatype_t typelist[256] = { 1000 }; /* unknown */
83 dns_rdataclass_t classlist[] = { dns_rdataclass_in, dns_rdataclass_hs,
84 dns_rdataclass_ch, dns_rdataclass_any,
85 60 };
86 dns_rdata_t rdata1 = DNS_RDATA_INIT, rdata2 = DNS_RDATA_INIT,
87 rdata3 = DNS_RDATA_INIT;
88 dns_rdatacallbacks_t callbacks;
89 isc_buffer_t source, target;
90 isc_result_t result;
91 unsigned char fromtext[1024];
92 unsigned char fromwire[1024];
93 unsigned char towire[1024];
94 unsigned int classes = (sizeof(classlist) / sizeof(classlist[0]));
95 unsigned int types = 1, flags, t;
96
97 /*
98 * First 2 bytes are used to select type and class.
99 * dns_rdata_fromwire() only accepts input up to 2^16-1 octets.
100 */
101 if (size < 2 || size > 0xffff + 2) {
102 return 0;
103 }
104
105 /*
106 * Append known types to list.
107 */
108 for (t = 1; t <= 0x10000; t++) {
109 char typebuf[256];
110 if (dns_rdatatype_ismeta(t)) {
111 continue;
112 }
113 dns_rdatatype_format(t, typebuf, sizeof(typebuf));
114 if (strncmp(typebuf, "TYPE", 4) != 0) {
115 /* Assert when we need to grow typelist. */
116 assert(types < sizeof(typelist) / sizeof(typelist[0]));
117 typelist[types++] = t;
118 }
119 }
120
121 /*
122 * Random type and class from a limited set.
123 */
124 rdtype = typelist[(*data++) % types];
125 size--;
126 rdclass = classlist[(*data++) % classes];
127 size--;
128
129 if (debug) {
130 fprintf(stderr, "type=%u, class=%u\n", rdtype, rdclass);
131 }
132
133 dns_rdatacallbacks_init(&callbacks);
134 callbacks.warn = callbacks.error = nullmsg;
135
136 isc_buffer_constinit(&source, data, size);
137 isc_buffer_add(&source, size);
138 isc_buffer_setactive(&source, size);
139
140 isc_buffer_init(&target, fromwire, sizeof(fromwire));
141
142 /*
143 * Reject invalid rdata. (Disallow decompression as we are
144 * reading a packet)
145 */
146 CHECK(dns_rdata_fromwire(&rdata1, rdclass, rdtype, &source,
147 DNS_DECOMPRESS_NEVER, &target));
148 assert(rdata1.length == size);
149
150 /*
151 * Convert to text from wire.
152 */
153 isc_buffer_init(&target, totext, sizeof(totext) - 1);
154 result = dns_rdata_totext(&rdata1, NULL, &target);
155 assert(result == ISC_R_SUCCESS);
156
157 /*
158 * Make debugging easier by NUL terminating.
159 */
160 totext[isc_buffer_usedlength(&target)] = 0;
161
162 /*
163 * Convert to wire from text.
164 */
165 isc_buffer_constinit(&source, totext, isc_buffer_usedlength(&target));
166 isc_buffer_add(&source, isc_buffer_usedlength(&target));
167 CHECK(isc_lex_openbuffer(lex, &source));
168
169 isc_buffer_init(&target, fromtext, sizeof(fromtext));
170 result = dns_rdata_fromtext(&rdata2, rdclass, rdtype, lex, dns_rootname,
171 0, mctx, &target, &callbacks);
172 if (debug && result != ISC_R_SUCCESS) {
173 fprintf(stderr, "'%s'\n", totext);
174 }
175 assert(result == ISC_R_SUCCESS);
176 assert(rdata2.length == size);
177 assert(!memcmp(rdata2.data, data, size));
178
179 /*
180 * Convert to multi-line text from wire.
181 */
182 isc_buffer_init(&target, totext, sizeof(totext));
183 flags = dns_master_styleflags(&dns_master_style_default);
184 result = dns_rdata_tofmttext(&rdata1, dns_rootname, flags, 80 - 32, 4,
185 "\n", &target);
186 assert(result == ISC_R_SUCCESS);
187
188 /*
189 * Convert to wire from text.
190 */
191 isc_buffer_constinit(&source, totext, isc_buffer_usedlength(&target));
192 isc_buffer_add(&source, isc_buffer_usedlength(&target));
193 CHECK(isc_lex_openbuffer(lex, &source));
194
195 isc_buffer_init(&target, fromtext, sizeof(fromtext));
196 result = dns_rdata_fromtext(&rdata3, rdclass, rdtype, lex, dns_rootname,
197 0, mctx, &target, &callbacks);
198 assert(result == ISC_R_SUCCESS);
199 assert(rdata3.length == size);
200 assert(!memcmp(rdata3.data, data, size));
201
202 /*
203 * Convert rdata back to wire.
204 */
205 dns_compress_init(&cctx, mctx, DNS_COMPRESS_DISABLED);
206 isc_buffer_init(&target, towire, sizeof(towire));
207 result = dns_rdata_towire(&rdata1, &cctx, &target);
208 dns_compress_invalidate(&cctx);
209 assert(result == ISC_R_SUCCESS);
210 assert(target.used == size);
211 assert(!memcmp(target.base, data, size));
212
213 return 0;
214 }