902636
From a7a87a751a9893830d031a957a751b7622b71fb2 Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Mon, 27 Jan 2020 19:01:29 +0100
902636
Subject: [PATCH 058/116] virtiofsd: move to a new pid namespace
902636
MIME-Version: 1.0
902636
Content-Type: text/plain; charset=UTF-8
902636
Content-Transfer-Encoding: 8bit
902636
902636
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Message-id: <20200127190227.40942-55-dgilbert@redhat.com>
902636
Patchwork-id: 93510
902636
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 054/112] virtiofsd: move to a new pid namespace
902636
Bugzilla: 1694164
902636
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
902636
From: Stefan Hajnoczi <stefanha@redhat.com>
902636
902636
virtiofsd needs access to /proc/self/fd.  Let's move to a new pid
902636
namespace so that a compromised process cannot see another other
902636
processes running on the system.
902636
902636
One wrinkle in this approach: unshare(CLONE_NEWPID) affects *child*
902636
processes and not the current process.  Therefore we need to fork the
902636
pid 1 process that will actually run virtiofsd and leave a parent in
902636
waitpid(2).  This is not the same thing as daemonization and parent
902636
processes should not notice a difference.
902636
902636
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit 8e1d4ef231d8327be219f7aea7aa15d181375bbc)
902636
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
902636
---
902636
 tools/virtiofsd/passthrough_ll.c | 134 +++++++++++++++++++++++++--------------
902636
 1 file changed, 86 insertions(+), 48 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index 27ab328..0947d14 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -51,7 +51,10 @@
902636
 #include <string.h>
902636
 #include <sys/file.h>
902636
 #include <sys/mount.h>
902636
+#include <sys/prctl.h>
902636
 #include <sys/syscall.h>
902636
+#include <sys/types.h>
902636
+#include <sys/wait.h>
902636
 #include <sys/xattr.h>
902636
 #include <unistd.h>
902636
 
902636
@@ -1945,24 +1948,95 @@ static void print_capabilities(void)
902636
 }
902636
 
902636
 /*
902636
- * Called after our UNIX domain sockets have been created, now we can move to
902636
- * an empty network namespace to prevent TCP/IP and other network activity in
902636
- * case this process is compromised.
902636
+ * Move to a new mount, net, and pid namespaces to isolate this process.
902636
  */
902636
-static void setup_net_namespace(void)
902636
+static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
902636
 {
902636
-    if (unshare(CLONE_NEWNET) != 0) {
902636
-        fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWNET): %m\n");
902636
+    pid_t child;
902636
+
902636
+    /*
902636
+     * Create a new pid namespace for *child* processes.  We'll have to
902636
+     * fork in order to enter the new pid namespace.  A new mount namespace
902636
+     * is also needed so that we can remount /proc for the new pid
902636
+     * namespace.
902636
+     *
902636
+     * Our UNIX domain sockets have been created.  Now we can move to
902636
+     * an empty network namespace to prevent TCP/IP and other network
902636
+     * activity in case this process is compromised.
902636
+     */
902636
+    if (unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET) != 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWPID | CLONE_NEWNS): %m\n");
902636
+        exit(1);
902636
+    }
902636
+
902636
+    child = fork();
902636
+    if (child < 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "fork() failed: %m\n");
902636
+        exit(1);
902636
+    }
902636
+    if (child > 0) {
902636
+        pid_t waited;
902636
+        int wstatus;
902636
+
902636
+        /* The parent waits for the child */
902636
+        do {
902636
+            waited = waitpid(child, &wstatus, 0);
902636
+        } while (waited < 0 && errno == EINTR && !se->exited);
902636
+
902636
+        /* We were terminated by a signal, see fuse_signals.c */
902636
+        if (se->exited) {
902636
+            exit(0);
902636
+        }
902636
+
902636
+        if (WIFEXITED(wstatus)) {
902636
+            exit(WEXITSTATUS(wstatus));
902636
+        }
902636
+
902636
+        exit(1);
902636
+    }
902636
+
902636
+    /* Send us SIGTERM when the parent thread terminates, see prctl(2) */
902636
+    prctl(PR_SET_PDEATHSIG, SIGTERM);
902636
+
902636
+    /*
902636
+     * If the mounts have shared propagation then we want to opt out so our
902636
+     * mount changes don't affect the parent mount namespace.
902636
+     */
902636
+    if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_SLAVE): %m\n");
902636
+        exit(1);
902636
+    }
902636
+
902636
+    /* The child must remount /proc to use the new pid namespace */
902636
+    if (mount("proc", "/proc", "proc",
902636
+              MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "mount(/proc): %m\n");
902636
+        exit(1);
902636
+    }
902636
+
902636
+    /* Now we can get our /proc/self/fd directory file descriptor */
902636
+    lo->proc_self_fd = open("/proc/self/fd", O_PATH);
902636
+    if (lo->proc_self_fd == -1) {
902636
+        fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
902636
         exit(1);
902636
     }
902636
 }
902636
 
902636
-/* This magic is based on lxc's lxc_pivot_root() */
902636
-static void setup_pivot_root(const char *source)
902636
+/*
902636
+ * Make the source directory our root so symlinks cannot escape and no other
902636
+ * files are accessible.  Assumes unshare(CLONE_NEWNS) was already called.
902636
+ */
902636
+static void setup_mounts(const char *source)
902636
 {
902636
     int oldroot;
902636
     int newroot;
902636
 
902636
+    if (mount(source, source, NULL, MS_BIND, NULL) < 0) {
902636
+        fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
902636
+        exit(1);
902636
+    }
902636
+
902636
+    /* This magic is based on lxc's lxc_pivot_root() */
902636
     oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
902636
     if (oldroot < 0) {
902636
         fuse_log(FUSE_LOG_ERR, "open(/): %m\n");
902636
@@ -2009,47 +2083,14 @@ static void setup_pivot_root(const char *source)
902636
     close(oldroot);
902636
 }
902636
 
902636
-static void setup_proc_self_fd(struct lo_data *lo)
902636
-{
902636
-    lo->proc_self_fd = open("/proc/self/fd", O_PATH);
902636
-    if (lo->proc_self_fd == -1) {
902636
-        fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
902636
-        exit(1);
902636
-    }
902636
-}
902636
-
902636
-/*
902636
- * Make the source directory our root so symlinks cannot escape and no other
902636
- * files are accessible.
902636
- */
902636
-static void setup_mount_namespace(const char *source)
902636
-{
902636
-    if (unshare(CLONE_NEWNS) != 0) {
902636
-        fuse_log(FUSE_LOG_ERR, "unshare(CLONE_NEWNS): %m\n");
902636
-        exit(1);
902636
-    }
902636
-
902636
-    if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0) {
902636
-        fuse_log(FUSE_LOG_ERR, "mount(/, MS_REC|MS_PRIVATE): %m\n");
902636
-        exit(1);
902636
-    }
902636
-
902636
-    if (mount(source, source, NULL, MS_BIND, NULL) < 0) {
902636
-        fuse_log(FUSE_LOG_ERR, "mount(%s, %s, MS_BIND): %m\n", source, source);
902636
-        exit(1);
902636
-    }
902636
-
902636
-    setup_pivot_root(source);
902636
-}
902636
-
902636
 /*
902636
  * Lock down this process to prevent access to other processes or files outside
902636
  * source directory.  This reduces the impact of arbitrary code execution bugs.
902636
  */
902636
-static void setup_sandbox(struct lo_data *lo)
902636
+static void setup_sandbox(struct lo_data *lo, struct fuse_session *se)
902636
 {
902636
-    setup_net_namespace();
902636
-    setup_mount_namespace(lo->source);
902636
+    setup_namespaces(lo, se);
902636
+    setup_mounts(lo->source);
902636
 }
902636
 
902636
 int main(int argc, char *argv[])
902636
@@ -2173,10 +2214,7 @@ int main(int argc, char *argv[])
902636
 
902636
     fuse_daemonize(opts.foreground);
902636
 
902636
-    /* Must be after daemonize to get the right /proc/self/fd */
902636
-    setup_proc_self_fd(&lo);
902636
-
902636
-    setup_sandbox(&lo);
902636
+    setup_sandbox(&lo, se);
902636
 
902636
     /* Block until ctrl+c or fusermount -u */
902636
     ret = virtio_loop(se);
902636
-- 
902636
1.8.3.1
902636