thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-virtiofsd-passthrough_ll-create-new-files-in-caller-.patch

22c213
From af14ef1dba9356e566c9c7531b8fd23361c2b16d Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:12 +0100
22c213
Subject: [PATCH 041/116] virtiofsd: passthrough_ll: create new files in
22c213
 caller's context
22c213
MIME-Version: 1.0
22c213
Content-Type: text/plain; charset=UTF-8
22c213
Content-Transfer-Encoding: 8bit
22c213
22c213
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
Message-id: <20200127190227.40942-38-dgilbert@redhat.com>
22c213
Patchwork-id: 93488
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 037/112] virtiofsd: passthrough_ll: create new files in caller's context
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: Vivek Goyal <vgoyal@redhat.com>
22c213
22c213
We need to create files in the caller's context. Otherwise after
22c213
creating a file, the caller might not be able to do file operations on
22c213
that file.
22c213
22c213
Changed effective uid/gid to caller's uid/gid, create file and then
22c213
switch back to uid/gid 0.
22c213
22c213
Use syscall(setresuid, ...) otherwise glibc does some magic to change EUID
22c213
in all threads, which is not what we want.
22c213
22c213
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
22c213
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 929cfb7a9a1b101cdfc9ac19807ecab4c81a13e4)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/passthrough_ll.c | 96 +++++++++++++++++++++++++++++++++++++---
22c213
 1 file changed, 91 insertions(+), 5 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
22c213
index cd27c09..5e06179 100644
22c213
--- a/tools/virtiofsd/passthrough_ll.c
22c213
+++ b/tools/virtiofsd/passthrough_ll.c
22c213
@@ -50,6 +50,7 @@
22c213
 #include <stdlib.h>
22c213
 #include <string.h>
22c213
 #include <sys/file.h>
22c213
+#include <sys/syscall.h>
22c213
 #include <sys/xattr.h>
22c213
 #include <unistd.h>
22c213
 
22c213
@@ -83,6 +84,11 @@ struct lo_inode {
22c213
     uint64_t refcount; /* protected by lo->mutex */
22c213
 };
22c213
 
22c213
+struct lo_cred {
22c213
+    uid_t euid;
22c213
+    gid_t egid;
22c213
+};
22c213
+
22c213
 enum {
22c213
     CACHE_NEVER,
22c213
     CACHE_NORMAL,
22c213
@@ -383,6 +389,69 @@ static void lo_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
22c213
     }
22c213
 }
22c213
 
22c213
+/*
22c213
+ * On some archs, setres*id is limited to 2^16 but they
22c213
+ * provide setres*id32 variants that allow 2^32.
22c213
+ * Others just let setres*id do 2^32 anyway.
22c213
+ */
22c213
+#ifdef SYS_setresgid32
22c213
+#define OURSYS_setresgid SYS_setresgid32
22c213
+#else
22c213
+#define OURSYS_setresgid SYS_setresgid
22c213
+#endif
22c213
+
22c213
+#ifdef SYS_setresuid32
22c213
+#define OURSYS_setresuid SYS_setresuid32
22c213
+#else
22c213
+#define OURSYS_setresuid SYS_setresuid
22c213
+#endif
22c213
+
22c213
+/*
22c213
+ * Change to uid/gid of caller so that file is created with
22c213
+ * ownership of caller.
22c213
+ * TODO: What about selinux context?
22c213
+ */
22c213
+static int lo_change_cred(fuse_req_t req, struct lo_cred *old)
22c213
+{
22c213
+    int res;
22c213
+
22c213
+    old->euid = geteuid();
22c213
+    old->egid = getegid();
22c213
+
22c213
+    res = syscall(OURSYS_setresgid, -1, fuse_req_ctx(req)->gid, -1);
22c213
+    if (res == -1) {
22c213
+        return errno;
22c213
+    }
22c213
+
22c213
+    res = syscall(OURSYS_setresuid, -1, fuse_req_ctx(req)->uid, -1);
22c213
+    if (res == -1) {
22c213
+        int errno_save = errno;
22c213
+
22c213
+        syscall(OURSYS_setresgid, -1, old->egid, -1);
22c213
+        return errno_save;
22c213
+    }
22c213
+
22c213
+    return 0;
22c213
+}
22c213
+
22c213
+/* Regain Privileges */
22c213
+static void lo_restore_cred(struct lo_cred *old)
22c213
+{
22c213
+    int res;
22c213
+
22c213
+    res = syscall(OURSYS_setresuid, -1, old->euid, -1);
22c213
+    if (res == -1) {
22c213
+        fuse_log(FUSE_LOG_ERR, "seteuid(%u): %m\n", old->euid);
22c213
+        exit(1);
22c213
+    }
22c213
+
22c213
+    res = syscall(OURSYS_setresgid, -1, old->egid, -1);
22c213
+    if (res == -1) {
22c213
+        fuse_log(FUSE_LOG_ERR, "setegid(%u): %m\n", old->egid);
22c213
+        exit(1);
22c213
+    }
22c213
+}
22c213
+
22c213
 static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
22c213
                              const char *name, mode_t mode, dev_t rdev,
22c213
                              const char *link)
22c213
@@ -391,12 +460,21 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino_t parent,
22c213
     int saverr;
22c213
     struct lo_inode *dir = lo_inode(req, parent);
22c213
     struct fuse_entry_param e;
22c213
+    struct lo_cred old = {};
22c213
 
22c213
     saverr = ENOMEM;
22c213
 
22c213
+    saverr = lo_change_cred(req, &old;;
22c213
+    if (saverr) {
22c213
+        goto out;
22c213
+    }
22c213
+
22c213
     res = mknod_wrapper(dir->fd, name, link, mode, rdev);
22c213
 
22c213
     saverr = errno;
22c213
+
22c213
+    lo_restore_cred(&old;;
22c213
+
22c213
     if (res == -1) {
22c213
         goto out;
22c213
     }
22c213
@@ -794,26 +872,34 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
22c213
     struct lo_data *lo = lo_data(req);
22c213
     struct fuse_entry_param e;
22c213
     int err;
22c213
+    struct lo_cred old = {};
22c213
 
22c213
     if (lo_debug(req)) {
22c213
         fuse_log(FUSE_LOG_DEBUG, "lo_create(parent=%" PRIu64 ", name=%s)\n",
22c213
                  parent, name);
22c213
     }
22c213
 
22c213
+    err = lo_change_cred(req, &old;;
22c213
+    if (err) {
22c213
+        goto out;
22c213
+    }
22c213
+
22c213
     fd = openat(lo_fd(req, parent), name, (fi->flags | O_CREAT) & ~O_NOFOLLOW,
22c213
                 mode);
22c213
-    if (fd == -1) {
22c213
-        return (void)fuse_reply_err(req, errno);
22c213
-    }
22c213
+    err = fd == -1 ? errno : 0;
22c213
+    lo_restore_cred(&old;;
22c213
 
22c213
-    fi->fh = fd;
22c213
+    if (!err) {
22c213
+        fi->fh = fd;
22c213
+        err = lo_do_lookup(req, parent, name, &e);
22c213
+    }
22c213
     if (lo->cache == CACHE_NEVER) {
22c213
         fi->direct_io = 1;
22c213
     } else if (lo->cache == CACHE_ALWAYS) {
22c213
         fi->keep_cache = 1;
22c213
     }
22c213
 
22c213
-    err = lo_do_lookup(req, parent, name, &e);
22c213
+out:
22c213
     if (err) {
22c213
         fuse_reply_err(req, err);
22c213
     } else {
22c213
-- 
22c213
1.8.3.1
22c213