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