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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
* Copyright (c) 2018-2024 OARC, Inc.
* All rights reserved.
*
* This file is part of dnsjit.
*
* dnsjit 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, either version 3 of the License, or
* (at your option) any later version.
*
* dnsjit 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 dnsjit. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "core/object/dns.h"
#include "core/object/payload.h"
#include "core/assert.h"
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#else
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#else
#ifdef HAVE_MACHINE_ENDIAN_H
#include <machine/endian.h>
#endif
#endif
#endif
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif
#ifndef bswap_16
#ifndef bswap16
#define bswap_16(x) swap16(x)
#define bswap_32(x) swap32(x)
#define bswap_64(x) swap64(x)
#else
#define bswap_16(x) bswap16(x)
#define bswap_32(x) bswap32(x)
#define bswap_64(x) bswap64(x)
#endif
#endif
#define _ERR_MALFORMED -2
#define _ERR_NEEDLABELS -3
static core_log_t _log = LOG_T_INIT("core.object.dns");
static core_object_dns_t _defaults = CORE_OBJECT_DNS_INIT(0);
static core_object_dns_label_t _defaults_label = { 0 };
static core_object_dns_rr_t _defaults_rr = { 0 };
static core_object_dns_q_t _defaults_q = { 0 };
core_log_t* core_object_dns_log()
{
return &_log;
}
core_object_dns_t* core_object_dns_new()
{
core_object_dns_t* self;
mlfatal_oom(self = malloc(sizeof(core_object_dns_t)));
*self = _defaults;
return self;
}
core_object_dns_t* core_object_dns_copy(const core_object_dns_t* self)
{
core_object_dns_t* copy;
mlassert_self();
mlfatal_oom(copy = malloc(sizeof(core_object_dns_t)));
memcpy(copy, self, sizeof(core_object_dns_t));
copy->obj_prev = 0;
return (core_object_dns_t*)copy;
}
void core_object_dns_free(core_object_dns_t* self)
{
mlassert_self();
free(self);
}
void core_object_dns_reset(core_object_dns_t* self)
{
mlassert_self();
*self = _defaults;
}
#define need8(v, p, l) \
if (l < 1) { \
break; \
} \
v = *p; \
p += 1; \
l -= 1
static inline uint16_t _need16(const void* ptr)
{
uint16_t v;
memcpy(&v, ptr, sizeof(v));
return be16toh(v);
}
#define need16(v, p, l) \
if (l < 2) { \
break; \
} \
v = _need16(p); \
p += 2; \
l -= 2
static inline uint32_t _need32(const void* ptr)
{
uint32_t v;
memcpy(&v, ptr, sizeof(v));
return be32toh(v);
}
#define need32(v, p, l) \
if (l < 4) { \
break; \
} \
v = _need32(p); \
p += 4; \
l -= 4
#define needxb(b, x, p, l) \
if (l < x) { \
break; \
} \
memcpy(b, p, x); \
p += x; \
l -= x
#define advancexb(x, p, l) \
if (l < x) { \
break; \
} \
p += x; \
l -= x
int core_object_dns_parse_header(core_object_dns_t* self)
{
const core_object_payload_t* payload;
uint8_t byte;
mlassert_self();
if (!(payload = (core_object_payload_t*)self->obj_prev) || payload->obj_type != CORE_OBJECT_PAYLOAD) {
mlfatal("no obj_prev or invalid type");
}
if (!payload->payload || !payload->len) {
mlfatal("no payload set or zero length");
}
self->payload = self->at = payload->payload;
self->len = self->left = payload->len;
for (;;) {
if (self->includes_dnslen) {
need16(self->dnslen, self->at, self->left);
self->have_dnslen = 1;
}
need16(self->id, self->at, self->left);
self->have_id = 1;
need8(byte, self->at, self->left);
self->qr = byte & (1 << 7) ? 1 : 0;
self->opcode = (byte >> 3) & 0xf;
self->aa = byte & (1 << 2) ? 1 : 0;
self->tc = byte & (1 << 1) ? 1 : 0;
self->rd = byte & (1 << 0) ? 1 : 0;
self->have_qr = self->have_opcode = self->have_aa = self->have_tc = self->have_rd = 1;
need8(byte, self->at, self->left);
self->ra = byte & (1 << 7) ? 1 : 0;
self->z = byte & (1 << 6) ? 1 : 0;
self->ad = byte & (1 << 5) ? 1 : 0;
self->cd = byte & (1 << 4) ? 1 : 0;
self->rcode = byte & 0xf;
self->have_ra = self->have_z = self->have_ad = self->have_cd = self->have_rcode = 1;
need16(self->qdcount, self->at, self->left);
self->have_qdcount = 1;
need16(self->ancount, self->at, self->left);
self->have_ancount = 1;
need16(self->nscount, self->at, self->left);
self->have_nscount = 1;
need16(self->arcount, self->at, self->left);
self->have_arcount = 1;
return 0;
}
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
static inline size_t _rdata_labels(uint16_t type)
{
switch (type) {
case CORE_OBJECT_DNS_TYPE_NS:
case CORE_OBJECT_DNS_TYPE_MD:
case CORE_OBJECT_DNS_TYPE_MF:
case CORE_OBJECT_DNS_TYPE_CNAME:
case CORE_OBJECT_DNS_TYPE_MB:
case CORE_OBJECT_DNS_TYPE_MG:
case CORE_OBJECT_DNS_TYPE_MR:
case CORE_OBJECT_DNS_TYPE_PTR:
case CORE_OBJECT_DNS_TYPE_NXT:
case CORE_OBJECT_DNS_TYPE_DNAME:
case CORE_OBJECT_DNS_TYPE_NSEC:
case CORE_OBJECT_DNS_TYPE_TKEY:
case CORE_OBJECT_DNS_TYPE_TSIG:
return 1;
case CORE_OBJECT_DNS_TYPE_SOA:
case CORE_OBJECT_DNS_TYPE_MINFO:
case CORE_OBJECT_DNS_TYPE_RP:
case CORE_OBJECT_DNS_TYPE_TALINK:
return 2;
case CORE_OBJECT_DNS_TYPE_MX:
case CORE_OBJECT_DNS_TYPE_AFSDB:
case CORE_OBJECT_DNS_TYPE_RT:
case CORE_OBJECT_DNS_TYPE_KX:
case CORE_OBJECT_DNS_TYPE_LP:
return 1;
case CORE_OBJECT_DNS_TYPE_PX:
return 2;
case CORE_OBJECT_DNS_TYPE_SIG:
case CORE_OBJECT_DNS_TYPE_RRSIG:
return 1;
case CORE_OBJECT_DNS_TYPE_SRV:
return 1;
case CORE_OBJECT_DNS_TYPE_NAPTR:
return 1;
case CORE_OBJECT_DNS_TYPE_HIP:
return 1;
default:
break;
}
return 0;
}
static inline size_t _label(core_object_dns_t* self, core_object_dns_label_t* label, size_t labels)
{
size_t n;
for (n = 0; self->left && n < labels; n++) {
core_object_dns_label_t* l = &label[n];
*l = _defaults_label;
need8(l->length, self->at, self->left);
if ((l->length & 0xc0) == 0xc0) {
need8(l->offset, self->at, self->left);
l->offset |= (l->length & 0x3f) << 8;
l->have_offset = 1;
return n;
} else if (l->length & 0xc0) {
l->extension_bits = l->length >> 6;
l->have_extension_bits = 1;
return n;
} else if (l->length) {
l->have_length = 1;
l->offset = self->at - self->payload - 1;
advancexb(l->length, self->at, self->left);
l->have_dn = 1;
} else {
l->is_end = 1;
return n;
}
}
return n;
}
int core_object_dns_parse_q(core_object_dns_t* self, core_object_dns_q_t* q, core_object_dns_label_t* label, size_t labels)
{
mlassert_self();
mlassert(q, "q is nil");
mlassert(label, "label is nil");
mlassert(labels, "labels is zero");
mlassert(self->at, "at is nil");
for (;;) {
*q = _defaults_q;
q->labels = _label(self, label, labels);
if (q->labels < labels) {
core_object_dns_label_t* l = &label[q->labels];
if (!(l->have_offset | l->have_extension_bits | l->is_end)) {
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
} else {
mlwarning("need more labels, aborting DNS parsing");
return _ERR_NEEDLABELS;
}
q->labels++;
need16(q->type, self->at, self->left);
q->have_type = 1;
need16(q->class, self->at, self->left);
q->have_class = 1;
return 0;
}
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
int core_object_dns_parse_rr(core_object_dns_t* self, core_object_dns_rr_t* rr, core_object_dns_label_t* label, size_t labels)
{
size_t rdata_label_sets;
mlassert_self();
mlassert(rr, "rr is nil");
mlassert(label, "label is nil");
mlassert(labels, "labels is zero");
mlassert(self->at, "at is nil");
for (;;) {
*rr = _defaults_rr;
rr->labels = _label(self, label, labels);
if (rr->labels < labels) {
core_object_dns_label_t* l = &label[rr->labels];
if (!(l->have_offset | l->have_extension_bits | l->is_end)) {
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
} else {
mlwarning("need more labels, aborting DNS parsing");
return _ERR_NEEDLABELS;
}
rr->labels++;
need16(rr->type, self->at, self->left);
rr->have_type = 1;
need16(rr->class, self->at, self->left);
rr->have_class = 1;
need32(rr->ttl, self->at, self->left);
rr->have_ttl = 1;
need16(rr->rdlength, self->at, self->left);
rr->have_rdlength = 1;
rr->rdata_offset = self->at - self->payload;
if (!(rdata_label_sets = _rdata_labels(rr->type))) {
advancexb(rr->rdlength, self->at, self->left);
rr->have_rdata = 1;
return 0;
}
switch (rr->type) {
case CORE_OBJECT_DNS_TYPE_MX:
case CORE_OBJECT_DNS_TYPE_AFSDB:
case CORE_OBJECT_DNS_TYPE_RT:
case CORE_OBJECT_DNS_TYPE_KX:
case CORE_OBJECT_DNS_TYPE_LP:
case CORE_OBJECT_DNS_TYPE_PX:
advancexb(2, self->at, self->left);
break;
case CORE_OBJECT_DNS_TYPE_SIG:
case CORE_OBJECT_DNS_TYPE_RRSIG:
advancexb(18, self->at, self->left);
break;
case CORE_OBJECT_DNS_TYPE_SRV:
advancexb(6, self->at, self->left);
break;
case CORE_OBJECT_DNS_TYPE_NAPTR: {
uint8_t naptr_length;
advancexb(4, self->at, self->left);
need8(naptr_length, self->at, self->left);
advancexb(naptr_length, self->at, self->left);
need8(naptr_length, self->at, self->left);
advancexb(naptr_length, self->at, self->left);
need8(naptr_length, self->at, self->left);
advancexb(naptr_length, self->at, self->left);
} break;
case CORE_OBJECT_DNS_TYPE_HIP: {
uint8_t hit_length;
uint16_t pk_length;
need8(hit_length, self->at, self->left);
advancexb(1, self->at, self->left);
need16(pk_length, self->at, self->left);
advancexb(hit_length, self->at, self->left);
advancexb(pk_length, self->at, self->left);
if (self->at - self->payload >= rr->rdata_offset + rr->rdlength) {
rdata_label_sets = 0;
}
} break;
}
while (rdata_label_sets) {
rr->rdata_labels += _label(self, &label[rr->labels + rr->rdata_labels], labels - rr->labels - rr->rdata_labels);
if (rr->labels + rr->rdata_labels < labels) {
core_object_dns_label_t* l = &label[rr->labels + rr->rdata_labels];
if (!(l->have_offset | l->have_extension_bits | l->is_end)) {
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
} else {
mlwarning("need more labels, aborting DNS parsing");
return _ERR_NEEDLABELS;
}
rr->rdata_labels++;
if (rr->type == CORE_OBJECT_DNS_TYPE_HIP && self->at - self->payload < rr->rdata_offset + rr->rdlength) {
continue;
}
rdata_label_sets--;
}
if (self->at - self->payload < rr->rdata_offset + rr->rdlength) {
rr->padding_offset = self->at - self->payload;
rr->padding_length = rr->rdlength - (rr->padding_offset - rr->rdata_offset);
advancexb(rr->padding_length, self->at, self->left);
/*
* TODO:
*
* This can indicate padding but we do not set that we have padding
* yet because we need to fully understand all record types before
* that and process valid data after the labels
*
rr->have_padding = 1;
*/
} else if (self->at - self->payload > rr->rdata_offset + rr->rdlength) {
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
rr->have_rdata = 1;
return 0;
}
// TODO: error here on malformed/truncated? could be quite spammy
return _ERR_MALFORMED;
}
|