|
|
1d442b |
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
|
|
|
1d442b |
Date: Mon, 27 Jan 2020 19:01:32 +0000
|
|
|
1d442b |
Subject: [PATCH] virtiofsd: cap-ng helpers
|
|
|
1d442b |
MIME-Version: 1.0
|
|
|
1d442b |
Content-Type: text/plain; charset=UTF-8
|
|
|
1d442b |
Content-Transfer-Encoding: 8bit
|
|
|
1d442b |
|
|
|
1d442b |
libcap-ng reads /proc during capng_get_caps_process, and virtiofsd's
|
|
|
1d442b |
sandboxing doesn't have /proc mounted; thus we have to do the
|
|
|
1d442b |
caps read before we sandbox it and save/restore the state.
|
|
|
1d442b |
|
|
|
1d442b |
Signed-off-by: Dr. David Alan Gilbert <dgilbert@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 2405f3c0d19eb4d516a88aa4e5c54e5f9c6bbea3)
|
|
|
1d442b |
---
|
|
|
1d442b |
Makefile | 4 +-
|
|
|
1d442b |
tools/virtiofsd/passthrough_ll.c | 72 ++++++++++++++++++++++++++++++++
|
|
|
1d442b |
2 files changed, 74 insertions(+), 2 deletions(-)
|
|
|
1d442b |
|
|
|
1d442b |
diff --git a/Makefile b/Makefile
|
|
|
1d442b |
index 9a17e34603..14793cad11 100644
|
|
|
1d442b |
--- a/Makefile
|
|
|
1d442b |
+++ b/Makefile
|
|
|
1d442b |
@@ -330,7 +330,7 @@ endif
|
|
|
1d442b |
endif
|
|
|
1d442b |
endif
|
|
|
1d442b |
|
|
|
1d442b |
-ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP),yy)
|
|
|
1d442b |
+ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP)$(CONFIG_LIBCAP_NG),yyy)
|
|
|
1d442b |
HELPERS-y += virtiofsd$(EXESUF)
|
|
|
1d442b |
vhost-user-json-y += tools/virtiofsd/50-qemu-virtiofsd.json
|
|
|
1d442b |
endif
|
|
|
1d442b |
@@ -681,7 +681,7 @@ rdmacm-mux$(EXESUF): $(rdmacm-mux-obj-y) $(COMMON_LDADDS)
|
|
|
1d442b |
$(call LINK, $^)
|
|
|
1d442b |
|
|
|
1d442b |
# relies on Linux-specific syscalls
|
|
|
1d442b |
-ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP),yy)
|
|
|
1d442b |
+ifeq ($(CONFIG_LINUX)$(CONFIG_SECCOMP)$(CONFIG_LIBCAP_NG),yyy)
|
|
|
1d442b |
virtiofsd$(EXESUF): $(virtiofsd-obj-y) libvhost-user.a $(COMMON_LDADDS)
|
|
|
1d442b |
$(call LINK, $^)
|
|
|
1d442b |
endif
|
|
|
1d442b |
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
|
|
|
1d442b |
index bd8925bd83..97e7c75667 100644
|
|
|
1d442b |
--- a/tools/virtiofsd/passthrough_ll.c
|
|
|
1d442b |
+++ b/tools/virtiofsd/passthrough_ll.c
|
|
|
1d442b |
@@ -39,6 +39,7 @@
|
|
|
1d442b |
#include "fuse_virtio.h"
|
|
|
1d442b |
#include "fuse_lowlevel.h"
|
|
|
1d442b |
#include <assert.h>
|
|
|
1d442b |
+#include <cap-ng.h>
|
|
|
1d442b |
#include <dirent.h>
|
|
|
1d442b |
#include <errno.h>
|
|
|
1d442b |
#include <inttypes.h>
|
|
|
1d442b |
@@ -139,6 +140,13 @@ static const struct fuse_opt lo_opts[] = {
|
|
|
1d442b |
|
|
|
1d442b |
static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n);
|
|
|
1d442b |
|
|
|
1d442b |
+static struct {
|
|
|
1d442b |
+ pthread_mutex_t mutex;
|
|
|
1d442b |
+ void *saved;
|
|
|
1d442b |
+} cap;
|
|
|
1d442b |
+/* That we loaded cap-ng in the current thread from the saved */
|
|
|
1d442b |
+static __thread bool cap_loaded = 0;
|
|
|
1d442b |
+
|
|
|
1d442b |
static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st);
|
|
|
1d442b |
|
|
|
1d442b |
static int is_dot_or_dotdot(const char *name)
|
|
|
1d442b |
@@ -162,6 +170,37 @@ static struct lo_data *lo_data(fuse_req_t req)
|
|
|
1d442b |
return (struct lo_data *)fuse_req_userdata(req);
|
|
|
1d442b |
}
|
|
|
1d442b |
|
|
|
1d442b |
+/*
|
|
|
1d442b |
+ * Load capng's state from our saved state if the current thread
|
|
|
1d442b |
+ * hadn't previously been loaded.
|
|
|
1d442b |
+ * returns 0 on success
|
|
|
1d442b |
+ */
|
|
|
1d442b |
+static int load_capng(void)
|
|
|
1d442b |
+{
|
|
|
1d442b |
+ if (!cap_loaded) {
|
|
|
1d442b |
+ pthread_mutex_lock(&cap.mutex);
|
|
|
1d442b |
+ capng_restore_state(&cap.saved);
|
|
|
1d442b |
+ /*
|
|
|
1d442b |
+ * restore_state free's the saved copy
|
|
|
1d442b |
+ * so make another.
|
|
|
1d442b |
+ */
|
|
|
1d442b |
+ cap.saved = capng_save_state();
|
|
|
1d442b |
+ if (!cap.saved) {
|
|
|
1d442b |
+ fuse_log(FUSE_LOG_ERR, "capng_save_state (thread)\n");
|
|
|
1d442b |
+ return -EINVAL;
|
|
|
1d442b |
+ }
|
|
|
1d442b |
+ pthread_mutex_unlock(&cap.mutex);
|
|
|
1d442b |
+
|
|
|
1d442b |
+ /*
|
|
|
1d442b |
+ * We want to use the loaded state for our pid,
|
|
|
1d442b |
+ * not the original
|
|
|
1d442b |
+ */
|
|
|
1d442b |
+ capng_setpid(syscall(SYS_gettid));
|
|
|
1d442b |
+ cap_loaded = true;
|
|
|
1d442b |
+ }
|
|
|
1d442b |
+ return 0;
|
|
|
1d442b |
+}
|
|
|
1d442b |
+
|
|
|
1d442b |
static void lo_map_init(struct lo_map *map)
|
|
|
1d442b |
{
|
|
|
1d442b |
map->elems = NULL;
|
|
|
1d442b |
@@ -2023,6 +2062,35 @@ static void setup_namespaces(struct lo_data *lo, struct fuse_session *se)
|
|
|
1d442b |
}
|
|
|
1d442b |
}
|
|
|
1d442b |
|
|
|
1d442b |
+/*
|
|
|
1d442b |
+ * Capture the capability state, we'll need to restore this for individual
|
|
|
1d442b |
+ * threads later; see load_capng.
|
|
|
1d442b |
+ */
|
|
|
1d442b |
+static void setup_capng(void)
|
|
|
1d442b |
+{
|
|
|
1d442b |
+ /* Note this accesses /proc so has to happen before the sandbox */
|
|
|
1d442b |
+ if (capng_get_caps_process()) {
|
|
|
1d442b |
+ fuse_log(FUSE_LOG_ERR, "capng_get_caps_process\n");
|
|
|
1d442b |
+ exit(1);
|
|
|
1d442b |
+ }
|
|
|
1d442b |
+ pthread_mutex_init(&cap.mutex, NULL);
|
|
|
1d442b |
+ pthread_mutex_lock(&cap.mutex);
|
|
|
1d442b |
+ cap.saved = capng_save_state();
|
|
|
1d442b |
+ if (!cap.saved) {
|
|
|
1d442b |
+ fuse_log(FUSE_LOG_ERR, "capng_save_state\n");
|
|
|
1d442b |
+ exit(1);
|
|
|
1d442b |
+ }
|
|
|
1d442b |
+ pthread_mutex_unlock(&cap.mutex);
|
|
|
1d442b |
+}
|
|
|
1d442b |
+
|
|
|
1d442b |
+static void cleanup_capng(void)
|
|
|
1d442b |
+{
|
|
|
1d442b |
+ free(cap.saved);
|
|
|
1d442b |
+ cap.saved = NULL;
|
|
|
1d442b |
+ pthread_mutex_destroy(&cap.mutex);
|
|
|
1d442b |
+}
|
|
|
1d442b |
+
|
|
|
1d442b |
+
|
|
|
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 |
@@ -2216,12 +2284,16 @@ int main(int argc, char *argv[])
|
|
|
1d442b |
|
|
|
1d442b |
fuse_daemonize(opts.foreground);
|
|
|
1d442b |
|
|
|
1d442b |
+ /* Must be before sandbox since it wants /proc */
|
|
|
1d442b |
+ setup_capng();
|
|
|
1d442b |
+
|
|
|
1d442b |
setup_sandbox(&lo, se);
|
|
|
1d442b |
|
|
|
1d442b |
/* Block until ctrl+c or fusermount -u */
|
|
|
1d442b |
ret = virtio_loop(se);
|
|
|
1d442b |
|
|
|
1d442b |
fuse_session_unmount(se);
|
|
|
1d442b |
+ cleanup_capng();
|
|
|
1d442b |
err_out3:
|
|
|
1d442b |
fuse_remove_signal_handlers(se);
|
|
|
1d442b |
err_out2:
|