183429
From bb224602e105859e93a52f8c9f464eb9cbe79b0a Mon Sep 17 00:00:00 2001
183429
From: Rob Crittenden <rcritten@redhat.com>
183429
Date: Mon, 26 Jun 2023 13:06:51 -0400
183429
Subject: [PATCH] Fix memory leak in the OTP last token plugin
183429
183429
Three memory leaks are addressed:
183429
183429
1. String values retrieved from the pblock need to be manually
183429
freed.
183429
183429
2. The list of objectclasses retreived from the pblock need to be
183429
freed.
183429
183429
3. Internal search results need to be freed.
183429
183429
Fixes: https://pagure.io/freeipa/issue/9403
183429
183429
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
183429
Reviewed-By: Rafael Guterres Jeffman <rjeffman@redhat.com>
183429
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
183429
---
183429
 .../ipa-otp-lasttoken/ipa_otp_lasttoken.c     | 38 +++++++++++++------
183429
 daemons/ipa-slapi-plugins/libotp/otp_token.c  |  1 +
183429
 2 files changed, 27 insertions(+), 12 deletions(-)
183429
183429
diff --git a/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c b/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
183429
index b7a2ba7f012fdbf90284ee6605788e196aa4793b..11106b239f9de9074125979cfae7c02e434936e1 100644
183429
--- a/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
183429
+++ b/daemons/ipa-slapi-plugins/ipa-otp-lasttoken/ipa_otp_lasttoken.c
183429
@@ -54,7 +54,7 @@ void *ipa_otp_lasttoken_plugin_id;
183429
 
183429
 static bool entry_is_token(Slapi_Entry *entry)
183429
 {
183429
-    char **ocls;
183429
+    char **ocls = NULL;
183429
 
183429
     ocls = slapi_entry_attr_get_charray(entry, SLAPI_ATTR_OBJECTCLASS);
183429
     for (size_t i = 0; ocls != NULL && ocls[i] != NULL; i++) {
183429
@@ -64,6 +64,7 @@ static bool entry_is_token(Slapi_Entry *entry)
183429
         }
183429
     }
183429
 
183429
+    slapi_ch_array_free(ocls);
183429
     return false;
183429
 }
183429
 
183429
@@ -138,7 +139,8 @@ static bool is_pwd_enabled(const char *user_dn)
183429
 static bool is_allowed(Slapi_PBlock *pb, Slapi_Entry *entry)
183429
 {
183429
     Slapi_DN *target_sdn = NULL;
183429
-    const char *bind_dn;
183429
+    char *bind_dn;
183429
+    bool rv = false;
183429
 
183429
     /* Ignore internal operations. */
183429
     if (slapi_op_internal(pb))
183429
@@ -147,23 +149,35 @@ static bool is_allowed(Slapi_PBlock *pb, Slapi_Entry *entry)
183429
     /* Load parameters. */
183429
     (void) slapi_pblock_get(pb, SLAPI_TARGET_SDN, &target_sdn);
183429
     (void) slapi_pblock_get(pb, SLAPI_CONN_DN, &bind_dn);
183429
-    if (target_sdn == NULL || bind_dn == NULL) {
183429
-        LOG_FATAL("Missing parameters!\n");
183429
-        return false;
183429
+    if (bind_dn == NULL) {
183429
+        LOG_FATAL("bind_dn parameter missing!\n");
183429
+        goto done;
183429
+    }
183429
+    if (target_sdn == NULL) {
183429
+        LOG_FATAL("target_sdn parameter missing!\n");
183429
+        goto done;
183429
     }
183429
 
183429
     if (entry != NULL
183429
             ? !entry_is_token(entry)
183429
-            : !sdn_in_otp_container(target_sdn))
183429
-        return true;
183429
+            : !sdn_in_otp_container(target_sdn)) {
183429
+        rv = true;
183429
+        goto done;
183429
+    }
183429
 
183429
-    if (!sdn_is_only_enabled_token(target_sdn, bind_dn))
183429
-        return true;
183429
+    if (!sdn_is_only_enabled_token(target_sdn, bind_dn)) {
183429
+        rv = true;
183429
+        goto done;
183429
+    }
183429
 
183429
-    if (is_pwd_enabled(bind_dn))
183429
-        return true;
183429
+    if (is_pwd_enabled(bind_dn)) {
183429
+        rv = true;
183429
+        goto done;
183429
+    }
183429
 
183429
-    return false;
183429
+done:
183429
+    slapi_ch_free_string(&bind_dn);
183429
+    return rv;
183429
 }
183429
 
183429
 static inline int send_error(Slapi_PBlock *pb, int rc, const char *errstr)
183429
diff --git a/daemons/ipa-slapi-plugins/libotp/otp_token.c b/daemons/ipa-slapi-plugins/libotp/otp_token.c
183429
index a3cbfb0621c071f8addb29f7ce02f870a807c61d..4be4ede07cbbd0d26bcc9952ef4d84d777076ae7 100644
183429
--- a/daemons/ipa-slapi-plugins/libotp/otp_token.c
183429
+++ b/daemons/ipa-slapi-plugins/libotp/otp_token.c
183429
@@ -398,6 +398,7 @@ static struct otp_token **find(const struct otp_config *cfg, const char *user_dn
183429
     }
183429
 
183429
 error:
183429
+    slapi_free_search_results_internal(pb);
183429
     slapi_pblock_destroy(pb);
183429
     return tokens;
183429
 }
183429
-- 
183429
2.41.0
183429