-
Notifications
You must be signed in to change notification settings - Fork 74
/
rfc1035.c
391 lines (343 loc) · 11.2 KB
/
rfc1035.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991, or
(at your option) version 3 dated 29 June, 2007.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file is modified from dnsmasq/src/rfc1035.c.
*/
#include "rfc1035.h"
#define CHECK_LEN(header, pp, plen, len) \
((size_t)((pp) - (unsigned char *) (header) + (len)) <= (plen))
#define ADD_RDLEN(header, pp, plen, len) \
(!CHECK_LEN(header, pp, plen, len) ? 0 : (((pp) += (len)), 1))
int
extract_name(struct dns_header *header, size_t plen, unsigned char **pp,
char *name, int isExtract, int extrabytes)
{
unsigned char *cp = (unsigned char *) name, *p = *pp, *p1 = NULL;
unsigned int j, l, hops = 0;
int retvalue = 1;
if (isExtract) {
*cp = 0;
}
while (1) {
unsigned int label_type;
if (!CHECK_LEN(header, p, plen, 1)) {
return 0;
}
if ((l = *p++) == 0) {
/* end marker */
/* check that there are the correct no of bytes after the name */
if (!CHECK_LEN(header, p, plen, extrabytes)) {
return 0;
}
if (isExtract) {
if (cp != (unsigned char *) name) {
cp--;
}
*cp = 0; /* terminate: lose final period */
} else if (*cp != 0) {
retvalue = 2;
}
if (p1) { /* we jumped via compression */
*pp = p1;
} else {
*pp = p;
}
return retvalue;
}
label_type = l & 0xc0;
if (label_type == 0xc0) { /* pointer */
if (!CHECK_LEN(header, p, plen, 1)) {
return 0;
}
/* get offset */
l = (l & 0x3f) << 8;
l |= *p++;
if (!p1) { /* first jump, save location to go back to */
p1 = p;
}
hops++; /* break malicious infinite loops */
if (hops > 255) {
return 0;
}
p = l + (unsigned char *) header;
} else if (label_type == 0x80) {
return 0; /* reserved */
} else if (label_type == 0x40) { /* ELT */
unsigned int count, digs;
if ((l & 0x3f) != 1) {
return 0; /* we only understand bitstrings */
}
if (!isExtract) {
return 0; /* Cannot compare bitsrings */
}
count = *p++;
if (count == 0) {
count = 256;
}
digs = ((count - 1) >> 2) + 1;
/* output is \[x<hex>/siz]. which is digs+9 chars */
if (cp - (unsigned char *) name + digs + 9 >= MAXDNAME) {
return 0;
}
if (!CHECK_LEN(header, p, plen, (count - 1) >> 3)) {
return 0;
}
*cp++ = '\\';
*cp++ = '[';
*cp++ = 'x';
for (j = 0; j < digs; j++) {
unsigned int dig;
if (j % 2 == 0) {
dig = *p >> 4;
} else {
dig = *p++ & 0x0f;
}
*cp++ = dig < 10 ? dig + '0' : dig + 'A' - 10;
}
cp += sprintf((char *) cp, "/%d]", count);
/* do this here to overwrite the zero char from sprintf */
*cp++ = '.';
} else { /* label_type = 0 -> label. */
if (cp - (unsigned char *) name + l + 1 >= MAXDNAME) {
return 0;
}
if (!CHECK_LEN(header, p, plen, l)) {
return 0;
}
for (j = 0; j < l; j++, p++) {
if (isExtract) {
unsigned char c = *p;
if (isascii(c) && !iscntrl(c) && c != '.') {
*cp++ = *p;
} else {
return 0;
}
} else {
unsigned char c1 = *cp, c2 = *p;
if (c1 == 0) {
retvalue = 2;
} else {
cp++;
if (c1 >= 'A' && c1 <= 'Z') {
c1 += 'a' - 'A';
}
if (c2 >= 'A' && c2 <= 'Z') {
c2 += 'a' - 'A';
}
if (c1 != c2) {
retvalue = 2;
}
}
}
}
if (isExtract) {
*cp++ = '.';
} else if (*cp != 0 && *cp++ != '.') {
retvalue = 2;
}
}
}
}
/* Hash the question section. This is used to safely detect query
retransmission and to detect answers to questions we didn't ask, which
might be poisoning attacks. Note that we decode the name rather
than hash the raw bytes, since replies might be compressed differently.
We ignore case in the names for the same reason. Return all-ones
if there is not question section. */
int
questions_hash(uint64_t *hash, struct dns_header *header, size_t plen,
char *name, const unsigned char key[crypto_shorthash_KEYBYTES])
{
unsigned char *p = (unsigned char *) (header + 1);
size_t name_len;
if (ntohs(header->qdcount) != 1 ||
!extract_name(header, plen, &p, name, 1, 4) ||
(name_len = strlen(name)) > MAXDNAME) {
return -1;
}
crypto_shorthash((unsigned char *) hash, (const unsigned char *) name, name_len, key);
return 0;
}
static unsigned char *
skip_name(unsigned char *ansp, struct dns_header *header, size_t plen,
int extrabytes)
{
while (1) {
unsigned int label_type;
if (!CHECK_LEN(header, ansp, plen, 1)) {
return NULL;
}
label_type = (*ansp) & 0xc0;
if (label_type == 0xc0) {
/* pointer for compression. */
ansp += 2;
break;
} else if (label_type == 0x80) {
return NULL; /* reserved */
} else if (label_type == 0x40) {
/* Extended label type */
unsigned int count;
if (!CHECK_LEN(header, ansp, plen, 2)) {
return NULL;
}
if (((*ansp++) & 0x3f) != 1) {
return NULL; /* we only understand bitstrings */
}
count = *(ansp++); /* Bits in bitstring */
if (count == 0) { /* count == 0 means 256 bits */
ansp += 32;
} else {
ansp += ((count - 1) >> 3) + 1;
}
} else { /* label type == 0 Bottom six bits is length */
unsigned int len = (*ansp++) & 0x3f;
if (!ADD_RDLEN(header, ansp, plen, len)) {
return NULL;
}
if (len == 0) {
break; /* zero length label marks the end. */
}
}
}
if (!CHECK_LEN(header, ansp, plen, extrabytes)) {
return NULL;
}
return ansp;
}
unsigned char *
skip_questions(struct dns_header *header, size_t plen)
{
int q;
unsigned char *ansp = (unsigned char *) (header + 1);
for (q = ntohs(header->qdcount); q != 0; q--) {
if (!(ansp = skip_name(ansp, header, plen, 4))) {
return NULL;
}
ansp += 4; /* class and type */
}
return ansp;
}
unsigned char *
do_rfc1035_name(unsigned char *p, char *sval)
{
int j;
while (sval && *sval) {
unsigned char *cp = p++;
for (j = 0; *sval && (*sval != '.'); sval++, j++) {
*p++ = *sval;
}
*cp = j;
if (*sval) {
sval++;
}
}
return p;
}
int
add_resource_record(struct dns_header *header, unsigned int nameoffset, size_t plen,
unsigned char **pp, unsigned long ttl, unsigned int *offset,
unsigned short type, unsigned short class, char *format,
...)
{
va_list ap;
unsigned char *sav, *p = *pp;
int j;
unsigned short usval;
long lval;
char *sval;
if (!CHECK_LEN(header, p, plen, 12)) {
return 0;
}
PUTSHORT(nameoffset | 0xc000, p);
PUTSHORT(type, p);
PUTSHORT(class, p);
PUTLONG(ttl, p); /* TTL */
sav = p; /* Save pointer to RDLength field */
PUTSHORT(0, p); /* Placeholder RDLength */
va_start(ap, format); /* make ap point to 1st unamed argument */
for (; *format; format++)
switch (*format) {
#ifdef HAVE_IPV6
case '6':
if (!CHECK_LEN(header, p, plen, IN6ADDRSZ)) {
return 0;
}
sval = va_arg(ap, char *);
memcpy(p, sval, IN6ADDRSZ);
p += IN6ADDRSZ;
break;
#endif
case '4':
if (!CHECK_LEN(header, p, plen, INADDRSZ)) {
return 0;
}
sval = va_arg(ap, char *);
memcpy(p, sval, INADDRSZ);
p += INADDRSZ;
break;
case 's':
if (!CHECK_LEN(header, p, plen, 2)) {
return 0;
}
usval = va_arg(ap, int);
PUTSHORT(usval, p);
break;
case 'l':
if (!CHECK_LEN(header, p, plen, 4)) {
return 0;
}
lval = va_arg(ap, long);
PUTLONG(lval, p);
break;
case 'd':
/* get domain-name answer arg and store it in RDATA field */
if (offset) {
*offset = p - (unsigned char *) header;
}
p = do_rfc1035_name(p, va_arg(ap, char *));
*p++ = 0;
break;
case 't':
usval = va_arg(ap, int);
sval = va_arg(ap, char *);
if (!CHECK_LEN(header, p, plen, usval)) {
return 0;
}
if (usval != 0) {
memcpy(p, sval, usval);
}
p += usval;
break;
case 'z':
sval = va_arg(ap, char *);
usval = sval ? strlen(sval) : 0;
if (usval > 255) {
usval = 255;
}
if (!CHECK_LEN(header, p, plen, (1 + usval))) {
return 0;
}
*p++ = (unsigned char) usval;
memcpy(p, sval, usval);
p += usval;
break;
}
va_end(ap); /* clean up variable argument pointer */
j = p - sav - 2;
if (!CHECK_LEN(header, sav, plen, 2)) {
return 0;
}
PUTSHORT(j, sav); /* Now, store real RDLength */
*pp = p;
return 1;
}