Blame SOURCES/CVE-2021-25220.patch

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