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