From c2e50e2ab7d6ccba3f0a73a23661bcac1d610a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sj=C3=B6lund?= Date: Mon, 9 Feb 2026 16:58:20 +0100 Subject: [PATCH] libcrun: check setenv failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: https://github.com/containers/crun/issues/1998 Signed-off-by: Erik Sjölund --- src/libcrun/container.c | 9 ++++++--- src/libcrun/utils.c | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libcrun/container.c b/src/libcrun/container.c index c2f50a9b9e..98b4404997 100644 --- a/src/libcrun/container.c +++ b/src/libcrun/container.c @@ -1188,8 +1188,9 @@ setup_environment (runtime_spec_schema_config_schema *def, uid_t container_uid, ret = set_home_env (container_uid); if (UNLIKELY (ret < 0 && errno != ENOTSUP)) { - setenv ("HOME", "/", 1); libcrun_warning ("cannot detect HOME environment variable, setting default"); + if (setenv ("HOME", "/", 1) < 0) + return crun_make_error (err, errno, "setenv HOME"); } } @@ -1407,8 +1408,9 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket, /* Set primary process to 1 explicitly if nothing is configured and LISTEN_FD is not set. */ if (entrypoint_args->context->listen_fds > 0 && getenv ("LISTEN_PID") == NULL) { - setenv ("LISTEN_PID", "1", 1); libcrun_warning ("setting LISTEN_PID=1 since no previous configuration was found"); + if (setenv ("LISTEN_PID", "1", 1) < 0) + return crun_make_error (err, errno, "setenv LISTENPID"); } /* Attempt to chdir immediately here, before doing the setresuid. If we fail here, let's @@ -3691,8 +3693,9 @@ exec_process_entrypoint (libcrun_context_t *context, ret = set_home_env (container_uid); if (UNLIKELY (ret < 0 && errno != ENOTSUP)) { - setenv ("HOME", "/", 1); libcrun_warning ("cannot detect HOME environment variable, setting default"); + if (setenv ("HOME", "/", 1) < 0) + return crun_make_error (err, errno, "setenv HOME"); } } diff --git a/src/libcrun/utils.c b/src/libcrun/utils.c index 8c0d2dac43..dff63aaf62 100644 --- a/src/libcrun/utils.c +++ b/src/libcrun/utils.c @@ -1476,8 +1476,11 @@ set_home_env (uid_t id) if (ret_pw && ret_pw->pw_uid == id) { - setenv ("HOME", ret_pw->pw_dir, 1); - return 0; + ret = setenv ("HOME", ret_pw->pw_dir, 1); + if (UNLIKELY(ret < 0)) + goto error; + else + return 0; } }