Blame SOURCES/0003-SSS_CLIENT-delete-key-in-lib-destructor.patch

3b3728
From 45a5630e0cfe95ab90bf4a7dd1b32f418c4c759e Mon Sep 17 00:00:00 2001
3b3728
From: Alexey Tikhonov <atikhono@redhat.com>
3b3728
Date: Fri, 23 Dec 2022 16:36:58 +0100
3b3728
Subject: [PATCH] SSS_CLIENT: delete key in lib destructor
3b3728
3b3728
pthread_key_delete() disables thread at-exit destructors.
3b3728
Otherwise an attempt to execute already unloaded `sss_at_thread_exit()`
3b3728
would trigger segfault.
3b3728
3b3728
This doesn't solve an issue with leaking on `dlclose()` FDs initialized in
3b3728
multiple threads, but better than crash.
3b3728
3b3728
Resolves: https://github.com/SSSD/sssd/issues/6505
3b3728
3b3728
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
3b3728
Reviewed-by: Sumit Bose <sbose@redhat.com>
3b3728
(cherry picked from commit 08ccd23fb2c831d6ea918a59b777a0073d414858)
3b3728
---
3b3728
 src/sss_client/common.c | 24 +++++++++++++++++++-----
3b3728
 1 file changed, 19 insertions(+), 5 deletions(-)
3b3728
3b3728
diff --git a/src/sss_client/common.c b/src/sss_client/common.c
3b3728
index d762dff49..2c888faa9 100644
3b3728
--- a/src/sss_client/common.c
3b3728
+++ b/src/sss_client/common.c
3b3728
@@ -27,6 +27,7 @@
3b3728
 #include <nss.h>
3b3728
 #include <security/pam_modules.h>
3b3728
 #include <errno.h>
3b3728
+#include <stdatomic.h>
3b3728
 #include <sys/types.h>
3b3728
 #include <sys/socket.h>
3b3728
 #include <sys/un.h>
3b3728
@@ -63,7 +64,8 @@
3b3728
 
3b3728
 #ifdef HAVE_PTHREAD_EXT
3b3728
 static pthread_key_t sss_sd_key;
3b3728
-static pthread_once_t sss_sd_key_initialized = PTHREAD_ONCE_INIT;
3b3728
+static pthread_once_t sss_sd_key_init = PTHREAD_ONCE_INIT;
3b3728
+static atomic_bool sss_sd_key_initialized = false;
3b3728
 static __thread int sss_cli_sd = -1; /* the sss client socket descriptor */
3b3728
 static __thread struct stat sss_cli_sb; /* the sss client stat buffer */
3b3728
 #else
3b3728
@@ -71,9 +73,6 @@ static int sss_cli_sd = -1; /* the sss client socket descriptor */
3b3728
 static struct stat sss_cli_sb; /* the sss client stat buffer */
3b3728
 #endif
3b3728
 
3b3728
-#if HAVE_FUNCTION_ATTRIBUTE_DESTRUCTOR
3b3728
-__attribute__((destructor))
3b3728
-#endif
3b3728
 void sss_cli_close_socket(void)
3b3728
 {
3b3728
     if (sss_cli_sd != -1) {
3b3728
@@ -91,9 +90,24 @@ static void sss_at_thread_exit(void *v)
3b3728
 static void init_sd_key(void)
3b3728
 {
3b3728
     pthread_key_create(&sss_sd_key, sss_at_thread_exit);
3b3728
+    sss_sd_key_initialized = true;
3b3728
+}
3b3728
+#endif
3b3728
+
3b3728
+#if HAVE_FUNCTION_ATTRIBUTE_DESTRUCTOR
3b3728
+__attribute__((destructor)) void sss_at_lib_unload(void)
3b3728
+{
3b3728
+#ifdef HAVE_PTHREAD_EXT
3b3728
+    if (sss_sd_key_initialized) {
3b3728
+        sss_sd_key_initialized = false;
3b3728
+        pthread_key_delete(sss_sd_key);
3b3728
+    }
3b3728
+#endif
3b3728
+    sss_cli_close_socket();
3b3728
 }
3b3728
 #endif
3b3728
 
3b3728
+
3b3728
 /* Requests:
3b3728
  *
3b3728
  * byte 0-3: 32bit unsigned with length (the complete packet length: 0 to X)
3b3728
@@ -572,7 +586,7 @@ static int sss_cli_open_socket(int *errnop, const char *socket_name, int timeout
3b3728
     }
3b3728
 
3b3728
 #ifdef HAVE_PTHREAD_EXT
3b3728
-    pthread_once(&sss_sd_key_initialized, init_sd_key); /* once for all threads */
3b3728
+    pthread_once(&sss_sd_key_init, init_sd_key); /* once for all threads */
3b3728
 
3b3728
     /* It actually doesn't matter what value to set for a key.
3b3728
      * The only important thing: key must be non-NULL to ensure
3b3728
-- 
3b3728
2.37.3
3b3728