a8f6fd0
diff --git a/bind/bind-9.11.36/lib/dns/resolver.c b/bind/bind-9.11.36/lib/dns/resolver.c
a8f6fd0
index b34cb12..8ae9a99 100644
a8f6fd0
--- a/bind/bind-9.11.36/lib/dns/resolver.c
a8f6fd0
+++ b/bind/bind-9.11.36/lib/dns/resolver.c
a8f6fd0
@@ -63,6 +63,7 @@
a8f6fd0
 #include <dns/stats.h>
a8f6fd0
 #include <dns/tsig.h>
a8f6fd0
 #include <dns/validator.h>
a8f6fd0
+#include <dns/zone.h>
a8f6fd0
 
a8f6fd0
 #ifdef WANT_QUERYTRACE
a8f6fd0
 #define RTRACE(m)       isc_log_write(dns_lctx, \
a8f6fd0
@@ -311,6 +312,8 @@ struct fetchctx {
a8f6fd0
 	bool			ns_ttl_ok;
a8f6fd0
 	uint32_t			ns_ttl;
a8f6fd0
 	isc_counter_t *			qc;
a8f6fd0
+	dns_fixedname_t			fwdfname;
a8f6fd0
+	dns_name_t			*fwdname;
a8f6fd0
 
a8f6fd0
 	/*%
a8f6fd0
 	 * The number of events we're waiting for.
a8f6fd0
@@ -3389,6 +3392,7 @@ fctx_getaddresses(fetchctx_t *fctx, bool badcache) {
a8f6fd0
 		if (result == ISC_R_SUCCESS) {
a8f6fd0
 			fwd = ISC_LIST_HEAD(forwarders->fwdrs);
a8f6fd0
 			fctx->fwdpolicy = forwarders->fwdpolicy;
a8f6fd0
+			dns_name_copy(domain, fctx->fwdname, NULL);
a8f6fd0
 			if (fctx->fwdpolicy == dns_fwdpolicy_only &&
a8f6fd0
 			    isstrictsubdomain(domain, &fctx->domain)) {
a8f6fd0
 				fcount_decr(fctx);
a8f6fd0
@@ -4418,6 +4422,9 @@ fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
a8f6fd0
 	fctx->restarts = 0;
a8f6fd0
 	fctx->querysent = 0;
a8f6fd0
 	fctx->referrals = 0;
a8f6fd0
+
a8f6fd0
+	fctx->fwdname = dns_fixedname_initname(&fctx->fwdfname);
a8f6fd0
+
a8f6fd0
 	TIME_NOW(&fctx->start);
a8f6fd0
 	fctx->timeouts = 0;
a8f6fd0
 	fctx->lamecount = 0;
a8f6fd0
@@ -4476,8 +4483,10 @@ fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
a8f6fd0
 		domain = dns_fixedname_initname(&fixed);
a8f6fd0
 		result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
a8f6fd0
 					    domain, &forwarders);
a8f6fd0
-		if (result == ISC_R_SUCCESS)
a8f6fd0
+		if (result == ISC_R_SUCCESS) {
a8f6fd0
 			fctx->fwdpolicy = forwarders->fwdpolicy;
a8f6fd0
+			dns_name_copy(domain, fctx->fwdname, NULL);
a8f6fd0
+		}
a8f6fd0
 
a8f6fd0
 		if (fctx->fwdpolicy != dns_fwdpolicy_only) {
a8f6fd0
 			/*
a8f6fd0
@@ -6226,6 +6235,112 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
a8f6fd0
 		rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
a8f6fd0
 }
a8f6fd0
 
a8f6fd0
+/*
a8f6fd0
+ * Returns true if 'name' is external to the namespace for which
a8f6fd0
+ * the server being queried can answer, either because it's not a
a8f6fd0
+ * subdomain or because it's below a forward declaration or a
a8f6fd0
+ * locally served zone.
a8f6fd0
+ */
a8f6fd0
+static inline bool
a8f6fd0
+name_external(dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) {
a8f6fd0
+	isc_result_t result;
a8f6fd0
+	dns_forwarders_t *forwarders = NULL;
a8f6fd0
+	dns_fixedname_t fixed, zfixed;
a8f6fd0
+	dns_name_t *fname = dns_fixedname_initname(&fixed);
a8f6fd0
+	dns_name_t *zfname = dns_fixedname_initname(&zfixed);
a8f6fd0
+	dns_name_t *apex = NULL;
a8f6fd0
+	dns_name_t suffix;
a8f6fd0
+	dns_zone_t *zone = NULL;
a8f6fd0
+	unsigned int labels;
a8f6fd0
+	dns_namereln_t rel;
a8f6fd0
+	/*
a8f6fd0
+	 * The following two variables do not influence code flow; they are
a8f6fd0
+	 * only necessary for calling dns_name_fullcompare().
a8f6fd0
+	 */
a8f6fd0
+	int _orderp = 0;
a8f6fd0
+	unsigned int _nlabelsp = 0;
a8f6fd0
+
a8f6fd0
+	apex = ISFORWARDER(fctx->addrinfo) ? fctx->fwdname : &fctx->domain;
a8f6fd0
+
a8f6fd0
+	/*
a8f6fd0
+	 * The name is outside the queried namespace.
a8f6fd0
+	 */
a8f6fd0
+	rel = dns_name_fullcompare(name, apex, &_orderp, &_nlabelsp);
a8f6fd0
+	if (rel != dns_namereln_subdomain && rel != dns_namereln_equal) {
a8f6fd0
+		return (true);
a8f6fd0
+	}
a8f6fd0
+
a8f6fd0
+	/*
a8f6fd0
+	 * If the record lives in the parent zone, adjust the name so we
a8f6fd0
+	 * look for the correct zone or forward clause.
a8f6fd0
+	 */
a8f6fd0
+	labels = dns_name_countlabels(name);
a8f6fd0
+	if (dns_rdatatype_atparent(type) && labels > 1U) {
a8f6fd0
+		dns_name_init(&suffix, NULL);
a8f6fd0
+		dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
a8f6fd0
+		name = &suffix;
a8f6fd0
+	} else if (rel == dns_namereln_equal) {
a8f6fd0
+		/* If 'name' is 'apex', no further checking is needed. */
a8f6fd0
+		return (false);
a8f6fd0
+	}
a8f6fd0
+
a8f6fd0
+	/*
a8f6fd0
+	 * If there is a locally served zone between 'apex' and 'name'
a8f6fd0
+	 * then don't cache.
a8f6fd0
+	 */
a8f6fd0
+	LOCK(&fctx->res->view->lock);
a8f6fd0
+	if (fctx->res->view->zonetable != NULL) {
a8f6fd0
+		unsigned int options = DNS_ZTFIND_NOEXACT;
a8f6fd0
+		result = dns_zt_find(fctx->res->view->zonetable, name, options,
a8f6fd0
+				     zfname, &zone);
a8f6fd0
+		if (zone != NULL) {
a8f6fd0
+			dns_zone_detach(&zone);
a8f6fd0
+		}
a8f6fd0
+		if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
a8f6fd0
+			if (dns_name_fullcompare(zfname, apex, &_orderp,
a8f6fd0
+						 &_nlabelsp) ==
a8f6fd0
+			    dns_namereln_subdomain)
a8f6fd0
+			{
a8f6fd0
+				UNLOCK(&fctx->res->view->lock);
a8f6fd0
+				return (true);
a8f6fd0
+			}
a8f6fd0
+		}
a8f6fd0
+	}
a8f6fd0
+	UNLOCK(&fctx->res->view->lock);
a8f6fd0
+
a8f6fd0
+	/*
a8f6fd0
+	 * Look for a forward declaration below 'name'.
a8f6fd0
+	 */
a8f6fd0
+	result = dns_fwdtable_find2(fctx->res->view->fwdtable, name, fname,
a8f6fd0
+				    &forwarders);
a8f6fd0
+
a8f6fd0
+	if (ISFORWARDER(fctx->addrinfo)) {
a8f6fd0
+		/*
a8f6fd0
+		 * See if the forwarder declaration is better.
a8f6fd0
+		 */
a8f6fd0
+		if (result == ISC_R_SUCCESS) {
a8f6fd0
+			return (!dns_name_equal(fname, fctx->fwdname));
a8f6fd0
+		}
a8f6fd0
+
a8f6fd0
+		/*
a8f6fd0
+		 * If the lookup failed, the configuration must have
a8f6fd0
+		 * changed: play it safe and don't cache.
a8f6fd0
+		 */
a8f6fd0
+		return (true);
a8f6fd0
+	} else if (result == ISC_R_SUCCESS &&
a8f6fd0
+		   forwarders->fwdpolicy == dns_fwdpolicy_only &&
a8f6fd0
+		   !ISC_LIST_EMPTY(forwarders->fwdrs))
a8f6fd0
+	{
a8f6fd0
+		/*
a8f6fd0
+		 * If 'name' is covered by a 'forward only' clause then we
a8f6fd0
+		 * can't cache this repsonse.
a8f6fd0
+		 */
a8f6fd0
+		return (true);
a8f6fd0
+	}
a8f6fd0
+
a8f6fd0
+	return (false);
a8f6fd0
+}
a8f6fd0
+
a8f6fd0
 static isc_result_t
a8f6fd0
 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
a8f6fd0
 	      dns_section_t section)
a8f6fd0
@@ -6254,7 +6369,7 @@ check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
a8f6fd0
 	result = dns_message_findname(rmessage, section, addname,
a8f6fd0
 				      dns_rdatatype_any, 0, &name, NULL);
a8f6fd0
 	if (result == ISC_R_SUCCESS) {
a8f6fd0
-		external = !dns_name_issubdomain(name, &fctx->domain);
a8f6fd0
+		external = name_external(name, type, fctx);
a8f6fd0
 		if (type == dns_rdatatype_a) {
a8f6fd0
 			for (rdataset = ISC_LIST_HEAD(name->list);
a8f6fd0
 			     rdataset != NULL;
a8f6fd0
@@ -7136,6 +7251,13 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) {
a8f6fd0
 			break;
a8f6fd0
 
a8f6fd0
 		case dns_namereln_subdomain:
a8f6fd0
+			/*
a8f6fd0
+			 * Don't accept DNAME from parent namespace.
a8f6fd0
+			 */
a8f6fd0
+			if (name_external(name, dns_rdatatype_dname, fctx)) {
a8f6fd0
+				continue;
a8f6fd0
+			}
a8f6fd0
+
a8f6fd0
 			/*
a8f6fd0
 			 * In-scope DNAME records must have at least
a8f6fd0
 			 * as many labels as the domain being queried.
a8f6fd0
@@ -7371,11 +7493,9 @@ answer_response(fetchctx_t *fctx, dns_message_t *message) {
a8f6fd0
 	 */
a8f6fd0
 	result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
a8f6fd0
 	while (!done && result == ISC_R_SUCCESS) {
a8f6fd0
-		bool external;
a8f6fd0
 		name = NULL;
a8f6fd0
 		dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
a8f6fd0
-		external = !dns_name_issubdomain(name, &fctx->domain);
a8f6fd0
-		if (!external) {
a8f6fd0
+		if (!name_external(name, dns_rdatatype_ns, fctx)) {
a8f6fd0
 			/*
a8f6fd0
 			 * We expect to find NS or SIG NS rdatasets, and
a8f6fd0
 			 * nothing else.