Blame SOURCES/0117-LDAP-Fix-leak-of-file-descriptors.patch

518b87
From bb3365aee62f616c9d0c8cc8d737ef69d46544d3 Mon Sep 17 00:00:00 2001
518b87
From: Lukas Slebodnik <lslebodn@redhat.com>
518b87
Date: Thu, 22 Oct 2015 10:30:12 +0200
518b87
Subject: [PATCH 117/117] LDAP: Fix leak of file descriptors
518b87
518b87
The state "struct sss_ldap_init_state" contains socket
518b87
created in function sss_ldap_init_send. We register callback
518b87
sdap_async_sys_connect_timeout for handling issue with connection
518b87
518b87
The tevent request "sss_ldap_init_send" is usually (nested) subrequest
518b87
of "struct resolve_service_state" related request created in fucntion
518b87
fo_resolve_service_send. Function fo_resolve_service_send also register
518b87
timeout callback fo_resolve_service_timeout to state "struct
518b87
resolve_service_state".
518b87
518b87
It might happen that fo_resolve_service_timeout will be called before
518b87
sss_ldap_init_send timeout and we could not handle tiemout error
518b87
for state "struct sss_ldap_init_state" and therefore created socket
518b87
was not closed.
518b87
518b87
We tried to release resources in function sdap_handle_release.
518b87
But the structure "struct sdap_handle" had not been initialized yet
518b87
with LDAP handle and therefore associated file descriptor could not be closed.
518b87
518b87
[fo_resolve_service_timeout] (0x0080): Service resolving timeout reached
518b87
[fo_resolve_service_recv] (0x0020): TEVENT_REQ_RETURN_ON_ERROR ret[110]
518b87
[sdap_handle_release] (0x2000): Trace: sh[0x7f6713410270], connected[0], ops[(nil)], ldap[(nil)], destructor_lock[0], release_memory
518b87
[be_resolve_server_done] (0x1000): Server resolution failed: 14
518b87
[be_resolve_server_recv] (0x0020): TEVENT_REQ_RETURN_ON_ERROR ret[14]
518b87
[check_online_callback] (0x0100): Backend returned: (1, 0, <NULL>) [Provider is Offline (Success)]
518b87
518b87
Resolves:
518b87
https://fedorahosted.org/sssd/ticket/2792
518b87
518b87
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
518b87
(cherry picked from commit a10f67d4c64f3b1243de5d86a996475361adf0ac)
518b87
(cherry picked from commit db2fdba6f3cecd0612439988e61be60d5d8576bf)
518b87
(cherry picked from commit 2136f71c94660bcdde83f80feb83734389d57674)
518b87
---
518b87
 src/util/sss_ldap.c | 29 +++++++++++++++++++++--------
518b87
 1 file changed, 21 insertions(+), 8 deletions(-)
518b87
518b87
diff --git a/src/util/sss_ldap.c b/src/util/sss_ldap.c
518b87
index dd63b4b4f22f0aa1b540bc04ede211ac9cb88ebe..f42f9404bb9b79cdeb6a01c0a6e5025bb0370a6c 100644
518b87
--- a/src/util/sss_ldap.c
518b87
+++ b/src/util/sss_ldap.c
518b87
@@ -304,6 +304,22 @@ struct sss_ldap_init_state {
518b87
 #endif
518b87
 };
518b87
 
518b87
+static int sss_ldap_init_state_destructor(void *data)
518b87
+{
518b87
+    struct sss_ldap_init_state *state = (struct sss_ldap_init_state *)data;
518b87
+
518b87
+    if (state->ldap) {
518b87
+        DEBUG(SSSDBG_TRACE_FUNC,
518b87
+              "calling ldap_unbind_ext for ldap:[%p] sd:[%d]\n",
518b87
+              state->ldap, state->sd);
518b87
+        ldap_unbind_ext(state->ldap, NULL, NULL);
518b87
+    } else if (state->sd != -1) {
518b87
+        DEBUG(SSSDBG_TRACE_FUNC, "closing socket [%d]\n", state->sd);
518b87
+        close(state->sd);
518b87
+    }
518b87
+
518b87
+    return 0;
518b87
+}
518b87
 
518b87
 struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx,
518b87
                                       struct tevent_context *ev,
518b87
@@ -321,6 +337,8 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx,
518b87
         return NULL;
518b87
     }
518b87
 
518b87
+    talloc_set_destructor((TALLOC_CTX *)state, sss_ldap_init_state_destructor);
518b87
+
518b87
     state->ldap = NULL;
518b87
     state->uri = uri;
518b87
 
518b87
@@ -370,9 +388,6 @@ struct tevent_req *sss_ldap_init_send(TALLOC_CTX *mem_ctx,
518b87
     return req;
518b87
 
518b87
 fail:
518b87
-    if(state->sd >= 0) {
518b87
-        close(state->sd);
518b87
-    }
518b87
     tevent_req_error(req, ret);
518b87
 #else
518b87
     DEBUG(SSSDBG_MINOR_FAILURE, "ldap_init_fd not available, "
518b87
@@ -455,11 +470,6 @@ static void sss_ldap_init_sys_connect_done(struct tevent_req *subreq)
518b87
     return;
518b87
 
518b87
 fail:
518b87
-    if (state->ldap) {
518b87
-        ldap_unbind_ext(state->ldap, NULL, NULL);
518b87
-    } else {
518b87
-        close(state->sd);
518b87
-    }
518b87
     tevent_req_error(req, ret);
518b87
 }
518b87
 #endif
518b87
@@ -470,6 +480,9 @@ int sss_ldap_init_recv(struct tevent_req *req, LDAP **ldap, int *sd)
518b87
                                                     struct sss_ldap_init_state);
518b87
     TEVENT_REQ_RETURN_ON_ERROR(req);
518b87
 
518b87
+    /* Everything went well therefore we do not want to release resources */
518b87
+    talloc_set_destructor(state, NULL);
518b87
+
518b87
     *ldap = state->ldap;
518b87
     *sd = state->sd;
518b87
 
518b87
-- 
518b87
2.4.11
518b87