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

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