[libvirt] [PATCH 15/17] virt-login-shell: fully reset container environment

Daniel P. Berrange berrange at redhat.com
Thu Apr 14 15:22:18 UTC 2016


The virt-login-shell environment will be initialized with
an arbitrary number of environment variables determined
by the SSH daemon and PAM configuration. Most of these are
not relevant inside the container, and at best they are
noise and at worst they'll break apps. For example if
XDG_RUNTIME_DIR is leaked to the container, it'll break
any apps using it, since  the directory it points to is
only visible to the host OS filesystem, not the container
FS.

Use clearenv() to blank out everything and then set known
good values for PATH, SHELL, USER, LOGNAME and TERM. Everything
else is left up to the login shell to initialize.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 tools/virt-login-shell.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/tools/virt-login-shell.c b/tools/virt-login-shell.c
index 3f2e3d0..6e8056d 100644
--- a/tools/virt-login-shell.c
+++ b/tools/virt-login-shell.c
@@ -219,6 +219,7 @@ main(int argc, char **argv)
     size_t i;
     const char *cmdstr = NULL;
     char *tmp;
+    char *term = NULL;
     virErrorPtr saved_err = NULL;
 
     struct option opt[] = {
@@ -231,8 +232,6 @@ main(int argc, char **argv)
         return EXIT_CANCELED;
     }
 
-    setenv("PATH", "/bin:/usr/bin", 1);
-
     virSetErrorFunc(NULL, NULL);
     virSetErrorLogPriorityFunc(NULL);
 
@@ -378,6 +377,9 @@ main(int argc, char **argv)
         goto cleanup;
     shargv[0][0] = '-';
 
+    if (VIR_STRDUP(term, getenv("TERM")) < 0)
+        goto cleanup;
+
     /* A fork is required to create new process in correct pid namespace.  */
     if ((cpid = virFork()) < 0)
         goto cleanup;
@@ -389,6 +391,15 @@ main(int argc, char **argv)
             tmpfd = i;
             VIR_MASS_CLOSE(tmpfd);
         }
+
+        clearenv();
+        setenv("PATH", "/bin:/usr/bin", 1);
+        setenv("SHELL", shcmd, 1);
+        setenv("USER", name, 1);
+        setenv("LOGNAME", name, 1);
+        if (term)
+            setenv("TERM", term, 1);
+
         if (execv(shcmd, (char *const*) shargv) < 0) {
             virReportSystemError(errno, _("Unable to exec shell %s"),
                                  shcmd);
@@ -413,6 +424,7 @@ main(int argc, char **argv)
         virConnectClose(conn);
     virStringFreeList(shargv);
     VIR_FREE(shcmd);
+    VIR_FREE(term);
     VIR_FREE(name);
     VIR_FREE(homedir);
     VIR_FREE(seclabel);
-- 
2.5.5




More information about the libvir-list mailing list