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