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