From 7c42c04f3fa1c6b936debe48b435b9ef9da464bd Mon Sep 17 00:00:00 2001
From: Matthijs Mekking <matthijs@isc.org>
Date: Mon, 16 May 2022 19:00:47 +0200
Subject: [PATCH] Fix CID 352776: Concurrent data access violations

*** CID 352776:  Concurrent data access violations  (MISSING_LOCK)
/lib/dns/dst_api.c: 474 in dst_key_setmodified()
468     dst_key_isexternal(dst_key_t *key) {
469		return (key->external);
470     }
471
472     void
473     dst_key_setmodified(dst_key_t *key, bool value) {
>>>     CID 352776:  Concurrent data access violations  (MISSING_LOCK)
>>>     Accessing "key->modified" without holding lock
>>>	"dst_key.mdlock". Elsewhere, "dst_key.modified" is accessed with
>>>	"dst_key.mdlock" held 8 out of 11 times (8 of these accesses
>>>	strongly imply that it is necessary).
474		key->modified = value;
475     }
476
477     bool
478     dst_key_ismodified(dst_key_t *key) {
479		return (key->modified);

(cherry picked from commit 1fa24d0afbc01d25d71446156758b3a945db5b5f)
Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/7c42c04f3fa1c6b936debe48b435b9ef9da464bd
---
 lib/dns/dst_api.c         | 12 ++++++++++--
 lib/dns/include/dst/dst.h |  2 +-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
index 8873a041b8..e5a52aea37 100644
--- a/lib/dns/dst_api.c
+++ b/lib/dns/dst_api.c
@@ -492,12 +492,20 @@ dst_key_isexternal(dst_key_t *key) {
 
 void
 dst_key_setmodified(dst_key_t *key, bool value) {
+	isc_mutex_lock(&key->mdlock);
 	key->modified = value;
+	isc_mutex_unlock(&key->mdlock);
 }
 
 bool
-dst_key_ismodified(dst_key_t *key) {
-	return (key->modified);
+dst_key_ismodified(const dst_key_t *key) {
+	bool modified;
+
+	isc_mutex_lock(&(((dst_key_t *)key)->mdlock));
+	modified = key->modified;
+	isc_mutex_unlock(&(((dst_key_t *)key)->mdlock));
+
+	return (modified);
 }
 
 isc_result_t
diff --git a/lib/dns/include/dst/dst.h b/lib/dns/include/dst/dst.h
index eab5501029..3185e9f91b 100644
--- a/lib/dns/include/dst/dst.h
+++ b/lib/dns/include/dst/dst.h
@@ -1119,7 +1119,7 @@ dst_key_setmodified(dst_key_t *key, bool value);
  */
 
 bool
-dst_key_ismodified(dst_key_t *key);
+dst_key_ismodified(const dst_key_t *key);
 /*%<
  * Check if the key file has been modified.
  *
-- 
2.27.0

