/src/bind9/fuzz/dns_rdata_fromtext.c
Line | Count | Source |
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 <stdbool.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include <isc/attributes.h> |
18 | | #include <isc/buffer.h> |
19 | | #include <isc/commandline.h> |
20 | | #include <isc/lex.h> |
21 | | #include <isc/mem.h> |
22 | | #include <isc/string.h> |
23 | | #include <isc/util.h> |
24 | | |
25 | | #include <dns/fixedname.h> |
26 | | #include <dns/name.h> |
27 | | #include <dns/rdata.h> |
28 | | #include <dns/rdataclass.h> |
29 | | #include <dns/rdatatype.h> |
30 | | #include <dns/result.h> |
31 | | |
32 | | #include "fuzz.h" |
33 | | |
34 | | bool debug = false; |
35 | | |
36 | | int |
37 | 18 | LLVMFuzzerInitialize(int *argc, char ***argv) { |
38 | 18 | UNUSED(argc); |
39 | 18 | UNUSED(argv); |
40 | 18 | return 0; |
41 | 18 | } |
42 | | |
43 | | /* following code was copied from named-rrchecker */ |
44 | | isc_lexspecials_t specials = { ['('] = 1, [')'] = 1, ['"'] = 1 }; |
45 | | |
46 | | int |
47 | 16.8k | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
48 | 16.8k | isc_mem_t *mctx = NULL; |
49 | 16.8k | isc_mem_create("fuzz", &mctx); |
50 | | |
51 | 16.8k | isc_lex_t *lex = NULL; |
52 | 16.8k | isc_token_t token; |
53 | | |
54 | 16.8k | isc_result_t result; |
55 | 16.8k | unsigned int options = 0; |
56 | 16.8k | dns_rdatatype_t rdtype; |
57 | 16.8k | dns_rdataclass_t rdclass; |
58 | | |
59 | 16.8k | char wiredata[64 * 1024]; |
60 | 16.8k | isc_buffer_t wirebuf; |
61 | 16.8k | isc_buffer_init(&wirebuf, wiredata, sizeof(wiredata)); |
62 | | |
63 | 16.8k | dns_rdata_t rdata = DNS_RDATA_INIT; |
64 | 16.8k | dns_name_t *name = NULL; |
65 | | |
66 | 16.8k | isc_buffer_t inbuf; |
67 | 16.8k | isc_buffer_constinit(&inbuf, data, size); |
68 | 16.8k | isc_buffer_add(&inbuf, size); |
69 | 16.8k | isc_buffer_setactive(&inbuf, size); |
70 | | |
71 | 16.8k | isc_lex_create(mctx, 256, &lex); |
72 | | |
73 | | /* |
74 | | * Set up to lex DNS master file. |
75 | | */ |
76 | 16.8k | isc_lex_setspecials(lex, specials); |
77 | 16.8k | options = ISC_LEXOPT_EOL; |
78 | 16.8k | isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE); |
79 | | |
80 | 16.8k | RUNTIME_CHECK(isc_lex_openbuffer(lex, &inbuf) == ISC_R_SUCCESS); |
81 | | |
82 | 16.8k | CHECK(isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token)); |
83 | 16.6k | if (token.type == isc_tokentype_eof) { |
84 | 0 | goto cleanup; |
85 | 0 | } |
86 | 16.6k | if (token.type == isc_tokentype_eol) { |
87 | 17 | goto cleanup; |
88 | 17 | } |
89 | | /* |
90 | | * Get class. |
91 | | */ |
92 | 16.6k | if (token.type == isc_tokentype_number) { |
93 | 16.1k | if (token.value.as_ulong > 0xffff) { |
94 | 54 | goto cleanup; |
95 | 54 | } |
96 | 16.1k | rdclass = (dns_rdataclass_t)token.value.as_ulong; |
97 | 16.1k | } else if (token.type == isc_tokentype_string) { |
98 | 523 | CHECK(dns_rdataclass_fromtext(&rdclass, |
99 | 523 | &token.value.as_textregion)); |
100 | 23 | } else { |
101 | 3 | goto cleanup; |
102 | 3 | } |
103 | 16.1k | CHECK(isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token)); |
104 | 16.0k | if (token.type == isc_tokentype_eol) { |
105 | 2 | goto cleanup; |
106 | 2 | } |
107 | 16.0k | if (token.type == isc_tokentype_eof) { |
108 | 0 | goto cleanup; |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * Get type. |
113 | | */ |
114 | 16.0k | if (token.type == isc_tokentype_number) { |
115 | 12.1k | if (token.value.as_ulong > 0xffff) { |
116 | 16 | goto cleanup; |
117 | 16 | } |
118 | 12.1k | rdtype = (dns_rdatatype_t)token.value.as_ulong; |
119 | 12.1k | } else if (token.type == isc_tokentype_string) { |
120 | 3.89k | CHECK(dns_rdatatype_fromtext(&rdtype, |
121 | 3.89k | &token.value.as_textregion)); |
122 | 297 | } else { |
123 | 1 | goto cleanup; |
124 | 1 | } |
125 | | |
126 | 12.4k | result = dns_rdata_fromtext(&rdata, rdclass, rdtype, lex, name, 0, mctx, |
127 | 12.4k | &wirebuf, NULL); |
128 | 12.4k | if (debug) { |
129 | 0 | fprintf(stderr, "dns_rdata_fromtext: %s\n", |
130 | 0 | isc_result_totext(result)); |
131 | 0 | } |
132 | | |
133 | 16.8k | cleanup: |
134 | 16.8k | isc_lex_close(lex); |
135 | 16.8k | isc_lex_destroy(&lex); |
136 | 16.8k | isc_mem_detach(&mctx); |
137 | 16.8k | return 0; |
138 | 12.4k | } |