From 15ae4585d278d8a137d038636f39acc62ab67743 Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Thu, 27 Aug 2020 11:18:50 +1000
Subject: [PATCH] `counter->used` was read without the lock being held.

WARNING: ThreadSanitizer: data race (pid=11785)
  Write of size 4 at 0x7b180001ba10 by thread T12 (mutexes: write M835834548863482336):
    #0 isc_counter_increment /builds/isc-projects/bind9/lib/isc/counter.c:70:15 (libisc.so.1107+0x1dcb6)
    #1 fctx_try /builds/isc-projects/bind9/lib/dns/resolver.c:3851:11 (libdns.so.1110+0x17e312)
    #2 resume_dslookup /builds/isc-projects/bind9/lib/dns/resolver.c:7505:3 (libdns.so.1110+0x18ccf0)
    #3 dispatch /builds/isc-projects/bind9/lib/isc/task.c:1157:7 (libisc.so.1107+0x507f5)
    #4 run /builds/isc-projects/bind9/lib/isc/task.c:1331:2 (libisc.so.1107+0x4d749)

  Previous read of size 4 at 0x7b180001ba10 by thread T7:
    #0 isc_counter_used /builds/isc-projects/bind9/lib/isc/counter.c:82:19 (libisc.so.1107+0x1dd5f)
    #1 fctx_try /builds/isc-projects/bind9/lib/dns/resolver.c:3798:6 (libdns.so.1110+0x17e0d1)
    #2 fctx_start /builds/isc-projects/bind9/lib/dns/resolver.c:4219:4 (libdns.so.1110+0x178833)
    #3 dispatch /builds/isc-projects/bind9/lib/isc/task.c:1157:7 (libisc.so.1107+0x507f5)
    #4 run /builds/isc-projects/bind9/lib/isc/task.c:1331:2 (libisc.so.1107+0x4d749)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/15ae4585d278d8a137d038636f39acc62ab67743
---
 lib/isc/counter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/isc/counter.c b/lib/isc/counter.c
index 8c70051837..e7cba062f2 100644
--- a/lib/isc/counter.c
+++ b/lib/isc/counter.c
@@ -77,9 +77,15 @@ isc_counter_increment(isc_counter_t *counter) {
 
 unsigned int
 isc_counter_used(isc_counter_t *counter) {
+	unsigned int used;
+
 	REQUIRE(VALID_COUNTER(counter));
 
-	return (counter->used);
+	LOCK(&counter->lock);
+	used = counter->used;
+	UNLOCK(&counter->lock);
+
+	return (used);
 }
 
 void
-- 
2.23.0

