Blame SOURCES/bind-9.16-CVE-2021-25220.patch

dc3c8a
From 5b2798e01346cd77741873091babf6c4a3128449 Mon Sep 17 00:00:00 2001
dc3c8a
From: Mark Andrews <marka@isc.org>
dc3c8a
Date: Wed, 19 Jan 2022 17:38:18 +1100
dc3c8a
Subject: [PATCH] Add additional name checks when using a forwarder
dc3c8a
dc3c8a
When using a forwarder, check that the owner name of response
dc3c8a
records are within the bailiwick of the forwarded name space.
dc3c8a
dc3c8a
(cherry picked from commit 24155213be59faad17f0215ecf73ea49ab781e5b)
dc3c8a
dc3c8a
Check that the forward declaration is unchanged and not overridden
dc3c8a
dc3c8a
If we are using a fowarder, in addition to checking that names to
dc3c8a
be cached are subdomains of the forwarded namespace, we must also
dc3c8a
check that there are no subsidiary forwarded namespaces which would
dc3c8a
take precedence. To be safe, we don't cache any responses if the
dc3c8a
forwarding configuration has changed since the query was sent.
dc3c8a
dc3c8a
(cherry picked from commit 3fc7accd88cd0890f8f57bb13765876774298ba3)
dc3c8a
dc3c8a
Check cached names for possible "forward only" clause
dc3c8a
dc3c8a
When caching additional and glue data *not* from a forwarder, we must
dc3c8a
check that there is no "forward only" clause covering the owner name
dc3c8a
that would take precedence.  Such names would normally be allowed by
dc3c8a
baliwick rules, but a "forward only" zone introduces a new baliwick
dc3c8a
scope.
dc3c8a
dc3c8a
(cherry picked from commit ea06552a3d1fed56f7d3a13710e084ec79797b78)
dc3c8a
dc3c8a
Look for zones deeper than the current domain or forward name
dc3c8a
dc3c8a
When caching glue, we need to ensure that there is no closer
dc3c8a
source of truth for the name. If the owner name for the glue
dc3c8a
record would be answered by a locally configured zone, do not
dc3c8a
cache.
dc3c8a
dc3c8a
(cherry picked from commit 71b24210542730355149130770deea3e58d8527a)
dc3c8a
---
dc3c8a
 lib/dns/resolver.c | 128 +++++++++++++++++++++++++++++++++++++++++++--
dc3c8a
 1 file changed, 123 insertions(+), 5 deletions(-)
dc3c8a
dc3c8a
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
dc3c8a
index a7bc661bb7..7603a07b7b 100644
dc3c8a
--- a/lib/dns/resolver.c
dc3c8a
+++ b/lib/dns/resolver.c
dc3c8a
@@ -63,6 +63,8 @@
dc3c8a
 #include <dns/stats.h>
dc3c8a
 #include <dns/tsig.h>
dc3c8a
 #include <dns/validator.h>
dc3c8a
+#include <dns/zone.h>
dc3c8a
+
dc3c8a
 #ifdef WANT_QUERYTRACE
dc3c8a
 #define RTRACE(m)                                                             \
dc3c8a
 	isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,                     \
dc3c8a
@@ -337,6 +339,8 @@ struct fetchctx {
dc3c8a
 	dns_fetch_t *qminfetch;
dc3c8a
 	dns_rdataset_t qminrrset;
dc3c8a
 	dns_name_t qmindcname;
dc3c8a
+	dns_fixedname_t fwdfname;
dc3c8a
+	dns_name_t *fwdname;
dc3c8a
 
dc3c8a
 	/*%
dc3c8a
 	 * The number of events we're waiting for.
dc3c8a
@@ -3764,6 +3768,7 @@ fctx_getaddresses(fetchctx_t *fctx, bool badcache) {
dc3c8a
 		if (result == ISC_R_SUCCESS) {
dc3c8a
 			fwd = ISC_LIST_HEAD(forwarders->fwdrs);
dc3c8a
 			fctx->fwdpolicy = forwarders->fwdpolicy;
dc3c8a
+			dns_name_copynf(domain, fctx->fwdname);
dc3c8a
 			if (fctx->fwdpolicy == dns_fwdpolicy_only &&
dc3c8a
 			    isstrictsubdomain(domain, &fctx->domain))
dc3c8a
 			{
dc3c8a
@@ -5153,6 +5158,9 @@ fctx_create(dns_resolver_t *res, const dns_name_t *name, dns_rdatatype_t type,
dc3c8a
 	fctx->restarts = 0;
dc3c8a
 	fctx->querysent = 0;
dc3c8a
 	fctx->referrals = 0;
dc3c8a
+
dc3c8a
+	fctx->fwdname = dns_fixedname_initname(&fctx->fwdfname);
dc3c8a
+
dc3c8a
 	TIME_NOW(&fctx->start);
dc3c8a
 	fctx->timeouts = 0;
dc3c8a
 	fctx->lamecount = 0;
dc3c8a
@@ -5215,6 +5223,7 @@ fctx_create(dns_resolver_t *res, const dns_name_t *name, dns_rdatatype_t type,
dc3c8a
 					   fname, &forwarders);
dc3c8a
 		if (result == ISC_R_SUCCESS) {
dc3c8a
 			fctx->fwdpolicy = forwarders->fwdpolicy;
dc3c8a
+			dns_name_copynf(fname, fctx->fwdname);
dc3c8a
 		}
dc3c8a
 
dc3c8a
 		if (fctx->fwdpolicy != dns_fwdpolicy_only) {
dc3c8a
@@ -7118,6 +7127,107 @@ mark_related(dns_name_t *name, dns_rdataset_t *rdataset, bool external,
dc3c8a
 	}
dc3c8a
 }
dc3c8a
 
dc3c8a
+/*
dc3c8a
+ * Returns true if 'name' is external to the namespace for which
dc3c8a
+ * the server being queried can answer, either because it's not a
dc3c8a
+ * subdomain or because it's below a forward declaration or a
dc3c8a
+ * locally served zone.
dc3c8a
+ */
dc3c8a
+static inline bool
dc3c8a
+name_external(const dns_name_t *name, dns_rdatatype_t type, fetchctx_t *fctx) {
dc3c8a
+	isc_result_t result;
dc3c8a
+	dns_forwarders_t *forwarders = NULL;
dc3c8a
+	dns_fixedname_t fixed, zfixed;
dc3c8a
+	dns_name_t *fname = dns_fixedname_initname(&fixed);
dc3c8a
+	dns_name_t *zfname = dns_fixedname_initname(&zfixed);
dc3c8a
+	dns_name_t *apex = NULL;
dc3c8a
+	dns_name_t suffix;
dc3c8a
+	dns_zone_t *zone = NULL;
dc3c8a
+	unsigned int labels;
dc3c8a
+	dns_namereln_t rel;
dc3c8a
+
dc3c8a
+	apex = ISFORWARDER(fctx->addrinfo) ? fctx->fwdname : &fctx->domain;
dc3c8a
+
dc3c8a
+	/*
dc3c8a
+	 * The name is outside the queried namespace.
dc3c8a
+	 */
dc3c8a
+	rel = dns_name_fullcompare(name, apex, &(int){ 0 },
dc3c8a
+				   &(unsigned int){ 0U });
dc3c8a
+	if (rel != dns_namereln_subdomain && rel != dns_namereln_equal) {
dc3c8a
+		return (true);
dc3c8a
+	}
dc3c8a
+
dc3c8a
+	/*
dc3c8a
+	 * If the record lives in the parent zone, adjust the name so we
dc3c8a
+	 * look for the correct zone or forward clause.
dc3c8a
+	 */
dc3c8a
+	labels = dns_name_countlabels(name);
dc3c8a
+	if (dns_rdatatype_atparent(type) && labels > 1U) {
dc3c8a
+		dns_name_init(&suffix, NULL);
dc3c8a
+		dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
dc3c8a
+		name = &suffix;
dc3c8a
+	} else if (rel == dns_namereln_equal) {
dc3c8a
+		/* If 'name' is 'apex', no further checking is needed. */
dc3c8a
+		return (false);
dc3c8a
+	}
dc3c8a
+
dc3c8a
+	/*
dc3c8a
+	 * If there is a locally served zone between 'apex' and 'name'
dc3c8a
+	 * then don't cache.
dc3c8a
+	 */
dc3c8a
+	LOCK(&fctx->res->view->lock);
dc3c8a
+	if (fctx->res->view->zonetable != NULL) {
dc3c8a
+		unsigned int options = DNS_ZTFIND_NOEXACT | DNS_ZTFIND_MIRROR;
dc3c8a
+		result = dns_zt_find(fctx->res->view->zonetable, name, options,
dc3c8a
+				     zfname, &zone);
dc3c8a
+		if (zone != NULL) {
dc3c8a
+			dns_zone_detach(&zone);
dc3c8a
+		}
dc3c8a
+		if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
dc3c8a
+			if (dns_name_fullcompare(zfname, apex, &(int){ 0 },
dc3c8a
+						 &(unsigned int){ 0U }) ==
dc3c8a
+			    dns_namereln_subdomain)
dc3c8a
+			{
dc3c8a
+				UNLOCK(&fctx->res->view->lock);
dc3c8a
+				return (true);
dc3c8a
+			}
dc3c8a
+		}
dc3c8a
+	}
dc3c8a
+	UNLOCK(&fctx->res->view->lock);
dc3c8a
+
dc3c8a
+	/*
dc3c8a
+	 * Look for a forward declaration below 'name'.
dc3c8a
+	 */
dc3c8a
+	result = dns_fwdtable_find(fctx->res->view->fwdtable, name, fname,
dc3c8a
+				   &forwarders);
dc3c8a
+
dc3c8a
+	if (ISFORWARDER(fctx->addrinfo)) {
dc3c8a
+		/*
dc3c8a
+		 * See if the forwarder declaration is better.
dc3c8a
+		 */
dc3c8a
+		if (result == ISC_R_SUCCESS) {
dc3c8a
+			return (!dns_name_equal(fname, fctx->fwdname));
dc3c8a
+		}
dc3c8a
+
dc3c8a
+		/*
dc3c8a
+		 * If the lookup failed, the configuration must have
dc3c8a
+		 * changed: play it safe and don't cache.
dc3c8a
+		 */
dc3c8a
+		return (true);
dc3c8a
+	} else if (result == ISC_R_SUCCESS &&
dc3c8a
+		   forwarders->fwdpolicy == dns_fwdpolicy_only &&
dc3c8a
+		   !ISC_LIST_EMPTY(forwarders->fwdrs))
dc3c8a
+	{
dc3c8a
+		/*
dc3c8a
+		 * If 'name' is covered by a 'forward only' clause then we
dc3c8a
+		 * can't cache this repsonse.
dc3c8a
+		 */
dc3c8a
+		return (true);
dc3c8a
+	}
dc3c8a
+
dc3c8a
+	return (false);
dc3c8a
+}
dc3c8a
+
dc3c8a
 static isc_result_t
dc3c8a
 check_section(void *arg, const dns_name_t *addname, dns_rdatatype_t type,
dc3c8a
 	      dns_section_t section) {
dc3c8a
@@ -7144,7 +7254,7 @@ check_section(void *arg, const dns_name_t *addname, dns_rdatatype_t type,
dc3c8a
 	result = dns_message_findname(rctx->query->rmessage, section, addname,
dc3c8a
 				      dns_rdatatype_any, 0, &name, NULL);
dc3c8a
 	if (result == ISC_R_SUCCESS) {
dc3c8a
-		external = !dns_name_issubdomain(name, &fctx->domain);
dc3c8a
+		external = name_external(name, type, fctx);
dc3c8a
 		if (type == dns_rdatatype_a) {
dc3c8a
 			for (rdataset = ISC_LIST_HEAD(name->list);
dc3c8a
 			     rdataset != NULL;
dc3c8a
@@ -8768,6 +8878,13 @@ rctx_answer_scan(respctx_t *rctx) {
dc3c8a
 			break;
dc3c8a
 
dc3c8a
 		case dns_namereln_subdomain:
dc3c8a
+			/*
dc3c8a
+			 * Don't accept DNAME from parent namespace.
dc3c8a
+			 */
dc3c8a
+			if (name_external(name, dns_rdatatype_dname, fctx)) {
dc3c8a
+				continue;
dc3c8a
+			}
dc3c8a
+
dc3c8a
 			/*
dc3c8a
 			 * In-scope DNAME records must have at least
dc3c8a
 			 * as many labels as the domain being queried.
dc3c8a
@@ -9081,13 +9198,11 @@ rctx_authority_positive(respctx_t *rctx) {
dc3c8a
 				       DNS_SECTION_AUTHORITY);
dc3c8a
 	while (!done && result == ISC_R_SUCCESS) {
dc3c8a
 		dns_name_t *name = NULL;
dc3c8a
-		bool external;
dc3c8a
 
dc3c8a
 		dns_message_currentname(rctx->query->rmessage,
dc3c8a
 					DNS_SECTION_AUTHORITY, &name);
dc3c8a
-		external = !dns_name_issubdomain(name, &fctx->domain);
dc3c8a
 
dc3c8a
-		if (!external) {
dc3c8a
+		if (!name_external(name, dns_rdatatype_ns, fctx)) {
dc3c8a
 			dns_rdataset_t *rdataset = NULL;
dc3c8a
 
dc3c8a
 			/*
dc3c8a
@@ -9474,7 +9589,10 @@ rctx_authority_dnssec(respctx_t *rctx) {
dc3c8a
 		}
dc3c8a
 
dc3c8a
 		if (!dns_name_issubdomain(name, &fctx->domain)) {
dc3c8a
-			/* Invalid name found; preserve it for logging later */
dc3c8a
+			/*
dc3c8a
+			 * Invalid name found; preserve it for logging
dc3c8a
+			 * later.
dc3c8a
+			 */
dc3c8a
 			rctx->found_name = name;
dc3c8a
 			rctx->found_type = ISC_LIST_HEAD(name->list)->type;
dc3c8a
 			continue;
dc3c8a
-- 
dc3c8a
2.34.1
dc3c8a