Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec: do not inherit env variables from main pid #283

Merged
merged 1 commit into from Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/libcrun/container.c
Expand Up @@ -2169,9 +2169,27 @@ libcrun_container_exec (libcrun_context_t *context, const char *id, runtime_spec
if (UNLIKELY (ret < 0))
return ret;

for (i = 0; i < process->env_len; i++)
if (putenv (process->env[i]) < 0)
libcrun_fail_with_error ( errno, "putenv `%s`", process->env[i]);
ret = clearenv ();
if (UNLIKELY (ret < 0))
return crun_make_error (err, 0, "clearenv");

if (process->env_len)
{
for (i = 0; i < process->env_len; i++)
if (putenv (process->env[i]) < 0)
libcrun_fail_with_error ( errno, "putenv `%s`", process->env[i]);
}
else if (container->container_def->process->env_len)
{
char *e;

for (i = 0; i < container->container_def->process->env_len; i++)
{
e = container->container_def->process->env[i];
if (putenv (e) < 0)
libcrun_fail_with_error ( errno, "putenv `%s`", e);
}
}

if (getenv ("HOME") == NULL)
{
Expand Down
33 changes: 0 additions & 33 deletions src/libcrun/linux.c
Expand Up @@ -2799,28 +2799,6 @@ join_process_parent_helper (pid_t child_pid,
return pid;
}

static int
inherit_env (pid_t pid_to_join, libcrun_error_t *err)
{
int ret = 0;
size_t len;
char *str;
cleanup_free char *path;
/* Not a memory leak here. The data used by putenv must not be freed. */
char *content = NULL;

xasprintf (&path, "/proc/%d/environ", pid_to_join);

ret = read_all_file (path, &content, &len, err);
if (UNLIKELY (ret < 0))
return ret;

for (str = content; str < content + len; str += strlen (str) + 1)
if (putenv (str) < 0)
return crun_make_error (err, errno, "putenv `%s`", str);
return ret;
}

int
libcrun_join_process (libcrun_container_t *container, pid_t pid_to_join, libcrun_container_status_t *status, int detach, int *terminal_fd, libcrun_error_t *err)
{
Expand Down Expand Up @@ -2866,17 +2844,6 @@ libcrun_join_process (libcrun_container_t *container, pid_t pid_to_join, libcrun
close_and_reset (&sync_socket_fd[0]);
sync_fd = sync_socket_fd[1];

ret = clearenv ();
if (UNLIKELY (ret < 0))
{
crun_make_error (err, 0, "clearenv");
goto exit;
}

ret = inherit_env (pid_to_join, err);
if (UNLIKELY (ret < 0))
goto exit;

if (def->linux->namespaces_len >= 10)
{
crun_make_error (err, 0, "invalid configuration");
Expand Down