From 17924f4bdfbd99e06057c090d6ac3e8074deb642 Mon Sep 17 00:00:00 2001
From: Evan Hunt <each@isc.org>
Date: Fri, 2 Sep 2022 14:44:58 -0700
Subject: [PATCH] fix an incorrect detach in update processing

when processing UDPATE requests, hold the request handle until
we either drop the request or respond to it.

Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/17924f4bdfbd99e06057c090d6ac3e8074deb642
(cherry picked from commit 00e0758e1218f82fd1fe995c161ce4243bbbbb89)
---
 lib/ns/update.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/lib/ns/update.c b/lib/ns/update.c
index c4bde3d4eb..a21b725f39 100644
--- a/lib/ns/update.c
+++ b/lib/ns/update.c
@@ -1569,19 +1569,17 @@ respond(ns_client_t *client, isc_result_t result) {
 
 	msg_result = dns_message_reply(client->message, true);
 	if (msg_result != ISC_R_SUCCESS) {
-		goto msg_failure;
+		isc_log_write(ns_lctx, NS_LOGCATEGORY_UPDATE,
+			      NS_LOGMODULE_UPDATE, ISC_LOG_ERROR,
+			      "could not create update response message: %s",
+			      isc_result_totext(msg_result));
+		ns_client_drop(client, msg_result);
+		isc_nmhandle_detach(&client->reqhandle);
+		return;
 	}
-	client->message->rcode = dns_result_torcode(result);
 
+	client->message->rcode = dns_result_torcode(result);
 	ns_client_send(client);
-	return;
-
-msg_failure:
-	isc_log_write(ns_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
-		      ISC_LOG_ERROR,
-		      "could not create update response message: %s",
-		      isc_result_totext(msg_result));
-	ns_client_drop(client, msg_result);
 	isc_nmhandle_detach(&client->reqhandle);
 }
 
@@ -1595,7 +1593,8 @@ ns_update_start(ns_client_t *client, isc_nmhandle_t *handle,
 	dns_zone_t *zone = NULL, *raw = NULL;
 
 	/*
-	 * Attach to the request handle
+	 * Attach to the request handle. This will be held until
+	 * we respond, or drop the request.
 	 */
 	isc_nmhandle_attach(handle, &client->reqhandle);
 
@@ -1677,8 +1676,6 @@ ns_update_start(ns_client_t *client, isc_nmhandle_t *handle,
 	default:
 		FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
 	}
-
-	isc_nmhandle_detach(&client->reqhandle);
 	return;
 
 failure:
@@ -1696,7 +1693,6 @@ failure:
 	if (zone != NULL) {
 		dns_zone_detach(&zone);
 	}
-	isc_nmhandle_detach(&client->reqhandle);
 }
 
 /*%
@@ -3554,6 +3550,7 @@ forward_done(isc_task_t *task, isc_event_t *event) {
 	ns_client_sendraw(client, uev->answer);
 	dns_message_detach(&uev->answer);
 	isc_event_free(&event);
+	isc_nmhandle_detach(&client->reqhandle);
 	isc_nmhandle_detach(&client->updatehandle);
 }
 
-- 
2.23.0