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