From d8fa989c2934398c9cd665aea1a6d34616dfcb1a Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Thu, 20 Aug 2020 10:55:58 +1000
Subject: [PATCH] Lock access to flags in dns__zone_loadpending

==================
WARNING: ThreadSanitizer: data race (pid=1938)
  Read of size 4 at 0x7b7800000dd4 by main thread:
    #0 dns__zone_loadpending /builds/isc-projects/bind9/lib/dns/zone.c:2308:10 (libdns.so.1110+0x1da342)
    #1 asyncload_zone /builds/isc-projects/bind9/lib/dns/tests/zt_test.c:204:9 (zt_test+0x4b8303)
    #2 <null> <null> (libcmocka.so.0+0x50d8)
    #3 __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:308:16 (libc.so.6+0x2409a)

  Previous write of size 4 at 0x7b7800000dd4 by thread T16 (mutexes: write M2181):
    #0 zone_asyncload /builds/isc-projects/bind9/lib/dns/zone.c:2237:3 (libdns.so.1110+0x1da08d)
    #1 dispatch /builds/isc-projects/bind9/lib/isc/task.c:1157:7 (libisc.so.1107+0x50845)
    #2 run /builds/isc-projects/bind9/lib/isc/task.c:1331:2 (libisc.so.1107+0x4d799)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/d8fa989c2934398c9cd665aea1a6d34616dfcb1a
---
 lib/dns/zone.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/dns/zone.c b/lib/dns/zone.c
index c8e880f4c1..97e24c7e7f 100644
--- a/lib/dns/zone.c
+++ b/lib/dns/zone.c
@@ -2303,9 +2303,14 @@ dns_zone_asyncload2(dns_zone_t *zone, dns_zt_zoneloaded_t done, void * arg,
 
 bool
 dns__zone_loadpending(dns_zone_t *zone) {
+	bool result;
+
 	REQUIRE(DNS_ZONE_VALID(zone));
 
-	return (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING));
+	LOCK_ZONE(zone);
+	result = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING) != 0;
+	UNLOCK_ZONE(zone);
+	return (result);
 }
 
 isc_result_t
-- 
2.23.0

