From 042e1e3a44fbe087c41810ee8ee74d7c5cd973d6 Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Fri, 16 Oct 2020 10:42:52 +1100
Subject: [PATCH] Lock check of DNS_ZONEFLG_EXITING flag

    WARNING: ThreadSanitizer: data race
    Read of size 4 at 0x000000000001 by thread T1 (mutexes: write M1):
    #0 dns_zone_refresh lib/dns/zone.c:10447
    #1 dns_zone_notifyreceive2 lib/dns/zone.c:13646
    #2 dns_zone_notifyreceive2 lib/dns/zone.c:13497
    #3 ns_notify_start bin/named/notify.c:150
    #4 client_request bin/named/client.c:3137
    #5 dispatch lib/isc/task.c:1157
    #6 run lib/isc/task.c:1331
    #7 <null> <null>

    Previous write of size 4 at 0x000000000001 by thread T2 (mutexes: write M2):
    #0 dns_zone_refresh lib/dns/zone.c:10466
    #1 zone_maintenance lib/dns/zone.c:10236
    #2 zone_timer lib/dns/zone.c:13136
    #3 dispatch lib/isc/task.c:1157
    #4 run lib/isc/task.c:1331
    #5 <null> <null>
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/042e1e3a44fbe087c41810ee8ee74d7c5cd973d6
---
 lib/dns/zone.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/dns/zone.c b/lib/dns/zone.c
index d3d9f32bcc..f09805f322 100644
--- a/lib/dns/zone.c
+++ b/lib/dns/zone.c
@@ -10462,15 +10462,17 @@ dns_zone_refresh(dns_zone_t *zone) {
 
 	REQUIRE(DNS_ZONE_VALID(zone));
 
-	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
+	LOCK_ZONE(zone);
+	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
+		UNLOCK_ZONE(zone);
 		return;
+	}
 
 	/*
 	 * Set DNS_ZONEFLG_REFRESH so that there is only one refresh operation
 	 * in progress at a time.
 	 */
 
-	LOCK_ZONE(zone);
 	oldflags = zone->flags;
 	if (zone->masterscnt == 0) {
 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOMASTERS);
-- 
2.23.0

