Blame SOURCES/CVE-2021-25220.patch

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