77c23f
From 852a0a22d674b0594aecf0912a0885d197f34978 Mon Sep 17 00:00:00 2001
77c23f
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
77c23f
Date: Tue, 5 May 2020 16:35:57 +0100
77c23f
Subject: [PATCH 6/9] virtiofsd: jail lo->proc_self_fd
77c23f
77c23f
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
77c23f
Message-id: <20200505163600.22956-5-dgilbert@redhat.com>
77c23f
Patchwork-id: 96275
77c23f
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 4/7] virtiofsd: jail lo->proc_self_fd
77c23f
Bugzilla: 1817445
77c23f
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
77c23f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
77c23f
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
77c23f
77c23f
From: Miklos Szeredi <mszeredi@redhat.com>
77c23f
77c23f
While it's not possible to escape the proc filesystem through
77c23f
lo->proc_self_fd, it is possible to escape to the root of the proc
77c23f
filesystem itself through "../..".
77c23f
77c23f
Use a temporary mount for opening lo->proc_self_fd, that has it's root at
77c23f
/proc/self/fd/, preventing access to the ancestor directories.
77c23f
77c23f
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
77c23f
Message-Id: <20200429124733.22488-1-mszeredi@redhat.com>
77c23f
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
77c23f
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
77c23f
(cherry picked from commit 397ae982f4df46e7d4b2625c431062c9146f3b83)
77c23f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
77c23f
---
77c23f
 tools/virtiofsd/passthrough_ll.c | 27 +++++++++++++++++++++++++--
77c23f
 1 file changed, 25 insertions(+), 2 deletions(-)
77c23f
77c23f
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
77c23f
index 184ad0f..73d8405 100644
77c23f
--- a/tools/virtiofsd/passthrough_ll.c
77c23f
+++ b/tools/virtiofsd/passthrough_ll.c
77c23f
@@ -2540,6 +2540,8 @@ static void print_capabilities(void)
77c23f
 static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
77c23f
 {
77c23f
     pid_t child;
77c23f
+    char template[] = "virtiofsd-XXXXXX";
77c23f
+    char *tmpdir;
77c23f
 
77c23f
     /*
77c23f
      * Create a new pid namespace for *child* processes.  We'll have to
77c23f
@@ -2601,12 +2603,33 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
77c23f
         exit(1);
77c23f
     }
77c23f
 
77c23f
+    tmpdir = mkdtemp(template);
77c23f
+    if (!tmpdir) {
77c23f
+        fuse_log(FUSE_LOG_ERR, "tmpdir(%s): %m\n", template);
77c23f
+        exit(1);
77c23f
+    }
77c23f
+
77c23f
+    if (mount("/proc/self/fd", tmpdir, NULL, MS_BIND, NULL) < 0) {
77c23f
+        fuse_log(FUSE_LOG_ERR, "mount(/proc/self/fd, %s, MS_BIND): %m\n",
77c23f
+                 tmpdir);
77c23f
+        exit(1);
77c23f
+    }
77c23f
+
77c23f
     /* Now we can get our /proc/self/fd directory file descriptor */
77c23f
-    lo->proc_self_fd = open("/proc/self/fd", O_PATH);
77c23f
+    lo->proc_self_fd = open(tmpdir, O_PATH);
77c23f
     if (lo->proc_self_fd == -1) {
77c23f
-        fuse_log(FUSE_LOG_ERR, "open(/proc/self/fd, O_PATH): %m\n");
77c23f
+        fuse_log(FUSE_LOG_ERR, "open(%s, O_PATH): %m\n", tmpdir);
77c23f
         exit(1);
77c23f
     }
77c23f
+
77c23f
+    if (umount2(tmpdir, MNT_DETACH) < 0) {
77c23f
+        fuse_log(FUSE_LOG_ERR, "umount2(%s, MNT_DETACH): %m\n", tmpdir);
77c23f
+        exit(1);
77c23f
+    }
77c23f
+
77c23f
+    if (rmdir(tmpdir) < 0) {
77c23f
+        fuse_log(FUSE_LOG_ERR, "rmdir(%s): %m\n", tmpdir);
77c23f
+    }
77c23f
 }
77c23f
 
77c23f
 /*
77c23f
-- 
77c23f
1.8.3.1
77c23f