cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From f62613d8058bcb60b26727d980a37537103b0033 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:01:32 +0100
ddf19c
Subject: [PATCH 061/116] virtiofsd: cap-ng helpers
ddf19c
MIME-Version: 1.0
ddf19c
Content-Type: text/plain; charset=UTF-8
ddf19c
Content-Transfer-Encoding: 8bit
ddf19c
ddf19c
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Message-id: <20200127190227.40942-58-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93512
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 057/112] virtiofsd: cap-ng helpers
ddf19c
Bugzilla: 1694164
ddf19c
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ddf19c
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
ddf19c
libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's
ddf19c
sandboxing doesn't have /proc mounted; thus we have to do the
ddf19c
caps read before we sandbox it and save/restore the state.
ddf19c
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit 2405f3c0d19eb4d516a88aa4e5c54e5f9c6bbea3)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 Makefile                         |  4 +--
ddf19c
 tools/virtiofsd/passthrough_ll.c | 72 ++++++++++++++++++++++++++++++++++++++++
ddf19c
 2 files changed, 74 insertions(+), 2 deletions(-)
ddf19c
ddf19c
diff --git a/Makefile b/Makefile
ddf19c
index 6879a06..ff05c30 100644
ddf19c
--- a/Makefile
ddf19c
+++ b/Makefile
ddf19c
@@ -330,7 +330,7 @@ endif
ddf19c
 endif
ddf19c
 endif
ddf19c
 
ddf19c
-ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP),yy)
ddf19c
+ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP)$(CONFIG_LIBCAP_NG),yyy)
ddf19c
 HELPERS-y += virtiofsd$(EXESUF)
ddf19c
 vhost-user-json-y += tools/virtiofsd/50-qemu-virtiofsd.json
ddf19c
 endif
ddf19c
@@ -682,7 +682,7 @@ rdmacm-mux$(EXESUF): $(rdmacm-mux-obj-y) $(COMMON_LDADDS)
ddf19c
 	$(call LINK, $^)
ddf19c
 
ddf19c
 # relies on Linux-specific syscalls
ddf19c
-ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP),yy)
ddf19c
+ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP)$(CONFIG_LIBCAP_NG),yyy)
ddf19c
 virtiofsd$(EXESUF): $(virtiofsd-obj-y) libvhost-user.a $(COMMON_LDADDS)
ddf19c
 	$(call LINK, $^)
ddf19c
 endif
ddf19c
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
ddf19c
index bd8925b..97e7c75 100644
ddf19c
--- a/tools/virtiofsd/passthrough_ll.c
ddf19c
+++ b/tools/virtiofsd/passthrough_ll.c
ddf19c
@@ -39,6 +39,7 @@
ddf19c
 #include "fuse_virtio.h"
ddf19c
 #include "fuse_lowlevel.h"
ddf19c
 #include <assert.h>
ddf19c
+#include <cap-ng.h>
ddf19c
 #include <dirent.h>
ddf19c
 #include <errno.h>
ddf19c
 #include <inttypes.h>
ddf19c
@@ -139,6 +140,13 @@ static const struct fuse_opt lo_opts[] = {
ddf19c
 
ddf19c
 static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n);
ddf19c
 
ddf19c
+static struct {
ddf19c
+    pthread_mutex_t mutex;
ddf19c
+    void *saved;
ddf19c
+} cap;
ddf19c
+/* That we loaded cap-ng in the current thread from the saved */
ddf19c
+static __thread bool cap_loaded = 0;
ddf19c
+
ddf19c
 static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
ddf19c
 
ddf19c
 static int is_dot_or_dotdot(const char *name)
ddf19c
@@ -162,6 +170,37 @@ static struct lo_data *lo_data(fuse_req_t req)
ddf19c
     return (struct lo_data *)fuse_req_userdata(req);
ddf19c
 }
ddf19c
 
ddf19c
+/*
ddf19c
+ * Load capng's state from our saved state if the current thread
ddf19c
+ * hadn't previously been loaded.
ddf19c
+ * returns 0 on success
ddf19c
+ */
ddf19c
+static int load_capng(void)
ddf19c
+{
ddf19c
+    if (!cap_loaded) {
ddf19c
+        pthread_mutex_lock(&cap.mutex);
ddf19c
+        capng_restore_state(&cap.saved);
ddf19c
+        /*
ddf19c
+         * restore_state free's the saved copy
ddf19c
+         * so make another.
ddf19c
+         */
ddf19c
+        cap.saved = capng_save_state();
ddf19c
+        if (!cap.saved) {
ddf19c
+            fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
ddf19c
+            return -EINVAL;
ddf19c
+        }
ddf19c
+        pthread_mutex_unlock(&cap.mutex);
ddf19c
+
ddf19c
+        /*
ddf19c
+         * We want to use the loaded state for our pid,
ddf19c
+         * not the original
ddf19c
+         */
ddf19c
+        capng_setpid(syscall(SYS_gettid));
ddf19c
+        cap_loaded = true;
ddf19c
+    }
ddf19c
+    return 0;
ddf19c
+}
ddf19c
+
ddf19c
 static void lo_map_init(struct lo_map *map)
ddf19c
 {
ddf19c
     map->elems = NULL;
ddf19c
@@ -2024,6 +2063,35 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
ddf19c
 }
ddf19c
 
ddf19c
 /*
ddf19c
+ * Capture the capability state, we'll need to restore this for individual
ddf19c
+ * threads later; see load_capng.
ddf19c
+ */
ddf19c
+static void setup_capng(void)
ddf19c
+{
ddf19c
+    /* Note this accesses /proc so has to happen before the sandbox */
ddf19c
+    if (capng_get_caps_process()) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
ddf19c
+        exit(1);
ddf19c
+    }
ddf19c
+    pthread_mutex_init(&cap.mutex, NULL);
ddf19c
+    pthread_mutex_lock(&cap.mutex);
ddf19c
+    cap.saved = capng_save_state();
ddf19c
+    if (!cap.saved) {
ddf19c
+        fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
ddf19c
+        exit(1);
ddf19c
+    }
ddf19c
+    pthread_mutex_unlock(&cap.mutex);
ddf19c
+}
ddf19c
+
ddf19c
+static void cleanup_capng(void)
ddf19c
+{
ddf19c
+    free(cap.saved);
ddf19c
+    cap.saved = NULL;
ddf19c
+    pthread_mutex_destroy(&cap.mutex);
ddf19c
+}
ddf19c
+
ddf19c
+
ddf19c
+/*
ddf19c
  * Make the source directory our root so symlinks cannot escape and no other
ddf19c
  * files are accessible.  Assumes unshare(CLONE_NEWNS) was already called.
ddf19c
  */
ddf19c
@@ -2216,12 +2284,16 @@ int main(int argc, char *argv[])
ddf19c
 
ddf19c
     fuse_daemonize(opts.foreground);
ddf19c
 
ddf19c
+    /* Must be before sandbox since it wants /proc */
ddf19c
+    setup_capng();
ddf19c
+
ddf19c
     setup_sandbox(&lo, se);
ddf19c
 
ddf19c
     /* Block until ctrl+c or fusermount -u */
ddf19c
     ret = virtio_loop(se);
ddf19c
 
ddf19c
     fuse_session_unmount(se);
ddf19c
+    cleanup_capng();
ddf19c
 err_out3:
ddf19c
     fuse_remove_signal_handlers(se);
ddf19c
 err_out2:
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c