Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-distro / recipes-security / gssproxy / files / Fix-most-memory-leaks.patch
1 From 9f9ab1e13c72b7c1fd06b6ba085ba2853bb9c3ca Mon Sep 17 00:00:00 2001
2 From: Alexander Scheel <ascheel@redhat.com>
3 Date: Thu, 29 Jun 2017 10:59:46 -0400
4 Subject: [PATCH] Fix most memory leaks
5
6 Signed-off-by: Alexander Scheel <ascheel@redhat.com>
7 [rharwood@redhat.com: commit message, whitespace]
8 Reviewed-by: Robbie Harwood <rharwood@redhat.com>
9 Merges: #203
10 Related: #176
11 (cherry picked from commit 470cf4d745d57f0597124a35b2faf86ba1107bb5)
12 [rharwood@redhat.com: backport around missing program support]
13 ---
14  proxy/src/gp_config.c            |  1 +
15  proxy/src/gp_creds.c             |  2 ++
16  proxy/src/gp_export.c            |  3 ++-
17  proxy/src/gp_rpc_acquire_cred.c  | 17 ++++++++-----
18  proxy/src/gssproxy.c             | 42 +++++++++++++++++++++++---------
19  proxy/src/mechglue/gpp_context.c |  2 ++
20  proxy/tests/t_acquire.c          |  3 +++
21  7 files changed, 51 insertions(+), 19 deletions(-)
22
23 diff --git a/proxy/src/gp_config.c b/proxy/src/gp_config.c
24 index a671333..b4ab90c 100644
25 --- a/proxy/src/gp_config.c
26 +++ b/proxy/src/gp_config.c
27 @@ -75,6 +75,7 @@ static void gp_service_free(struct gp_service *svc)
28          free_cred_store_elements(&svc->krb5.store);
29          gp_free_creds_handle(&svc->krb5.creds_handle);
30      }
31 +    free(svc->socket);
32      SELINUX_context_free(svc->selinux_ctx);
33      memset(svc, 0, sizeof(struct gp_service));
34  }
35 diff --git a/proxy/src/gp_creds.c b/proxy/src/gp_creds.c
36 index fdc6bdf..2cb4ce7 100644
37 --- a/proxy/src/gp_creds.c
38 +++ b/proxy/src/gp_creds.c
39 @@ -1049,6 +1049,8 @@ uint32_t gp_count_tickets(uint32_t *min, gss_cred_id_t cred, uint32_t *ccsum)
40              goto done;
41          }
42  
43 +        krb5_free_cred_contents(context, &creds);
44 +
45          /* TODO: Should we do a real checksum over all creds->ticket data and
46           * flags in future ? */
47          (*ccsum)++;
48 diff --git a/proxy/src/gp_export.c b/proxy/src/gp_export.c
49 index 4e081df..ab08bb7 100644
50 --- a/proxy/src/gp_export.c
51 +++ b/proxy/src/gp_export.c
52 @@ -47,7 +47,7 @@ uint32_t gp_init_creds_with_keytab(uint32_t *min, const char *svc_name,
53      krb5_keytab ktid = NULL;
54      krb5_kt_cursor cursor;
55      krb5_keytab_entry entry;
56 -    krb5_enctype *permitted;
57 +    krb5_enctype *permitted = NULL;
58      uint32_t ret_maj = 0;
59      uint32_t ret_min = 0;
60      int ret;
61 @@ -127,6 +127,7 @@ uint32_t gp_init_creds_with_keytab(uint32_t *min, const char *svc_name,
62      ret_maj = GSS_S_COMPLETE;
63  
64  done:
65 +    krb5_free_enctypes(handle->context, permitted);
66      if (ktid) {
67          (void)krb5_kt_close(handle->context, ktid);
68      }
69 diff --git a/proxy/src/gp_rpc_acquire_cred.c b/proxy/src/gp_rpc_acquire_cred.c
70 index fcb4fbe..7ddb427 100644
71 --- a/proxy/src/gp_rpc_acquire_cred.c
72 +++ b/proxy/src/gp_rpc_acquire_cred.c
73 @@ -130,17 +130,18 @@ int gp_acquire_cred(struct gp_call_ctx *gpcall,
74          }
75      }
76  
77 -    acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
78 -    if (!acr->output_cred_handle) {
79 -        ret_maj = GSS_S_FAILURE;
80 -        ret_min = ENOMEM;
81 -        goto done;
82 -    }
83  
84      if (out_cred == in_cred) {
85          acr->output_cred_handle = aca->input_cred_handle;
86          aca->input_cred_handle = NULL;
87      } else {
88 +        acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
89 +        if (!acr->output_cred_handle) {
90 +            ret_maj = GSS_S_FAILURE;
91 +            ret_min = ENOMEM;
92 +            goto done;
93 +        }
94 +
95          ret_maj = gp_export_gssx_cred(&ret_min, gpcall,
96                                        &out_cred, acr->output_cred_handle);
97          if (ret_maj) {
98 @@ -154,6 +155,10 @@ done:
99  
100      GPRPCDEBUG(gssx_res_acquire_cred, acr);
101  
102 +    if (add_out_cred != &in_cred && add_out_cred != &out_cred)
103 +        gss_release_cred(&ret_min, add_out_cred);
104 +    if (in_cred != out_cred)
105 +        gss_release_cred(&ret_min, &in_cred);
106      gss_release_cred(&ret_min, &out_cred);
107      gss_release_oid_set(&ret_min, &use_mechs);
108      gss_release_oid_set(&ret_min, &desired_mechs);
109 diff --git a/proxy/src/gssproxy.c b/proxy/src/gssproxy.c
110 index a020218..5c5937d 100644
111 --- a/proxy/src/gssproxy.c
112 +++ b/proxy/src/gssproxy.c
113 @@ -157,7 +157,7 @@ int main(int argc, const char *argv[])
114      verto_ctx *vctx;
115      verto_ev *ev;
116      int wait_fd;
117 -    int ret;
118 +    int ret = -1;
119  
120      struct poptOption long_options[] = {
121          POPT_AUTOHELP
122 @@ -187,13 +187,17 @@ int main(int argc, const char *argv[])
123              fprintf(stderr, "\nInvalid option %s: %s\n\n",
124                      poptBadOption(pc, 0), poptStrerror(opt));
125              poptPrintUsage(pc, stderr, 0);
126 -            return 1;
127 +
128 +            ret = 1;
129 +            goto cleanup;
130          }
131      }
132  
133      if (opt_version) {
134          puts(VERSION""DISTRO_VERSION""PRERELEASE_VERSION);
135 -        return 0;
136 +        poptFreeContext(pc);
137 +        ret = 0;
138 +        goto cleanup;
139      }
140  
141      if (opt_debug || opt_debug_level > 0) {
142 @@ -204,7 +208,8 @@ int main(int argc, const char *argv[])
143      if (opt_daemon && opt_interactive) {
144          fprintf(stderr, "Option -i|--interactive is not allowed together with -D|--daemon\n");
145          poptPrintUsage(pc, stderr, 0);
146 -        return 1;
147 +        ret = 0;
148 +        goto cleanup;
149      }
150  
151      if (opt_interactive) {
152 @@ -218,7 +223,8 @@ int main(int argc, const char *argv[])
153                                  opt_config_socket,
154                                  opt_daemon);
155      if (!gpctx->config) {
156 -        exit(EXIT_FAILURE);
157 +        ret = EXIT_FAILURE;
158 +        goto cleanup;
159      }
160  
161      init_server(gpctx->config->daemonize, &wait_fd);
162 @@ -229,7 +235,8 @@ int main(int argc, const char *argv[])
163      if (!vctx) {
164          fprintf(stderr, "Failed to initialize event loop. "
165                          "Is there at least one libverto backend installed?\n");
166 -        return 1;
167 +        ret = 1;
168 +        goto cleanup;
169      }
170      gpctx->vctx = vctx;
171  
172 @@ -237,12 +244,13 @@ int main(int argc, const char *argv[])
173      ev = verto_add_signal(vctx, VERTO_EV_FLAG_PERSIST, hup_handler, SIGHUP);
174      if (!ev) {
175          fprintf(stderr, "Failed to register SIGHUP handler with verto!\n");
176 -        return 1;
177 +        ret = 1;
178 +        goto cleanup;
179      }
180  
181      ret = init_sockets(vctx, NULL);
182      if (ret != 0) {
183 -        return ret;
184 +        goto cleanup;
185      }
186  
187      /* We need to tell nfsd that GSS-Proxy is available before it starts,
188 @@ -256,12 +264,14 @@ int main(int argc, const char *argv[])
189  
190      ret = drop_privs(gpctx->config);
191      if (ret) {
192 -        exit(EXIT_FAILURE);
193 +        ret = EXIT_FAILURE;
194 +        goto cleanup;
195      }
196  
197      ret = gp_workers_init(gpctx);
198      if (ret) {
199 -        exit(EXIT_FAILURE);
200 +        ret = EXIT_FAILURE;
201 +        goto cleanup;
202      }
203  
204      verto_run(vctx);
205 @@ -271,9 +281,17 @@ int main(int argc, const char *argv[])
206  
207      fini_server();
208  
209 -    poptFreeContext(pc);
210  
211      free_config(&gpctx->config);
212 +    free(gpctx);
213  
214 -    return 0;
215 +    ret = 0;
216 +
217 +cleanup:
218 +    poptFreeContext(pc);
219 +    free(opt_config_file);
220 +    free(opt_config_dir);
221 +    free(opt_config_socket);
222 +
223 +    return ret;
224  }
225 diff --git a/proxy/src/mechglue/gpp_context.c b/proxy/src/mechglue/gpp_context.c
226 index 2f41e4f..69e69e0 100644
227 --- a/proxy/src/mechglue/gpp_context.c
228 +++ b/proxy/src/mechglue/gpp_context.c
229 @@ -362,6 +362,8 @@ OM_uint32 gssi_delete_sec_context(OM_uint32 *minor_status,
230          }
231      }
232  
233 +    free(ctx);
234 +
235      return rmaj;
236  }
237  
238 diff --git a/proxy/tests/t_acquire.c b/proxy/tests/t_acquire.c
239 index 2bb7706..5334565 100644
240 --- a/proxy/tests/t_acquire.c
241 +++ b/proxy/tests/t_acquire.c
242 @@ -132,5 +132,8 @@ done:
243      gss_release_buffer(&ret_min, &in_token);
244      gss_release_buffer(&ret_min, &out_token);
245      gss_release_cred(&ret_min, &cred_handle);
246 +    gss_release_name(&ret_min, &target_name);
247 +    gss_delete_sec_context(&ret_min, &init_ctx, GSS_C_NO_BUFFER);
248 +    gss_delete_sec_context(&ret_min, &accept_ctx, GSS_C_NO_BUFFER);
249      return ret;
250  }