cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
902636
From 8721796f22a8a61d82974088e542377ee6db209e Mon Sep 17 00:00:00 2001
902636
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
902636
Date: Tue, 3 Mar 2020 18:43:14 +0000
902636
Subject: [PATCH 18/18] virtiofsd: Fix xattr operations
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: <20200303184314.155564-8-dgilbert@redhat.com>
902636
Patchwork-id: 94123
902636
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH 7/7] virtiofsd: Fix xattr operations
902636
Bugzilla: 1797064
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
902636
RH-Acked-by: Ján Tomko <jtomko@redhat.com>
902636
902636
From: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
902636
902636
Current virtiofsd has problems about xattr operations and
902636
they does not work properly for directory/symlink/special file.
902636
902636
The fundamental cause is that virtiofsd uses openat() + f...xattr()
902636
systemcalls for xattr operation but we should not open symlink/special
902636
file in the daemon. Therefore the function is restricted.
902636
902636
Fix this problem by:
902636
 1. during setup of each thread, call unshare(CLONE_FS)
902636
 2. in xattr operations (i.e. lo_getxattr), if inode is not a regular
902636
    file or directory, use fchdir(proc_loot_fd) + ...xattr() +
902636
    fchdir(root.fd) instead of openat() + f...xattr()
902636
902636
    (Note: for a regular file/directory openat() + f...xattr()
902636
     is still used for performance reason)
902636
902636
With this patch, xfstests generic/062 passes on virtiofs.
902636
902636
This fix is suggested by Miklos Szeredi and Stefan Hajnoczi.
902636
The original discussion can be found here:
902636
  https://www.redhat.com/archives/virtio-fs/2019-October/msg00046.html
902636
902636
Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
902636
Message-Id: <20200227055927.24566-3-misono.tomohiro@jp.fujitsu.com>
902636
Acked-by: Vivek Goyal <vgoyal@redhat.com>
902636
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
902636
(cherry picked from commit bdfd66788349acc43cd3f1298718ad491663cfcc)
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
---
902636
 tools/virtiofsd/fuse_virtio.c    |  13 +++++
902636
 tools/virtiofsd/passthrough_ll.c | 105 +++++++++++++++++++++------------------
902636
 tools/virtiofsd/seccomp.c        |   6 +++
902636
 3 files changed, 77 insertions(+), 47 deletions(-)
902636
902636
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
902636
index dd1c605..3b6d16a 100644
902636
--- a/tools/virtiofsd/fuse_virtio.c
902636
+++ b/tools/virtiofsd/fuse_virtio.c
902636
@@ -426,6 +426,8 @@ err:
902636
     return ret;
902636
 }
902636
 
902636
+static __thread bool clone_fs_called;
902636
+
902636
 /* Process one FVRequest in a thread pool */
902636
 static void fv_queue_worker(gpointer data, gpointer user_data)
902636
 {
902636
@@ -441,6 +443,17 @@ static void fv_queue_worker(gpointer data, gpointer user_data)
902636
 
902636
     assert(se->bufsize > sizeof(struct fuse_in_header));
902636
 
902636
+    if (!clone_fs_called) {
902636
+        int ret;
902636
+
902636
+        /* unshare FS for xattr operation */
902636
+        ret = unshare(CLONE_FS);
902636
+        /* should not fail */
902636
+        assert(ret == 0);
902636
+
902636
+        clone_fs_called = true;
902636
+    }
902636
+
902636
     /*
902636
      * An element contains one request and the space to send our response
902636
      * They're spread over multiple descriptors in a scatter/gather set
902636
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
902636
index 50c7273..9cba3f1 100644
902636
--- a/tools/virtiofsd/passthrough_ll.c
902636
+++ b/tools/virtiofsd/passthrough_ll.c
902636
@@ -123,7 +123,7 @@ struct lo_inode {
902636
     pthread_mutex_t plock_mutex;
902636
     GHashTable *posix_locks; /* protected by lo_inode->plock_mutex */
902636
 
902636
-    bool is_symlink;
902636
+    mode_t filetype;
902636
 };
902636
 
902636
 struct lo_cred {
902636
@@ -695,7 +695,7 @@ static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode,
902636
     struct lo_inode *parent;
902636
     char path[PATH_MAX];
902636
 
902636
-    if (inode->is_symlink) {
902636
+    if (S_ISLNK(inode->filetype)) {
902636
         res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH);
902636
         if (res == -1 && errno == EINVAL) {
902636
             /* Sorry, no race free way to set times on symlink. */
902636
@@ -929,7 +929,8 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t parent, const char *name,
902636
             goto out_err;
902636
         }
902636
 
902636
-        inode->is_symlink = S_ISLNK(e->attr.st_mode);
902636
+        /* cache only filetype */
902636
+        inode->filetype = (e->attr.st_mode & S_IFMT);
902636
 
902636
         /*
902636
          * One for the caller and one for nlookup (released in
902636
@@ -1139,7 +1140,7 @@ static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode,
902636
     struct lo_inode *parent;
902636
     char path[PATH_MAX];
902636
 
902636
-    if (inode->is_symlink) {
902636
+    if (S_ISLNK(inode->filetype)) {
902636
         res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH);
902636
         if (res == -1 && (errno == ENOENT || errno == EINVAL)) {
902636
             /* Sorry, no race free way to hard-link a symlink. */
902636
@@ -2193,12 +2194,6 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
902636
     fuse_log(FUSE_LOG_DEBUG, "lo_getxattr(ino=%" PRIu64 ", name=%s size=%zd)\n",
902636
              ino, name, size);
902636
 
902636
-    if (inode->is_symlink) {
902636
-        /* Sorry, no race free way to getxattr on symlink. */
902636
-        saverr = EPERM;
902636
-        goto out;
902636
-    }
902636
-
902636
     if (size) {
902636
         value = malloc(size);
902636
         if (!value) {
902636
@@ -2207,12 +2202,25 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
902636
     }
902636
 
902636
     sprintf(procname, "%i", inode->fd);
902636
-    fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
-    if (fd < 0) {
902636
-        goto out_err;
902636
+    /*
902636
+     * It is not safe to open() non-regular/non-dir files in file server
902636
+     * unless O_PATH is used, so use that method for regular files/dir
902636
+     * only (as it seems giving less performance overhead).
902636
+     * Otherwise, call fchdir() to avoid open().
902636
+     */
902636
+    if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
902636
+        fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
+        if (fd < 0) {
902636
+            goto out_err;
902636
+        }
902636
+        ret = fgetxattr(fd, name, value, size);
902636
+    } else {
902636
+        /* fchdir should not fail here */
902636
+        assert(fchdir(lo->proc_self_fd) == 0);
902636
+        ret = getxattr(procname, name, value, size);
902636
+        assert(fchdir(lo->root.fd) == 0);
902636
     }
902636
 
902636
-    ret = fgetxattr(fd, name, value, size);
902636
     if (ret == -1) {
902636
         goto out_err;
902636
     }
902636
@@ -2266,12 +2274,6 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
902636
     fuse_log(FUSE_LOG_DEBUG, "lo_listxattr(ino=%" PRIu64 ", size=%zd)\n", ino,
902636
              size);
902636
 
902636
-    if (inode->is_symlink) {
902636
-        /* Sorry, no race free way to listxattr on symlink. */
902636
-        saverr = EPERM;
902636
-        goto out;
902636
-    }
902636
-
902636
     if (size) {
902636
         value = malloc(size);
902636
         if (!value) {
902636
@@ -2280,12 +2282,19 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
902636
     }
902636
 
902636
     sprintf(procname, "%i", inode->fd);
902636
-    fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
-    if (fd < 0) {
902636
-        goto out_err;
902636
+    if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
902636
+        fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
+        if (fd < 0) {
902636
+            goto out_err;
902636
+        }
902636
+        ret = flistxattr(fd, value, size);
902636
+    } else {
902636
+        /* fchdir should not fail here */
902636
+        assert(fchdir(lo->proc_self_fd) == 0);
902636
+        ret = listxattr(procname, value, size);
902636
+        assert(fchdir(lo->root.fd) == 0);
902636
     }
902636
 
902636
-    ret = flistxattr(fd, value, size);
902636
     if (ret == -1) {
902636
         goto out_err;
902636
     }
902636
@@ -2339,20 +2348,21 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
902636
     fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=%" PRIu64
902636
              ", name=%s value=%s size=%zd)\n", ino, name, value, size);
902636
 
902636
-    if (inode->is_symlink) {
902636
-        /* Sorry, no race free way to setxattr on symlink. */
902636
-        saverr = EPERM;
902636
-        goto out;
902636
-    }
902636
-
902636
     sprintf(procname, "%i", inode->fd);
902636
-    fd = openat(lo->proc_self_fd, procname, O_RDWR);
902636
-    if (fd < 0) {
902636
-        saverr = errno;
902636
-        goto out;
902636
+    if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
902636
+        fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
+        if (fd < 0) {
902636
+            saverr = errno;
902636
+            goto out;
902636
+        }
902636
+        ret = fsetxattr(fd, name, value, size, flags);
902636
+    } else {
902636
+        /* fchdir should not fail here */
902636
+        assert(fchdir(lo->proc_self_fd) == 0);
902636
+        ret = setxattr(procname, name, value, size, flags);
902636
+        assert(fchdir(lo->root.fd) == 0);
902636
     }
902636
 
902636
-    ret = fsetxattr(fd, name, value, size, flags);
902636
     saverr = ret == -1 ? errno : 0;
902636
 
902636
 out:
902636
@@ -2387,20 +2397,21 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
902636
     fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=%" PRIu64 ", name=%s)\n", ino,
902636
              name);
902636
 
902636
-    if (inode->is_symlink) {
902636
-        /* Sorry, no race free way to setxattr on symlink. */
902636
-        saverr = EPERM;
902636
-        goto out;
902636
-    }
902636
-
902636
     sprintf(procname, "%i", inode->fd);
902636
-    fd = openat(lo->proc_self_fd, procname, O_RDWR);
902636
-    if (fd < 0) {
902636
-        saverr = errno;
902636
-        goto out;
902636
+    if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) {
902636
+        fd = openat(lo->proc_self_fd, procname, O_RDONLY);
902636
+        if (fd < 0) {
902636
+            saverr = errno;
902636
+            goto out;
902636
+        }
902636
+        ret = fremovexattr(fd, name);
902636
+    } else {
902636
+        /* fchdir should not fail here */
902636
+        assert(fchdir(lo->proc_self_fd) == 0);
902636
+        ret = removexattr(procname, name);
902636
+        assert(fchdir(lo->root.fd) == 0);
902636
     }
902636
 
902636
-    ret = fremovexattr(fd, name);
902636
     saverr = ret == -1 ? errno : 0;
902636
 
902636
 out:
902636
@@ -2800,7 +2811,7 @@ static void setup_root(struct lo_data *lo, struct lo_inode *root)
902636
         exit(1);
902636
     }
902636
 
902636
-    root->is_symlink = false;
902636
+    root->filetype = S_IFDIR;
902636
     root->fd = fd;
902636
     root->key.ino = stat.st_ino;
902636
     root->key.dev = stat.st_dev;
902636
diff --git a/tools/virtiofsd/seccomp.c b/tools/virtiofsd/seccomp.c
902636
index 2d9d4a7..bd9e7b0 100644
902636
--- a/tools/virtiofsd/seccomp.c
902636
+++ b/tools/virtiofsd/seccomp.c
902636
@@ -41,6 +41,7 @@ static const int syscall_whitelist[] = {
902636
     SCMP_SYS(exit),
902636
     SCMP_SYS(exit_group),
902636
     SCMP_SYS(fallocate),
902636
+    SCMP_SYS(fchdir),
902636
     SCMP_SYS(fchmodat),
902636
     SCMP_SYS(fchownat),
902636
     SCMP_SYS(fcntl),
902636
@@ -62,7 +63,9 @@ static const int syscall_whitelist[] = {
902636
     SCMP_SYS(getpid),
902636
     SCMP_SYS(gettid),
902636
     SCMP_SYS(gettimeofday),
902636
+    SCMP_SYS(getxattr),
902636
     SCMP_SYS(linkat),
902636
+    SCMP_SYS(listxattr),
902636
     SCMP_SYS(lseek),
902636
     SCMP_SYS(madvise),
902636
     SCMP_SYS(mkdirat),
902636
@@ -85,6 +88,7 @@ static const int syscall_whitelist[] = {
902636
     SCMP_SYS(recvmsg),
902636
     SCMP_SYS(renameat),
902636
     SCMP_SYS(renameat2),
902636
+    SCMP_SYS(removexattr),
902636
     SCMP_SYS(rt_sigaction),
902636
     SCMP_SYS(rt_sigprocmask),
902636
     SCMP_SYS(rt_sigreturn),
902636
@@ -98,10 +102,12 @@ static const int syscall_whitelist[] = {
902636
     SCMP_SYS(setresuid32),
902636
 #endif
902636
     SCMP_SYS(set_robust_list),
902636
+    SCMP_SYS(setxattr),
902636
     SCMP_SYS(symlinkat),
902636
     SCMP_SYS(time), /* Rarely needed, except on static builds */
902636
     SCMP_SYS(tgkill),
902636
     SCMP_SYS(unlinkat),
902636
+    SCMP_SYS(unshare),
902636
     SCMP_SYS(utimensat),
902636
     SCMP_SYS(write),
902636
     SCMP_SYS(writev),
902636
-- 
902636
1.8.3.1
902636