| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* Code to safely remove RRs from a DNS answer */ |
| 18 |
|
| 19 |
#include "dnsmasq.h" |
| 20 |
|
| 21 |
/* Go through a domain name, find "pointers" and fix them up based on how many bytes |
| 22 |
we've chopped out of the packet, or check they don't point into an elided part. */ |
| 23 |
static int check_name(unsigned char **namep, struct dns_header *header, size_t plen, int fixup, unsigned char **rrs, int rr_count) |
| 24 |
{ |
| 25 |
unsigned char *ansp = *namep; |
| 26 |
|
| 27 |
while(1) |
| 28 |
{ |
| 29 |
unsigned int label_type; |
| 30 |
|
| 31 |
if (!CHECK_LEN(header, ansp, plen, 1)) |
| 32 |
return 0; |
| 33 |
|
| 34 |
label_type = (*ansp) & 0xc0; |
| 35 |
|
| 36 |
if (label_type == 0xc0) |
| 37 |
{ |
| 38 |
/* pointer for compression. */ |
| 39 |
unsigned int offset; |
| 40 |
int i; |
| 41 |
unsigned char *p; |
| 42 |
|
| 43 |
if (!CHECK_LEN(header, ansp, plen, 2)) |
| 44 |
return 0; |
| 45 |
|
| 46 |
offset = ((*ansp++) & 0x3f) << 8; |
| 47 |
offset |= *ansp++; |
| 48 |
|
| 49 |
p = offset + (unsigned char *)header; |
| 50 |
|
| 51 |
for (i = 0; i < rr_count; i++) |
| 52 |
if (p < rrs[i]) |
| 53 |
break; |
| 54 |
else |
| 55 |
if (i & 1) |
| 56 |
offset -= rrs[i] - rrs[i-1]; |
| 57 |
|
| 58 |
/* does the pointer end up in an elided RR? */ |
| 59 |
if (i & 1) |
| 60 |
return 0; |
| 61 |
|
| 62 |
/* No, scale the pointer */ |
| 63 |
if (fixup) |
| 64 |
{ |
| 65 |
ansp -= 2; |
| 66 |
*ansp++ = (offset >> 8) | 0xc0; |
| 67 |
*ansp++ = offset & 0xff; |
| 68 |
} |
| 69 |
break; |
| 70 |
} |
| 71 |
else if (label_type == 0x80) |
| 72 |
return 0; /* reserved */ |
| 73 |
else if (label_type == 0x40) |
| 74 |
{ |
| 75 |
/* Extended label type */ |
| 76 |
unsigned int count; |
| 77 |
|
| 78 |
if (!CHECK_LEN(header, ansp, plen, 2)) |
| 79 |
return 0; |
| 80 |
|
| 81 |
if (((*ansp++) & 0x3f) != 1) |
| 82 |
return 0; /* we only understand bitstrings */ |
| 83 |
|
| 84 |
count = *(ansp++); /* Bits in bitstring */ |
| 85 |
|
| 86 |
if (count == 0) /* count == 0 means 256 bits */ |
| 87 |
ansp += 32; |
| 88 |
else |
| 89 |
ansp += ((count-1)>>3)+1; |
| 90 |
} |
| 91 |
else |
| 92 |
{ /* label type == 0 Bottom six bits is length */ |
| 93 |
unsigned int len = (*ansp++) & 0x3f; |
| 94 |
|
| 95 |
if (!ADD_RDLEN(header, ansp, plen, len)) |
| 96 |
return 0; |
| 97 |
|
| 98 |
if (len == 0) |
| 99 |
break; /* zero length label marks the end. */ |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
*namep = ansp; |
| 104 |
|
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
/* Go through RRs and check or fixup the domain names contained within */ |
| 109 |
static int check_rrs(unsigned char *p, struct dns_header *header, size_t plen, int fixup, unsigned char **rrs, int rr_count) |
| 110 |
{ |
| 111 |
int i, j, type, class, rdlen; |
| 112 |
unsigned char *pp; |
| 113 |
|
| 114 |
for (i = 0; i < ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount); i++) |
| 115 |
{ |
| 116 |
pp = p; |
| 117 |
|
| 118 |
if (!(p = skip_name(p, header, plen, 10))) |
| 119 |
return 0; |
| 120 |
|
| 121 |
GETSHORT(type, p); |
| 122 |
GETSHORT(class, p); |
| 123 |
p += 4; /* TTL */ |
| 124 |
GETSHORT(rdlen, p); |
| 125 |
|
| 126 |
/* If this RR is to be elided, don't fix up its contents */ |
| 127 |
for (j = 0; j < rr_count; j += 2) |
| 128 |
if (rrs[j] == pp) |
| 129 |
break; |
| 130 |
|
| 131 |
if (j >= rr_count) |
| 132 |
{ |
| 133 |
/* fixup name of RR */ |
| 134 |
if (!check_name(&pp, header, plen, fixup, rrs, rr_count)) |
| 135 |
return 0; |
| 136 |
|
| 137 |
if (class == C_IN) |
| 138 |
{ |
| 139 |
short *d; |
| 140 |
|
| 141 |
for (pp = p, d = rrfilter_desc(type); *d != -1; d++) |
| 142 |
{ |
| 143 |
if (*d != 0) |
| 144 |
pp += *d; |
| 145 |
else if (!check_name(&pp, header, plen, fixup, rrs, rr_count)) |
| 146 |
return 0; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (!ADD_RDLEN(header, p, plen, rdlen)) |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
return 1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* mode may be remove EDNS0 or DNSSEC RRs or remove A or AAAA from answer section. |
| 160 |
* returns number of modified records. */ |
| 161 |
size_t rrfilter(struct dns_header *header, size_t *plen, int mode) |
| 162 |
{ |
| 163 |
static unsigned char **rrs = NULL; |
| 164 |
static int rr_sz = 0; |
| 165 |
|
| 166 |
unsigned char *p = (unsigned char *)(header+1); |
| 167 |
size_t rr_found = 0; |
| 168 |
int i, rdlen, qtype, qclass, chop_an, chop_ns, chop_ar; |
| 169 |
|
| 170 |
if (mode == RRFILTER_CONF && !daemon->filter_rr) |
| 171 |
return 0; |
| 172 |
|
| 173 |
if (ntohs(header->qdcount) != 1 || |
| 174 |
!(p = skip_name(p, header, *plen, 4))) |
| 175 |
return 0; |
| 176 |
|
| 177 |
GETSHORT(qtype, p); |
| 178 |
GETSHORT(qclass, p); |
| 179 |
|
| 180 |
/* First pass, find pointers to start and end of all the records we wish to elide: |
| 181 |
records added for DNSSEC, unless explicitly queried for */ |
| 182 |
for (chop_ns = 0, chop_an = 0, chop_ar = 0, i = 0; |
| 183 |
i < ntohs(header->ancount) + ntohs(header->nscount) + ntohs(header->arcount); |
| 184 |
i++) |
| 185 |
{ |
| 186 |
unsigned char *pstart = p; |
| 187 |
int type, class; |
| 188 |
|
| 189 |
if (!(p = skip_name(p, header, *plen, 10))) |
| 190 |
return rr_found; |
| 191 |
|
| 192 |
GETSHORT(type, p); |
| 193 |
GETSHORT(class, p); |
| 194 |
p += 4; /* TTL */ |
| 195 |
GETSHORT(rdlen, p); |
| 196 |
|
| 197 |
if (!ADD_RDLEN(header, p, *plen, rdlen)) |
| 198 |
return rr_found; |
| 199 |
|
| 200 |
if (mode == RRFILTER_EDNS0) /* EDNS */ |
| 201 |
{ |
| 202 |
/* EDNS mode, remove T_OPT from additional section only */ |
| 203 |
if (i < (ntohs(header->nscount) + ntohs(header->ancount)) || type != T_OPT) |
| 204 |
continue; |
| 205 |
} |
| 206 |
else if (mode == RRFILTER_DNSSEC) |
| 207 |
{ |
| 208 |
if (type != T_NSEC && type != T_NSEC3 && type != T_RRSIG) |
| 209 |
/* DNSSEC mode, remove SIGs and NSECs from all three sections. */ |
| 210 |
continue; |
| 211 |
|
| 212 |
/* Don't remove the answer. */ |
| 213 |
if (i < ntohs(header->ancount) && type == qtype && class == qclass) |
| 214 |
continue; |
| 215 |
} |
| 216 |
else if (qtype == T_ANY && rr_on_list(daemon->filter_rr, T_ANY)) |
| 217 |
{ |
| 218 |
/* Filter replies to ANY queries in the spirit of |
| 219 |
RFC RFC 8482 para 4.3 */ |
| 220 |
if (class != C_IN || |
| 221 |
type == T_A || type == T_AAAA || type == T_MX || type == T_CNAME) |
| 222 |
continue; |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
/* Only looking at answer section now. */ |
| 227 |
if (i >= ntohs(header->ancount)) |
| 228 |
break; |
| 229 |
|
| 230 |
if (class != C_IN) |
| 231 |
continue; |
| 232 |
|
| 233 |
if (!rr_on_list(daemon->filter_rr, type)) |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 237 |
if (!expand_workspace(&rrs, &rr_sz, rr_found + 1)) |
| 238 |
return rr_found; |
| 239 |
|
| 240 |
rrs[rr_found++] = pstart; |
| 241 |
rrs[rr_found++] = p; |
| 242 |
|
| 243 |
if (i < ntohs(header->ancount)) |
| 244 |
chop_an++; |
| 245 |
else if (i < (ntohs(header->nscount) + ntohs(header->ancount))) |
| 246 |
chop_ns++; |
| 247 |
else |
| 248 |
chop_ar++; |
| 249 |
} |
| 250 |
|
| 251 |
/* Nothing to do. */ |
| 252 |
if (rr_found == 0) |
| 253 |
return rr_found; |
| 254 |
|
| 255 |
/* Second pass, look for pointers in names in the records we're keeping and make sure they don't |
| 256 |
point to records we're going to elide. This is theoretically possible, but unlikely. If |
| 257 |
it happens, we give up and leave the answer unchanged. */ |
| 258 |
p = (unsigned char *)(header+1); |
| 259 |
|
| 260 |
/* question first */ |
| 261 |
if (!check_name(&p, header, *plen, 0, rrs, rr_found)) |
| 262 |
return rr_found; |
| 263 |
p += 4; /* qclass, qtype */ |
| 264 |
|
| 265 |
/* Now answers and NS */ |
| 266 |
if (!check_rrs(p, header, *plen, 0, rrs, rr_found)) |
| 267 |
return rr_found; |
| 268 |
|
| 269 |
/* Third pass, actually fix up pointers in the records */ |
| 270 |
p = (unsigned char *)(header+1); |
| 271 |
|
| 272 |
check_name(&p, header, *plen, 1, rrs, rr_found); |
| 273 |
p += 4; /* qclass, qtype */ |
| 274 |
|
| 275 |
check_rrs(p, header, *plen, 1, rrs, rr_found); |
| 276 |
|
| 277 |
/* Fourth pass, elide records */ |
| 278 |
for (p = rrs[0], i = 1; (unsigned)i < rr_found; i += 2) |
| 279 |
{ |
| 280 |
unsigned char *start = rrs[i]; |
| 281 |
unsigned char *end = ((unsigned)i != rr_found - 1) ? rrs[i+1] : ((unsigned char *)header) + *plen; |
| 282 |
|
| 283 |
memmove(p, start, end-start); |
| 284 |
p += end-start; |
| 285 |
} |
| 286 |
|
| 287 |
*plen = p - (unsigned char *)header; |
| 288 |
header->ancount = htons(ntohs(header->ancount) - chop_an); |
| 289 |
header->nscount = htons(ntohs(header->nscount) - chop_ns); |
| 290 |
header->arcount = htons(ntohs(header->arcount) - chop_ar); |
| 291 |
|
| 292 |
return rr_found; |
| 293 |
} |
| 294 |
|
| 295 |
/* This is used in the DNSSEC code too, hence it's exported */ |
| 296 |
short *rrfilter_desc(int type) |
| 297 |
{ |
| 298 |
/* List of RRtypes which include domains in the data. |
| 299 |
0 -> domain |
| 300 |
integer -> no. of plain bytes |
| 301 |
-1 -> end |
| 302 |
|
| 303 |
zero is not a valid RRtype, so the final entry is returned for |
| 304 |
anything which needs no mangling. |
| 305 |
*/ |
| 306 |
|
| 307 |
static short rr_desc[] = |
| 308 |
{ |
| 309 |
T_NS, 0, -1, |
| 310 |
T_MD, 0, -1, |
| 311 |
T_MF, 0, -1, |
| 312 |
T_CNAME, 0, -1, |
| 313 |
T_SOA, 0, 0, -1, |
| 314 |
T_MB, 0, -1, |
| 315 |
T_MG, 0, -1, |
| 316 |
T_MR, 0, -1, |
| 317 |
T_PTR, 0, -1, |
| 318 |
T_MINFO, 0, 0, -1, |
| 319 |
T_MX, 2, 0, -1, |
| 320 |
T_RP, 0, 0, -1, |
| 321 |
T_AFSDB, 2, 0, -1, |
| 322 |
T_RT, 2, 0, -1, |
| 323 |
T_SIG, 18, 0, -1, |
| 324 |
T_PX, 2, 0, 0, -1, |
| 325 |
T_NXT, 0, -1, |
| 326 |
T_KX, 2, 0, -1, |
| 327 |
T_SRV, 6, 0, -1, |
| 328 |
T_DNAME, 0, -1, |
| 329 |
0, -1 /* wildcard/catchall */ |
| 330 |
}; |
| 331 |
|
| 332 |
short *p = rr_desc; |
| 333 |
|
| 334 |
while (*p != type && *p != 0) |
| 335 |
while (*p++ != -1); |
| 336 |
|
| 337 |
return p+1; |
| 338 |
} |
| 339 |
|
| 340 |
/* Convert from presentation format to wire format, in place. |
| 341 |
Also map UC -> LC. |
| 342 |
Note that using extract_name to get presentation format |
| 343 |
then calling to_wire() removes compression and maps case, |
| 344 |
thus generating names in canonical form. |
| 345 |
Calling to_wire followed by from_wire is almost an identity, |
| 346 |
except that the UC remains mapped to LC. |
| 347 |
*/ |
| 348 |
int to_wire(char *name) |
| 349 |
{ |
| 350 |
unsigned char *l, *p, *q, term; |
| 351 |
unsigned int len; |
| 352 |
|
| 353 |
for (l = (unsigned char*)name; *l != 0; l = p) |
| 354 |
{ |
| 355 |
for (p = l; *p != '.' && *p != 0; p++) |
| 356 |
if (*p >= 'A' && *p <= 'Z') |
| 357 |
*p = *p - 'A' + 'a'; |
| 358 |
else if (*p == NAME_ESCAPE) |
| 359 |
{ |
| 360 |
for (q = p; *q; q++) |
| 361 |
*q = *(q+1); |
| 362 |
(*p)--; |
| 363 |
} |
| 364 |
term = *p; |
| 365 |
|
| 366 |
if ((len = p - l) != 0) |
| 367 |
memmove(l+1, l, len); |
| 368 |
*l = len; |
| 369 |
|
| 370 |
p++; |
| 371 |
|
| 372 |
if (term == 0) |
| 373 |
*p = 0; |
| 374 |
} |
| 375 |
|
| 376 |
return l + 1 - (unsigned char *)name; |
| 377 |
} |
| 378 |
|
| 379 |
/* Note: no compression allowed in input. */ |
| 380 |
void from_wire(char *name) |
| 381 |
{ |
| 382 |
unsigned char *l, *p, *last; |
| 383 |
unsigned int len; |
| 384 |
|
| 385 |
for (last = (unsigned char *)name; *last != 0; last += *last+1); |
| 386 |
|
| 387 |
for (l = (unsigned char *)name; *l != 0; l += len+1) |
| 388 |
{ |
| 389 |
len = *l; |
| 390 |
memmove(l, l+1, len); |
| 391 |
for (p = l; p < l + len; p++) |
| 392 |
{ |
| 393 |
unsigned char c = *p; |
| 394 |
if (IS_NAME_ESCAPE(c)) |
| 395 |
{ |
| 396 |
memmove(p+1, p, 1 + last - p); |
| 397 |
last++; |
| 398 |
len++; |
| 399 |
*p++ = NAME_ESCAPE; |
| 400 |
(*p)++; |
| 401 |
} |
| 402 |
} |
| 403 |
l[len] = '.'; |
| 404 |
} |
| 405 |
|
| 406 |
if ((char *)l != name) |
| 407 |
*(l-1) = 0; |
| 408 |
} |