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