Blob Blame History Raw
From d386e94ef49d95d7305a3e6578e41a2cf61dfc5c Mon Sep 17 00:00:00 2001
From: Alexey Tikhonov <atikhono@redhat.com>
Date: Tue, 16 Aug 2022 21:51:03 +0200
Subject: [PATCH 6/6] CLIENT:MC: pointer to the context mutex shouldn't be
 touched
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Even brief window inside `sss_nss_mc_destroy_ctx()` when `mutex == NULL`
was creating a possibility for a race.

Reviewed-by: Sumit Bose <sbose@redhat.com>
Reviewed-by: Tomáš Halman <thalman@redhat.com>
(cherry picked from commit 4ac93d9c5df59cdb7f397b4467f1c1c4822ff757)
---
 src/sss_client/nss_mc.h        |  4 +++-
 src/sss_client/nss_mc_common.c | 20 ++++++++++----------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/sss_client/nss_mc.h b/src/sss_client/nss_mc.h
index 0f88521e9..9ab2736fa 100644
--- a/src/sss_client/nss_mc.h
+++ b/src/sss_client/nss_mc.h
@@ -44,7 +44,9 @@ enum sss_mc_state {
     RECYCLED,
 };
 
-/* common stuff */
+/* In the case this structure is extended, don't forget to update
+ * `SSS_CLI_MC_CTX_INITIALIZER` and `sss_nss_mc_destroy_ctx()`.
+ */
 struct sss_cli_mc_ctx {
     enum sss_mc_state initialized;
 #if HAVE_PTHREAD
diff --git a/src/sss_client/nss_mc_common.c b/src/sss_client/nss_mc_common.c
index f38a4a85a..3128861bf 100644
--- a/src/sss_client/nss_mc_common.c
+++ b/src/sss_client/nss_mc_common.c
@@ -130,25 +130,25 @@ errno_t sss_nss_check_header(struct sss_cli_mc_ctx *ctx)
 
 static void sss_nss_mc_destroy_ctx(struct sss_cli_mc_ctx *ctx)
 {
-    uint32_t active_threads = ctx->active_threads;
-#if HAVE_PTHREAD
-    pthread_mutex_t *mutex = ctx->mutex;
-#endif
 
     if ((ctx->mmap_base != NULL) && (ctx->mmap_size != 0)) {
         munmap(ctx->mmap_base, ctx->mmap_size);
     }
+    ctx->mmap_base = NULL;
+    ctx->mmap_size = 0;
+
     if (ctx->fd != -1) {
         close(ctx->fd);
     }
-    memset(ctx, 0, sizeof(struct sss_cli_mc_ctx));
     ctx->fd = -1;
 
-    /* restore count of active threads */
-    ctx->active_threads = active_threads;
-#if HAVE_PTHREAD
-    ctx->mutex = mutex;
-#endif
+    ctx->seed = 0;
+    ctx->data_table = NULL;
+    ctx->dt_size = 0;
+    ctx->hash_table = NULL;
+    ctx->ht_size = 0;
+    ctx->initialized = UNINITIALIZED;
+    /* `mutex` and `active_threads` should be left intact */
 }
 
 static errno_t sss_nss_mc_init_ctx(const char *name,
-- 
2.37.1