1f3433
From 9f9ab1e13c72b7c1fd06b6ba085ba2853bb9c3ca Mon Sep 17 00:00:00 2001
1f3433
From: Alexander Scheel <ascheel@redhat.com>
1f3433
Date: Thu, 29 Jun 2017 10:59:46 -0400
1f3433
Subject: [PATCH] Fix most memory leaks
1f3433
1f3433
Signed-off-by: Alexander Scheel <ascheel@redhat.com>
1f3433
[rharwood@redhat.com: commit message, whitespace]
1f3433
Reviewed-by: Robbie Harwood <rharwood@redhat.com>
1f3433
Merges: #203
1f3433
Related: #176
1f3433
(cherry picked from commit 470cf4d745d57f0597124a35b2faf86ba1107bb5)
1f3433
[rharwood@redhat.com: backport around missing program support]
1f3433
---
1f3433
 proxy/src/gp_config.c            |  1 +
1f3433
 proxy/src/gp_creds.c             |  2 ++
1f3433
 proxy/src/gp_export.c            |  3 ++-
68bf20
 proxy/src/gp_rpc_acquire_cred.c  | 17 ++++++++-----
68bf20
 proxy/src/gssproxy.c             | 42 +++++++++++++++++++++++---------
1f3433
 proxy/src/mechglue/gpp_context.c |  2 ++
1f3433
 proxy/tests/t_acquire.c          |  3 +++
1f3433
 7 files changed, 51 insertions(+), 19 deletions(-)
1f3433
1f3433
diff --git a/proxy/src/gp_config.c b/proxy/src/gp_config.c
1f3433
index a671333..b4ab90c 100644
1f3433
--- a/proxy/src/gp_config.c
1f3433
+++ b/proxy/src/gp_config.c
1f3433
@@ -75,6 +75,7 @@ static void gp_service_free(struct gp_service *svc)
1f3433
         free_cred_store_elements(&svc->krb5.store);
1f3433
         gp_free_creds_handle(&svc->krb5.creds_handle);
1f3433
     }
1f3433
+    free(svc->socket);
1f3433
     SELINUX_context_free(svc->selinux_ctx);
1f3433
     memset(svc, 0, sizeof(struct gp_service));
1f3433
 }
1f3433
diff --git a/proxy/src/gp_creds.c b/proxy/src/gp_creds.c
1f3433
index fdc6bdf..2cb4ce7 100644
1f3433
--- a/proxy/src/gp_creds.c
1f3433
+++ b/proxy/src/gp_creds.c
1f3433
@@ -1049,6 +1049,8 @@ uint32_t gp_count_tickets(uint32_t *min, gss_cred_id_t cred, uint32_t *ccsum)
1f3433
             goto done;
1f3433
         }
1f3433
 
1f3433
+        krb5_free_cred_contents(context, &creds);
1f3433
+
1f3433
         /* TODO: Should we do a real checksum over all creds->ticket data and
1f3433
          * flags in future ? */
1f3433
         (*ccsum)++;
1f3433
diff --git a/proxy/src/gp_export.c b/proxy/src/gp_export.c
1f3433
index 4e081df..ab08bb7 100644
1f3433
--- a/proxy/src/gp_export.c
1f3433
+++ b/proxy/src/gp_export.c
1f3433
@@ -47,7 +47,7 @@ uint32_t gp_init_creds_with_keytab(uint32_t *min, const char *svc_name,
1f3433
     krb5_keytab ktid = NULL;
1f3433
     krb5_kt_cursor cursor;
1f3433
     krb5_keytab_entry entry;
1f3433
-    krb5_enctype *permitted;
1f3433
+    krb5_enctype *permitted = NULL;
1f3433
     uint32_t ret_maj = 0;
1f3433
     uint32_t ret_min = 0;
1f3433
     int ret;
1f3433
@@ -127,6 +127,7 @@ uint32_t gp_init_creds_with_keytab(uint32_t *min, const char *svc_name,
1f3433
     ret_maj = GSS_S_COMPLETE;
1f3433
 
1f3433
 done:
1f3433
+    krb5_free_enctypes(handle->context, permitted);
1f3433
     if (ktid) {
1f3433
         (void)krb5_kt_close(handle->context, ktid);
1f3433
     }
1f3433
diff --git a/proxy/src/gp_rpc_acquire_cred.c b/proxy/src/gp_rpc_acquire_cred.c
1f3433
index fcb4fbe..7ddb427 100644
1f3433
--- a/proxy/src/gp_rpc_acquire_cred.c
1f3433
+++ b/proxy/src/gp_rpc_acquire_cred.c
1f3433
@@ -130,17 +130,18 @@ int gp_acquire_cred(struct gp_call_ctx *gpcall,
1f3433
         }
1f3433
     }
1f3433
 
1f3433
-    acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
1f3433
-    if (!acr->output_cred_handle) {
1f3433
-        ret_maj = GSS_S_FAILURE;
1f3433
-        ret_min = ENOMEM;
1f3433
-        goto done;
1f3433
-    }
1f3433
 
1f3433
     if (out_cred == in_cred) {
1f3433
         acr->output_cred_handle = aca->input_cred_handle;
1f3433
         aca->input_cred_handle = NULL;
1f3433
     } else {
1f3433
+        acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
1f3433
+        if (!acr->output_cred_handle) {
1f3433
+            ret_maj = GSS_S_FAILURE;
1f3433
+            ret_min = ENOMEM;
1f3433
+            goto done;
1f3433
+        }
1f3433
+
1f3433
         ret_maj = gp_export_gssx_cred(&ret_min, gpcall,
1f3433
                                       &out_cred, acr->output_cred_handle);
1f3433
         if (ret_maj) {
1f3433
@@ -154,6 +155,10 @@ done:
1f3433
 
1f3433
     GPRPCDEBUG(gssx_res_acquire_cred, acr);
1f3433
 
1f3433
+    if (add_out_cred != &in_cred && add_out_cred != &out_cred)
1f3433
+        gss_release_cred(&ret_min, add_out_cred);
1f3433
+    if (in_cred != out_cred)
1f3433
+        gss_release_cred(&ret_min, &in_cred);
1f3433
     gss_release_cred(&ret_min, &out_cred);
1f3433
     gss_release_oid_set(&ret_min, &use_mechs);
1f3433
     gss_release_oid_set(&ret_min, &desired_mechs);
1f3433
diff --git a/proxy/src/gssproxy.c b/proxy/src/gssproxy.c
1f3433
index a020218..5c5937d 100644
1f3433
--- a/proxy/src/gssproxy.c
1f3433
+++ b/proxy/src/gssproxy.c
1f3433
@@ -157,7 +157,7 @@ int main(int argc, const char *argv[])
1f3433
     verto_ctx *vctx;
1f3433
     verto_ev *ev;
1f3433
     int wait_fd;
1f3433
-    int ret;
1f3433
+    int ret = -1;
1f3433
 
1f3433
     struct poptOption long_options[] = {
1f3433
         POPT_AUTOHELP
1f3433
@@ -187,13 +187,17 @@ int main(int argc, const char *argv[])
1f3433
             fprintf(stderr, "\nInvalid option %s: %s\n\n",
1f3433
                     poptBadOption(pc, 0), poptStrerror(opt));
1f3433
             poptPrintUsage(pc, stderr, 0);
1f3433
-            return 1;
1f3433
+
1f3433
+            ret = 1;
1f3433
+            goto cleanup;
1f3433
         }
1f3433
     }
1f3433
 
1f3433
     if (opt_version) {
1f3433
         puts(VERSION""DISTRO_VERSION""PRERELEASE_VERSION);
1f3433
-        return 0;
1f3433
+        poptFreeContext(pc);
1f3433
+        ret = 0;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     if (opt_debug || opt_debug_level > 0) {
1f3433
@@ -204,7 +208,8 @@ int main(int argc, const char *argv[])
1f3433
     if (opt_daemon && opt_interactive) {
1f3433
         fprintf(stderr, "Option -i|--interactive is not allowed together with -D|--daemon\n");
1f3433
         poptPrintUsage(pc, stderr, 0);
1f3433
-        return 1;
1f3433
+        ret = 0;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     if (opt_interactive) {
1f3433
@@ -218,7 +223,8 @@ int main(int argc, const char *argv[])
1f3433
                                 opt_config_socket,
1f3433
                                 opt_daemon);
1f3433
     if (!gpctx->config) {
1f3433
-        exit(EXIT_FAILURE);
1f3433
+        ret = EXIT_FAILURE;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     init_server(gpctx->config->daemonize, &wait_fd);
1f3433
@@ -229,7 +235,8 @@ int main(int argc, const char *argv[])
1f3433
     if (!vctx) {
1f3433
         fprintf(stderr, "Failed to initialize event loop. "
1f3433
                         "Is there at least one libverto backend installed?\n");
1f3433
-        return 1;
1f3433
+        ret = 1;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
     gpctx->vctx = vctx;
1f3433
 
1f3433
@@ -237,12 +244,13 @@ int main(int argc, const char *argv[])
1f3433
     ev = verto_add_signal(vctx, VERTO_EV_FLAG_PERSIST, hup_handler, SIGHUP);
1f3433
     if (!ev) {
1f3433
         fprintf(stderr, "Failed to register SIGHUP handler with verto!\n");
1f3433
-        return 1;
1f3433
+        ret = 1;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     ret = init_sockets(vctx, NULL);
1f3433
     if (ret != 0) {
1f3433
-        return ret;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     /* We need to tell nfsd that GSS-Proxy is available before it starts,
1f3433
@@ -256,12 +264,14 @@ int main(int argc, const char *argv[])
1f3433
 
1f3433
     ret = drop_privs(gpctx->config);
1f3433
     if (ret) {
1f3433
-        exit(EXIT_FAILURE);
1f3433
+        ret = EXIT_FAILURE;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     ret = gp_workers_init(gpctx);
1f3433
     if (ret) {
1f3433
-        exit(EXIT_FAILURE);
1f3433
+        ret = EXIT_FAILURE;
1f3433
+        goto cleanup;
1f3433
     }
1f3433
 
1f3433
     verto_run(vctx);
1f3433
@@ -271,9 +281,17 @@ int main(int argc, const char *argv[])
1f3433
 
1f3433
     fini_server();
1f3433
 
1f3433
-    poptFreeContext(pc);
1f3433
 
1f3433
     free_config(&gpctx->config);
1f3433
+    free(gpctx);
1f3433
 
1f3433
-    return 0;
1f3433
+    ret = 0;
1f3433
+
1f3433
+cleanup:
1f3433
+    poptFreeContext(pc);
1f3433
+    free(opt_config_file);
1f3433
+    free(opt_config_dir);
1f3433
+    free(opt_config_socket);
1f3433
+
1f3433
+    return ret;
1f3433
 }
1f3433
diff --git a/proxy/src/mechglue/gpp_context.c b/proxy/src/mechglue/gpp_context.c
1f3433
index 2f41e4f..69e69e0 100644
1f3433
--- a/proxy/src/mechglue/gpp_context.c
1f3433
+++ b/proxy/src/mechglue/gpp_context.c
1f3433
@@ -362,6 +362,8 @@ OM_uint32 gssi_delete_sec_context(OM_uint32 *minor_status,
1f3433
         }
1f3433
     }
1f3433
 
1f3433
+    free(ctx);
1f3433
+
1f3433
     return rmaj;
1f3433
 }
1f3433
 
1f3433
diff --git a/proxy/tests/t_acquire.c b/proxy/tests/t_acquire.c
1f3433
index 2bb7706..5334565 100644
1f3433
--- a/proxy/tests/t_acquire.c
1f3433
+++ b/proxy/tests/t_acquire.c
1f3433
@@ -132,5 +132,8 @@ done:
1f3433
     gss_release_buffer(&ret_min, &in_token);
1f3433
     gss_release_buffer(&ret_min, &out_token);
1f3433
     gss_release_cred(&ret_min, &cred_handle);
1f3433
+    gss_release_name(&ret_min, &target_name);
1f3433
+    gss_delete_sec_context(&ret_min, &init_ctx, GSS_C_NO_BUFFER);
1f3433
+    gss_delete_sec_context(&ret_min, &accept_ctx, GSS_C_NO_BUFFER);
1f3433
     return ret;
1f3433
 }