render / rpms / qemu

Forked from rpms/qemu 9 months ago
Clone

Blame 0043-virtiofsd-passthrough_ll-create-new-files-in-caller-.patch

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