From 0f7f6201e31a9da68d57290b5f4ed31e8248b972 Mon Sep 17 00:00:00 2001
From: Evan Hunt <each@isc.org>
Date: Sat, 11 Jul 2020 21:30:53 -0700
Subject: [PATCH] make sure new_zone_lock is locked before unlocking it

it was possible for the count_newzones() function to try to
unlock view->new_zone_lock on return before locking it, which
caused a crash on shutdown.

(cherry picked from commit ed37c63e2bbc4afe299dbe38ae98871dcc1d3470)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/0f7f6201e31a9da68d57290b5f4ed31e8248b972
---
 bin/named/server.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/bin/named/server.c b/bin/named/server.c
index 4df714e527..e752aa71a9 100644
--- a/bin/named/server.c
+++ b/bin/named/server.c
@@ -6789,6 +6789,8 @@ count_newzones(dns_view_t *view, ns_cfgctx_t *nzcfg, int *num_zonesp) {
 
 	REQUIRE(num_zonesp != NULL);
 
+	LOCK(&view->new_zone_lock);
+
 	CHECK(migrate_nzf(view));
 
 	isc_log_write(ns_g_lctx,
@@ -6797,8 +6799,6 @@ count_newzones(dns_view_t *view, ns_cfgctx_t *nzcfg, int *num_zonesp) {
 		      "for view '%s'",
 		      view->new_zone_db, view->name);
 
-	LOCK(&view->new_zone_lock);
-
 	CHECK(nzd_count(view, &n));
 
 	*num_zonesp = n;
@@ -12006,6 +12006,10 @@ nzd_count(dns_view_t *view, int *countp) {
 	return (result);
 }
 
+/*
+ * Migrate zone configuration from an NZF file to an NZD database.
+ * Caller must hold view->new_zone_lock.
+ */
 static isc_result_t
 migrate_nzf(dns_view_t *view) {
 	isc_result_t result;
@@ -12021,8 +12025,6 @@ migrate_nzf(dns_view_t *view) {
 	MDB_val key, data;
 	ns_dzarg_t dzarg;
 
-	LOCK(&view->new_zone_lock);
-
 	/*
 	 * If NZF file doesn't exist, or NZD DB exists and already
 	 * has data, return without attempting migration.
@@ -12166,8 +12168,6 @@ migrate_nzf(dns_view_t *view) {
 		result = nzd_close(&txn, commit);
 	}
 
-	UNLOCK(&view->new_zone_lock);
-
 	if (text != NULL) {
 		isc_buffer_free(&text);
 	}
-- 
2.23.0

