File 0009-sss-client-handle-key-value-in-destructor.patch of Package sssd.36868

From 2b0f908283c498a5f385a9b6b3058bce40c0a575 Mon Sep 17 00:00:00 2001
From: Diaa Sami <disami@redhat.com>
Date: Tue, 14 May 2024 15:28:18 -0400
Subject: [PATCH] sss-client: handle key value in destructor

When the pthread key destructor is called the key value is already set
to NULL by the caller. As a result the data stored in the value can only
be accessed by the first argument passed to the destructor and not by
pthread_getspecific() as the previous code did.

Resolves: https://github.com/SSSD/sssd/issues/7189

Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
(cherry picked from commit b439847bc88ad7b89f0596af822c0ffbf2a579df)
Signed-off-by: Diaa Sami <disami@redhat.com>

Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
---
 src/sss_client/common.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/sss_client/common.c b/src/sss_client/common.c
index c0d769061..a67ee3e07 100644
--- a/src/sss_client/common.c
+++ b/src/sss_client/common.c
@@ -93,8 +93,22 @@ void sss_cli_close_socket(void)
 #ifdef HAVE_PTHREAD_EXT
 static void sss_at_thread_exit(void *v)
 {
-    sss_cli_close_socket();
+    /* At this point the key value is already set to NULL and the only way to
+     * access the data from the value is via the argument passed to the
+     * destructor (sss_at_thread_exit). See e.g.
+     * https://www.man7.org/linux/man-pages/man3/pthread_key_create.3p.html
+     * for details. */
+
+    struct sss_socket_descriptor_t *descriptor = (struct sss_socket_descriptor_t *) v;
+
+    if (descriptor->sd != -1) {
+        close(descriptor->sd);
+        descriptor->sd = -1;
+    }
+
     free(v);
+
+    /* Most probably redudant, but better safe than sorry. */
     pthread_setspecific(sss_sd_key, NULL);
 }
 
-- 
2.43.0

openSUSE Build Service is sponsored by