From 80a8322d6594cfaa9ffe90d3de0c315a0d34efc3 Mon Sep 17 00:00:00 2001
From: Evan Hunt <each@isc.org>
Date: Fri, 2 Sep 2022 15:41:26 -0700
Subject: [PATCH] clean up properly when interface creation fails

Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/80a8322d6594cfaa9ffe90d3de0c315a0d34efc3

previously, if ns_clientmgr_create() failed, the interface was not
cleaned up correctly and an assertion or segmentation fault could
follow. this has been fixed.
---
 lib/ns/interfacemgr.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c
index facb0d141b..51429de716 100644
--- a/lib/ns/interfacemgr.c
+++ b/lib/ns/interfacemgr.c
@@ -391,7 +391,7 @@ ns_interfacemgr_shutdown(ns_interfacemgr_t *mgr) {
 static isc_result_t
 ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
 		    const char *name, ns_interface_t **ifpret) {
-	ns_interface_t *ifp;
+	ns_interface_t *ifp = NULL;
 	isc_result_t result;
 	int disp;
 
@@ -422,13 +422,13 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
 	ISC_LINK_INIT(ifp, link);
 
 	ns_interfacemgr_attach(mgr, &ifp->mgr);
+	isc_refcount_init(&ifp->references, 1);
+	ifp->magic = IFACE_MAGIC;
+
 	LOCK(&mgr->lock);
 	ISC_LIST_APPEND(mgr->interfaces, ifp, link);
 	UNLOCK(&mgr->lock);
 
-	isc_refcount_init(&ifp->references, 1);
-	ifp->magic = IFACE_MAGIC;
-
 	result = ns_clientmgr_create(mgr->mctx, mgr->sctx, mgr->taskmgr,
 				     mgr->timermgr, ifp, mgr->ncpus,
 				     &ifp->clientmgr);
@@ -444,11 +444,17 @@ ns_interface_create(ns_interfacemgr_t *mgr, isc_sockaddr_t *addr,
 	return (ISC_R_SUCCESS);
 
 failure:
-	isc_mutex_destroy(&ifp->lock);
+	LOCK(&ifp->mgr->lock);
+	ISC_LIST_UNLINK(ifp->mgr->interfaces, ifp, link);
+	UNLOCK(&ifp->mgr->lock);
 
 	ifp->magic = 0;
-	isc_mem_put(mgr->mctx, ifp, sizeof(*ifp));
+	ns_interfacemgr_detach(&ifp->mgr);
+	isc_refcount_decrement(&ifp->references);
+	isc_refcount_destroy(&ifp->references);
+	isc_mutex_destroy(&ifp->lock);
 
+	isc_mem_put(mgr->mctx, ifp, sizeof(*ifp));
 	return (ISC_R_UNEXPECTED);
 }
 
-- 
2.23.0