]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
nsenter: support empty environ[]
authorKarel Zak <kzak@redhat.com>
Thu, 7 Nov 2024 11:26:25 +0000 (12:26 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 7 Nov 2024 11:40:25 +0000 (12:40 +0100)
There is no error when the /proc/#/environ file does not contain any
NAME=value items.

Backport from master (v2.41) branch.

Fixes: https://github.com/util-linux/util-linux/issues/3270
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/env.c
sys-utils/nsenter.c

index 24ae90d0051cdf01cb2648a5fa36531cd68d4142..f35006f3a0360b262192f45200bbb2c81dc2bff9 100644 (file)
--- a/lib/env.c
+++ b/lib/env.c
@@ -93,6 +93,7 @@ struct ul_env_list *env_from_fd(int fd)
        ssize_t rc = 0;
        struct ul_env_list *ls = NULL;
 
+       errno = 0;
        if ((rc = read_all_alloc(fd, &buf)) < 1)
                return NULL;
        buf[rc] = '\0';
index 8f7bac922f7e48140191d7d79d4e1736add9c827..58130e932df5c9c68fd337e8731a94a5a2b99942 100644 (file)
@@ -365,7 +365,6 @@ int main(int argc, char *argv[])
        uid_t uid = 0;
        gid_t gid = 0;
        int keepcaps = 0;
-       struct ul_env_list *envls;
 #ifdef HAVE_LIBSELINUX
        bool selinux = 0;
 #endif
@@ -644,12 +643,15 @@ int main(int argc, char *argv[])
 
        /* Pass environment variables of the target process to the spawned process */
        if (env_fd >= 0) {
-               if ((envls = env_from_fd(env_fd)) == NULL)
+               struct ul_env_list *ls;
+
+               ls = env_from_fd(env_fd);
+               if (!ls && errno)
                        err(EXIT_FAILURE, _("failed to get environment variables"));
                clearenv();
-               if (env_list_setenv(envls) < 0)
+               if (ls && env_list_setenv(ls) < 0)
                        err(EXIT_FAILURE, _("failed to set environment variables"));
-               env_list_free(envls);
+               env_list_free(ls);
                close(env_fd);
        }