Debian Codegraph @

fuzz/dns_message_parse.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
18 #include <isc/buffer.h>
19 #include <isc/commandline.h>
20 #include <isc/file.h>
21 #include <isc/mem.h>
22 #include <isc/result.h>
23 #include <isc/string.h>
24 #include <isc/util.h>
25
26 #include <dns/message.h>
27
28 #include "fuzz.h"
29
30 bool debug = false;
31
32 static isc_mem_t *mctx = NULL;
33 static uint8_t *output = NULL;
34 static size_t output_len = 1024;
35 static uint8_t render_buf[64 * 1024 - 1];
36
37 int
38 LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) {
39 isc_mem_create(&mctx);
40 output = isc_mem_get(mctx, output_len);
41
42 return 0;
43 }
44
45 static isc_result_t
46 parse_message(isc_buffer_t *input, dns_message_t **messagep) {
47 isc_result_t result;
48 dns_message_t *message = NULL;
49
50 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &message);
51
52 result = dns_message_parse(message, input, DNS_MESSAGEPARSE_BESTEFFORT);
53 if (result == DNS_R_RECOVERABLE) {
54 result = ISC_R_SUCCESS;
55 }
56
57 if (result == ISC_R_SUCCESS && messagep != NULL) {
58 *messagep = message;
59 } else {
60 dns_message_detach(&message);
61 }
62
63 return result;
64 }
65
66 static isc_result_t
67 print_message(dns_message_t *message) {
68 isc_result_t result;
69 isc_buffer_t buffer;
70
71 do {
72 isc_buffer_init(&buffer, output, output_len);
73 result = dns_message_totext(message, &dns_master_style_debug, 0,
74 &buffer);
75 if (result == ISC_R_NOSPACE) {
76 isc_mem_put(mctx, output, output_len);
77 output_len *= 2;
78 output = isc_mem_get(mctx, output_len);
79 continue;
80 }
81 } while (result == ISC_R_NOSPACE);
82
83 if (debug) {
84 fprintf(stderr, "%.*s\n", (int)isc_buffer_usedlength(&buffer),
85 output);
86 }
87
88 return result;
89 }
90
91 #define CHECKRESULT(r, f) \
92 { \
93 r = (f); \
94 if (r != ISC_R_SUCCESS) { \
95 goto cleanup; \
96 } \
97 }
98
99 static isc_result_t
100 render_message(dns_message_t **messagep) {
101 isc_result_t result;
102 dns_message_t *message = *messagep;
103 isc_buffer_t buffer;
104 dns_compress_t cctx;
105
106 isc_buffer_init(&buffer, render_buf, sizeof(render_buf));
107
108 message->from_to_wire = DNS_MESSAGE_INTENTRENDER;
109 for (size_t i = 0; i < DNS_SECTION_MAX; i++) {
110 message->counts[i] = 0;
111 }
112
113 dns_compress_init(&cctx, mctx, 0);
114
115 CHECKRESULT(result, dns_message_renderbegin(message, &cctx, &buffer));
116
117 CHECKRESULT(result, dns_message_rendersection(message,
118 DNS_SECTION_QUESTION, 0));
119
120 CHECKRESULT(result,
121 dns_message_rendersection(message, DNS_SECTION_ANSWER, 0));
122 CHECKRESULT(result, dns_message_rendersection(
123 message, DNS_SECTION_AUTHORITY, 0));
124
125 CHECKRESULT(result, dns_message_rendersection(
126 message, DNS_SECTION_ADDITIONAL, 0));
127
128 dns_message_renderend(message);
129
130 dns_compress_invalidate(&cctx);
131
132 message->from_to_wire = DNS_MESSAGE_INTENTPARSE;
133
134 dns_message_detach(messagep);
135
136 result = parse_message(&buffer, messagep);
137
138 return result;
139
140 cleanup:
141 dns_compress_invalidate(&cctx);
142 return result;
143 }
144
145 int
146 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
147 isc_buffer_t buffer;
148 isc_result_t result;
149 dns_message_t *message = NULL;
150
151 if (size > 65535) {
152 return 0;
153 }
154
155 isc_buffer_constinit(&buffer, data, size);
156 isc_buffer_add(&buffer, size);
157 isc_buffer_setactive(&buffer, size);
158
159 result = parse_message(&buffer, &message);
160 if (result != ISC_R_SUCCESS) {
161 goto cleanup;
162 }
163
164 result = print_message(message);
165 if (result != ISC_R_SUCCESS) {
166 goto cleanup;
167 }
168
169 result = render_message(&message);
170 if (result != ISC_R_SUCCESS) {
171 goto cleanup;
172 }
173
174 result = print_message(message);
175 if (result != ISC_R_SUCCESS) {
176 goto cleanup;
177 }
178
179 cleanup:
180 if (message != NULL) {
181 dns_message_detach(&message);
182 }
183
184 return 0;
185 }