From fa994f917d51997c6bc125161a473d1ed8d40229 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 31 Jan 2016 23:53:31 +0100 Subject: [PATCH] activate: fix memory allocation for execv() parameters Make sure we construct the full environment block on the heap, so that we can clean things up properly if execv() fails. --- src/activate/activate.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/activate/activate.c b/src/activate/activate.c index 9508344..6709d9b 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -151,24 +151,44 @@ static int launch(char* name, char **argv, char **env, int fds) { return log_oom(); STRV_FOREACH(s, arg_setenv) { - if (strchr(*s, '=')) - envp[n_env++] = *s; - else { + if (strchr(*s, '=')) { + char *k; + + k = strdup(*s); + if (!k) + return log_oom(); + + envp[n_env++] = k; + } else { _cleanup_free_ char *p; + const char *n; p = strappend(*s, "="); if (!p) return log_oom(); - envp[n_env] = strv_find_prefix(env, p); - if (envp[n_env]) - n_env ++; + + n = strv_find_prefix(env, p); + if (!n) + continue; + + envp[n_env] = strdup(n); + if (!envp[n_env]) + return log_oom(); } } for (i = 0; i < ELEMENTSOF(tocopy); i++) { - envp[n_env] = strv_find_prefix(env, tocopy[i]); - if (envp[n_env]) - n_env ++; + const char *n; + + n = strv_find_prefix(env, tocopy[i]); + if (!n) + continue; + + envp[n_env] = strdup(n); + if (!envp[n_env]) + return log_oom(); + + n_env ++; } if ((asprintf((char**)(envp + n_env++), "LISTEN_FDS=%d", fds) < 0) || -- 2.7.4