yeahuh / rpms / qemu-kvm

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