summaryrefslogtreecommitdiffstats
path: root/fuzz/dns_rdata_fromtext.c
blob: b52a18f276d6e65959a79889f3699ff1e7d90cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#include <stdbool.h>
#include <stdlib.h>

#include <isc/attributes.h>
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/lex.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/string.h>
#include <isc/util.h>

#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
#include <dns/result.h>

#include "fuzz.h"

bool debug = false;

int
LLVMFuzzerInitialize(int *argc, char ***argv) {
	UNUSED(argc);
	UNUSED(argv);
	return (0);
}

/* following code was copied from named-rrchecker */
isc_lexspecials_t specials = { ['('] = 1, [')'] = 1, ['"'] = 1 };

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
	isc_mem_t *mctx = NULL;
	isc_mem_create(&mctx);

	isc_lex_t *lex = NULL;
	isc_token_t token;

	isc_result_t result;
	unsigned int options = 0;
	dns_rdatatype_t rdtype;
	dns_rdataclass_t rdclass;

	char wiredata[64 * 1024];
	isc_buffer_t wirebuf;
	isc_buffer_init(&wirebuf, wiredata, sizeof(wiredata));

	dns_rdata_t rdata = DNS_RDATA_INIT;
	dns_name_t *name = NULL;

	isc_buffer_t inbuf;
	isc_buffer_constinit(&inbuf, data, size);
	isc_buffer_add(&inbuf, size);
	isc_buffer_setactive(&inbuf, size);

	RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);

	/*
	 * Set up to lex DNS master file.
	 */
	isc_lex_setspecials(lex, specials);
	options = ISC_LEXOPT_EOL;
	isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);

	RUNTIME_CHECK(isc_lex_openbuffer(lex, &inbuf) == ISC_R_SUCCESS);

	result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token);
	if (result != ISC_R_SUCCESS) {
		goto cleanup;
	}
	if (token.type == isc_tokentype_eof) {
		goto cleanup;
	}
	if (token.type == isc_tokentype_eol) {
		goto cleanup;
	}
	/*
	 * Get class.
	 */
	if (token.type == isc_tokentype_number) {
		if (token.value.as_ulong > 0xffff) {
			goto cleanup;
		}
		rdclass = (dns_rdataclass_t)token.value.as_ulong;
	} else if (token.type == isc_tokentype_string) {
		result = dns_rdataclass_fromtext(&rdclass,
						 &token.value.as_textregion);
		if (result != ISC_R_SUCCESS) {
			goto cleanup;
		}
	} else {
		goto cleanup;
	}
	result = isc_lex_gettoken(lex, options | ISC_LEXOPT_NUMBER, &token);
	if (result != ISC_R_SUCCESS) {
		goto cleanup;
	}
	if (token.type == isc_tokentype_eol) {
		goto cleanup;
	}
	if (token.type == isc_tokentype_eof) {
		goto cleanup;
	}

	/*
	 * Get type.
	 */
	if (token.type == isc_tokentype_number) {
		if (token.value.as_ulong > 0xffff) {
			goto cleanup;
		}
		rdtype = (dns_rdatatype_t)token.value.as_ulong;
	} else if (token.type == isc_tokentype_string) {
		result = dns_rdatatype_fromtext(&rdtype,
						&token.value.as_textregion);
		if (result != ISC_R_SUCCESS) {
			goto cleanup;
		}
	} else {
		goto cleanup;
	}

	result = dns_rdata_fromtext(&rdata, rdclass, rdtype, lex, name, 0, mctx,
				    &wirebuf, NULL);
	if (debug) {
		fprintf(stderr, "dns_rdata_fromtext: %s\n",
			isc_result_totext(result));
	}

cleanup:
	isc_lex_close(lex);
	isc_lex_destroy(&lex);
	isc_mem_destroy(&mctx);
	return (0);
}