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
remove_rrset(const char * str,sldns_buffer * pkt,struct msg_parse * msg,struct rrset_parse * prev,struct rrset_parse ** rrset)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
has_additional(uint16_t t)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
get_additional_name(struct rrset_parse * rrset,struct rr_parse * rr,uint8_t ** nm,size_t * nmlen,sldns_buffer * pkt)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
mark_additional_rrset(sldns_buffer * pkt,struct msg_parse * msg,struct rrset_parse * rrset)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
parse_get_cname_target(struct rrset_parse * rrset,uint8_t ** sname,size_t * snamelen,sldns_buffer * pkt)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
synth_cname(uint8_t * qname,size_t qnamelen,struct rrset_parse * dname_rrset,uint8_t * alias,size_t * aliaslen,sldns_buffer * pkt)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*
synth_cname_rrset(uint8_t ** sname,size_t * snamelen,uint8_t * alias,size_t aliaslen,struct regional * region,struct msg_parse * msg,struct rrset_parse * rrset,struct rrset_parse * prev,struct rrset_parse * nx,sldns_buffer * pkt)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;
28654cc57acSflorian 	memmove(cn->rr_first->ttl_data, rrset->rr_first->ttl_data,
28754cc57acSflorian 		sizeof(uint32_t)); /* RFC6672: synth CNAME TTL == DNAME TTL */
288ae8c6e27Sflorian 	sldns_write_uint16(cn->rr_first->ttl_data+4, aliaslen);
289ae8c6e27Sflorian 	memmove(cn->rr_first->ttl_data+6, alias, aliaslen);
290ae8c6e27Sflorian 	cn->rr_first->size = sizeof(uint16_t)+aliaslen;
291ae8c6e27Sflorian 
292ae8c6e27Sflorian 	/* link it in */
293ae8c6e27Sflorian 	cn->rrset_all_next = nx;
294ae8c6e27Sflorian 	if(prev)
295ae8c6e27Sflorian 		prev->rrset_all_next = cn;
296ae8c6e27Sflorian 	else	msg->rrset_first = cn;
297ae8c6e27Sflorian 	if(nx == NULL)
298ae8c6e27Sflorian 		msg->rrset_last = cn;
299ae8c6e27Sflorian 	msg->rrset_count ++;
300ae8c6e27Sflorian 	msg->an_rrsets++;
301ae8c6e27Sflorian 	/* it is not inserted in the msg hashtable. */
302ae8c6e27Sflorian 
303ae8c6e27Sflorian 	*sname = cn->rr_first->ttl_data + sizeof(uint32_t)+sizeof(uint16_t);
304ae8c6e27Sflorian 	*snamelen = aliaslen;
305ae8c6e27Sflorian 	return cn;
306ae8c6e27Sflorian }
307ae8c6e27Sflorian 
308ae8c6e27Sflorian /** check if DNAME applies to a name */
309ae8c6e27Sflorian static int
pkt_strict_sub(sldns_buffer * pkt,uint8_t * sname,uint8_t * dr)310ae8c6e27Sflorian pkt_strict_sub(sldns_buffer* pkt, uint8_t* sname, uint8_t* dr)
311ae8c6e27Sflorian {
312ae8c6e27Sflorian 	uint8_t buf1[LDNS_MAX_DOMAINLEN+1];
313ae8c6e27Sflorian 	uint8_t buf2[LDNS_MAX_DOMAINLEN+1];
314ae8c6e27Sflorian 	/* decompress names */
315ae8c6e27Sflorian 	dname_pkt_copy(pkt, buf1, sname);
316ae8c6e27Sflorian 	dname_pkt_copy(pkt, buf2, dr);
317ae8c6e27Sflorian 	return dname_strict_subdomain_c(buf1, buf2);
318ae8c6e27Sflorian }
319ae8c6e27Sflorian 
320ae8c6e27Sflorian /** check subdomain with decompression */
321ae8c6e27Sflorian static int
pkt_sub(sldns_buffer * pkt,uint8_t * comprname,uint8_t * zone)322ae8c6e27Sflorian pkt_sub(sldns_buffer* pkt, uint8_t* comprname, uint8_t* zone)
323ae8c6e27Sflorian {
324ae8c6e27Sflorian 	uint8_t buf[LDNS_MAX_DOMAINLEN+1];
325ae8c6e27Sflorian 	dname_pkt_copy(pkt, buf, comprname);
326ae8c6e27Sflorian 	return dname_subdomain_c(buf, zone);
327ae8c6e27Sflorian }
328ae8c6e27Sflorian 
329ae8c6e27Sflorian /** check subdomain with decompression, compressed is parent */
330ae8c6e27Sflorian static int
sub_of_pkt(sldns_buffer * pkt,uint8_t * zone,uint8_t * comprname)331ae8c6e27Sflorian sub_of_pkt(sldns_buffer* pkt, uint8_t* zone, uint8_t* comprname)
332ae8c6e27Sflorian {
333ae8c6e27Sflorian 	uint8_t buf[LDNS_MAX_DOMAINLEN+1];
334ae8c6e27Sflorian 	dname_pkt_copy(pkt, buf, comprname);
335ae8c6e27Sflorian 	return dname_subdomain_c(zone, buf);
336ae8c6e27Sflorian }
337ae8c6e27Sflorian 
338ae8c6e27Sflorian /** Check if there are SOA records in the authority section (negative) */
339ae8c6e27Sflorian static int
soa_in_auth(struct msg_parse * msg)340ae8c6e27Sflorian soa_in_auth(struct msg_parse* msg)
341ae8c6e27Sflorian {
342ae8c6e27Sflorian 	struct rrset_parse* rrset;
343ae8c6e27Sflorian 	for(rrset = msg->rrset_first; rrset; rrset = rrset->rrset_all_next)
344ae8c6e27Sflorian 		if(rrset->type == LDNS_RR_TYPE_SOA &&
345ae8c6e27Sflorian 			rrset->section == LDNS_SECTION_AUTHORITY)
346ae8c6e27Sflorian 			return 1;
347ae8c6e27Sflorian 	return 0;
348ae8c6e27Sflorian }
349ae8c6e27Sflorian 
350d500c338Sflorian /** Check if type is allowed in the authority section */
351d500c338Sflorian static int
type_allowed_in_authority_section(uint16_t tp)352d500c338Sflorian type_allowed_in_authority_section(uint16_t tp)
353d500c338Sflorian {
354d500c338Sflorian 	if(tp == LDNS_RR_TYPE_SOA || tp == LDNS_RR_TYPE_NS ||
355d500c338Sflorian 		tp == LDNS_RR_TYPE_DS || tp == LDNS_RR_TYPE_NSEC ||
356d500c338Sflorian 		tp == LDNS_RR_TYPE_NSEC3)
357d500c338Sflorian 		return 1;
358d500c338Sflorian 	return 0;
359d500c338Sflorian }
360d500c338Sflorian 
361d500c338Sflorian /** Check if type is allowed in the additional section */
362d500c338Sflorian static int
type_allowed_in_additional_section(uint16_t tp)363d500c338Sflorian type_allowed_in_additional_section(uint16_t tp)
364d500c338Sflorian {
365d500c338Sflorian 	if(tp == LDNS_RR_TYPE_A || tp == LDNS_RR_TYPE_AAAA)
366d500c338Sflorian 		return 1;
367d500c338Sflorian 	return 0;
368d500c338Sflorian }
369d500c338Sflorian 
370*7037e34cSflorian /** Shorten RRset */
371*7037e34cSflorian static void
shorten_rrset(sldns_buffer * pkt,struct rrset_parse * rrset,int count)372*7037e34cSflorian shorten_rrset(sldns_buffer* pkt, struct rrset_parse* rrset, int count)
373*7037e34cSflorian {
374*7037e34cSflorian 	/* The too large NS RRset is shortened. This is so that too large
375*7037e34cSflorian 	 * content does not overwhelm the cache. It may make the rrset
376*7037e34cSflorian 	 * bogus if it was signed, and then the domain is not resolved any
377*7037e34cSflorian 	 * more, that is okay, the NS RRset was too large. During a referral
378*7037e34cSflorian 	 * it can be shortened and then the first part of the list could
379*7037e34cSflorian 	 * be used to resolve. The scrub continues to disallow glue for the
380*7037e34cSflorian 	 * removed nameserver RRs and removes that too. Because the glue
381*7037e34cSflorian 	 * is not marked as okay, since the RRs have been removed here. */
382*7037e34cSflorian 	int i;
383*7037e34cSflorian 	struct rr_parse* rr = rrset->rr_first, *prev = NULL;
384*7037e34cSflorian 	if(!rr)
385*7037e34cSflorian 		return;
386*7037e34cSflorian 	for(i=0; i<count; i++) {
387*7037e34cSflorian 		prev = rr;
388*7037e34cSflorian 		rr = rr->next;
389*7037e34cSflorian 		if(!rr)
390*7037e34cSflorian 			return; /* The RRset is already short. */
391*7037e34cSflorian 	}
392*7037e34cSflorian 	if(verbosity >= VERB_QUERY
393*7037e34cSflorian 		&& rrset->dname_len <= LDNS_MAX_DOMAINLEN) {
394*7037e34cSflorian 		uint8_t buf[LDNS_MAX_DOMAINLEN+1];
395*7037e34cSflorian 		dname_pkt_copy(pkt, buf, rrset->dname);
396*7037e34cSflorian 		log_nametypeclass(VERB_QUERY, "normalize: shorten RRset:", buf,
397*7037e34cSflorian 			rrset->type, ntohs(rrset->rrset_class));
398*7037e34cSflorian 	}
399*7037e34cSflorian 	/* remove further rrs */
400*7037e34cSflorian 	rrset->rr_last = prev;
401*7037e34cSflorian 	rrset->rr_count = count;
402*7037e34cSflorian 	while(rr) {
403*7037e34cSflorian 		rrset->size -= rr->size;
404*7037e34cSflorian 		rr = rr->next;
405*7037e34cSflorian 	}
406*7037e34cSflorian 	if(rrset->rr_last)
407*7037e34cSflorian 		rrset->rr_last->next = NULL;
408*7037e34cSflorian 	else	rrset->rr_first = NULL;
409*7037e34cSflorian }
410*7037e34cSflorian 
411ae8c6e27Sflorian /**
412ae8c6e27Sflorian  * This routine normalizes a response. This includes removing "irrelevant"
413ae8c6e27Sflorian  * records from the answer and additional sections and (re)synthesizing
414ae8c6e27Sflorian  * CNAMEs from DNAMEs, if present.
415ae8c6e27Sflorian  *
416ae8c6e27Sflorian  * @param pkt: packet.
417ae8c6e27Sflorian  * @param msg: msg to normalize.
418ae8c6e27Sflorian  * @param qinfo: original query.
419ae8c6e27Sflorian  * @param region: where to allocate synthesized CNAMEs.
420d500c338Sflorian  * @param env: module env with config options.
421ae8c6e27Sflorian  * @return 0 on error.
422ae8c6e27Sflorian  */
423ae8c6e27Sflorian static int
scrub_normalize(sldns_buffer * pkt,struct msg_parse * msg,struct query_info * qinfo,struct regional * region,struct module_env * env)424ae8c6e27Sflorian scrub_normalize(sldns_buffer* pkt, struct msg_parse* msg,
425d500c338Sflorian 	struct query_info* qinfo, struct regional* region,
426d500c338Sflorian 	struct module_env* env)
427ae8c6e27Sflorian {
428ae8c6e27Sflorian 	uint8_t* sname = qinfo->qname;
429ae8c6e27Sflorian 	size_t snamelen = qinfo->qname_len;
430ae8c6e27Sflorian 	struct rrset_parse* rrset, *prev, *nsset=NULL;
431*7037e34cSflorian 	int cname_length = 0; /* number of CNAMEs, or DNAMEs */
432ae8c6e27Sflorian 
433ae8c6e27Sflorian 	if(FLAGS_GET_RCODE(msg->flags) != LDNS_RCODE_NOERROR &&
434ae8c6e27Sflorian 		FLAGS_GET_RCODE(msg->flags) != LDNS_RCODE_NXDOMAIN)
435ae8c6e27Sflorian 		return 1;
436ae8c6e27Sflorian 
437ae8c6e27Sflorian 	/* For the ANSWER section, remove all "irrelevant" records and add
438ae8c6e27Sflorian 	 * synthesized CNAMEs from DNAMEs
439ae8c6e27Sflorian 	 * This will strip out-of-order CNAMEs as well. */
440ae8c6e27Sflorian 
441ae8c6e27Sflorian 	/* walk through the parse packet rrset list, keep track of previous
442ae8c6e27Sflorian 	 * for insert and delete ease, and examine every RRset */
443ae8c6e27Sflorian 	prev = NULL;
444ae8c6e27Sflorian 	rrset = msg->rrset_first;
445ae8c6e27Sflorian 	while(rrset && rrset->section == LDNS_SECTION_ANSWER) {
446*7037e34cSflorian 		if(cname_length > 11 /* env->cfg.iter_scrub_cname */) {
447*7037e34cSflorian 			/* Too many CNAMEs, or DNAMEs, from the authority
448*7037e34cSflorian 			 * server, scrub down the length to something
449*7037e34cSflorian 			 * shorter. This deletes everything after the limit
450*7037e34cSflorian 			 * is reached. The iterator is going to look up
451*7037e34cSflorian 			 * the content one by one anyway. */
452*7037e34cSflorian 			remove_rrset("normalize: removing because too many cnames:",
453*7037e34cSflorian 				pkt, msg, prev, &rrset);
454*7037e34cSflorian 			continue;
455*7037e34cSflorian 		}
456ae8c6e27Sflorian 		if(rrset->type == LDNS_RR_TYPE_DNAME &&
457ae8c6e27Sflorian 			pkt_strict_sub(pkt, sname, rrset->dname)) {
458ae8c6e27Sflorian 			/* check if next rrset is correct CNAME. else,
459ae8c6e27Sflorian 			 * synthesize a CNAME */
460ae8c6e27Sflorian 			struct rrset_parse* nx = rrset->rrset_all_next;
461ae8c6e27Sflorian 			uint8_t alias[LDNS_MAX_DOMAINLEN+1];
462ae8c6e27Sflorian 			size_t aliaslen = 0;
463ae8c6e27Sflorian 			if(rrset->rr_count != 1) {
464ae8c6e27Sflorian 				verbose(VERB_ALGO, "Found DNAME rrset with "
465ae8c6e27Sflorian 					"size > 1: %u",
466ae8c6e27Sflorian 					(unsigned)rrset->rr_count);
467ae8c6e27Sflorian 				return 0;
468ae8c6e27Sflorian 			}
469ae8c6e27Sflorian 			if(!synth_cname(sname, snamelen, rrset, alias,
470ae8c6e27Sflorian 				&aliaslen, pkt)) {
471ae8c6e27Sflorian 				verbose(VERB_ALGO, "synthesized CNAME "
472ae8c6e27Sflorian 					"too long");
473ae8c6e27Sflorian 				return 0;
474ae8c6e27Sflorian 			}
475*7037e34cSflorian 			cname_length++;
476ae8c6e27Sflorian 			if(nx && nx->type == LDNS_RR_TYPE_CNAME &&
477ae8c6e27Sflorian 			   dname_pkt_compare(pkt, sname, nx->dname) == 0) {
478ae8c6e27Sflorian 				/* check next cname */
479ae8c6e27Sflorian 				uint8_t* t = NULL;
480ae8c6e27Sflorian 				size_t tlen = 0;
4815a7d75e6Ssthen 				if(!parse_get_cname_target(nx, &t, &tlen, pkt))
482ae8c6e27Sflorian 					return 0;
483ae8c6e27Sflorian 				if(dname_pkt_compare(pkt, alias, t) == 0) {
484ae8c6e27Sflorian 					/* it's OK and better capitalized */
485ae8c6e27Sflorian 					prev = rrset;
486ae8c6e27Sflorian 					rrset = nx;
487ae8c6e27Sflorian 					continue;
488ae8c6e27Sflorian 				}
489ae8c6e27Sflorian 				/* synth ourselves */
490ae8c6e27Sflorian 			}
491ae8c6e27Sflorian 			/* synth a CNAME rrset */
492ae8c6e27Sflorian 			prev = synth_cname_rrset(&sname, &snamelen, alias,
493ae8c6e27Sflorian 				aliaslen, region, msg, rrset, rrset, nx, pkt);
494ae8c6e27Sflorian 			if(!prev) {
495ae8c6e27Sflorian 				log_err("out of memory synthesizing CNAME");
496ae8c6e27Sflorian 				return 0;
497ae8c6e27Sflorian 			}
498ae8c6e27Sflorian 			/* FIXME: resolve the conflict between synthesized
499ae8c6e27Sflorian 			 * CNAME ttls and the cache. */
500ae8c6e27Sflorian 			rrset = nx;
501ae8c6e27Sflorian 			continue;
502ae8c6e27Sflorian 
503ae8c6e27Sflorian 		}
504ae8c6e27Sflorian 
505ae8c6e27Sflorian 		/* The only records in the ANSWER section not allowed to */
506ae8c6e27Sflorian 		if(dname_pkt_compare(pkt, sname, rrset->dname) != 0) {
507ae8c6e27Sflorian 			remove_rrset("normalize: removing irrelevant RRset:",
508ae8c6e27Sflorian 				pkt, msg, prev, &rrset);
509ae8c6e27Sflorian 			continue;
510ae8c6e27Sflorian 		}
511ae8c6e27Sflorian 
512ae8c6e27Sflorian 		/* Follow the CNAME chain. */
513ae8c6e27Sflorian 		if(rrset->type == LDNS_RR_TYPE_CNAME) {
514ae8c6e27Sflorian 			struct rrset_parse* nx = rrset->rrset_all_next;
515ae8c6e27Sflorian 			uint8_t* oldsname = sname;
516*7037e34cSflorian 			cname_length++;
517ae8c6e27Sflorian 			/* see if the next one is a DNAME, if so, swap them */
518ae8c6e27Sflorian 			if(nx && nx->section == LDNS_SECTION_ANSWER &&
519ae8c6e27Sflorian 				nx->type == LDNS_RR_TYPE_DNAME &&
520ae8c6e27Sflorian 				nx->rr_count == 1 &&
521ae8c6e27Sflorian 				pkt_strict_sub(pkt, sname, nx->dname)) {
522ae8c6e27Sflorian 				/* there is a DNAME after this CNAME, it
523ae8c6e27Sflorian 				 * is in the ANSWER section, and the DNAME
524ae8c6e27Sflorian 				 * applies to the name we cover */
525ae8c6e27Sflorian 				/* check if the alias of the DNAME equals
526ae8c6e27Sflorian 				 * this CNAME */
527ae8c6e27Sflorian 				uint8_t alias[LDNS_MAX_DOMAINLEN+1];
528ae8c6e27Sflorian 				size_t aliaslen = 0;
529ae8c6e27Sflorian 				uint8_t* t = NULL;
530ae8c6e27Sflorian 				size_t tlen = 0;
531ae8c6e27Sflorian 				if(synth_cname(sname, snamelen, nx, alias,
532ae8c6e27Sflorian 					&aliaslen, pkt) &&
5335a7d75e6Ssthen 					parse_get_cname_target(rrset, &t, &tlen, pkt) &&
534ae8c6e27Sflorian 			   		dname_pkt_compare(pkt, alias, t) == 0) {
535ae8c6e27Sflorian 					/* the synthesized CNAME equals the
536ae8c6e27Sflorian 					 * current CNAME.  This CNAME is the
537ae8c6e27Sflorian 					 * one that the DNAME creates, and this
538ae8c6e27Sflorian 					 * CNAME is better capitalised */
539ae8c6e27Sflorian 					verbose(VERB_ALGO, "normalize: re-order of DNAME and its CNAME");
540ae8c6e27Sflorian 					if(prev) prev->rrset_all_next = nx;
541ae8c6e27Sflorian 					else msg->rrset_first = nx;
542ae8c6e27Sflorian 					if(nx->rrset_all_next == NULL)
543ae8c6e27Sflorian 						msg->rrset_last = rrset;
544ae8c6e27Sflorian 					rrset->rrset_all_next =
545ae8c6e27Sflorian 						nx->rrset_all_next;
546ae8c6e27Sflorian 					nx->rrset_all_next = rrset;
547ae8c6e27Sflorian 					/* prev = nx; unused, enable if there
548ae8c6e27Sflorian 					 * is other rrset removal code after
549ae8c6e27Sflorian 					 * this */
550ae8c6e27Sflorian 				}
551ae8c6e27Sflorian 			}
552ae8c6e27Sflorian 
553ae8c6e27Sflorian 			/* move to next name in CNAME chain */
5545a7d75e6Ssthen 			if(!parse_get_cname_target(rrset, &sname, &snamelen, pkt))
555ae8c6e27Sflorian 				return 0;
556ae8c6e27Sflorian 			prev = rrset;
557ae8c6e27Sflorian 			rrset = rrset->rrset_all_next;
558ae8c6e27Sflorian 			/* in CNAME ANY response, can have data after CNAME */
559ae8c6e27Sflorian 			if(qinfo->qtype == LDNS_RR_TYPE_ANY) {
560ae8c6e27Sflorian 				while(rrset && rrset->section ==
561ae8c6e27Sflorian 					LDNS_SECTION_ANSWER &&
562ae8c6e27Sflorian 					dname_pkt_compare(pkt, oldsname,
563ae8c6e27Sflorian 					rrset->dname) == 0) {
564*7037e34cSflorian 					if(rrset->type == LDNS_RR_TYPE_NS &&
565*7037e34cSflorian 						rrset->rr_count > 20 /* env->cfg->iter_scrub_ns */) {
566*7037e34cSflorian 						shorten_rrset(pkt, rrset, 20 /* env->cfg->iter_scrub_ns */);
567*7037e34cSflorian 					}
568ae8c6e27Sflorian 					prev = rrset;
569ae8c6e27Sflorian 					rrset = rrset->rrset_all_next;
570ae8c6e27Sflorian 				}
571ae8c6e27Sflorian 			}
572ae8c6e27Sflorian 			continue;
573ae8c6e27Sflorian 		}
574ae8c6e27Sflorian 
575ae8c6e27Sflorian 		/* Otherwise, make sure that the RRset matches the qtype. */
576ae8c6e27Sflorian 		if(qinfo->qtype != LDNS_RR_TYPE_ANY &&
577ae8c6e27Sflorian 			qinfo->qtype != rrset->type) {
578ae8c6e27Sflorian 			remove_rrset("normalize: removing irrelevant RRset:",
579ae8c6e27Sflorian 				pkt, msg, prev, &rrset);
580ae8c6e27Sflorian 			continue;
581ae8c6e27Sflorian 		}
582ae8c6e27Sflorian 
583*7037e34cSflorian 		if(rrset->type == LDNS_RR_TYPE_NS &&
584*7037e34cSflorian 			rrset->rr_count > 20 /* env->cfg->iter_scrub_ns */) {
585*7037e34cSflorian 			shorten_rrset(pkt, rrset, 20 /* env->cfg->iter_scrub_ns */);
586*7037e34cSflorian 		}
587*7037e34cSflorian 
588ae8c6e27Sflorian 		/* Mark the additional names from relevant rrset as OK. */
589ae8c6e27Sflorian 		/* only for RRsets that match the query name, other ones
590ae8c6e27Sflorian 		 * will be removed by sanitize, so no additional for them */
591ae8c6e27Sflorian 		if(dname_pkt_compare(pkt, qinfo->qname, rrset->dname) == 0)
592ae8c6e27Sflorian 			mark_additional_rrset(pkt, msg, rrset);
593ae8c6e27Sflorian 
594ae8c6e27Sflorian 		prev = rrset;
595ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
596ae8c6e27Sflorian 	}
597ae8c6e27Sflorian 
598ae8c6e27Sflorian 	/* Mark additional names from AUTHORITY */
599ae8c6e27Sflorian 	while(rrset && rrset->section == LDNS_SECTION_AUTHORITY) {
600d500c338Sflorian 		/* protect internals of recursor by making sure to del these */
601ae8c6e27Sflorian 		if(rrset->type==LDNS_RR_TYPE_DNAME ||
602ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_CNAME ||
603ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_A ||
604ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_AAAA) {
605ae8c6e27Sflorian 			remove_rrset("normalize: removing irrelevant "
606ae8c6e27Sflorian 				"RRset:", pkt, msg, prev, &rrset);
607ae8c6e27Sflorian 			continue;
608ae8c6e27Sflorian 		}
609d500c338Sflorian 		/* Allowed list of types in the authority section */
610d500c338Sflorian 		if(env->cfg->harden_unknown_additional &&
611d500c338Sflorian 			!type_allowed_in_authority_section(rrset->type)) {
612d500c338Sflorian 			remove_rrset("normalize: removing irrelevant "
613d500c338Sflorian 				"RRset:", pkt, msg, prev, &rrset);
614d500c338Sflorian 			continue;
615d500c338Sflorian 		}
616ae8c6e27Sflorian 		/* only one NS set allowed in authority section */
617ae8c6e27Sflorian 		if(rrset->type==LDNS_RR_TYPE_NS) {
618ae8c6e27Sflorian 			/* NS set must be pertinent to the query */
619ae8c6e27Sflorian 			if(!sub_of_pkt(pkt, qinfo->qname, rrset->dname)) {
620ae8c6e27Sflorian 				remove_rrset("normalize: removing irrelevant "
621ae8c6e27Sflorian 					"RRset:", pkt, msg, prev, &rrset);
622ae8c6e27Sflorian 				continue;
623ae8c6e27Sflorian 			}
624ae8c6e27Sflorian 			/* we don't want NS sets for NXDOMAIN answers,
625ae8c6e27Sflorian 			 * because they could contain poisonous contents,
626ae8c6e27Sflorian 			 * from. eg. fragmentation attacks, inserted after
627ae8c6e27Sflorian 			 * long RRSIGs in the packet get to the packet
628ae8c6e27Sflorian 			 * border and such */
629ae8c6e27Sflorian 			/* also for NODATA answers */
630ae8c6e27Sflorian 			if(FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NXDOMAIN ||
631ae8c6e27Sflorian 			   (FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NOERROR
632ae8c6e27Sflorian 			    && soa_in_auth(msg) && msg->an_rrsets == 0)) {
633ae8c6e27Sflorian 				remove_rrset("normalize: removing irrelevant "
634ae8c6e27Sflorian 					"RRset:", pkt, msg, prev, &rrset);
635ae8c6e27Sflorian 				continue;
636ae8c6e27Sflorian 			}
637ae8c6e27Sflorian 			if(nsset == NULL) {
638ae8c6e27Sflorian 				nsset = rrset;
639ae8c6e27Sflorian 			} else {
640ae8c6e27Sflorian 				remove_rrset("normalize: removing irrelevant "
641ae8c6e27Sflorian 					"RRset:", pkt, msg, prev, &rrset);
642ae8c6e27Sflorian 				continue;
643ae8c6e27Sflorian 			}
644*7037e34cSflorian 			if(rrset->rr_count > 20 /* env->cfg->iter_scrub_ns */) {
645*7037e34cSflorian 				/* If this is not a referral, and the NS RRset
646*7037e34cSflorian 				 * is signed, then remove it entirely, so
647*7037e34cSflorian 				 * that when it becomes bogus it does not
648*7037e34cSflorian 				 * make the message that is otherwise fine
649*7037e34cSflorian 				 * into a bogus message. */
650*7037e34cSflorian 				if(!(msg->an_rrsets == 0 &&
651*7037e34cSflorian 					FLAGS_GET_RCODE(msg->flags) ==
652*7037e34cSflorian 					LDNS_RCODE_NOERROR &&
653*7037e34cSflorian 					!soa_in_auth(msg) &&
654*7037e34cSflorian 					!(msg->flags & BIT_AA)) &&
655*7037e34cSflorian 					rrset->rrsig_count != 0) {
656*7037e34cSflorian 					remove_rrset("normalize: removing too large NS "
657*7037e34cSflorian 						"RRset:", pkt, msg, prev, &rrset);
658*7037e34cSflorian 					continue;
659*7037e34cSflorian 				} else {
660*7037e34cSflorian 					shorten_rrset(pkt, rrset, 20 /* env->cfg->iter_scrub_ns */);
661*7037e34cSflorian 				}
662*7037e34cSflorian 			}
663ae8c6e27Sflorian 		}
664ae8c6e27Sflorian 		/* if this is type DS and we query for type DS we just got
665ae8c6e27Sflorian 		 * a referral answer for our type DS query, fix packet */
666ae8c6e27Sflorian 		if(rrset->type==LDNS_RR_TYPE_DS &&
667ae8c6e27Sflorian 			qinfo->qtype == LDNS_RR_TYPE_DS &&
668ae8c6e27Sflorian 			dname_pkt_compare(pkt, qinfo->qname, rrset->dname) == 0) {
669ae8c6e27Sflorian 			rrset->section = LDNS_SECTION_ANSWER;
670ae8c6e27Sflorian 			msg->ancount = rrset->rr_count + rrset->rrsig_count;
671ae8c6e27Sflorian 			msg->nscount = 0;
672ae8c6e27Sflorian 			msg->arcount = 0;
673ae8c6e27Sflorian 			msg->an_rrsets = 1;
674ae8c6e27Sflorian 			msg->ns_rrsets = 0;
675ae8c6e27Sflorian 			msg->ar_rrsets = 0;
676ae8c6e27Sflorian 			msg->rrset_count = 1;
677ae8c6e27Sflorian 			msg->rrset_first = rrset;
678ae8c6e27Sflorian 			msg->rrset_last = rrset;
679ae8c6e27Sflorian 			rrset->rrset_all_next = NULL;
680ae8c6e27Sflorian 			return 1;
681ae8c6e27Sflorian 		}
682ae8c6e27Sflorian 		mark_additional_rrset(pkt, msg, rrset);
683ae8c6e27Sflorian 		prev = rrset;
684ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
685ae8c6e27Sflorian 	}
686ae8c6e27Sflorian 
687ae8c6e27Sflorian 	/* For each record in the additional section, remove it if it is an
688ae8c6e27Sflorian 	 * address record and not in the collection of additional names
689ae8c6e27Sflorian 	 * found in ANSWER and AUTHORITY. */
690ae8c6e27Sflorian 	/* These records have not been marked OK previously */
691ae8c6e27Sflorian 	while(rrset && rrset->section == LDNS_SECTION_ADDITIONAL) {
692ae8c6e27Sflorian 		if(rrset->type==LDNS_RR_TYPE_A ||
693ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_AAAA)
694ae8c6e27Sflorian 		{
695ae8c6e27Sflorian 			if((rrset->flags & RRSET_SCRUB_OK)) {
696ae8c6e27Sflorian 				/* remove flag to clean up flags variable */
697ae8c6e27Sflorian 				rrset->flags &= ~RRSET_SCRUB_OK;
698ae8c6e27Sflorian 			} else {
699ae8c6e27Sflorian 				remove_rrset("normalize: removing irrelevant "
700ae8c6e27Sflorian 					"RRset:", pkt, msg, prev, &rrset);
701ae8c6e27Sflorian 				continue;
702ae8c6e27Sflorian 			}
703ae8c6e27Sflorian 		}
704d500c338Sflorian 		/* protect internals of recursor by making sure to del these */
705ae8c6e27Sflorian 		if(rrset->type==LDNS_RR_TYPE_DNAME ||
706ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_CNAME ||
707ae8c6e27Sflorian 			rrset->type==LDNS_RR_TYPE_NS) {
708ae8c6e27Sflorian 			remove_rrset("normalize: removing irrelevant "
709ae8c6e27Sflorian 				"RRset:", pkt, msg, prev, &rrset);
710ae8c6e27Sflorian 			continue;
711ae8c6e27Sflorian 		}
712d500c338Sflorian 		/* Allowed list of types in the additional section */
713d500c338Sflorian 		if(env->cfg->harden_unknown_additional &&
714d500c338Sflorian 			!type_allowed_in_additional_section(rrset->type)) {
715d500c338Sflorian 			remove_rrset("normalize: removing irrelevant "
716d500c338Sflorian 				"RRset:", pkt, msg, prev, &rrset);
717d500c338Sflorian 			continue;
718d500c338Sflorian 		}
719ae8c6e27Sflorian 		prev = rrset;
720ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
721ae8c6e27Sflorian 	}
722ae8c6e27Sflorian 
723ae8c6e27Sflorian 	return 1;
724ae8c6e27Sflorian }
725ae8c6e27Sflorian 
726ae8c6e27Sflorian /**
727ae8c6e27Sflorian  * Store potential poison in the cache (only if hardening disabled).
728ae8c6e27Sflorian  * The rrset is stored in the cache but removed from the message.
729ae8c6e27Sflorian  * So that it will be used for infrastructure purposes, but not be
730ae8c6e27Sflorian  * returned to the client.
731ae8c6e27Sflorian  * @param pkt: packet
732ae8c6e27Sflorian  * @param msg: message parsed
733ae8c6e27Sflorian  * @param env: environment with cache
734ae8c6e27Sflorian  * @param rrset: to store.
735ae8c6e27Sflorian  */
736ae8c6e27Sflorian static void
store_rrset(sldns_buffer * pkt,struct msg_parse * msg,struct module_env * env,struct rrset_parse * rrset)737ae8c6e27Sflorian store_rrset(sldns_buffer* pkt, struct msg_parse* msg, struct module_env* env,
738ae8c6e27Sflorian 	struct rrset_parse* rrset)
739ae8c6e27Sflorian {
740ae8c6e27Sflorian 	struct ub_packed_rrset_key* k;
741ae8c6e27Sflorian 	struct packed_rrset_data* d;
742ae8c6e27Sflorian 	struct rrset_ref ref;
743ae8c6e27Sflorian 	time_t now = *env->now;
744ae8c6e27Sflorian 
745ae8c6e27Sflorian 	k = alloc_special_obtain(env->alloc);
746ae8c6e27Sflorian 	if(!k)
747ae8c6e27Sflorian 		return;
748ae8c6e27Sflorian 	k->entry.data = NULL;
749ae8c6e27Sflorian 	if(!parse_copy_decompress_rrset(pkt, msg, rrset, NULL, k)) {
750ae8c6e27Sflorian 		alloc_special_release(env->alloc, k);
751ae8c6e27Sflorian 		return;
752ae8c6e27Sflorian 	}
753ae8c6e27Sflorian 	d = (struct packed_rrset_data*)k->entry.data;
754ae8c6e27Sflorian 	packed_rrset_ttl_add(d, now);
755ae8c6e27Sflorian 	ref.key = k;
756ae8c6e27Sflorian 	ref.id = k->id;
757ae8c6e27Sflorian 	/*ignore ret: it was in the cache, ref updated */
758ae8c6e27Sflorian 	(void)rrset_cache_update(env->rrset_cache, &ref, env->alloc, now);
759ae8c6e27Sflorian }
760ae8c6e27Sflorian 
761ae8c6e27Sflorian /**
762ae8c6e27Sflorian  * Check if right hand name in NSEC is within zone
763411c5950Sflorian  * @param pkt: the packet buffer for decompression.
764ae8c6e27Sflorian  * @param rrset: the NSEC rrset
765ae8c6e27Sflorian  * @param zonename: the zone name.
766ae8c6e27Sflorian  * @return true if BAD.
767ae8c6e27Sflorian  */
sanitize_nsec_is_overreach(sldns_buffer * pkt,struct rrset_parse * rrset,uint8_t * zonename)768411c5950Sflorian static int sanitize_nsec_is_overreach(sldns_buffer* pkt,
769411c5950Sflorian 	struct rrset_parse* rrset, uint8_t* zonename)
770ae8c6e27Sflorian {
771ae8c6e27Sflorian 	struct rr_parse* rr;
772ae8c6e27Sflorian 	uint8_t* rhs;
773ae8c6e27Sflorian 	size_t len;
774ae8c6e27Sflorian 	log_assert(rrset->type == LDNS_RR_TYPE_NSEC);
775ae8c6e27Sflorian 	for(rr = rrset->rr_first; rr; rr = rr->next) {
776411c5950Sflorian 		size_t pos = sldns_buffer_position(pkt);
777411c5950Sflorian 		size_t rhspos;
778ae8c6e27Sflorian 		rhs = rr->ttl_data+4+2;
779ae8c6e27Sflorian 		len = sldns_read_uint16(rr->ttl_data+4);
780411c5950Sflorian 		rhspos = rhs-sldns_buffer_begin(pkt);
781411c5950Sflorian 		sldns_buffer_set_position(pkt, rhspos);
782411c5950Sflorian 		if(pkt_dname_len(pkt) == 0) {
783411c5950Sflorian 			/* malformed */
784411c5950Sflorian 			sldns_buffer_set_position(pkt, pos);
785ae8c6e27Sflorian 			return 1;
786ae8c6e27Sflorian 		}
787411c5950Sflorian 		if(sldns_buffer_position(pkt)-rhspos > len) {
788411c5950Sflorian 			/* outside of rdata boundaries */
789411c5950Sflorian 			sldns_buffer_set_position(pkt, pos);
790411c5950Sflorian 			return 1;
791411c5950Sflorian 		}
792411c5950Sflorian 		sldns_buffer_set_position(pkt, pos);
793411c5950Sflorian 		if(!pkt_sub(pkt, rhs, zonename)) {
794ae8c6e27Sflorian 			/* overreaching */
795ae8c6e27Sflorian 			return 1;
796ae8c6e27Sflorian 		}
797ae8c6e27Sflorian 	}
798ae8c6e27Sflorian 	/* all NSEC RRs OK */
799ae8c6e27Sflorian 	return 0;
800ae8c6e27Sflorian }
801ae8c6e27Sflorian 
80254cc57acSflorian /** Remove individual RRs, if the length is wrong. Returns true if the RRset
80354cc57acSflorian  * has been removed. */
80454cc57acSflorian static int
scrub_sanitize_rr_length(sldns_buffer * pkt,struct msg_parse * msg,struct rrset_parse * prev,struct rrset_parse ** rrset,int * added_ede,struct module_qstate * qstate)80554cc57acSflorian scrub_sanitize_rr_length(sldns_buffer* pkt, struct msg_parse* msg,
80654cc57acSflorian 	struct rrset_parse* prev, struct rrset_parse** rrset, int* added_ede,
80754cc57acSflorian 	struct module_qstate* qstate)
80854cc57acSflorian {
80954cc57acSflorian 	struct rr_parse* rr, *rr_prev = NULL;
81054cc57acSflorian 	for(rr = (*rrset)->rr_first; rr; rr = rr->next) {
81154cc57acSflorian 
81254cc57acSflorian 		/* Sanity check for length of records
81354cc57acSflorian 		 * An A record should be 6 bytes only
81454cc57acSflorian 		 * (2 bytes for length and 4 for IPv4 addr)*/
81554cc57acSflorian 		if((*rrset)->type == LDNS_RR_TYPE_A && rr->size != 6 ) {
81654cc57acSflorian 			if(!*added_ede) {
81754cc57acSflorian 				*added_ede = 1;
81854cc57acSflorian 				errinf_ede(qstate, "sanitize: records of inappropriate length have been removed.",
81954cc57acSflorian 					LDNS_EDE_OTHER);
82054cc57acSflorian 			}
82154cc57acSflorian 			if(msgparse_rrset_remove_rr("sanitize: removing type A RR of inappropriate length:",
82254cc57acSflorian 				pkt, *rrset, rr_prev, rr, NULL, 0)) {
82354cc57acSflorian 				remove_rrset("sanitize: removing type A RRset of inappropriate length:",
82454cc57acSflorian 					pkt, msg, prev, rrset);
82554cc57acSflorian 				return 1;
82654cc57acSflorian 			}
82754cc57acSflorian 			continue;
82854cc57acSflorian 		}
82954cc57acSflorian 
83054cc57acSflorian 		/* Sanity check for length of records
83154cc57acSflorian 		 * An AAAA record should be 18 bytes only
83254cc57acSflorian 		 * (2 bytes for length and 16 for IPv6 addr)*/
83354cc57acSflorian 		if((*rrset)->type == LDNS_RR_TYPE_AAAA && rr->size != 18 ) {
83454cc57acSflorian 			if(!*added_ede) {
83554cc57acSflorian 				*added_ede = 1;
83654cc57acSflorian 				errinf_ede(qstate, "sanitize: records of inappropriate length have been removed.",
83754cc57acSflorian 					LDNS_EDE_OTHER);
83854cc57acSflorian 			}
83954cc57acSflorian 			if(msgparse_rrset_remove_rr("sanitize: removing type AAAA RR of inappropriate length:",
84054cc57acSflorian 				pkt, *rrset, rr_prev, rr, NULL, 0)) {
84154cc57acSflorian 				remove_rrset("sanitize: removing type AAAA RRset of inappropriate length:",
84254cc57acSflorian 					pkt, msg, prev, rrset);
84354cc57acSflorian 				return 1;
84454cc57acSflorian 			}
84554cc57acSflorian 			continue;
84654cc57acSflorian 		}
84754cc57acSflorian 		rr_prev = rr;
84854cc57acSflorian 	}
84954cc57acSflorian 	return 0;
85054cc57acSflorian }
85154cc57acSflorian 
852ae8c6e27Sflorian /**
853ae8c6e27Sflorian  * Given a response event, remove suspect RRsets from the response.
854ae8c6e27Sflorian  * "Suspect" rrsets are potentially poison. Note that this routine expects
855ae8c6e27Sflorian  * the response to be in a "normalized" state -- that is, all "irrelevant"
856ae8c6e27Sflorian  * RRsets have already been removed, CNAMEs are in order, etc.
857ae8c6e27Sflorian  *
858ae8c6e27Sflorian  * @param pkt: packet.
859ae8c6e27Sflorian  * @param msg: msg to normalize.
860ae8c6e27Sflorian  * @param qinfo: the question originally asked.
861ae8c6e27Sflorian  * @param zonename: name of server zone.
862ae8c6e27Sflorian  * @param env: module environment with config and cache.
863ae8c6e27Sflorian  * @param ie: iterator environment with private address data.
86454cc57acSflorian  * @param qstate: for setting errinf for EDE error messages.
865ae8c6e27Sflorian  * @return 0 on error.
866ae8c6e27Sflorian  */
867ae8c6e27Sflorian static int
scrub_sanitize(sldns_buffer * pkt,struct msg_parse * msg,struct query_info * qinfo,uint8_t * zonename,struct module_env * env,struct iter_env * ie,struct module_qstate * qstate)868ae8c6e27Sflorian scrub_sanitize(sldns_buffer* pkt, struct msg_parse* msg,
869ae8c6e27Sflorian 	struct query_info* qinfo, uint8_t* zonename, struct module_env* env,
87054cc57acSflorian 	struct iter_env* ie, struct module_qstate* qstate)
871ae8c6e27Sflorian {
872ae8c6e27Sflorian 	int del_addi = 0; /* if additional-holding rrsets are deleted, we
873ae8c6e27Sflorian 		do not trust the normalized additional-A-AAAA any more */
87454cc57acSflorian 	int added_rrlen_ede = 0;
875ae8c6e27Sflorian 	struct rrset_parse* rrset, *prev;
876ae8c6e27Sflorian 	prev = NULL;
877ae8c6e27Sflorian 	rrset = msg->rrset_first;
878ae8c6e27Sflorian 
879ae8c6e27Sflorian 	/* the first DNAME is allowed to stay. It needs checking before
880ae8c6e27Sflorian 	 * it can be used from the cache. After normalization, an initial
881ae8c6e27Sflorian 	 * DNAME will have a correctly synthesized CNAME after it. */
882ae8c6e27Sflorian 	if(rrset && rrset->type == LDNS_RR_TYPE_DNAME &&
883ae8c6e27Sflorian 		rrset->section == LDNS_SECTION_ANSWER &&
884ae8c6e27Sflorian 		pkt_strict_sub(pkt, qinfo->qname, rrset->dname) &&
885ae8c6e27Sflorian 		pkt_sub(pkt, rrset->dname, zonename)) {
886ae8c6e27Sflorian 		prev = rrset; /* DNAME allowed to stay in answer section */
887ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
888ae8c6e27Sflorian 	}
889ae8c6e27Sflorian 
890ae8c6e27Sflorian 	/* remove all records from the answer section that are
891ae8c6e27Sflorian 	 * not the same domain name as the query domain name.
892ae8c6e27Sflorian 	 * The answer section should contain rrsets with the same name
893ae8c6e27Sflorian 	 * as the question. For DNAMEs a CNAME has been synthesized.
894ae8c6e27Sflorian 	 * Wildcards have the query name in answer section.
895ae8c6e27Sflorian 	 * ANY queries get query name in answer section.
896ae8c6e27Sflorian 	 * Remainders of CNAME chains are cut off and resolved by iterator. */
897ae8c6e27Sflorian 	while(rrset && rrset->section == LDNS_SECTION_ANSWER) {
898ae8c6e27Sflorian 		if(dname_pkt_compare(pkt, qinfo->qname, rrset->dname) != 0) {
899ae8c6e27Sflorian 			if(has_additional(rrset->type)) del_addi = 1;
900ae8c6e27Sflorian 			remove_rrset("sanitize: removing extraneous answer "
901ae8c6e27Sflorian 				"RRset:", pkt, msg, prev, &rrset);
902ae8c6e27Sflorian 			continue;
903ae8c6e27Sflorian 		}
904ae8c6e27Sflorian 		prev = rrset;
905ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
906ae8c6e27Sflorian 	}
907ae8c6e27Sflorian 
908ae8c6e27Sflorian 	/* At this point, we brutally remove ALL rrsets that aren't
909ae8c6e27Sflorian 	 * children of the originating zone. The idea here is that,
910ae8c6e27Sflorian 	 * as far as we know, the server that we contacted is ONLY
911ae8c6e27Sflorian 	 * authoritative for the originating zone. It, of course, MAY
912ae8c6e27Sflorian 	 * be authoritative for any other zones, and of course, MAY
913ae8c6e27Sflorian 	 * NOT be authoritative for some subdomains of the originating
914ae8c6e27Sflorian 	 * zone. */
915ae8c6e27Sflorian 	prev = NULL;
916ae8c6e27Sflorian 	rrset = msg->rrset_first;
917ae8c6e27Sflorian 	while(rrset) {
918ae8c6e27Sflorian 
91954cc57acSflorian 		/* Sanity check for length of records */
92054cc57acSflorian 		if(rrset->type == LDNS_RR_TYPE_A ||
92154cc57acSflorian 			rrset->type == LDNS_RR_TYPE_AAAA) {
92254cc57acSflorian 			if(scrub_sanitize_rr_length(pkt, msg, prev, &rrset,
92354cc57acSflorian 				&added_rrlen_ede, qstate))
92454cc57acSflorian 				continue;
92554cc57acSflorian 		}
92654cc57acSflorian 
927ae8c6e27Sflorian 		/* remove private addresses */
928ae8c6e27Sflorian 		if( (rrset->type == LDNS_RR_TYPE_A ||
929ae8c6e27Sflorian 			rrset->type == LDNS_RR_TYPE_AAAA)) {
930ae8c6e27Sflorian 
931ae8c6e27Sflorian 			/* do not set servfail since this leads to too
932ae8c6e27Sflorian 			 * many drops of other people using rfc1918 space */
933ae8c6e27Sflorian 			/* also do not remove entire rrset, unless all records
934ae8c6e27Sflorian 			 * in it are bad */
935ae8c6e27Sflorian 			if(priv_rrset_bad(ie->priv, pkt, rrset)) {
936ae8c6e27Sflorian 				remove_rrset(NULL, pkt, msg, prev, &rrset);
937ae8c6e27Sflorian 				continue;
938ae8c6e27Sflorian 			}
939ae8c6e27Sflorian 		}
940ae8c6e27Sflorian 
941ae8c6e27Sflorian 		/* skip DNAME records -- they will always be followed by a
942ae8c6e27Sflorian 		 * synthesized CNAME, which will be relevant.
943ae8c6e27Sflorian 		 * FIXME: should this do something differently with DNAME
944ae8c6e27Sflorian 		 * rrsets NOT in Section.ANSWER? */
945ae8c6e27Sflorian 		/* But since DNAME records are also subdomains of the zone,
946ae8c6e27Sflorian 		 * same check can be used */
947ae8c6e27Sflorian 
948ae8c6e27Sflorian 		if(!pkt_sub(pkt, rrset->dname, zonename)) {
949ae8c6e27Sflorian 			if(msg->an_rrsets == 0 &&
950ae8c6e27Sflorian 				rrset->type == LDNS_RR_TYPE_NS &&
951ae8c6e27Sflorian 				rrset->section == LDNS_SECTION_AUTHORITY &&
952ae8c6e27Sflorian 				FLAGS_GET_RCODE(msg->flags) ==
953ae8c6e27Sflorian 				LDNS_RCODE_NOERROR && !soa_in_auth(msg) &&
954ae8c6e27Sflorian 				sub_of_pkt(pkt, zonename, rrset->dname)) {
955ae8c6e27Sflorian 				/* noerror, nodata and this NS rrset is above
956ae8c6e27Sflorian 				 * the zone. This is LAME!
957ae8c6e27Sflorian 				 * Leave in the NS for lame classification. */
958ae8c6e27Sflorian 				/* remove everything from the additional
959ae8c6e27Sflorian 				 * (we dont want its glue that was approved
960ae8c6e27Sflorian 				 * during the normalize action) */
961ae8c6e27Sflorian 				del_addi = 1;
962ae8c6e27Sflorian 			} else if(!env->cfg->harden_glue && (
963ae8c6e27Sflorian 				rrset->type == LDNS_RR_TYPE_A ||
964ae8c6e27Sflorian 				rrset->type == LDNS_RR_TYPE_AAAA)) {
965ae8c6e27Sflorian 				/* store in cache! Since it is relevant
966ae8c6e27Sflorian 				 * (from normalize) it will be picked up
967ae8c6e27Sflorian 				 * from the cache to be used later */
968ae8c6e27Sflorian 				store_rrset(pkt, msg, env, rrset);
969ae8c6e27Sflorian 				remove_rrset("sanitize: storing potential "
970ae8c6e27Sflorian 				"poison RRset:", pkt, msg, prev, &rrset);
971ae8c6e27Sflorian 				continue;
972ae8c6e27Sflorian 			} else {
973ae8c6e27Sflorian 				if(has_additional(rrset->type)) del_addi = 1;
974ae8c6e27Sflorian 				remove_rrset("sanitize: removing potential "
975ae8c6e27Sflorian 				"poison RRset:", pkt, msg, prev, &rrset);
976ae8c6e27Sflorian 				continue;
977ae8c6e27Sflorian 			}
978ae8c6e27Sflorian 		}
979ae8c6e27Sflorian 		if(del_addi && rrset->section == LDNS_SECTION_ADDITIONAL) {
980ae8c6e27Sflorian 			remove_rrset("sanitize: removing potential "
981ae8c6e27Sflorian 			"poison reference RRset:", pkt, msg, prev, &rrset);
982ae8c6e27Sflorian 			continue;
983ae8c6e27Sflorian 		}
984ae8c6e27Sflorian 		/* check if right hand side of NSEC is within zone */
985ae8c6e27Sflorian 		if(rrset->type == LDNS_RR_TYPE_NSEC &&
986411c5950Sflorian 			sanitize_nsec_is_overreach(pkt, rrset, zonename)) {
987ae8c6e27Sflorian 			remove_rrset("sanitize: removing overreaching NSEC "
988ae8c6e27Sflorian 				"RRset:", pkt, msg, prev, &rrset);
989ae8c6e27Sflorian 			continue;
990ae8c6e27Sflorian 		}
991ae8c6e27Sflorian 		prev = rrset;
992ae8c6e27Sflorian 		rrset = rrset->rrset_all_next;
993ae8c6e27Sflorian 	}
994ae8c6e27Sflorian 	return 1;
995ae8c6e27Sflorian }
996ae8c6e27Sflorian 
997ae8c6e27Sflorian int
scrub_message(sldns_buffer * pkt,struct msg_parse * msg,struct query_info * qinfo,uint8_t * zonename,struct regional * region,struct module_env * env,struct module_qstate * qstate,struct iter_env * ie)998ae8c6e27Sflorian scrub_message(sldns_buffer* pkt, struct msg_parse* msg,
999ae8c6e27Sflorian 	struct query_info* qinfo, uint8_t* zonename, struct regional* region,
100054cc57acSflorian 	struct module_env* env, struct module_qstate* qstate,
100154cc57acSflorian 	struct iter_env* ie)
1002ae8c6e27Sflorian {
1003ae8c6e27Sflorian 	/* basic sanity checks */
1004ae8c6e27Sflorian 	log_nametypeclass(VERB_ALGO, "scrub for", zonename, LDNS_RR_TYPE_NS,
1005ae8c6e27Sflorian 		qinfo->qclass);
1006ae8c6e27Sflorian 	if(msg->qdcount > 1)
1007ae8c6e27Sflorian 		return 0;
1008ae8c6e27Sflorian 	if( !(msg->flags&BIT_QR) )
1009ae8c6e27Sflorian 		return 0;
1010ae8c6e27Sflorian 	msg->flags &= ~(BIT_AD|BIT_Z); /* force off bit AD and Z */
1011ae8c6e27Sflorian 
1012ae8c6e27Sflorian 	/* make sure that a query is echoed back when NOERROR or NXDOMAIN */
1013ae8c6e27Sflorian 	/* this is not required for basic operation but is a forgery
1014ae8c6e27Sflorian 	 * resistance (security) feature */
1015ae8c6e27Sflorian 	if((FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NOERROR ||
1016ae8c6e27Sflorian 		FLAGS_GET_RCODE(msg->flags) == LDNS_RCODE_NXDOMAIN) &&
1017ae8c6e27Sflorian 		msg->qdcount == 0)
1018ae8c6e27Sflorian 		return 0;
1019ae8c6e27Sflorian 
1020ae8c6e27Sflorian 	/* if a query is echoed back, make sure it is correct. Otherwise,
1021ae8c6e27Sflorian 	 * this may be not a reply to our query. */
1022ae8c6e27Sflorian 	if(msg->qdcount == 1) {
1023ae8c6e27Sflorian 		if(dname_pkt_compare(pkt, msg->qname, qinfo->qname) != 0)
1024ae8c6e27Sflorian 			return 0;
1025ae8c6e27Sflorian 		if(msg->qtype != qinfo->qtype || msg->qclass != qinfo->qclass)
1026ae8c6e27Sflorian 			return 0;
1027ae8c6e27Sflorian 	}
1028ae8c6e27Sflorian 
1029ae8c6e27Sflorian 	/* normalize the response, this cleans up the additional.  */
1030d500c338Sflorian 	if(!scrub_normalize(pkt, msg, qinfo, region, env))
1031ae8c6e27Sflorian 		return 0;
1032ae8c6e27Sflorian 	/* delete all out-of-zone information */
103354cc57acSflorian 	if(!scrub_sanitize(pkt, msg, qinfo, zonename, env, ie, qstate))
1034ae8c6e27Sflorian 		return 0;
1035ae8c6e27Sflorian 	return 1;
1036ae8c6e27Sflorian }
1037