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

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