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