81c2ba
From 05cdbc1006cee6daaa29e5423976d56047d22461 Mon Sep 17 00:00:00 2001
81c2ba
From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= <michal@isc.org>
81c2ba
Date: Thu, 8 Sep 2022 11:11:30 +0200
81c2ba
Subject: [PATCH] Bound the amount of work performed for delegations
81c2ba
81c2ba
Limit the amount of database lookups that can be triggered in
81c2ba
fctx_getaddresses() (i.e. when determining the name server addresses to
81c2ba
query next) by setting a hard limit on the number of NS RRs processed
81c2ba
for any delegation encountered.  Without any limit in place, named can
81c2ba
be forced to perform large amounts of database lookups per each query
81c2ba
received, which severely impacts resolver performance.
81c2ba
81c2ba
The limit used (20) is an arbitrary value that is considered to be big
81c2ba
enough for any sane DNS delegation.
81c2ba
81c2ba
(cherry picked from commit 3a44097fd6c6c260765b628cd1d2c9cb7efb0b2a)
81c2ba
(cherry picked from commit bf2ea6d8525bfd96a84dad221ba9e004adb710a8)
81c2ba
---
81c2ba
 lib/dns/resolver.c | 12 ++++++++++++
81c2ba
 1 file changed, 12 insertions(+)
81c2ba
81c2ba
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
81c2ba
index 8ae9a993bb..ac9a9ef5d0 100644
81c2ba
--- a/lib/dns/resolver.c
81c2ba
+++ b/lib/dns/resolver.c
81c2ba
@@ -180,6 +180,12 @@
81c2ba
  */
81c2ba
 #define NS_FAIL_LIMIT 4
81c2ba
 #define NS_RR_LIMIT   5
81c2ba
+/*
81c2ba
+ * IP address lookups are performed for at most NS_PROCESSING_LIMIT NS RRs in
81c2ba
+ * any NS RRset encountered, to avoid excessive resource use while processing
81c2ba
+ * large delegations.
81c2ba
+ */
81c2ba
+#define NS_PROCESSING_LIMIT 20
81c2ba
 
81c2ba
 /* Number of hash buckets for zone counters */
81c2ba
 #ifndef RES_DOMAIN_BUCKETS
81c2ba
@@ -3318,6 +3324,7 @@ fctx_getaddresses(fetchctx_t *fctx, bool badcache) {
81c2ba
 	bool need_alternate = false;
81c2ba
 	bool all_spilled = true;
81c2ba
 	unsigned int no_addresses = 0;
81c2ba
+	unsigned int ns_processed = 0;
81c2ba
 
81c2ba
 	FCTXTRACE5("getaddresses", "fctx->depth=", fctx->depth);
81c2ba
 
81c2ba
@@ -3504,6 +3511,11 @@ fctx_getaddresses(fetchctx_t *fctx, bool badcache) {
81c2ba
 
81c2ba
 		dns_rdata_reset(&rdata);
81c2ba
 		dns_rdata_freestruct(&ns);
81c2ba
+
81c2ba
+		if (++ns_processed >= NS_PROCESSING_LIMIT) {
81c2ba
+			result = ISC_R_NOMORE;
81c2ba
+			break;
81c2ba
+		}
81c2ba
 	}
81c2ba
 	if (result != ISC_R_NOMORE) {
81c2ba
 		return (result);
81c2ba
-- 
81c2ba
2.37.3
81c2ba