Blame SOURCES/unbound-1.16-CVE-2022-3204.patch

3619be
From 7af485f0fc9926425681ba0280ab6c2c8dd04530 Mon Sep 17 00:00:00 2001
3619be
From: "W.C.A. Wijngaards" <wouter@nlnetlabs.nl>
3619be
Date: Wed, 21 Sep 2022 11:10:38 +0200
3619be
Subject: [PATCH] - Patch for CVE-2022-3204 Non-Responsive Delegation Attack.
3619be
3619be
---
3619be
 unbound-1.16.2/iterator/iter_delegpt.c |  3 +++
3619be
 unbound-1.16.2/iterator/iter_delegpt.h |  2 ++
3619be
 unbound-1.16.2/iterator/iter_utils.c   |  3 +++
3619be
 unbound-1.16.2/iterator/iter_utils.h   |  9 +++++++
3619be
 unbound-1.16.2/iterator/iterator.c     | 36 +++++++++++++++++++++++++-
3619be
 unbound-1.16.2/services/cache/dns.c    |  3 +++
3619be
 unbound-1.16.2/services/mesh.c         |  7 +++++
3619be
 unbound-1.16.2/services/mesh.h         | 11 ++++++++
3619be
 8 files changed, 73 insertions(+), 1 deletion(-)
3619be
3619be
diff --git a/unbound-1.16.2/iterator/iter_delegpt.c b/unbound-1.16.2/iterator/iter_delegpt.c
3619be
index 4bffa1b..fd07aaa 100644
3619be
--- a/unbound-1.16.2/iterator/iter_delegpt.c
3619be
+++ b/unbound-1.16.2/iterator/iter_delegpt.c
3619be
@@ -78,6 +78,7 @@ struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* region)
3619be
 		if(!delegpt_add_ns(copy, region, ns->name, ns->lame,
3619be
 			ns->tls_auth_name, ns->port))
3619be
 			return NULL;
3619be
+		copy->nslist->cache_lookup_count = ns->cache_lookup_count;
3619be
 		copy->nslist->resolved = ns->resolved;
3619be
 		copy->nslist->got4 = ns->got4;
3619be
 		copy->nslist->got6 = ns->got6;
3619be
@@ -121,6 +122,7 @@ delegpt_add_ns(struct delegpt* dp, struct regional* region, uint8_t* name,
3619be
 	ns->namelen = len;
3619be
 	dp->nslist = ns;
3619be
 	ns->name = regional_alloc_init(region, name, ns->namelen);
3619be
+	ns->cache_lookup_count = 0;
3619be
 	ns->resolved = 0;
3619be
 	ns->got4 = 0;
3619be
 	ns->got6 = 0;
3619be
@@ -620,6 +622,7 @@ int delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame,
3619be
 	}
3619be
 	ns->next = dp->nslist;
3619be
 	dp->nslist = ns;
3619be
+	ns->cache_lookup_count = 0;
3619be
 	ns->resolved = 0;
3619be
 	ns->got4 = 0;
3619be
 	ns->got6 = 0;
3619be
diff --git a/unbound-1.16.2/iterator/iter_delegpt.h b/unbound-1.16.2/iterator/iter_delegpt.h
3619be
index 62c8edc..586597a 100644
3619be
--- a/unbound-1.16.2/iterator/iter_delegpt.h
3619be
+++ b/unbound-1.16.2/iterator/iter_delegpt.h
3619be
@@ -101,6 +101,8 @@ struct delegpt_ns {
3619be
 	uint8_t* name;
3619be
 	/** length of name */
3619be
 	size_t namelen;
3619be
+	/** number of cache lookups for the name */
3619be
+	int cache_lookup_count;
3619be
 	/** 
3619be
 	 * If the name has been resolved. false if not queried for yet.
3619be
 	 * true if the A, AAAA queries have been generated.
3619be
diff --git a/unbound-1.16.2/iterator/iter_utils.c b/unbound-1.16.2/iterator/iter_utils.c
3619be
index 3e13e59..56b184a 100644
3619be
--- a/unbound-1.16.2/iterator/iter_utils.c
3619be
+++ b/unbound-1.16.2/iterator/iter_utils.c
3619be
@@ -1209,6 +1209,9 @@ int iter_lookup_parent_glue_from_cache(struct module_env* env,
3619be
 	struct delegpt_ns* ns;
3619be
 	size_t num = delegpt_count_targets(dp);
3619be
 	for(ns = dp->nslist; ns; ns = ns->next) {
3619be
+		if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE)
3619be
+			continue;
3619be
+		ns->cache_lookup_count++;
3619be
 		/* get cached parentside A */
3619be
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
3619be
 			ns->namelen, LDNS_RR_TYPE_A, qinfo->qclass,
3619be
diff --git a/unbound-1.16.2/iterator/iter_utils.h b/unbound-1.16.2/iterator/iter_utils.h
3619be
index 8583fde..850be96 100644
3619be
--- a/unbound-1.16.2/iterator/iter_utils.h
3619be
+++ b/unbound-1.16.2/iterator/iter_utils.h
3619be
@@ -62,6 +62,15 @@ struct ub_packed_rrset_key;
3619be
 struct module_stack;
3619be
 struct outside_network;
3619be
 
3619be
+/* max number of lookups in the cache for target nameserver names.
3619be
+ * This stops, for large delegations, N*N lookups in the cache. */
3619be
+#define ITERATOR_NAME_CACHELOOKUP_MAX	3
3619be
+/* max number of lookups in the cache for parentside glue for nameserver names
3619be
+ * This stops, for larger delegations, N*N lookups in the cache.
3619be
+ * It is a little larger than the nonpside max, so it allows a couple extra
3619be
+ * lookups of parent side glue. */
3619be
+#define ITERATOR_NAME_CACHELOOKUP_MAX_PSIDE	5
3619be
+
3619be
 /**
3619be
  * Process config options and set iterator module state.
3619be
  * Sets default values if no config is found.
3619be
diff --git a/unbound-1.16.2/iterator/iterator.c b/unbound-1.16.2/iterator/iterator.c
3619be
index 25e5cfe..da9b799 100644
3619be
--- a/unbound-1.16.2/iterator/iterator.c
3619be
+++ b/unbound-1.16.2/iterator/iterator.c
3619be
@@ -1218,6 +1218,15 @@ generate_dnskey_prefetch(struct module_qstate* qstate,
3619be
 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
3619be
 		return;
3619be
 	}
3619be
+	/* we do not generate this prefetch when the query list is full,
3619be
+	 * the query is fetched, if needed, when the validator wants it.
3619be
+	 * At that time the validator waits for it, after spawning it.
3619be
+	 * This means there is one state that uses cpu and a socket, the
3619be
+	 * spawned while this one waits, and not several at the same time,
3619be
+	 * if we had created the lookup here. And this helps to keep
3619be
+	 * the total load down, but the query still succeeds to resolve. */
3619be
+	if(mesh_jostle_exceeded(qstate->env->mesh))
3619be
+		return;
3619be
 
3619be
 	/* if the DNSKEY is in the cache this lookup will stop quickly */
3619be
 	log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch", 
3619be
@@ -1911,6 +1920,14 @@ query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
3619be
 				return 0;
3619be
 			}
3619be
 			query_count++;
3619be
+			/* If the mesh query list is full, exit the loop here.
3619be
+			 * This makes the routine spawn one query at a time,
3619be
+			 * and this means there is no query state load
3619be
+			 * increase, because the spawned state uses cpu and a
3619be
+			 * socket while this state waits for that spawned
3619be
+			 * state. Next time we can look up further targets */
3619be
+			if(mesh_jostle_exceeded(qstate->env->mesh))
3619be
+				break;
3619be
 		}
3619be
 		/* Send the A request. */
3619be
 		if(ie->supports_ipv4 &&
3619be
@@ -1925,6 +1942,9 @@ query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
3619be
 				return 0;
3619be
 			}
3619be
 			query_count++;
3619be
+			/* If the mesh query list is full, exit the loop. */
3619be
+			if(mesh_jostle_exceeded(qstate->env->mesh))
3619be
+				break;
3619be
 		}
3619be
 
3619be
 		/* mark this target as in progress. */
3619be
@@ -2085,6 +2105,15 @@ processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
3619be
 			}
3619be
 			ns->done_pside6 = 1;
3619be
 			query_count++;
3619be
+			if(mesh_jostle_exceeded(qstate->env->mesh)) {
3619be
+				/* Wait for the lookup; do not spawn multiple
3619be
+				 * lookups at a time. */
3619be
+				verbose(VERB_ALGO, "try parent-side glue lookup");
3619be
+				iq->num_target_queries += query_count;
3619be
+				target_count_increase(iq, query_count);
3619be
+				qstate->ext_state[id] = module_wait_subquery;
3619be
+				return 0;
3619be
+			}
3619be
 		}
3619be
 		if(ie->supports_ipv4 && !ns->done_pside4) {
3619be
 			/* Send the A request. */
3619be
@@ -2560,7 +2589,12 @@ processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
3619be
 	if(iq->depth < ie->max_dependency_depth
3619be
 		&& iq->num_target_queries == 0
3619be
 		&& (!iq->target_count || iq->target_count[TARGET_COUNT_NX]==0)
3619be
-		&& iq->sent_count < TARGET_FETCH_STOP) {
3619be
+		&& iq->sent_count < TARGET_FETCH_STOP
3619be
+		/* if the mesh query list is full, then do not waste cpu
3619be
+		 * and sockets to fetch promiscuous targets. They can be
3619be
+		 * looked up when needed. */
3619be
+		&& !mesh_jostle_exceeded(qstate->env->mesh)
3619be
+		) {
3619be
 		tf_policy = ie->target_fetch_policy[iq->depth];
3619be
 	}
3619be
 
3619be
diff --git a/unbound-1.16.2/services/cache/dns.c b/unbound-1.16.2/services/cache/dns.c
3619be
index 6bca8d8..b6e5697 100644
3619be
--- a/unbound-1.16.2/services/cache/dns.c
3619be
+++ b/unbound-1.16.2/services/cache/dns.c
3619be
@@ -404,6 +404,9 @@ cache_fill_missing(struct module_env* env, uint16_t qclass,
3619be
 	struct ub_packed_rrset_key* akey;
3619be
 	time_t now = *env->now;
3619be
 	for(ns = dp->nslist; ns; ns = ns->next) {
3619be
+		if(ns->cache_lookup_count > ITERATOR_NAME_CACHELOOKUP_MAX)
3619be
+			continue;
3619be
+		ns->cache_lookup_count++;
3619be
 		akey = rrset_cache_lookup(env->rrset_cache, ns->name, 
3619be
 			ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
3619be
 		if(akey) {
3619be
diff --git a/unbound-1.16.2/services/mesh.c b/unbound-1.16.2/services/mesh.c
3619be
index 30bcf7c..2a41194 100644
3619be
--- a/unbound-1.16.2/services/mesh.c
3619be
+++ b/unbound-1.16.2/services/mesh.c
3619be
@@ -2240,3 +2240,10 @@ mesh_serve_expired_callback(void* arg)
3619be
 		mesh_do_callback(mstate, LDNS_RCODE_NOERROR, msg->rep, c, &tv;;
3619be
 	}
3619be
 }
3619be
+
3619be
+int mesh_jostle_exceeded(struct mesh_area* mesh)
3619be
+{
3619be
+	if(mesh->all.count < mesh->max_reply_states)
3619be
+		return 0;
3619be
+	return 1;
3619be
+}
3619be
diff --git a/unbound-1.16.2/services/mesh.h b/unbound-1.16.2/services/mesh.h
3619be
index 3be9b63..25121a6 100644
3619be
--- a/unbound-1.16.2/services/mesh.h
3619be
+++ b/unbound-1.16.2/services/mesh.h
3619be
@@ -685,4 +685,15 @@ struct dns_msg*
3619be
 mesh_serve_expired_lookup(struct module_qstate* qstate,
3619be
 	struct query_info* lookup_qinfo);
3619be
 
3619be
+/**
3619be
+ * See if the mesh has space for more queries. You can allocate queries
3619be
+ * anyway, but this checks for the allocated space.
3619be
+ * @param mesh: mesh area.
3619be
+ * @return true if the query list is full.
3619be
+ * 	It checks the number of all queries, not just number of reply states,
3619be
+ * 	that have a client address. So that spawned queries count too,
3619be
+ * 	that were created by the iterator, or other modules.
3619be
+ */
3619be
+int mesh_jostle_exceeded(struct mesh_area* mesh);
3619be
+
3619be
 #endif /* SERVICES_MESH_H */
3619be
-- 
3619be
2.37.3
3619be