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