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