1ae8c6e27Sflorian /* 2ae8c6e27Sflorian * iterator/iter_scrub.c - scrubbing, normalization, sanitization of DNS msgs. 3ae8c6e27Sflorian * 4ae8c6e27Sflorian * Copyright (c) 2007, NLnet Labs. All rights reserved. 5ae8c6e27Sflorian * 6ae8c6e27Sflorian * This software is open source. 7ae8c6e27Sflorian * 8ae8c6e27Sflorian * Redistribution and use in source and binary forms, with or without 9ae8c6e27Sflorian * modification, are permitted provided that the following conditions 10ae8c6e27Sflorian * are met: 11ae8c6e27Sflorian * 12ae8c6e27Sflorian * Redistributions of source code must retain the above copyright notice, 13ae8c6e27Sflorian * this list of conditions and the following disclaimer. 14ae8c6e27Sflorian * 15ae8c6e27Sflorian * Redistributions in binary form must reproduce the above copyright notice, 16ae8c6e27Sflorian * this list of conditions and the following disclaimer in the documentation 17ae8c6e27Sflorian * and/or other materials provided with the distribution. 18ae8c6e27Sflorian * 19ae8c6e27Sflorian * Neither the name of the NLNET LABS nor the names of its contributors may 20ae8c6e27Sflorian * be used to endorse or promote products derived from this software without 21ae8c6e27Sflorian * specific prior written permission. 22ae8c6e27Sflorian * 23ae8c6e27Sflorian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24ae8c6e27Sflorian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25ae8c6e27Sflorian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26ae8c6e27Sflorian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27ae8c6e27Sflorian * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28ae8c6e27Sflorian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29ae8c6e27Sflorian * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30ae8c6e27Sflorian * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31ae8c6e27Sflorian * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32ae8c6e27Sflorian * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33ae8c6e27Sflorian * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34ae8c6e27Sflorian */ 35ae8c6e27Sflorian 36ae8c6e27Sflorian /** 37ae8c6e27Sflorian * \file 38ae8c6e27Sflorian * 39ae8c6e27Sflorian * This file has routine(s) for cleaning up incoming DNS messages from 40ae8c6e27Sflorian * possible useless or malicious junk in it. 41ae8c6e27Sflorian */ 42ae8c6e27Sflorian #include "config.h" 43ae8c6e27Sflorian #include "iterator/iter_scrub.h" 44ae8c6e27Sflorian #include "iterator/iterator.h" 45ae8c6e27Sflorian #include "iterator/iter_priv.h" 46ae8c6e27Sflorian #include "services/cache/rrset.h" 47ae8c6e27Sflorian #include "util/log.h" 48ae8c6e27Sflorian #include "util/net_help.h" 49ae8c6e27Sflorian #include "util/regional.h" 50ae8c6e27Sflorian #include "util/config_file.h" 51ae8c6e27Sflorian #include "util/module.h" 52ae8c6e27Sflorian #include "util/data/msgparse.h" 53ae8c6e27Sflorian #include "util/data/dname.h" 54ae8c6e27Sflorian #include "util/data/msgreply.h" 55ae8c6e27Sflorian #include "util/alloc.h" 56ae8c6e27Sflorian #include "sldns/sbuffer.h" 57ae8c6e27Sflorian 58ae8c6e27Sflorian /** RRset flag used during scrubbing. The RRset is OK. */ 59ae8c6e27Sflorian #define RRSET_SCRUB_OK 0x80 60ae8c6e27Sflorian 61ae8c6e27Sflorian /** remove rrset, update loop variables */ 62ae8c6e27Sflorian static void 63ae8c6e27Sflorian remove_rrset(const char* str, sldns_buffer* pkt, struct msg_parse* msg, 64ae8c6e27Sflorian struct rrset_parse* prev, struct rrset_parse** rrset) 65ae8c6e27Sflorian { 66ae8c6e27Sflorian if(verbosity >= VERB_QUERY && str 67ae8c6e27Sflorian && (*rrset)->dname_len <= LDNS_MAX_DOMAINLEN) { 68ae8c6e27Sflorian uint8_t buf[LDNS_MAX_DOMAINLEN+1]; 69ae8c6e27Sflorian dname_pkt_copy(pkt, buf, (*rrset)->dname); 70ae8c6e27Sflorian log_nametypeclass(VERB_QUERY, str, buf, 71ae8c6e27Sflorian (*rrset)->type, ntohs((*rrset)->rrset_class)); 72ae8c6e27Sflorian } 73ae8c6e27Sflorian if(prev) 74ae8c6e27Sflorian prev->rrset_all_next = (*rrset)->rrset_all_next; 75ae8c6e27Sflorian else msg->rrset_first = (*rrset)->rrset_all_next; 76ae8c6e27Sflorian if(msg->rrset_last == *rrset) 77ae8c6e27Sflorian msg->rrset_last = prev; 78ae8c6e27Sflorian msg->rrset_count --; 79ae8c6e27Sflorian switch((*rrset)->section) { 80ae8c6e27Sflorian case LDNS_SECTION_ANSWER: msg->an_rrsets--; break; 81ae8c6e27Sflorian case LDNS_SECTION_AUTHORITY: msg->ns_rrsets--; break; 82ae8c6e27Sflorian case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets--; break; 83ae8c6e27Sflorian default: log_assert(0); 84ae8c6e27Sflorian } 85ae8c6e27Sflorian msgparse_bucket_remove(msg, *rrset); 86ae8c6e27Sflorian *rrset = (*rrset)->rrset_all_next; 87ae8c6e27Sflorian } 88ae8c6e27Sflorian 89ae8c6e27Sflorian /** return true if rr type has additional names in it */ 90ae8c6e27Sflorian static int 91ae8c6e27Sflorian has_additional(uint16_t t) 92ae8c6e27Sflorian { 93ae8c6e27Sflorian switch(t) { 94ae8c6e27Sflorian case LDNS_RR_TYPE_MB: 95ae8c6e27Sflorian case LDNS_RR_TYPE_MD: 96ae8c6e27Sflorian case LDNS_RR_TYPE_MF: 97ae8c6e27Sflorian case LDNS_RR_TYPE_NS: 98ae8c6e27Sflorian case LDNS_RR_TYPE_MX: 99ae8c6e27Sflorian case LDNS_RR_TYPE_KX: 100ae8c6e27Sflorian case LDNS_RR_TYPE_SRV: 101ae8c6e27Sflorian return 1; 102ae8c6e27Sflorian case LDNS_RR_TYPE_NAPTR: 103ae8c6e27Sflorian /* TODO: NAPTR not supported, glue stripped off */ 104ae8c6e27Sflorian return 0; 105ae8c6e27Sflorian } 106ae8c6e27Sflorian return 0; 107ae8c6e27Sflorian } 108ae8c6e27Sflorian 109ae8c6e27Sflorian /** get additional name from rrset RR, return false if no name present */ 110ae8c6e27Sflorian static int 111ae8c6e27Sflorian get_additional_name(struct rrset_parse* rrset, struct rr_parse* rr, 112ae8c6e27Sflorian uint8_t** nm, size_t* nmlen, sldns_buffer* pkt) 113ae8c6e27Sflorian { 114ae8c6e27Sflorian size_t offset = 0; 115ae8c6e27Sflorian size_t len, oldpos; 116ae8c6e27Sflorian switch(rrset->type) { 117ae8c6e27Sflorian case LDNS_RR_TYPE_MB: 118ae8c6e27Sflorian case LDNS_RR_TYPE_MD: 119ae8c6e27Sflorian case LDNS_RR_TYPE_MF: 120ae8c6e27Sflorian case LDNS_RR_TYPE_NS: 121ae8c6e27Sflorian offset = 0; 122ae8c6e27Sflorian break; 123ae8c6e27Sflorian case LDNS_RR_TYPE_MX: 124ae8c6e27Sflorian case LDNS_RR_TYPE_KX: 125ae8c6e27Sflorian offset = 2; 126ae8c6e27Sflorian break; 127ae8c6e27Sflorian case LDNS_RR_TYPE_SRV: 128ae8c6e27Sflorian offset = 6; 129ae8c6e27Sflorian break; 130ae8c6e27Sflorian case LDNS_RR_TYPE_NAPTR: 131ae8c6e27Sflorian /* TODO: NAPTR not supported, glue stripped off */ 132ae8c6e27Sflorian return 0; 133ae8c6e27Sflorian default: 134ae8c6e27Sflorian return 0; 135ae8c6e27Sflorian } 136ae8c6e27Sflorian len = sldns_read_uint16(rr->ttl_data+sizeof(uint32_t)); 137ae8c6e27Sflorian if(len < offset+1) 138ae8c6e27Sflorian return 0; /* rdata field too small */ 139ae8c6e27Sflorian *nm = rr->ttl_data+sizeof(uint32_t)+sizeof(uint16_t)+offset; 140ae8c6e27Sflorian oldpos = sldns_buffer_position(pkt); 141ae8c6e27Sflorian sldns_buffer_set_position(pkt, (size_t)(*nm - sldns_buffer_begin(pkt))); 142ae8c6e27Sflorian *nmlen = pkt_dname_len(pkt); 143ae8c6e27Sflorian sldns_buffer_set_position(pkt, oldpos); 144ae8c6e27Sflorian if(*nmlen == 0) 145ae8c6e27Sflorian return 0; 146ae8c6e27Sflorian return 1; 147ae8c6e27Sflorian } 148ae8c6e27Sflorian 149ae8c6e27Sflorian /** Place mark on rrsets in additional section they are OK */ 150ae8c6e27Sflorian static void 151ae8c6e27Sflorian mark_additional_rrset(sldns_buffer* pkt, struct msg_parse* msg, 152ae8c6e27Sflorian struct rrset_parse* rrset) 153ae8c6e27Sflorian { 154ae8c6e27Sflorian /* Mark A and AAAA for NS as appropriate additional section info. */ 155ae8c6e27Sflorian uint8_t* nm = NULL; 156ae8c6e27Sflorian size_t nmlen = 0; 157ae8c6e27Sflorian struct rr_parse* rr; 158ae8c6e27Sflorian 159ae8c6e27Sflorian if(!has_additional(rrset->type)) 160ae8c6e27Sflorian return; 161ae8c6e27Sflorian for(rr = rrset->rr_first; rr; rr = rr->next) { 162ae8c6e27Sflorian if(get_additional_name(rrset, rr, &nm, &nmlen, pkt)) { 163ae8c6e27Sflorian /* mark A */ 164ae8c6e27Sflorian hashvalue_type h = pkt_hash_rrset(pkt, nm, 165ae8c6e27Sflorian LDNS_RR_TYPE_A, rrset->rrset_class, 0); 166ae8c6e27Sflorian struct rrset_parse* r = msgparse_hashtable_lookup( 167ae8c6e27Sflorian msg, pkt, h, 0, nm, nmlen, 168ae8c6e27Sflorian LDNS_RR_TYPE_A, rrset->rrset_class); 169ae8c6e27Sflorian if(r && r->section == LDNS_SECTION_ADDITIONAL) { 170ae8c6e27Sflorian r->flags |= RRSET_SCRUB_OK; 171ae8c6e27Sflorian } 172ae8c6e27Sflorian 173ae8c6e27Sflorian /* mark AAAA */ 174ae8c6e27Sflorian h = pkt_hash_rrset(pkt, nm, LDNS_RR_TYPE_AAAA, 175ae8c6e27Sflorian rrset->rrset_class, 0); 176ae8c6e27Sflorian r = msgparse_hashtable_lookup(msg, pkt, h, 0, nm, 177ae8c6e27Sflorian nmlen, LDNS_RR_TYPE_AAAA, rrset->rrset_class); 178ae8c6e27Sflorian if(r && r->section == LDNS_SECTION_ADDITIONAL) { 179ae8c6e27Sflorian r->flags |= RRSET_SCRUB_OK; 180ae8c6e27Sflorian } 181ae8c6e27Sflorian } 182ae8c6e27Sflorian } 183ae8c6e27Sflorian } 184ae8c6e27Sflorian 185ae8c6e27Sflorian /** Get target name of a CNAME */ 186ae8c6e27Sflorian static int 187ae8c6e27Sflorian parse_get_cname_target(struct rrset_parse* rrset, uint8_t** sname, 1885a7d75e6Ssthen size_t* snamelen, sldns_buffer* pkt) 189ae8c6e27Sflorian { 1905a7d75e6Ssthen size_t oldpos, dlen; 191ae8c6e27Sflorian if(rrset->rr_count != 1) { 192ae8c6e27Sflorian struct rr_parse* sig; 193ae8c6e27Sflorian verbose(VERB_ALGO, "Found CNAME rrset with " 194ae8c6e27Sflorian "size > 1: %u", (unsigned)rrset->rr_count); 195ae8c6e27Sflorian /* use the first CNAME! */ 196ae8c6e27Sflorian rrset->rr_count = 1; 197ae8c6e27Sflorian rrset->size = rrset->rr_first->size; 198ae8c6e27Sflorian for(sig=rrset->rrsig_first; sig; sig=sig->next) 199ae8c6e27Sflorian rrset->size += sig->size; 200ae8c6e27Sflorian rrset->rr_last = rrset->rr_first; 201ae8c6e27Sflorian rrset->rr_first->next = NULL; 202ae8c6e27Sflorian } 203ae8c6e27Sflorian if(rrset->rr_first->size < sizeof(uint16_t)+1) 204ae8c6e27Sflorian return 0; /* CNAME rdata too small */ 205ae8c6e27Sflorian *sname = rrset->rr_first->ttl_data + sizeof(uint32_t) 206ae8c6e27Sflorian + sizeof(uint16_t); /* skip ttl, rdatalen */ 207ae8c6e27Sflorian *snamelen = rrset->rr_first->size - sizeof(uint16_t); 2085a7d75e6Ssthen 2095a7d75e6Ssthen if(rrset->rr_first->outside_packet) { 2105a7d75e6Ssthen if(!dname_valid(*sname, *snamelen)) 2115a7d75e6Ssthen return 0; 2125a7d75e6Ssthen return 1; 2135a7d75e6Ssthen } 2145a7d75e6Ssthen oldpos = sldns_buffer_position(pkt); 2155a7d75e6Ssthen sldns_buffer_set_position(pkt, (size_t)(*sname - sldns_buffer_begin(pkt))); 2165a7d75e6Ssthen dlen = pkt_dname_len(pkt); 2175a7d75e6Ssthen sldns_buffer_set_position(pkt, oldpos); 2185a7d75e6Ssthen if(dlen == 0) 2195a7d75e6Ssthen return 0; /* parse fail on the rdata name */ 2205a7d75e6Ssthen *snamelen = dlen; 221ae8c6e27Sflorian return 1; 222ae8c6e27Sflorian } 223ae8c6e27Sflorian 224ae8c6e27Sflorian /** Synthesize CNAME from DNAME, false if too long */ 225ae8c6e27Sflorian static int 226ae8c6e27Sflorian synth_cname(uint8_t* qname, size_t qnamelen, struct rrset_parse* dname_rrset, 227ae8c6e27Sflorian uint8_t* alias, size_t* aliaslen, sldns_buffer* pkt) 228ae8c6e27Sflorian { 229ae8c6e27Sflorian /* we already know that sname is a strict subdomain of DNAME owner */ 230ae8c6e27Sflorian uint8_t* dtarg = NULL; 231ae8c6e27Sflorian size_t dtarglen; 2325a7d75e6Ssthen if(!parse_get_cname_target(dname_rrset, &dtarg, &dtarglen, pkt)) 233ae8c6e27Sflorian return 0; 23457403691Sflorian if(qnamelen <= dname_rrset->dname_len) 23557403691Sflorian return 0; 23657403691Sflorian if(qnamelen == 0) 23757403691Sflorian return 0; 238ae8c6e27Sflorian log_assert(qnamelen > dname_rrset->dname_len); 239ae8c6e27Sflorian /* DNAME from com. to net. with qname example.com. -> example.net. */ 240ae8c6e27Sflorian /* so: \3com\0 to \3net\0 and qname \7example\3com\0 */ 241ae8c6e27Sflorian *aliaslen = qnamelen + dtarglen - dname_rrset->dname_len; 242ae8c6e27Sflorian if(*aliaslen > LDNS_MAX_DOMAINLEN) 243ae8c6e27Sflorian return 0; /* should have been RCODE YXDOMAIN */ 244ae8c6e27Sflorian /* decompress dnames into buffer, we know it fits */ 245ae8c6e27Sflorian dname_pkt_copy(pkt, alias, qname); 246ae8c6e27Sflorian dname_pkt_copy(pkt, alias+(qnamelen-dname_rrset->dname_len), dtarg); 247ae8c6e27Sflorian return 1; 248ae8c6e27Sflorian } 249ae8c6e27Sflorian 250ae8c6e27Sflorian /** synthesize a CNAME rrset */ 251ae8c6e27Sflorian static struct rrset_parse* 252ae8c6e27Sflorian synth_cname_rrset(uint8_t** sname, size_t* snamelen, uint8_t* alias, 253ae8c6e27Sflorian size_t aliaslen, struct regional* region, struct msg_parse* msg, 254ae8c6e27Sflorian struct rrset_parse* rrset, struct rrset_parse* prev, 255ae8c6e27Sflorian struct rrset_parse* nx, sldns_buffer* pkt) 256ae8c6e27Sflorian { 257ae8c6e27Sflorian struct rrset_parse* cn = (struct rrset_parse*)regional_alloc(region, 258ae8c6e27Sflorian sizeof(struct rrset_parse)); 259ae8c6e27Sflorian if(!cn) 260ae8c6e27Sflorian return NULL; 261ae8c6e27Sflorian memset(cn, 0, sizeof(*cn)); 262ae8c6e27Sflorian cn->rr_first = (struct rr_parse*)regional_alloc(region, 263ae8c6e27Sflorian sizeof(struct rr_parse)); 264ae8c6e27Sflorian if(!cn->rr_first) 265ae8c6e27Sflorian return NULL; 266ae8c6e27Sflorian cn->rr_last = cn->rr_first; 267ae8c6e27Sflorian /* CNAME from sname to alias */ 268ae8c6e27Sflorian cn->dname = (uint8_t*)regional_alloc(region, *snamelen); 269ae8c6e27Sflorian if(!cn->dname) 270ae8c6e27Sflorian return NULL; 271ae8c6e27Sflorian dname_pkt_copy(pkt, cn->dname, *sname); 272ae8c6e27Sflorian cn->dname_len = *snamelen; 273ae8c6e27Sflorian cn->type = LDNS_RR_TYPE_CNAME; 274ae8c6e27Sflorian cn->section = rrset->section; 275ae8c6e27Sflorian cn->rrset_class = rrset->rrset_class; 276ae8c6e27Sflorian cn->rr_count = 1; 277ae8c6e27Sflorian cn->size = sizeof(uint16_t) + aliaslen; 278ae8c6e27Sflorian cn->hash=pkt_hash_rrset(pkt, cn->dname, cn->type, cn->rrset_class, 0); 279ae8c6e27Sflorian /* allocate TTL + rdatalen + uncompressed dname */ 280ae8c6e27Sflorian memset(cn->rr_first, 0, sizeof(struct rr_parse)); 281ae8c6e27Sflorian cn->rr_first->outside_packet = 1; 282ae8c6e27Sflorian cn->rr_first->ttl_data = (uint8_t*)regional_alloc(region, 283ae8c6e27Sflorian sizeof(uint32_t)+sizeof(uint16_t)+aliaslen); 284ae8c6e27Sflorian if(!cn->rr_first->ttl_data) 285ae8c6e27Sflorian return NULL; 286ae8c6e27Sflorian sldns_write_uint32(cn->rr_first->ttl_data, 0); /* TTL = 0 */ 287ae8c6e27Sflorian sldns_write_uint16(cn->rr_first->ttl_data+4, aliaslen); 288ae8c6e27Sflorian memmove(cn->rr_first->ttl_data+6, alias, aliaslen); 289ae8c6e27Sflorian cn->rr_first->size = sizeof(uint16_t)+aliaslen; 290ae8c6e27Sflorian 291ae8c6e27Sflorian /* link it in */ 292ae8c6e27Sflorian cn->rrset_all_next = nx; 293ae8c6e27Sflorian if(prev) 294ae8c6e27Sflorian prev->rrset_all_next = cn; 295ae8c6e27Sflorian else msg->rrset_first = cn; 296ae8c6e27Sflorian if(nx == NULL) 297ae8c6e27Sflorian msg->rrset_last = cn; 298ae8c6e27Sflorian msg->rrset_count ++; 299ae8c6e27Sflorian msg->an_rrsets++; 300ae8c6e27Sflorian /* it is not inserted in the msg hashtable. */ 301ae8c6e27Sflorian 302ae8c6e27Sflorian *sname = cn->rr_first->ttl_data + sizeof(uint32_t)+sizeof(uint16_t); 303ae8c6e27Sflorian *snamelen = aliaslen; 304ae8c6e27Sflorian return cn; 305ae8c6e27Sflorian } 306ae8c6e27Sflorian 307ae8c6e27Sflorian /** check if DNAME applies to a name */ 308ae8c6e27Sflorian static int 309ae8c6e27Sflorian pkt_strict_sub(sldns_buffer* pkt, uint8_t* sname, uint8_t* dr) 310ae8c6e27Sflorian { 311ae8c6e27Sflorian uint8_t buf1[LDNS_MAX_DOMAINLEN+1]; 312ae8c6e27Sflorian uint8_t buf2[LDNS_MAX_DOMAINLEN+1]; 313ae8c6e27Sflorian /* decompress names */ 314ae8c6e27Sflorian dname_pkt_copy(pkt, buf1, sname); 315ae8c6e27Sflorian dname_pkt_copy(pkt, buf2, dr); 316ae8c6e27Sflorian return dname_strict_subdomain_c(buf1, buf2); 317ae8c6e27Sflorian } 318ae8c6e27Sflorian 319ae8c6e27Sflorian /** check subdomain with decompression */ 320ae8c6e27Sflorian static int 321ae8c6e27Sflorian pkt_sub(sldns_buffer* pkt, uint8_t* comprname, uint8_t* zone) 322ae8c6e27Sflorian { 323ae8c6e27Sflorian uint8_t buf[LDNS_MAX_DOMAINLEN+1]; 324ae8c6e27Sflorian dname_pkt_copy(pkt, buf, comprname); 325ae8c6e27Sflorian return dname_subdomain_c(buf, zone); 326ae8c6e27Sflorian } 327ae8c6e27Sflorian 328ae8c6e27Sflorian /** check subdomain with decompression, compressed is parent */ 329ae8c6e27Sflorian static int 330ae8c6e27Sflorian sub_of_pkt(sldns_buffer* pkt, uint8_t* zone, uint8_t* comprname) 331ae8c6e27Sflorian { 332ae8c6e27Sflorian uint8_t buf[LDNS_MAX_DOMAINLEN+1]; 333ae8c6e27Sflorian dname_pkt_copy(pkt, buf, comprname); 334ae8c6e27Sflorian return dname_subdomain_c(zone, buf); 335ae8c6e27Sflorian } 336ae8c6e27Sflorian 337ae8c6e27Sflorian /** Check if there are SOA records in the authority section (negative) */ 338ae8c6e27Sflorian static int 339ae8c6e27Sflorian soa_in_auth(struct msg_parse* msg) 340ae8c6e27Sflorian { 341ae8c6e27Sflorian struct rrset_parse* rrset; 342ae8c6e27Sflorian for(rrset = msg->rrset_first; rrset; rrset = rrset->rrset_all_next) 343ae8c6e27Sflorian if(rrset->type == LDNS_RR_TYPE_SOA && 344ae8c6e27Sflorian rrset->section == LDNS_SECTION_AUTHORITY) 345ae8c6e27Sflorian return 1; 346ae8c6e27Sflorian return 0; 347ae8c6e27Sflorian } 348ae8c6e27Sflorian 349ae8c6e27Sflorian /** 350ae8c6e27Sflorian * This routine normalizes a response. This includes removing "irrelevant" 351ae8c6e27Sflorian * records from the answer and additional sections and (re)synthesizing 352ae8c6e27Sflorian * CNAMEs from DNAMEs, if present. 353ae8c6e27Sflorian * 354ae8c6e27Sflorian * @param pkt: packet. 355ae8c6e27Sflorian * @param msg: msg to normalize. 356ae8c6e27Sflorian * @param qinfo: original query. 357ae8c6e27Sflorian * @param region: where to allocate synthesized CNAMEs. 358ae8c6e27Sflorian * @return 0 on error. 359ae8c6e27Sflorian */ 360ae8c6e27Sflorian static int 361ae8c6e27Sflorian scrub_normalize(sldns_buffer* pkt, struct msg_parse* msg, 362ae8c6e27Sflorian struct query_info* qinfo, struct regional* region) 363ae8c6e27Sflorian { 364ae8c6e27Sflorian uint8_t* sname = qinfo->qname; 365ae8c6e27Sflorian size_t snamelen = qinfo->qname_len; 366ae8c6e27Sflorian struct rrset_parse* rrset, *prev, *nsset=NULL; 367ae8c6e27Sflorian 368ae8c6e27Sflorian if(FLAGS_GET_RCODE(msg->flags) != LDNS_RCODE_NOERROR && 369ae8c6e27Sflorian FLAGS_GET_RCODE(msg->flags) != LDNS_RCODE_NXDOMAIN) 370ae8c6e27Sflorian return 1; 371ae8c6e27Sflorian 372ae8c6e27Sflorian /* For the ANSWER section, remove all "irrelevant" records and add 373ae8c6e27Sflorian * synthesized CNAMEs from DNAMEs 374ae8c6e27Sflorian * This will strip out-of-order CNAMEs as well. */ 375ae8c6e27Sflorian 376ae8c6e27Sflorian /* walk through the parse packet rrset list, keep track of previous 377ae8c6e27Sflorian * for insert and delete ease, and examine every RRset */ 378ae8c6e27Sflorian prev = NULL; 379ae8c6e27Sflorian rrset = msg->rrset_first; 380ae8c6e27Sflorian while(rrset && rrset->section == LDNS_SECTION_ANSWER) { 381ae8c6e27Sflorian if(rrset->type == LDNS_RR_TYPE_DNAME && 382ae8c6e27Sflorian pkt_strict_sub(pkt, sname, rrset->dname)) { 383ae8c6e27Sflorian /* check if next rrset is correct CNAME. else, 384ae8c6e27Sflorian * synthesize a CNAME */ 385ae8c6e27Sflorian struct rrset_parse* nx = rrset->rrset_all_next; 386ae8c6e27Sflorian uint8_t alias[LDNS_MAX_DOMAINLEN+1]; 387ae8c6e27Sflorian size_t aliaslen = 0; 388ae8c6e27Sflorian if(rrset->rr_count != 1) { 389ae8c6e27Sflorian verbose(VERB_ALGO, "Found DNAME rrset with " 390ae8c6e27Sflorian "size > 1: %u", 391ae8c6e27Sflorian (unsigned)rrset->rr_count); 392ae8c6e27Sflorian return 0; 393ae8c6e27Sflorian } 394ae8c6e27Sflorian if(!synth_cname(sname, snamelen, rrset, alias, 395ae8c6e27Sflorian &aliaslen, pkt)) { 396ae8c6e27Sflorian verbose(VERB_ALGO, "synthesized CNAME " 397ae8c6e27Sflorian "too long"); 398ae8c6e27Sflorian return 0; 399ae8c6e27Sflorian } 400ae8c6e27Sflorian if(nx && nx->type == LDNS_RR_TYPE_CNAME && 401ae8c6e27Sflorian dname_pkt_compare(pkt, sname, nx->dname) == 0) { 402ae8c6e27Sflorian /* check next cname */ 403ae8c6e27Sflorian uint8_t* t = NULL; 404ae8c6e27Sflorian size_t tlen = 0; 4055a7d75e6Ssthen if(!parse_get_cname_target(nx, &t, &tlen, pkt)) 406ae8c6e27Sflorian return 0; 407ae8c6e27Sflorian if(dname_pkt_compare(pkt, alias, t) == 0) { 408ae8c6e27Sflorian /* it's OK and better capitalized */ 409ae8c6e27Sflorian prev = rrset; 410ae8c6e27Sflorian rrset = nx; 411ae8c6e27Sflorian continue; 412ae8c6e27Sflorian } 413ae8c6e27Sflorian /* synth ourselves */ 414ae8c6e27Sflorian } 415ae8c6e27Sflorian /* synth a CNAME rrset */ 416ae8c6e27Sflorian prev = synth_cname_rrset(&sname, &snamelen, alias, 417ae8c6e27Sflorian aliaslen, region, msg, rrset, rrset, nx, pkt); 418ae8c6e27Sflorian if(!prev) { 419ae8c6e27Sflorian log_err("out of memory synthesizing CNAME"); 420ae8c6e27Sflorian return 0; 421ae8c6e27Sflorian } 422ae8c6e27Sflorian /* FIXME: resolve the conflict between synthesized 423ae8c6e27Sflorian * CNAME ttls and the cache. */ 424ae8c6e27Sflorian rrset = nx; 425ae8c6e27Sflorian continue; 426ae8c6e27Sflorian 427ae8c6e27Sflorian } 428ae8c6e27Sflorian 429ae8c6e27Sflorian /* The only records in the ANSWER section not allowed to */ 430ae8c6e27Sflorian if(dname_pkt_compare(pkt, sname, rrset->dname) != 0) { 431ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant RRset:", 432ae8c6e27Sflorian pkt, msg, prev, &rrset); 433ae8c6e27Sflorian continue; 434ae8c6e27Sflorian } 435ae8c6e27Sflorian 436ae8c6e27Sflorian /* Follow the CNAME chain. */ 437ae8c6e27Sflorian if(rrset->type == LDNS_RR_TYPE_CNAME) { 438ae8c6e27Sflorian struct rrset_parse* nx = rrset->rrset_all_next; 439ae8c6e27Sflorian uint8_t* oldsname = sname; 440ae8c6e27Sflorian /* see if the next one is a DNAME, if so, swap them */ 441ae8c6e27Sflorian if(nx && nx->section == LDNS_SECTION_ANSWER && 442ae8c6e27Sflorian nx->type == LDNS_RR_TYPE_DNAME && 443ae8c6e27Sflorian nx->rr_count == 1 && 444ae8c6e27Sflorian pkt_strict_sub(pkt, sname, nx->dname)) { 445ae8c6e27Sflorian /* there is a DNAME after this CNAME, it 446ae8c6e27Sflorian * is in the ANSWER section, and the DNAME 447ae8c6e27Sflorian * applies to the name we cover */ 448ae8c6e27Sflorian /* check if the alias of the DNAME equals 449ae8c6e27Sflorian * this CNAME */ 450ae8c6e27Sflorian uint8_t alias[LDNS_MAX_DOMAINLEN+1]; 451ae8c6e27Sflorian size_t aliaslen = 0; 452ae8c6e27Sflorian uint8_t* t = NULL; 453ae8c6e27Sflorian size_t tlen = 0; 454ae8c6e27Sflorian if(synth_cname(sname, snamelen, nx, alias, 455ae8c6e27Sflorian &aliaslen, pkt) && 4565a7d75e6Ssthen parse_get_cname_target(rrset, &t, &tlen, pkt) && 457ae8c6e27Sflorian dname_pkt_compare(pkt, alias, t) == 0) { 458ae8c6e27Sflorian /* the synthesized CNAME equals the 459ae8c6e27Sflorian * current CNAME. This CNAME is the 460ae8c6e27Sflorian * one that the DNAME creates, and this 461ae8c6e27Sflorian * CNAME is better capitalised */ 462ae8c6e27Sflorian verbose(VERB_ALGO, "normalize: re-order of DNAME and its CNAME"); 463ae8c6e27Sflorian if(prev) prev->rrset_all_next = nx; 464ae8c6e27Sflorian else msg->rrset_first = nx; 465ae8c6e27Sflorian if(nx->rrset_all_next == NULL) 466ae8c6e27Sflorian msg->rrset_last = rrset; 467ae8c6e27Sflorian rrset->rrset_all_next = 468ae8c6e27Sflorian nx->rrset_all_next; 469ae8c6e27Sflorian nx->rrset_all_next = rrset; 470ae8c6e27Sflorian /* prev = nx; unused, enable if there 471ae8c6e27Sflorian * is other rrset removal code after 472ae8c6e27Sflorian * this */ 473ae8c6e27Sflorian } 474ae8c6e27Sflorian } 475ae8c6e27Sflorian 476ae8c6e27Sflorian /* move to next name in CNAME chain */ 4775a7d75e6Ssthen if(!parse_get_cname_target(rrset, &sname, &snamelen, pkt)) 478ae8c6e27Sflorian return 0; 479ae8c6e27Sflorian prev = rrset; 480ae8c6e27Sflorian rrset = rrset->rrset_all_next; 481ae8c6e27Sflorian /* in CNAME ANY response, can have data after CNAME */ 482ae8c6e27Sflorian if(qinfo->qtype == LDNS_RR_TYPE_ANY) { 483ae8c6e27Sflorian while(rrset && rrset->section == 484ae8c6e27Sflorian LDNS_SECTION_ANSWER && 485ae8c6e27Sflorian dname_pkt_compare(pkt, oldsname, 486ae8c6e27Sflorian rrset->dname) == 0) { 487ae8c6e27Sflorian prev = rrset; 488ae8c6e27Sflorian rrset = rrset->rrset_all_next; 489ae8c6e27Sflorian } 490ae8c6e27Sflorian } 491ae8c6e27Sflorian continue; 492ae8c6e27Sflorian } 493ae8c6e27Sflorian 494ae8c6e27Sflorian /* Otherwise, make sure that the RRset matches the qtype. */ 495ae8c6e27Sflorian if(qinfo->qtype != LDNS_RR_TYPE_ANY && 496ae8c6e27Sflorian qinfo->qtype != rrset->type) { 497ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant RRset:", 498ae8c6e27Sflorian pkt, msg, prev, &rrset); 499ae8c6e27Sflorian continue; 500ae8c6e27Sflorian } 501ae8c6e27Sflorian 502ae8c6e27Sflorian /* Mark the additional names from relevant rrset as OK. */ 503ae8c6e27Sflorian /* only for RRsets that match the query name, other ones 504ae8c6e27Sflorian * will be removed by sanitize, so no additional for them */ 505ae8c6e27Sflorian if(dname_pkt_compare(pkt, qinfo->qname, rrset->dname) == 0) 506ae8c6e27Sflorian mark_additional_rrset(pkt, msg, rrset); 507ae8c6e27Sflorian 508ae8c6e27Sflorian prev = rrset; 509ae8c6e27Sflorian rrset = rrset->rrset_all_next; 510ae8c6e27Sflorian } 511ae8c6e27Sflorian 512ae8c6e27Sflorian /* Mark additional names from AUTHORITY */ 513ae8c6e27Sflorian while(rrset && rrset->section == LDNS_SECTION_AUTHORITY) { 514ae8c6e27Sflorian if(rrset->type==LDNS_RR_TYPE_DNAME || 515ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_CNAME || 516ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_A || 517ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_AAAA) { 518ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 519ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 520ae8c6e27Sflorian continue; 521ae8c6e27Sflorian } 522ae8c6e27Sflorian /* only one NS set allowed in authority section */ 523ae8c6e27Sflorian if(rrset->type==LDNS_RR_TYPE_NS) { 524ae8c6e27Sflorian /* NS set must be pertinent to the query */ 525ae8c6e27Sflorian if(!sub_of_pkt(pkt, qinfo->qname, rrset->dname)) { 526ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 527ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 528ae8c6e27Sflorian continue; 529ae8c6e27Sflorian } 530ae8c6e27Sflorian /* we don't want NS sets for NXDOMAIN answers, 531ae8c6e27Sflorian * because they could contain poisonous contents, 532ae8c6e27Sflorian * from. eg. fragmentation attacks, inserted after 533ae8c6e27Sflorian * long RRSIGs in the packet get to the packet 534ae8c6e27Sflorian * border and such */ 535ae8c6e27Sflorian /* also for NODATA answers */ 536ae8c6e27Sflorian if(FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NXDOMAIN || 537ae8c6e27Sflorian (FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NOERROR 538ae8c6e27Sflorian && soa_in_auth(msg) && msg->an_rrsets == 0)) { 539ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 540ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 541ae8c6e27Sflorian continue; 542ae8c6e27Sflorian } 543ae8c6e27Sflorian if(nsset == NULL) { 544ae8c6e27Sflorian nsset = rrset; 545ae8c6e27Sflorian } else { 546ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 547ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 548ae8c6e27Sflorian continue; 549ae8c6e27Sflorian } 550ae8c6e27Sflorian } 551ae8c6e27Sflorian /* if this is type DS and we query for type DS we just got 552ae8c6e27Sflorian * a referral answer for our type DS query, fix packet */ 553ae8c6e27Sflorian if(rrset->type==LDNS_RR_TYPE_DS && 554ae8c6e27Sflorian qinfo->qtype == LDNS_RR_TYPE_DS && 555ae8c6e27Sflorian dname_pkt_compare(pkt, qinfo->qname, rrset->dname) == 0) { 556ae8c6e27Sflorian rrset->section = LDNS_SECTION_ANSWER; 557ae8c6e27Sflorian msg->ancount = rrset->rr_count + rrset->rrsig_count; 558ae8c6e27Sflorian msg->nscount = 0; 559ae8c6e27Sflorian msg->arcount = 0; 560ae8c6e27Sflorian msg->an_rrsets = 1; 561ae8c6e27Sflorian msg->ns_rrsets = 0; 562ae8c6e27Sflorian msg->ar_rrsets = 0; 563ae8c6e27Sflorian msg->rrset_count = 1; 564ae8c6e27Sflorian msg->rrset_first = rrset; 565ae8c6e27Sflorian msg->rrset_last = rrset; 566ae8c6e27Sflorian rrset->rrset_all_next = NULL; 567ae8c6e27Sflorian return 1; 568ae8c6e27Sflorian } 569ae8c6e27Sflorian mark_additional_rrset(pkt, msg, rrset); 570ae8c6e27Sflorian prev = rrset; 571ae8c6e27Sflorian rrset = rrset->rrset_all_next; 572ae8c6e27Sflorian } 573ae8c6e27Sflorian 574ae8c6e27Sflorian /* For each record in the additional section, remove it if it is an 575ae8c6e27Sflorian * address record and not in the collection of additional names 576ae8c6e27Sflorian * found in ANSWER and AUTHORITY. */ 577ae8c6e27Sflorian /* These records have not been marked OK previously */ 578ae8c6e27Sflorian while(rrset && rrset->section == LDNS_SECTION_ADDITIONAL) { 579ae8c6e27Sflorian /* FIXME: what about other types? */ 580ae8c6e27Sflorian if(rrset->type==LDNS_RR_TYPE_A || 581ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_AAAA) 582ae8c6e27Sflorian { 583ae8c6e27Sflorian if((rrset->flags & RRSET_SCRUB_OK)) { 584ae8c6e27Sflorian /* remove flag to clean up flags variable */ 585ae8c6e27Sflorian rrset->flags &= ~RRSET_SCRUB_OK; 586ae8c6e27Sflorian } else { 587ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 588ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 589ae8c6e27Sflorian continue; 590ae8c6e27Sflorian } 591ae8c6e27Sflorian } 592ae8c6e27Sflorian if(rrset->type==LDNS_RR_TYPE_DNAME || 593ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_CNAME || 594ae8c6e27Sflorian rrset->type==LDNS_RR_TYPE_NS) { 595ae8c6e27Sflorian remove_rrset("normalize: removing irrelevant " 596ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 597ae8c6e27Sflorian continue; 598ae8c6e27Sflorian } 599ae8c6e27Sflorian prev = rrset; 600ae8c6e27Sflorian rrset = rrset->rrset_all_next; 601ae8c6e27Sflorian } 602ae8c6e27Sflorian 603ae8c6e27Sflorian return 1; 604ae8c6e27Sflorian } 605ae8c6e27Sflorian 606ae8c6e27Sflorian /** 607ae8c6e27Sflorian * Store potential poison in the cache (only if hardening disabled). 608ae8c6e27Sflorian * The rrset is stored in the cache but removed from the message. 609ae8c6e27Sflorian * So that it will be used for infrastructure purposes, but not be 610ae8c6e27Sflorian * returned to the client. 611ae8c6e27Sflorian * @param pkt: packet 612ae8c6e27Sflorian * @param msg: message parsed 613ae8c6e27Sflorian * @param env: environment with cache 614ae8c6e27Sflorian * @param rrset: to store. 615ae8c6e27Sflorian */ 616ae8c6e27Sflorian static void 617ae8c6e27Sflorian store_rrset(sldns_buffer* pkt, struct msg_parse* msg, struct module_env* env, 618ae8c6e27Sflorian struct rrset_parse* rrset) 619ae8c6e27Sflorian { 620ae8c6e27Sflorian struct ub_packed_rrset_key* k; 621ae8c6e27Sflorian struct packed_rrset_data* d; 622ae8c6e27Sflorian struct rrset_ref ref; 623ae8c6e27Sflorian time_t now = *env->now; 624ae8c6e27Sflorian 625ae8c6e27Sflorian k = alloc_special_obtain(env->alloc); 626ae8c6e27Sflorian if(!k) 627ae8c6e27Sflorian return; 628ae8c6e27Sflorian k->entry.data = NULL; 629ae8c6e27Sflorian if(!parse_copy_decompress_rrset(pkt, msg, rrset, NULL, k)) { 630ae8c6e27Sflorian alloc_special_release(env->alloc, k); 631ae8c6e27Sflorian return; 632ae8c6e27Sflorian } 633ae8c6e27Sflorian d = (struct packed_rrset_data*)k->entry.data; 634ae8c6e27Sflorian packed_rrset_ttl_add(d, now); 635ae8c6e27Sflorian ref.key = k; 636ae8c6e27Sflorian ref.id = k->id; 637ae8c6e27Sflorian /*ignore ret: it was in the cache, ref updated */ 638ae8c6e27Sflorian (void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, now); 639ae8c6e27Sflorian } 640ae8c6e27Sflorian 641ae8c6e27Sflorian /** 642ae8c6e27Sflorian * Check if right hand name in NSEC is within zone 643*411c5950Sflorian * @param pkt: the packet buffer for decompression. 644ae8c6e27Sflorian * @param rrset: the NSEC rrset 645ae8c6e27Sflorian * @param zonename: the zone name. 646ae8c6e27Sflorian * @return true if BAD. 647ae8c6e27Sflorian */ 648*411c5950Sflorian static int sanitize_nsec_is_overreach(sldns_buffer* pkt, 649*411c5950Sflorian struct rrset_parse* rrset, uint8_t* zonename) 650ae8c6e27Sflorian { 651ae8c6e27Sflorian struct rr_parse* rr; 652ae8c6e27Sflorian uint8_t* rhs; 653ae8c6e27Sflorian size_t len; 654ae8c6e27Sflorian log_assert(rrset->type == LDNS_RR_TYPE_NSEC); 655ae8c6e27Sflorian for(rr = rrset->rr_first; rr; rr = rr->next) { 656*411c5950Sflorian size_t pos = sldns_buffer_position(pkt); 657*411c5950Sflorian size_t rhspos; 658ae8c6e27Sflorian rhs = rr->ttl_data+4+2; 659ae8c6e27Sflorian len = sldns_read_uint16(rr->ttl_data+4); 660*411c5950Sflorian rhspos = rhs-sldns_buffer_begin(pkt); 661*411c5950Sflorian sldns_buffer_set_position(pkt, rhspos); 662*411c5950Sflorian if(pkt_dname_len(pkt) == 0) { 663*411c5950Sflorian /* malformed */ 664*411c5950Sflorian sldns_buffer_set_position(pkt, pos); 665ae8c6e27Sflorian return 1; 666ae8c6e27Sflorian } 667*411c5950Sflorian if(sldns_buffer_position(pkt)-rhspos > len) { 668*411c5950Sflorian /* outside of rdata boundaries */ 669*411c5950Sflorian sldns_buffer_set_position(pkt, pos); 670*411c5950Sflorian return 1; 671*411c5950Sflorian } 672*411c5950Sflorian sldns_buffer_set_position(pkt, pos); 673*411c5950Sflorian if(!pkt_sub(pkt, rhs, zonename)) { 674ae8c6e27Sflorian /* overreaching */ 675ae8c6e27Sflorian return 1; 676ae8c6e27Sflorian } 677ae8c6e27Sflorian } 678ae8c6e27Sflorian /* all NSEC RRs OK */ 679ae8c6e27Sflorian return 0; 680ae8c6e27Sflorian } 681ae8c6e27Sflorian 682ae8c6e27Sflorian /** 683ae8c6e27Sflorian * Given a response event, remove suspect RRsets from the response. 684ae8c6e27Sflorian * "Suspect" rrsets are potentially poison. Note that this routine expects 685ae8c6e27Sflorian * the response to be in a "normalized" state -- that is, all "irrelevant" 686ae8c6e27Sflorian * RRsets have already been removed, CNAMEs are in order, etc. 687ae8c6e27Sflorian * 688ae8c6e27Sflorian * @param pkt: packet. 689ae8c6e27Sflorian * @param msg: msg to normalize. 690ae8c6e27Sflorian * @param qinfo: the question originally asked. 691ae8c6e27Sflorian * @param zonename: name of server zone. 692ae8c6e27Sflorian * @param env: module environment with config and cache. 693ae8c6e27Sflorian * @param ie: iterator environment with private address data. 694ae8c6e27Sflorian * @return 0 on error. 695ae8c6e27Sflorian */ 696ae8c6e27Sflorian static int 697ae8c6e27Sflorian scrub_sanitize(sldns_buffer* pkt, struct msg_parse* msg, 698ae8c6e27Sflorian struct query_info* qinfo, uint8_t* zonename, struct module_env* env, 699ae8c6e27Sflorian struct iter_env* ie) 700ae8c6e27Sflorian { 701ae8c6e27Sflorian int del_addi = 0; /* if additional-holding rrsets are deleted, we 702ae8c6e27Sflorian do not trust the normalized additional-A-AAAA any more */ 703ae8c6e27Sflorian struct rrset_parse* rrset, *prev; 704ae8c6e27Sflorian prev = NULL; 705ae8c6e27Sflorian rrset = msg->rrset_first; 706ae8c6e27Sflorian 707ae8c6e27Sflorian /* the first DNAME is allowed to stay. It needs checking before 708ae8c6e27Sflorian * it can be used from the cache. After normalization, an initial 709ae8c6e27Sflorian * DNAME will have a correctly synthesized CNAME after it. */ 710ae8c6e27Sflorian if(rrset && rrset->type == LDNS_RR_TYPE_DNAME && 711ae8c6e27Sflorian rrset->section == LDNS_SECTION_ANSWER && 712ae8c6e27Sflorian pkt_strict_sub(pkt, qinfo->qname, rrset->dname) && 713ae8c6e27Sflorian pkt_sub(pkt, rrset->dname, zonename)) { 714ae8c6e27Sflorian prev = rrset; /* DNAME allowed to stay in answer section */ 715ae8c6e27Sflorian rrset = rrset->rrset_all_next; 716ae8c6e27Sflorian } 717ae8c6e27Sflorian 718ae8c6e27Sflorian /* remove all records from the answer section that are 719ae8c6e27Sflorian * not the same domain name as the query domain name. 720ae8c6e27Sflorian * The answer section should contain rrsets with the same name 721ae8c6e27Sflorian * as the question. For DNAMEs a CNAME has been synthesized. 722ae8c6e27Sflorian * Wildcards have the query name in answer section. 723ae8c6e27Sflorian * ANY queries get query name in answer section. 724ae8c6e27Sflorian * Remainders of CNAME chains are cut off and resolved by iterator. */ 725ae8c6e27Sflorian while(rrset && rrset->section == LDNS_SECTION_ANSWER) { 726ae8c6e27Sflorian if(dname_pkt_compare(pkt, qinfo->qname, rrset->dname) != 0) { 727ae8c6e27Sflorian if(has_additional(rrset->type)) del_addi = 1; 728ae8c6e27Sflorian remove_rrset("sanitize: removing extraneous answer " 729ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 730ae8c6e27Sflorian continue; 731ae8c6e27Sflorian } 732ae8c6e27Sflorian prev = rrset; 733ae8c6e27Sflorian rrset = rrset->rrset_all_next; 734ae8c6e27Sflorian } 735ae8c6e27Sflorian 736ae8c6e27Sflorian /* At this point, we brutally remove ALL rrsets that aren't 737ae8c6e27Sflorian * children of the originating zone. The idea here is that, 738ae8c6e27Sflorian * as far as we know, the server that we contacted is ONLY 739ae8c6e27Sflorian * authoritative for the originating zone. It, of course, MAY 740ae8c6e27Sflorian * be authoritative for any other zones, and of course, MAY 741ae8c6e27Sflorian * NOT be authoritative for some subdomains of the originating 742ae8c6e27Sflorian * zone. */ 743ae8c6e27Sflorian prev = NULL; 744ae8c6e27Sflorian rrset = msg->rrset_first; 745ae8c6e27Sflorian while(rrset) { 746ae8c6e27Sflorian 747ae8c6e27Sflorian /* remove private addresses */ 748ae8c6e27Sflorian if( (rrset->type == LDNS_RR_TYPE_A || 749ae8c6e27Sflorian rrset->type == LDNS_RR_TYPE_AAAA)) { 750ae8c6e27Sflorian 751ae8c6e27Sflorian /* do not set servfail since this leads to too 752ae8c6e27Sflorian * many drops of other people using rfc1918 space */ 753ae8c6e27Sflorian /* also do not remove entire rrset, unless all records 754ae8c6e27Sflorian * in it are bad */ 755ae8c6e27Sflorian if(priv_rrset_bad(ie->priv, pkt, rrset)) { 756ae8c6e27Sflorian remove_rrset(NULL, pkt, msg, prev, &rrset); 757ae8c6e27Sflorian continue; 758ae8c6e27Sflorian } 759ae8c6e27Sflorian } 760ae8c6e27Sflorian 761ae8c6e27Sflorian /* skip DNAME records -- they will always be followed by a 762ae8c6e27Sflorian * synthesized CNAME, which will be relevant. 763ae8c6e27Sflorian * FIXME: should this do something differently with DNAME 764ae8c6e27Sflorian * rrsets NOT in Section.ANSWER? */ 765ae8c6e27Sflorian /* But since DNAME records are also subdomains of the zone, 766ae8c6e27Sflorian * same check can be used */ 767ae8c6e27Sflorian 768ae8c6e27Sflorian if(!pkt_sub(pkt, rrset->dname, zonename)) { 769ae8c6e27Sflorian if(msg->an_rrsets == 0 && 770ae8c6e27Sflorian rrset->type == LDNS_RR_TYPE_NS && 771ae8c6e27Sflorian rrset->section == LDNS_SECTION_AUTHORITY && 772ae8c6e27Sflorian FLAGS_GET_RCODE(msg->flags) == 773ae8c6e27Sflorian LDNS_RCODE_NOERROR && !soa_in_auth(msg) && 774ae8c6e27Sflorian sub_of_pkt(pkt, zonename, rrset->dname)) { 775ae8c6e27Sflorian /* noerror, nodata and this NS rrset is above 776ae8c6e27Sflorian * the zone. This is LAME! 777ae8c6e27Sflorian * Leave in the NS for lame classification. */ 778ae8c6e27Sflorian /* remove everything from the additional 779ae8c6e27Sflorian * (we dont want its glue that was approved 780ae8c6e27Sflorian * during the normalize action) */ 781ae8c6e27Sflorian del_addi = 1; 782ae8c6e27Sflorian } else if(!env->cfg->harden_glue && ( 783ae8c6e27Sflorian rrset->type == LDNS_RR_TYPE_A || 784ae8c6e27Sflorian rrset->type == LDNS_RR_TYPE_AAAA)) { 785ae8c6e27Sflorian /* store in cache! Since it is relevant 786ae8c6e27Sflorian * (from normalize) it will be picked up 787ae8c6e27Sflorian * from the cache to be used later */ 788ae8c6e27Sflorian store_rrset(pkt, msg, env, rrset); 789ae8c6e27Sflorian remove_rrset("sanitize: storing potential " 790ae8c6e27Sflorian "poison RRset:", pkt, msg, prev, &rrset); 791ae8c6e27Sflorian continue; 792ae8c6e27Sflorian } else { 793ae8c6e27Sflorian if(has_additional(rrset->type)) del_addi = 1; 794ae8c6e27Sflorian remove_rrset("sanitize: removing potential " 795ae8c6e27Sflorian "poison RRset:", pkt, msg, prev, &rrset); 796ae8c6e27Sflorian continue; 797ae8c6e27Sflorian } 798ae8c6e27Sflorian } 799ae8c6e27Sflorian if(del_addi && rrset->section == LDNS_SECTION_ADDITIONAL) { 800ae8c6e27Sflorian remove_rrset("sanitize: removing potential " 801ae8c6e27Sflorian "poison reference RRset:", pkt, msg, prev, &rrset); 802ae8c6e27Sflorian continue; 803ae8c6e27Sflorian } 804ae8c6e27Sflorian /* check if right hand side of NSEC is within zone */ 805ae8c6e27Sflorian if(rrset->type == LDNS_RR_TYPE_NSEC && 806*411c5950Sflorian sanitize_nsec_is_overreach(pkt, rrset, zonename)) { 807ae8c6e27Sflorian remove_rrset("sanitize: removing overreaching NSEC " 808ae8c6e27Sflorian "RRset:", pkt, msg, prev, &rrset); 809ae8c6e27Sflorian continue; 810ae8c6e27Sflorian } 811ae8c6e27Sflorian prev = rrset; 812ae8c6e27Sflorian rrset = rrset->rrset_all_next; 813ae8c6e27Sflorian } 814ae8c6e27Sflorian return 1; 815ae8c6e27Sflorian } 816ae8c6e27Sflorian 817ae8c6e27Sflorian int 818ae8c6e27Sflorian scrub_message(sldns_buffer* pkt, struct msg_parse* msg, 819ae8c6e27Sflorian struct query_info* qinfo, uint8_t* zonename, struct regional* region, 820ae8c6e27Sflorian struct module_env* env, struct iter_env* ie) 821ae8c6e27Sflorian { 822ae8c6e27Sflorian /* basic sanity checks */ 823ae8c6e27Sflorian log_nametypeclass(VERB_ALGO, "scrub for", zonename, LDNS_RR_TYPE_NS, 824ae8c6e27Sflorian qinfo->qclass); 825ae8c6e27Sflorian if(msg->qdcount > 1) 826ae8c6e27Sflorian return 0; 827ae8c6e27Sflorian if( !(msg->flags&BIT_QR) ) 828ae8c6e27Sflorian return 0; 829ae8c6e27Sflorian msg->flags &= ~(BIT_AD|BIT_Z); /* force off bit AD and Z */ 830ae8c6e27Sflorian 831ae8c6e27Sflorian /* make sure that a query is echoed back when NOERROR or NXDOMAIN */ 832ae8c6e27Sflorian /* this is not required for basic operation but is a forgery 833ae8c6e27Sflorian * resistance (security) feature */ 834ae8c6e27Sflorian if((FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NOERROR || 835ae8c6e27Sflorian FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NXDOMAIN) && 836ae8c6e27Sflorian msg->qdcount == 0) 837ae8c6e27Sflorian return 0; 838ae8c6e27Sflorian 839ae8c6e27Sflorian /* if a query is echoed back, make sure it is correct. Otherwise, 840ae8c6e27Sflorian * this may be not a reply to our query. */ 841ae8c6e27Sflorian if(msg->qdcount == 1) { 842ae8c6e27Sflorian if(dname_pkt_compare(pkt, msg->qname, qinfo->qname) != 0) 843ae8c6e27Sflorian return 0; 844ae8c6e27Sflorian if(msg->qtype != qinfo->qtype || msg->qclass != qinfo->qclass) 845ae8c6e27Sflorian return 0; 846ae8c6e27Sflorian } 847ae8c6e27Sflorian 848ae8c6e27Sflorian /* normalize the response, this cleans up the additional. */ 849ae8c6e27Sflorian if(!scrub_normalize(pkt, msg, qinfo, region)) 850ae8c6e27Sflorian return 0; 851ae8c6e27Sflorian /* delete all out-of-zone information */ 852ae8c6e27Sflorian if(!scrub_sanitize(pkt, msg, qinfo, zonename, env, ie)) 853ae8c6e27Sflorian return 0; 854ae8c6e27Sflorian return 1; 855ae8c6e27Sflorian } 856