yeahuh / rpms / qemu-kvm

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