Debian Codegraph @

fuzz/dns_rdata_fromtext.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 <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 LLVMFuzzerInitialize(int *argc, char ***argv) {
38 UNUSED(argc);
39 UNUSED(argv);
40 return 0;
41 }
42
43 /* following code was copied from named-rrchecker */
44 isc_lexspecials_t specials = { ['('] = 1, [')'] = 1, ['"'] = 1 };
45
46 int
47 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
48 isc_mem_t *mctx = NULL;
49 isc_mem_create(&mctx);
50
51 isc_lex_t *lex = NULL;
52 isc_token_t token;
53
54 isc_result_t result;
55 unsigned int options = 0;
56 dns_rdatatype_t rdtype;
57 dns_rdataclass_t rdclass;
58
59 char wiredata[64 * 1024];
60 isc_buffer_t wirebuf;
61 isc_buffer_init(&wirebuf, wiredata, sizeof(wiredata));
62
63 dns_rdata_t rdata = DNS_RDATA_INIT;
64 dns_name_t *name = NULL;
65
66 isc_buffer_t inbuf;
67 isc_buffer_constinit(&inbuf, data, size);
68 isc_buffer_add(&inbuf, size);
69 isc_buffer_setactive(&inbuf, size);
70
71 isc_lex_create(mctx, 256, &lex);
72
73 /*
74 * Set up to lex DNS master file.
75 */
76 isc_lex_setspecials(lex, specials);
77 options = ISC_LEXOPT_EOL;
78 isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
79
80 RUNTIME_CHECK(isc_lex_openbuffer(lex, &inbuf) == ISC_R_SUCCESS);
81
82 result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token);
83 if (result != ISC_R_SUCCESS) {
84 goto cleanup;
85 }
86 if (token.type == isc_tokentype_eof) {
87 goto cleanup;
88 }
89 if (token.type == isc_tokentype_eol) {
90 goto cleanup;
91 }
92 /*
93 * Get class.
94 */
95 if (token.type == isc_tokentype_number) {
96 if (token.value.as_ulong > 0xffff) {
97 goto cleanup;
98 }
99 rdclass = (dns_rdataclass_t)token.value.as_ulong;
100 } else if (token.type == isc_tokentype_string) {
101 result = dns_rdataclass_fromtext(&rdclass,
102 &token.value.as_textregion);
103 if (result != ISC_R_SUCCESS) {
104 goto cleanup;
105 }
106 } else {
107 goto cleanup;
108 }
109 result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token);
110 if (result != ISC_R_SUCCESS) {
111 goto cleanup;
112 }
113 if (token.type == isc_tokentype_eol) {
114 goto cleanup;
115 }
116 if (token.type == isc_tokentype_eof) {
117 goto cleanup;
118 }
119
120 /*
121 * Get type.
122 */
123 if (token.type == isc_tokentype_number) {
124 if (token.value.as_ulong > 0xffff) {
125 goto cleanup;
126 }
127 rdtype = (dns_rdatatype_t)token.value.as_ulong;
128 } else if (token.type == isc_tokentype_string) {
129 result = dns_rdatatype_fromtext(&rdtype,
130 &token.value.as_textregion);
131 if (result != ISC_R_SUCCESS) {
132 goto cleanup;
133 }
134 } else {
135 goto cleanup;
136 }
137
138 result = dns_rdata_fromtext(&rdata, rdclass, rdtype, lex, name, 0, mctx,
139 &wirebuf, NULL);
140 if (debug) {
141 fprintf(stderr, "dns_rdata_fromtext: %s\n",
142 isc_result_totext(result));
143 }
144
145 cleanup:
146 isc_lex_close(lex);
147 isc_lex_destroy(&lex);
148 isc_mem_destroy(&mctx);
149 return 0;
150 }