4ba560
From 18036bb3f435eaa20d60093738c61e5da42a6cfe Mon Sep 17 00:00:00 2001
4ba560
From: Evan Hunt <each@isc.org>
4ba560
Date: Thu, 1 Sep 2022 16:05:04 -0700
4ba560
Subject: [PATCH] add an update quota
4ba560
4ba560
limit the number of simultaneous DNS UPDATE events that can be
4ba560
processed by adding a quota for update and update forwarding.
4ba560
this quota currently, arbitrarily, defaults to 100.
4ba560
4ba560
also add a statistics counter to record when the update quota
4ba560
has been exceeded.
4ba560
4ba560
(cherry picked from commit 7c47254a140c3e9cf383cda73c7b6a55c4782826)
4ba560
---
4ba560
 bin/named/bind9.xsl        |  4 +++-
4ba560
 bin/named/bind9.xsl.h      |  6 +++++-
4ba560
 bin/named/statschannel.c   |  5 +++--
4ba560
 doc/arm/reference.rst      |  5 +++++
4ba560
 lib/ns/include/ns/server.h |  1 +
4ba560
 lib/ns/include/ns/stats.h  |  4 +++-
4ba560
 lib/ns/server.c            |  2 ++
4ba560
 lib/ns/update.c            | 37 ++++++++++++++++++++++++++++++++++++-
4ba560
 8 files changed, 58 insertions(+), 6 deletions(-)
4ba560
4ba560
diff --git a/bin/named/bind9.xsl b/bin/named/bind9.xsl
4ba560
index 5078115..194625b 100644
4ba560
--- a/bin/named/bind9.xsl
4ba560
+++ b/bin/named/bind9.xsl
4ba560
@@ -12,7 +12,9 @@
4ba560
 
4ba560
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
4ba560
   <xsl:output method="html" indent="yes" version="4.0"/>
4ba560
-  <xsl:template match="statistics[@version="3.11"]">
4ba560
+  
4ba560
+  
4ba560
+  <xsl:template match="statistics[@version="3.11.1"]">
4ba560
     <html>
4ba560
       <head>
4ba560
         <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
4ba560
diff --git a/bin/named/bind9.xsl.h b/bin/named/bind9.xsl.h
4ba560
index e30f7f5..b182742 100644
4ba560
--- a/bin/named/bind9.xsl.h
4ba560
+++ b/bin/named/bind9.xsl.h
4ba560
@@ -20,7 +20,11 @@ static char xslmsg[] =
4ba560
 	"
4ba560
 	"xmlns=\"http://www.w3.org/1999/xhtml\" version=\"1.0\">\n"
4ba560
 	" <xsl:output method=\"html\" indent=\"yes\" version=\"4.0\"/>\n"
4ba560
-	" <xsl:template match=\"statistics[@version="3.11"]\">\n"
4ba560
+	" 
4ba560
+	"bin/named/statschannel.c -->\n"
4ba560
+	" 
4ba560
+	"the HTTP endpoints listed below -->\n"
4ba560
+	" <xsl:template match=\"statistics[@version="3.11.1"]\">\n"
4ba560
 	" <html>\n"
4ba560
 	" <head>\n"
4ba560
 	" 
4ba560
diff --git a/bin/named/statschannel.c b/bin/named/statschannel.c
4ba560
index 832ce93..7361ead 100644
4ba560
--- a/bin/named/statschannel.c
4ba560
+++ b/bin/named/statschannel.c
4ba560
@@ -335,6 +335,7 @@ init_desc(void) {
4ba560
 	SET_NSSTATDESC(reclimitdropped,
4ba560
 		       "queries dropped due to recursive client limit",
4ba560
 		       "RecLimitDropped");
4ba560
+	SET_NSSTATDESC(updatequota, "Update quota exceeded", "UpdateQuota");
4ba560
 
4ba560
 	INSIST(i == ns_statscounter_max);
4ba560
 
4ba560
@@ -2007,7 +2008,7 @@ generatexml(named_server_t *server, uint32_t flags, int *buflen,
4ba560
 					      "href=\"/bind9.xsl\""));
4ba560
 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics"));
4ba560
 	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
4ba560
-					 ISC_XMLCHAR "3.11"));
4ba560
+					 ISC_XMLCHAR "3.11.1"));
4ba560
 
4ba560
 	/* Set common fields for statistics dump */
4ba560
 	dumparg.type = isc_statsformat_xml;
4ba560
@@ -2876,7 +2877,7 @@ generatejson(named_server_t *server, size_t *msglen, const char **msg,
4ba560
 	/*
4ba560
 	 * These statistics are included no matter which URL we use.
4ba560
 	 */
4ba560
-	obj = json_object_new_string("1.5");
4ba560
+	obj = json_object_new_string("1.5.1");
4ba560
 	CHECKMEM(obj);
4ba560
 	json_object_object_add(bindstats, "json-stats-version", obj);
4ba560
 
4ba560
diff --git a/doc/arm/reference.rst b/doc/arm/reference.rst
4ba560
index 2d05aec..25c20d7 100644
4ba560
--- a/doc/arm/reference.rst
4ba560
+++ b/doc/arm/reference.rst
4ba560
@@ -6705,6 +6705,11 @@ Name Server Statistics Counters
4ba560
 ``UpdateBadPrereq``
4ba560
     This indicates the number of dynamic updates rejected due to a prerequisite failure.
4ba560
 
4ba560
+``UpdateQuota``
4ba560
+    This indicates the number of times a dynamic update or update
4ba560
+    forwarding request was rejected because the number of pending
4ba560
+    requests exceeded the update quota.
4ba560
+
4ba560
 ``RateDropped``
4ba560
     This indicates the number of responses dropped due to rate limits.
4ba560
 
4ba560
diff --git a/lib/ns/include/ns/server.h b/lib/ns/include/ns/server.h
4ba560
index 6a1f345..0abb579 100644
4ba560
--- a/lib/ns/include/ns/server.h
4ba560
+++ b/lib/ns/include/ns/server.h
4ba560
@@ -84,6 +84,7 @@ struct ns_server {
4ba560
 	isc_quota_t recursionquota;
4ba560
 	isc_quota_t tcpquota;
4ba560
 	isc_quota_t xfroutquota;
4ba560
+	isc_quota_t updquota;
4ba560
 
4ba560
 	/*% Test options and other configurables */
4ba560
 	uint32_t options;
4ba560
diff --git a/lib/ns/include/ns/stats.h b/lib/ns/include/ns/stats.h
4ba560
index 3c08799..95b15d0 100644
4ba560
--- a/lib/ns/include/ns/stats.h
4ba560
+++ b/lib/ns/include/ns/stats.h
4ba560
@@ -106,7 +106,9 @@ enum {
4ba560
 
4ba560
 	ns_statscounter_reclimitdropped = 66,
4ba560
 
4ba560
-	ns_statscounter_max = 67,
4ba560
+	ns_statscounter_updatequota = 67,
4ba560
+
4ba560
+	ns_statscounter_max = 68,
4ba560
 };
4ba560
 
4ba560
 void
4ba560
diff --git a/lib/ns/server.c b/lib/ns/server.c
4ba560
index a970a28..540bc2e 100644
4ba560
--- a/lib/ns/server.c
4ba560
+++ b/lib/ns/server.c
4ba560
@@ -52,6 +52,7 @@ ns_server_create(isc_mem_t *mctx, ns_matchview_t matchingview,
4ba560
 	isc_quota_init(&sctx->xfroutquota, 10);
4ba560
 	isc_quota_init(&sctx->tcpquota, 10);
4ba560
 	isc_quota_init(&sctx->recursionquota, 100);
4ba560
+	isc_quota_init(&sctx->updquota, 100);
4ba560
 
4ba560
 	CHECKFATAL(dns_tkeyctx_create(mctx, &sctx->tkeyctx));
4ba560
 
4ba560
@@ -131,6 +132,7 @@ ns_server_detach(ns_server_t **sctxp) {
4ba560
 			isc_mem_put(sctx->mctx, altsecret, sizeof(*altsecret));
4ba560
 		}
4ba560
 
4ba560
+		isc_quota_destroy(&sctx->updquota);
4ba560
 		isc_quota_destroy(&sctx->recursionquota);
4ba560
 		isc_quota_destroy(&sctx->tcpquota);
4ba560
 		isc_quota_destroy(&sctx->xfroutquota);
4ba560
diff --git a/lib/ns/update.c b/lib/ns/update.c
4ba560
index 546b70a..1871438 100644
4ba560
--- a/lib/ns/update.c
4ba560
+++ b/lib/ns/update.c
4ba560
@@ -1544,6 +1544,19 @@ send_update_event(ns_client_t *client, dns_zone_t *zone) {
4ba560
 	update_event_t *event = NULL;
4ba560
 	isc_task_t *zonetask = NULL;
4ba560
 
4ba560
+	result = isc_quota_attach(&client->manager->sctx->updquota,
4ba560
+				  &(isc_quota_t *){ NULL });
4ba560
+	if (result != ISC_R_SUCCESS) {
4ba560
+		update_log(client, zone, LOGLEVEL_PROTOCOL,
4ba560
+			   "update failed: too many DNS UPDATEs queued (%s)",
4ba560
+			   isc_result_totext(result));
4ba560
+		ns_stats_increment(client->manager->sctx->nsstats,
4ba560
+				   ns_statscounter_updatequota);
4ba560
+		ns_client_drop(client, result);
4ba560
+		isc_nmhandle_detach(&client->reqhandle);
4ba560
+		return (DNS_R_DROP);
4ba560
+	}
4ba560
+
4ba560
 	event = (update_event_t *)isc_event_allocate(
4ba560
 		client->mctx, client, DNS_EVENT_UPDATE, update_action, NULL,
4ba560
 		sizeof(*event));
4ba560
@@ -1676,12 +1689,18 @@ failure:
4ba560
 		       dns_zone_gettype(zone) == dns_zone_mirror);
4ba560
 		inc_stats(client, zone, ns_statscounter_updaterej);
4ba560
 	}
4ba560
+
4ba560
 	/*
4ba560
 	 * We failed without having sent an update event to the zone.
4ba560
 	 * We are still in the client task context, so we can
4ba560
 	 * simply give an error response without switching tasks.
4ba560
 	 */
4ba560
-	respond(client, result);
4ba560
+	if (result == DNS_R_DROP) {
4ba560
+		ns_client_drop(client, result);
4ba560
+	} else {
4ba560
+		respond(client, result);
4ba560
+	}
4ba560
+
4ba560
 	if (zone != NULL) {
4ba560
 		dns_zone_detach(&zone);
4ba560
 	}
4ba560
@@ -3489,6 +3508,7 @@ updatedone_action(isc_task_t *task, isc_event_t *event) {
4ba560
 
4ba560
 	respond(client, uev->result);
4ba560
 
4ba560
+	isc_quota_detach(&(isc_quota_t *){ &client->manager->sctx->updquota });
4ba560
 	isc_event_free(&event);
4ba560
 	isc_nmhandle_detach(&client->updatehandle);
4ba560
 }
4ba560
@@ -3505,6 +3525,8 @@ forward_fail(isc_task_t *task, isc_event_t *event) {
4ba560
 	INSIST(client->nupdates > 0);
4ba560
 	client->nupdates--;
4ba560
 	respond(client, DNS_R_SERVFAIL);
4ba560
+
4ba560
+	isc_quota_detach(&(isc_quota_t *){ &client->manager->sctx->updquota });
4ba560
 	isc_event_free(&event);
4ba560
 	isc_nmhandle_detach(&client->updatehandle);
4ba560
 }
4ba560
@@ -3542,6 +3564,8 @@ forward_done(isc_task_t *task, isc_event_t *event) {
4ba560
 	client->nupdates--;
4ba560
 	ns_client_sendraw(client, uev->answer);
4ba560
 	dns_message_detach(&uev->answer);
4ba560
+
4ba560
+	isc_quota_detach(&(isc_quota_t *){ &client->manager->sctx->updquota });
4ba560
 	isc_event_free(&event);
4ba560
 	isc_nmhandle_detach(&client->updatehandle);
4ba560
 }
4ba560
@@ -3576,6 +3600,17 @@ send_forward_event(ns_client_t *client, dns_zone_t *zone) {
4ba560
 	update_event_t *event = NULL;
4ba560
 	isc_task_t *zonetask = NULL;
4ba560
 
4ba560
+	result = isc_quota_attach(&client->manager->sctx->updquota,
4ba560
+				  &(isc_quota_t *){ NULL });
4ba560
+	if (result != ISC_R_SUCCESS) {
4ba560
+		update_log(client, zone, LOGLEVEL_PROTOCOL,
4ba560
+			   "update failed: too many DNS UPDATEs queued (%s)",
4ba560
+			   isc_result_totext(result));
4ba560
+		ns_stats_increment(client->manager->sctx->nsstats,
4ba560
+				   ns_statscounter_updatequota);
4ba560
+		return (DNS_R_DROP);
4ba560
+	}
4ba560
+
4ba560
 	event = (update_event_t *)isc_event_allocate(
4ba560
 		client->mctx, client, DNS_EVENT_UPDATE, forward_action, NULL,
4ba560
 		sizeof(*event));
4ba560
-- 
4ba560
2.39.2
4ba560