Debian Codegraph @

fuzz/dns_message_checksig.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 <inttypes.h>
15 #include <stdbool.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18
19 #include <isc/buffer.h>
20 #include <isc/commandline.h>
21 #include <isc/file.h>
22 #include <isc/mem.h>
23 #include <isc/result.h>
24 #include <isc/string.h>
25 #include <isc/tid.h>
26 #include <isc/util.h>
27
28 #include <dns/fixedname.h>
29 #include <dns/message.h>
30 #include <dns/name.h>
31 #include <dns/rcode.h>
32 #include <dns/tsig.h>
33 #include <dns/view.h>
34 #include <dns/zone.h>
35
36 #include "fuzz.h"
37
38 bool debug = false;
39
40 static isc_mem_t *mctx = NULL;
41
42 /*
43 * Packet dumps of validily signed request ./IN/SOA
44 * requests.
45 *
46 * TSIG:
47 *
48 * 0x0000: 600b 0900 006a 1140 0000 0000 0000 0000
49 * 0x0010: 0000 0000 0000 0001 0000 0000 0000 0000
50 * 0x0020: 0000 0000 0000 0001 cc88 0035 006a 007d
51 * 0x0030: 1dfa 0000 0001 0000 0000 0001 0000 0600
52 * 0x0040: 0108 7473 6967 2d6b 6579 0000 fa00 ff00
53 * 0x0050: 0000 0000 3d0b 686d 6163 2d73 6861 3235
54 * 0x0060: 3600 0000 622a cce1 012c 0020 224d 5807
55 * 0x0070: 648d 1400 9d8e fc1c d049 55e9 cc90 2187
56 * 0x0080: 3b5f af5c 8899 dc27 c8df b34b 1dfa 0000
57 * 0x0090: 0000
58 *
59 * SIG(0):
60 *
61 * 0x0000: 6004 0e00 013f 1140 0000 0000 0000 0000
62 * 0x0010: 0000 0000 0000 0001 0000 0000 0000 0000
63 * 0x0020: 0000 0000 0000 0001 c0a7 0035 013f 0152
64 * 0x0030: 0000 0000 0001 0000 0000 0001 0000 0600
65 * 0x0040: 0100 0018 00ff 0000 0000 011b 0000 0800
66 * 0x0050: 0000 0000 622a ce0d 622a cbb5 da71 0773
67 * 0x0060: 6967 306b 6579 0068 988b 27bf 5c89 5270
68 * 0x0070: c5ba ea8b 2e10 0512 9b44 48d3 69de b7ec
69 * 0x0080: 7c67 15f3 6bc7 b0dc 277b e8f1 6979 4c89
70 * 0x0090: 149a 0203 30a1 c0b7 a711 ee8a 8d90 ebb9
71 * 0x00a0: 9e33 dd65 33d5 5d1d 90db cf9c bb6a b346
72 * 0x00b0: 568f a399 71d7 c877 616d 2fb7 0f86 963f
73 * 0x00c0: aa00 850d 180a 9f83 cd4b d115 c79f 64c9
74 * 0x00d0: ff05 e751 6810 28b3 2249 c4ba 2d8d 57ba
75 * 0x00e0: 9aad f1fc b34e c237 9465 04fd fe4d 19c9
76 * 0x00f0: 2368 ec8e 7097 eaea e067 2b9c 06eb c383
77 * 0x0100: e901 a11e 606b 4cce c12a 0e57 8c09 b7cb
78 * 0x0110: 23bb ec05 b68b 1852 9288 b665 fe89 cf62
79 * 0x0120: 0a41 5e5a acbe 6903 cbb7 e7b6 cab4 e4a2
80 * 0x0130: b98f 884f c09d 5b39 c695 c84c 9a92 f110
81 * 0x0140: ccc3 f2ee 313f a2a1 1cda 5aa2 faec d593
82 * 0x0150: 4514 724a 868f 94b9 0547 4dc9 7b73 c85e
83 * 0x0160: 544c 73d4 e892 f9
84 */
85
86 #define HMACSHA256 "\x0bhmac-sha256"
87
88 static isc_stdtime_t fuzztime = 0x622acce1;
89 static isc_loopmgr_t *loopmgr = NULL;
90 static dns_view_t *view = NULL;
91 static dns_tsigkey_t *tsigkey = NULL;
92 static dns_tsigkeyring_t *ring = NULL;
93 static dns_tsigkeyring_t *emptyring = NULL;
94 static char *wd = NULL;
95 static char template[] = "/tmp/dns-message-checksig-XXXXXX";
96
97 static char f1[] = "Ksig0key.+008+55921.key";
98 static char c1[] = "sig0key. IN KEY 512 3 8 "
99 "AwEAAa22lgHi1vAbQvu5ETdTrm2H8rwga9tvyMa6LFiSDyevLvSv0Uo5 "
100 "uvfrXnxaLdtBMts6e1Ly2piSH9JRbOGMNibOK4EXWhWAn8MII4SWgQAs "
101 "bFwtiz4HyPn2wScrUQdo8DocKiQJBanesr7vDO8fdA6Rg1e0yAtSeNti "
102 "e8avx46/HJa6CFs3CoE0sf6oOFSxM954AgCBTXOGNBt1Nt3Bhfqt2qyA "
103 "TLFii5K1jLDTZDVkoiyDXL1M7wcTwKf9METgj1eQmH3GGlRM/OJ/j8xk "
104 "ZiFGbL3cipWdiH48031jiV2hlc92mKn8Ya0d9AN6c44piza/JSFydZXw "
105 "sY32nxzjDbs=\n";
106
107 static char f2[] = "Ksig0key.+008+55921.private";
108 static char c2[] = "Private-key-format: v1.3\n\
109 Algorithm: 8 (RSASHA256)\n\
110 Modulus: rbaWAeLW8BtC+7kRN1OubYfyvCBr22/IxrosWJIPJ68u9K/RSjm69+tefFot20Ey2zp7UvLamJIf0lFs4Yw2Js4rgRdaFYCfwwgjhJaBACxsXC2LPgfI+fbBJytRB2jwOhwqJAkFqd6yvu8M7x90DpGDV7TIC1J422J7xq/Hjr8clroIWzcKgTSx/qg4VLEz3ngCAIFNc4Y0G3U23cGF+q3arIBMsWKLkrWMsNNkNWSiLINcvUzvBxPAp/0wROCPV5CYfcYaVEz84n+PzGRmIUZsvdyKlZ2IfjzTfWOJXaGVz3aYqfxhrR30A3pzjimLNr8lIXJ1lfCxjfafHOMNuw==\n\
111 PublicExponent: AQAB\n\
112 PrivateExponent: GDfclFkR5ToFGH9rMTRMnP73Q5dzjLgkx4vyHcuzKtxcvAans4+hNj+NazckAy2E+mpzV2j95TJ4wZjSM2RvB5xLwBIc4Dg6oyAHL6Ikoae6gw64cHFOaYb808n8CyqWqfX+QWAz9sRSVZXnTuPViX3A+svR7ejVak9Bzr1NTDm0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E=\n\
118 Created: 20220311073606\n\
119 Publish: 20220311073606\n\
120 Activate: 20220311073606\n";
121
122 static char f3[] = "sig0key.db";
123 static char c3[] = "sig0key. 0 IN SOA . . 0 0 0 0 0\n\
124 sig0key. 0 IN NS .\n\
125 sig0key. 0 IN KEY 512 3 8 AwEAAa22lgHi1vAbQvu5ETdTrm2H8rwga9tvyMa6LFiSDyevLvSv0Uo5 uvfrXnxaLdtBMts6e1Ly2piSH9JRbOGMNibOK4EXWhWAn8MII4SWgQAs bFwtiz4HyPn2wScrUQdo8DocKiQJBanesr7vDO8fdA6Rg1e0yAtSeNti e8avx46/HJa6CFs3CoE0sf6oOFSxM954AgCBTXOGNBt1Nt3Bhfqt2qyA TLFii5K1jLDTZDVkoiyDXL1M7wcTwKf9METgj1eQmH3GGlRM/OJ/j8xk ZiFGbL3cipWdiH48031jiV2hlc92mKn8Ya0d9AN6c44piza/JSFydZXw sY32nxzjDbs=\n";
126
127 static bool destroy_dst = false;
128
129 int
130 LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) {
131 isc_result_t result;
132 dns_fixedname_t fixed;
133 dns_name_t *name = dns_fixedname_initname(&fixed);
134 unsigned char secret[16] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
136 0xff, 0xff, 0xff, 0xff };
137 dns_zone_t *zone = NULL;
138 char pathbuf[PATH_MAX];
139 FILE *fd;
140
141 wd = mkdtemp(template);
142 if (wd == NULL) {
143 fprintf(stderr, "mkdtemp failed\n");
144 return 1;
145 }
146
147 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", wd, f1);
148 fd = fopen(pathbuf, "w");
149 if (fd == NULL) {
150 fprintf(stderr, "fopen(%s) failed\n", pathbuf);
151 return 1;
152 }
153 fputs(c1, fd);
154 fclose(fd);
155
156 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", wd, f2);
157 fd = fopen(pathbuf, "w");
158 if (fd == NULL) {
159 fprintf(stderr, "fopen(%s) failed\n", pathbuf);
160 return 1;
161 }
162 fputs(c2, fd);
163 fclose(fd);
164
165 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", wd, f3);
166 fd = fopen(pathbuf, "w");
167 if (fd == NULL) {
168 fprintf(stderr, "fopen(%s) failed\n", pathbuf);
169 return 1;
170 }
171 fputs(c3, fd);
172 fclose(fd);
173
174 isc_mem_create(&mctx);
175
176 result = dst_lib_init(mctx, NULL);
177 if (result != ISC_R_SUCCESS) {
178 fprintf(stderr, "dst_lib_init failed: %s\n",
179 isc_result_totext(result));
180 return 1;
181 }
182 destroy_dst = true;
183
184 isc_loopmgr_create(mctx, 1, &loopmgr);
185
186 result = dns_view_create(mctx, loopmgr, NULL, dns_rdataclass_in, "view",
187 &view);
188 if (result != ISC_R_SUCCESS) {
189 fprintf(stderr, "dns_view_create failed: %s\n",
190 isc_result_totext(result));
191 return 1;
192 }
193
194 dns_tsigkeyring_create(mctx, &ring);
195 dns_tsigkeyring_create(mctx, &emptyring);
196
197 result = dns_name_fromstring(name, "tsig-key", dns_rootname, 0, NULL);
198 if (result != ISC_R_SUCCESS) {
199 fprintf(stderr, "dns_name_fromstring failed: %s\n",
200 isc_result_totext(result));
201 return 1;
202 }
203
204 result = dns_tsigkey_create(name, DST_ALG_HMACSHA256, secret,
205 sizeof(secret), mctx, &tsigkey);
206 if (result != ISC_R_SUCCESS) {
207 fprintf(stderr, "dns_tsigkey_create failed: %s\n",
208 isc_result_totext(result));
209 return 1;
210 }
211 result = dns_tsigkeyring_add(ring, tsigkey);
212 if (result != ISC_R_SUCCESS) {
213 fprintf(stderr, "dns_tsigkeyring_add failed: %s\n",
214 isc_result_totext(result));
215 return 1;
216 }
217
218 result = dns_name_fromstring(name, "sig0key", dns_rootname, 0, NULL);
219 if (result != ISC_R_SUCCESS) {
220 fprintf(stderr, "dns_name_fromstring failed: %s\n",
221 isc_result_totext(result));
222 return 1;
223 }
224
225 dns_zone_create(&zone, mctx, 0);
226
227 result = dns_zone_setorigin(zone, name);
228 if (result != ISC_R_SUCCESS) {
229 fprintf(stderr, "dns_zone_setorigin failed: %s\n",
230 isc_result_totext(result));
231 return 1;
232 }
233
234 dns_zone_setclass(zone, view->rdclass);
235 dns_zone_settype(zone, dns_zone_primary);
236
237 result = dns_zone_setkeydirectory(zone, wd);
238 if (result != ISC_R_SUCCESS) {
239 fprintf(stderr, "dns_zone_setkeydirectory failed: %s\n",
240 isc_result_totext(result));
241 return 1;
242 }
243
244 result = dns_zone_setfile(zone, pathbuf, dns_masterformat_text,
245 &dns_master_style_default);
246 if (result != ISC_R_SUCCESS) {
247 fprintf(stderr, "dns_zone_setfile failed: %s\n",
248 isc_result_totext(result));
249 return 1;
250 }
251
252 result = dns_zone_load(zone, false);
253 if (result != ISC_R_SUCCESS) {
254 fprintf(stderr, "dns_zone_load failed: %s\n",
255 isc_result_totext(result));
256 return 1;
257 }
258
259 result = dns_view_addzone(view, zone);
260 if (result != ISC_R_SUCCESS) {
261 fprintf(stderr, "dns_view_addzone failed: %s\n",
262 isc_result_totext(result));
263 return 1;
264 }
265
266 dns_zone_setview(zone, view);
267 dns_view_freeze(view);
268
269 dns_zone_detach(&zone);
270
271 return 0;
272 }
273
274 static isc_result_t
275 create_message(dns_message_t **messagep, const uint8_t *data, size_t size,
276 bool addasig, bool addtsig) {
277 isc_result_t result;
278 dns_message_t *message = NULL;
279 isc_buffer_t b;
280 static unsigned char buf[65535];
281
282 isc_buffer_init(&b, buf, sizeof(buf));
283
284 /* Message ID */
285 isc_buffer_putuint16(&b, 0);
286
287 /* QR, Opcode, other flags = 0, rcode = 0 */
288 isc_buffer_putuint16(&b, (*data & 0x1f) << 11);
289 /* Counts */
290 isc_buffer_putuint16(&b, 1);
291 isc_buffer_putuint16(&b, 0);
292 isc_buffer_putuint16(&b, 0);
293 isc_buffer_putuint16(&b, addasig ? 1 : 0);
294
295 /* Question ./IN/SOA */
296 isc_buffer_putuint8(&b, 0);
297 isc_buffer_putuint16(&b, 6);
298 isc_buffer_putuint16(&b, 1);
299
300 if (addasig) {
301 /* Signature */
302 if (addtsig) {
303 const unsigned char keyname[] = "\x08tsig-key";
304 isc_buffer_putmem(&b, keyname, sizeof(keyname));
305 isc_buffer_putuint16(&b, dns_rdatatype_tsig);
306 isc_buffer_putuint16(&b, dns_rdataclass_any);
307 } else {
308 isc_buffer_putuint8(&b, 0); /* '.' */
309 isc_buffer_putuint16(&b, dns_rdatatype_sig);
310 isc_buffer_putuint16(&b, dns_rdataclass_in);
311 }
312 isc_buffer_putuint32(&b, 0); /* ttl */
313 data++;
314 size--;
315 if (size > isc_buffer_availablelength(&b) - 2) {
316 size = isc_buffer_availablelength(&b) - 2;
317 }
318 isc_buffer_putuint16(&b, size);
319 isc_buffer_putmem(&b, data, size);
320 }
321
322 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &message);
323
324 result = dns_message_parse(message, &b, 0);
325 if (debug) {
326 fprintf(stderr, "dns_message_parse => %s\n",
327 isc_result_totext(result));
328 }
329 if (result != ISC_R_SUCCESS) {
330 dns_message_detach(&message);
331 } else {
332 if (debug) {
333 char text[200000];
334 isc_buffer_init(&b, text, sizeof(text));
335
336 result = dns_message_totext(
337 message, &dns_master_style_debug, 0, &b);
338 if (result == ISC_R_SUCCESS) {
339 fprintf(stderr, "%.*s", (int)b.used, text);
340 } else {
341 fprintf(stderr, "dns_message_totext => %s\n",
342 isc_result_totext(result));
343 }
344 }
345 *messagep = message;
346 }
347 return result;
348 }
349
350 int
351 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
352 isc_result_t result;
353 dns_message_t *message = NULL;
354 unsigned char query_tsig[23 + 32 + 6] = { 0 };
355 bool addasig = false;
356 bool addtime = false;
357 bool addtsig = false;
358 bool setquerytsig = false;
359 bool settsigkey = false;
360 bool subtime = false;
361 bool withring = false;
362 bool withview = false;
363
364 /*
365 * The first 2 octets affect setup.
366 * Octet 1 determines whether a signature is added and which type
367 * (addasig, addtsig), whether time should be adjusted (addtime,
368 * subtime), whether dns_message_setquerytsig and dns_message_settsigkey
369 * have been called, whether there is a keyring available with the
370 * TSIG key or a view is defined.
371 *
372 * The second octet defines if the message is a response and the
373 * opcode.
374 */
375 if (size > 65535 || size < 2) {
376 return 0;
377 }
378
379 addasig = (*data & 0x80) != 0;
380 addtime = (*data & 0x40) != 0;
381 addtsig = (*data & 0x20) != 0;
382 setquerytsig = (*data & 0x10) != 0;
383 settsigkey = (*data & 0x08) != 0;
384 subtime = (*data & 0x04) != 0;
385 withring = (*data & 0x02) != 0;
386 withview = (*data & 0x01) != 0;
387
388 data++;
389 size--;
390
391 if (debug) {
392 fprintf(stderr,
393 "addasig=%u addtime=%u addtsig=%u setquerytsig=%u "
394 "settsigkey=%u subtime=%u withring=%u\nwithview=%u\n",
395 addasig, addtime, addtsig, setquerytsig, settsigkey,
396 subtime, withring, withview);
397 }
398
399 result = create_message(&message, data, size, addasig, addtsig);
400 if (result != ISC_R_SUCCESS) {
401 return 0;
402 }
403
404 /*
405 * Make time calculations consistent.
406 */
407 message->fuzzing = 1;
408 message->fuzztime = fuzztime;
409 if (addtime) {
410 message->fuzztime += 1200;
411 }
412 if (subtime) {
413 message->fuzztime -= 1200;
414 }
415
416 if ((message->flags & DNS_MESSAGEFLAG_QR) != 0) {
417 if (setquerytsig) {
418 isc_buffer_t b;
419 unsigned char hmacname[] = HMACSHA256;
420 unsigned char hmacvalue[32] = {
421 0x22, 0x4d, 0x58, 0x07, 0x64, 0x8d, 0x14, 0x00,
422 0x9d, 0x8e, 0xfc, 0x1c, 0xd0, 0x49, 0x55, 0xe9,
423 0xcc, 0x90, 0x21, 0x87, 0x3b, 0x5f, 0xaf, 0x5c,
424 0x88, 0x99, 0xdc, 0x27, 0xc8, 0xdf, 0xb3, 0x4b
425 };
426
427 /*
428 * Valid TSIG rdata for tsig-key over a plain
429 * DNS QUERY for ./SOA/IN with no flags set.
430 */
431 isc_buffer_init(&b, query_tsig, sizeof(query_tsig));
432 isc_buffer_putmem(&b, hmacname, sizeof(hmacname));
433 isc_buffer_putuint16(&b, 0); /* time high */
434 isc_buffer_putuint32(&b, 0x622abec0); /* time low */
435 isc_buffer_putuint16(&b, 300); /* Fudge */
436 isc_buffer_putuint16(&b, 32); /* Mac Length */
437 /* Mac */
438 isc_buffer_putmem(&b, hmacvalue, 32);
439 isc_buffer_putuint16(&b, 7674); /* Original Id */
440 isc_buffer_putuint16(&b, 0); /* Error */
441 isc_buffer_putuint16(&b, 0); /* Other len */
442
443 dns_message_setquerytsig(message, &b);
444 }
445 }
446
447 if (settsigkey) {
448 result = dns_message_settsigkey(message, tsigkey);
449 if (debug) {
450 fprintf(stderr, "dns_message_settsigkey => %s\n",
451 isc_result_totext(result));
452 }
453 }
454
455 dns_view_setkeyring(view, withring ? ring : emptyring);
456
457 result = dns_message_checksig(message, withview ? view : NULL);
458 if (debug) {
459 char textbuf[64];
460 isc_buffer_t b;
461
462 fprintf(stderr, "dns_message_checksig => %s\n",
463 isc_result_totext(result));
464 isc_buffer_init(&b, textbuf, sizeof(textbuf));
465 dns_tsigrcode_totext(message->tsigstatus, &b);
466 fprintf(stderr, "tsigstatus=%.*s\n", (int)b.used, textbuf);
467 isc_buffer_init(&b, textbuf, sizeof(textbuf));
468 dns_tsigrcode_totext(message->sig0status, &b);
469 fprintf(stderr, "sig0status=%.*s\n", (int)b.used, textbuf);
470 }
471 if (result != ISC_R_SUCCESS) {
472 goto cleanup;
473 }
474
475 cleanup:
476 if (message != NULL) {
477 dns_message_detach(&message);
478 }
479
480 return 0;
481 }