Blame SOURCES/kvm-virtiofsd-passthrough_ll-add-fd_map-to-hide-file-des.patch

22c213
From 35337e604e9149d6d8fcf74b8b82ac33a8611ebb Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:01:16 +0100
22c213
Subject: [PATCH 045/116] virtiofsd: passthrough_ll: add fd_map to hide file
22c213
 descriptors
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: <20200127190227.40942-42-dgilbert@redhat.com>
22c213
Patchwork-id: 93494
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 041/112] virtiofsd: passthrough_ll: add fd_map to hide file descriptors
22c213
Bugzilla: 1694164
22c213
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
22c213
22c213
From: Stefan Hajnoczi <stefanha@redhat.com>
22c213
22c213
Do not expose file descriptor numbers to clients.  This prevents the
22c213
abuse of internal file descriptors (like stdin/stdout).
22c213
22c213
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
Fix from:
22c213
Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
22c213
dgilbert:
22c213
  Added lseek
22c213
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit 73b4d19dfc4248a74c1f3e511cfa934681d9c602)
22c213
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/passthrough_ll.c | 116 +++++++++++++++++++++++++++++++--------
22c213
 1 file changed, 94 insertions(+), 22 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
22c213
index 5f5a72f..9815bfa 100644
22c213
--- a/tools/virtiofsd/passthrough_ll.c
22c213
+++ b/tools/virtiofsd/passthrough_ll.c
22c213
@@ -60,6 +60,7 @@ struct lo_map_elem {
22c213
     union {
22c213
         struct lo_inode *inode;
22c213
         struct lo_dirp *dirp;
22c213
+        int fd;
22c213
         ssize_t freelist;
22c213
     };
22c213
     bool in_use;
22c213
@@ -107,6 +108,7 @@ struct lo_data {
22c213
     struct lo_inode root; /* protected by lo->mutex */
22c213
     struct lo_map ino_map; /* protected by lo->mutex */
22c213
     struct lo_map dirp_map; /* protected by lo->mutex */
22c213
+    struct lo_map fd_map; /* protected by lo->mutex */
22c213
 };
22c213
 
22c213
 static const struct fuse_opt lo_opts[] = {
22c213
@@ -237,6 +239,20 @@ static void lo_map_remove(struct lo_map *map, size_t key)
22c213
 }
22c213
 
22c213
 /* Assumes lo->mutex is held */
22c213
+static ssize_t lo_add_fd_mapping(fuse_req_t req, int fd)
22c213
+{
22c213
+    struct lo_map_elem *elem;
22c213
+
22c213
+    elem = lo_map_alloc_elem(&lo_data(req)->fd_map);
22c213
+    if (!elem) {
22c213
+        return -1;
22c213
+    }
22c213
+
22c213
+    elem->fd = fd;
22c213
+    return elem - lo_data(req)->fd_map.elems;
22c213
+}
22c213
+
22c213
+/* Assumes lo->mutex is held */
22c213
 static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp)
22c213
 {
22c213
     struct lo_map_elem *elem;
22c213
@@ -350,6 +366,22 @@ static int utimensat_empty_nofollow(struct lo_inode *inode,
22c213
     return utimensat(AT_FDCWD, procname, tv, 0);
22c213
 }
22c213
 
22c213
+static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi)
22c213
+{
22c213
+    struct lo_data *lo = lo_data(req);
22c213
+    struct lo_map_elem *elem;
22c213
+
22c213
+    pthread_mutex_lock(&lo->mutex);
22c213
+    elem = lo_map_get(&lo->fd_map, fi->fh);
22c213
+    pthread_mutex_unlock(&lo->mutex);
22c213
+
22c213
+    if (!elem) {
22c213
+        return -1;
22c213
+    }
22c213
+
22c213
+    return elem->fd;
22c213
+}
22c213
+
22c213
 static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
                        int valid, struct fuse_file_info *fi)
22c213
 {
22c213
@@ -358,6 +390,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
     struct lo_inode *inode;
22c213
     int ifd;
22c213
     int res;
22c213
+    int fd;
22c213
 
22c213
     inode = lo_inode(req, ino);
22c213
     if (!inode) {
22c213
@@ -367,9 +400,14 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
 
22c213
     ifd = inode->fd;
22c213
 
22c213
+    /* If fi->fh is invalid we'll report EBADF later */
22c213
+    if (fi) {
22c213
+        fd = lo_fi_fd(req, fi);
22c213
+    }
22c213
+
22c213
     if (valid & FUSE_SET_ATTR_MODE) {
22c213
         if (fi) {
22c213
-            res = fchmod(fi->fh, attr->st_mode);
22c213
+            res = fchmod(fd, attr->st_mode);
22c213
         } else {
22c213
             sprintf(procname, "/proc/self/fd/%i", ifd);
22c213
             res = chmod(procname, attr->st_mode);
22c213
@@ -389,7 +427,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
     }
22c213
     if (valid & FUSE_SET_ATTR_SIZE) {
22c213
         if (fi) {
22c213
-            res = ftruncate(fi->fh, attr->st_size);
22c213
+            res = ftruncate(fd, attr->st_size);
22c213
         } else {
22c213
             sprintf(procname, "/proc/self/fd/%i", ifd);
22c213
             res = truncate(procname, attr->st_size);
22c213
@@ -419,7 +457,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
22c213
         }
22c213
 
22c213
         if (fi) {
22c213
-            res = futimens(fi->fh, tv);
22c213
+            res = futimens(fd, tv);
22c213
         } else {
22c213
             res = utimensat_empty_nofollow(inode, tv);
22c213
         }
22c213
@@ -1096,7 +1134,18 @@ static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name,
22c213
     lo_restore_cred(&old;;
22c213
 
22c213
     if (!err) {
22c213
-        fi->fh = fd;
22c213
+        ssize_t fh;
22c213
+
22c213
+        pthread_mutex_lock(&lo->mutex);
22c213
+        fh = lo_add_fd_mapping(req, fd);
22c213
+        pthread_mutex_unlock(&lo->mutex);
22c213
+        if (fh == -1) {
22c213
+            close(fd);
22c213
+            fuse_reply_err(req, ENOMEM);
22c213
+            return;
22c213
+        }
22c213
+
22c213
+        fi->fh = fh;
22c213
         err = lo_do_lookup(req, parent, name, &e);
22c213
     }
22c213
     if (lo->cache == CACHE_NEVER) {
22c213
@@ -1140,6 +1189,7 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
22c213
 static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
22c213
 {
22c213
     int fd;
22c213
+    ssize_t fh;
22c213
     char buf[64];
22c213
     struct lo_data *lo = lo_data(req);
22c213
 
22c213
@@ -1175,7 +1225,16 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
22c213
         return (void)fuse_reply_err(req, errno);
22c213
     }
22c213
 
22c213
-    fi->fh = fd;
22c213
+    pthread_mutex_lock(&lo->mutex);
22c213
+    fh = lo_add_fd_mapping(req, fd);
22c213
+    pthread_mutex_unlock(&lo->mutex);
22c213
+    if (fh == -1) {
22c213
+        close(fd);
22c213
+        fuse_reply_err(req, ENOMEM);
22c213
+        return;
22c213
+    }
22c213
+
22c213
+    fi->fh = fh;
22c213
     if (lo->cache == CACHE_NEVER) {
22c213
         fi->direct_io = 1;
22c213
     } else if (lo->cache == CACHE_ALWAYS) {
22c213
@@ -1187,9 +1246,18 @@ static void lo_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
22c213
 static void lo_release(fuse_req_t req, fuse_ino_t ino,
22c213
                        struct fuse_file_info *fi)
22c213
 {
22c213
+    struct lo_data *lo = lo_data(req);
22c213
+    int fd;
22c213
+
22c213
     (void)ino;
22c213
 
22c213
-    close(fi->fh);
22c213
+    fd = lo_fi_fd(req, fi);
22c213
+
22c213
+    pthread_mutex_lock(&lo->mutex);
22c213
+    lo_map_remove(&lo->fd_map, fi->fh);
22c213
+    pthread_mutex_unlock(&lo->mutex);
22c213
+
22c213
+    close(fd);
22c213
     fuse_reply_err(req, 0);
22c213
 }
22c213
 
22c213
@@ -1197,7 +1265,7 @@ static void lo_flush(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
22c213
 {
22c213
     int res;
22c213
     (void)ino;
22c213
-    res = close(dup(fi->fh));
22c213
+    res = close(dup(lo_fi_fd(req, fi)));
22c213
     fuse_reply_err(req, res == -1 ? errno : 0);
22c213
 }
22c213
 
22c213
@@ -1224,7 +1292,7 @@ static void lo_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
22c213
             return (void)fuse_reply_err(req, errno);
22c213
         }
22c213
     } else {
22c213
-        fd = fi->fh;
22c213
+        fd = lo_fi_fd(req, fi);
22c213
     }
22c213
 
22c213
     if (datasync) {
22c213
@@ -1251,7 +1319,7 @@ static void lo_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset,
22c213
     }
22c213
 
22c213
     buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
22c213
-    buf.buf[0].fd = fi->fh;
22c213
+    buf.buf[0].fd = lo_fi_fd(req, fi);
22c213
     buf.buf[0].pos = offset;
22c213
 
22c213
     fuse_reply_data(req, &buf;;
22c213
@@ -1266,7 +1334,7 @@ static void lo_write_buf(fuse_req_t req, fuse_ino_t ino,
22c213
     struct fuse_bufvec out_buf = FUSE_BUFVEC_INIT(fuse_buf_size(in_buf));
22c213
 
22c213
     out_buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
22c213
-    out_buf.buf[0].fd = fi->fh;
22c213
+    out_buf.buf[0].fd = lo_fi_fd(req, fi);
22c213
     out_buf.buf[0].pos = off;
22c213
 
22c213
     if (lo_debug(req)) {
22c213
@@ -1303,7 +1371,7 @@ static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
22c213
     (void)ino;
22c213
 
22c213
 #ifdef CONFIG_FALLOCATE
22c213
-    err = fallocate(fi->fh, mode, offset, length);
22c213
+    err = fallocate(lo_fi_fd(req, fi), mode, offset, length);
22c213
     if (err < 0) {
22c213
         err = errno;
22c213
     }
22c213
@@ -1314,7 +1382,7 @@ static void lo_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, off_t offset,
22c213
         return;
22c213
     }
22c213
 
22c213
-    err = posix_fallocate(fi->fh, offset, length);
22c213
+    err = posix_fallocate(lo_fi_fd(req, fi), offset, length);
22c213
 #endif
22c213
 
22c213
     fuse_reply_err(req, err);
22c213
@@ -1326,7 +1394,7 @@ static void lo_flock(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi,
22c213
     int res;
22c213
     (void)ino;
22c213
 
22c213
-    res = flock(fi->fh, op);
22c213
+    res = flock(lo_fi_fd(req, fi), op);
22c213
 
22c213
     fuse_reply_err(req, res == -1 ? errno : 0);
22c213
 }
22c213
@@ -1551,17 +1619,19 @@ static void lo_copy_file_range(fuse_req_t req, fuse_ino_t ino_in, off_t off_in,
22c213
                                off_t off_out, struct fuse_file_info *fi_out,
22c213
                                size_t len, int flags)
22c213
 {
22c213
+    int in_fd, out_fd;
22c213
     ssize_t res;
22c213
 
22c213
-    if (lo_debug(req))
22c213
-        fuse_log(FUSE_LOG_DEBUG,
22c213
-                 "lo_copy_file_range(ino=%" PRIu64 "/fd=%lu, "
22c213
-                 "off=%lu, ino=%" PRIu64 "/fd=%lu, "
22c213
-                 "off=%lu, size=%zd, flags=0x%x)\n",
22c213
-                 ino_in, fi_in->fh, off_in, ino_out, fi_out->fh, off_out, len,
22c213
-                 flags);
22c213
+    in_fd = lo_fi_fd(req, fi_in);
22c213
+    out_fd = lo_fi_fd(req, fi_out);
22c213
+
22c213
+    fuse_log(FUSE_LOG_DEBUG,
22c213
+             "lo_copy_file_range(ino=%" PRIu64 "/fd=%d, "
22c213
+             "off=%lu, ino=%" PRIu64 "/fd=%d, "
22c213
+             "off=%lu, size=%zd, flags=0x%x)\n",
22c213
+             ino_in, in_fd, off_in, ino_out, out_fd, off_out, len, flags);
22c213
 
22c213
-    res = copy_file_range(fi_in->fh, &off_in, fi_out->fh, &off_out, len, flags);
22c213
+    res = copy_file_range(in_fd, &off_in, out_fd, &off_out, len, flags);
22c213
     if (res < 0) {
22c213
         fuse_reply_err(req, -errno);
22c213
     } else {
22c213
@@ -1576,7 +1646,7 @@ static void lo_lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
22c213
     off_t res;
22c213
 
22c213
     (void)ino;
22c213
-    res = lseek(fi->fh, off, whence);
22c213
+    res = lseek(lo_fi_fd(req, fi), off, whence);
22c213
     if (res != -1) {
22c213
         fuse_reply_lseek(req, res);
22c213
     } else {
22c213
@@ -1661,6 +1731,7 @@ int main(int argc, char *argv[])
22c213
     root_elem->inode = &lo.root;
22c213
 
22c213
     lo_map_init(&lo.dirp_map);
22c213
+    lo_map_init(&lo.fd_map);
22c213
 
22c213
     if (fuse_parse_cmdline(&args, &opts) != 0) {
22c213
         return 1;
22c213
@@ -1758,6 +1829,7 @@ err_out2:
22c213
 err_out1:
22c213
     fuse_opt_free_args(&args);
22c213
 
22c213
+    lo_map_destroy(&lo.fd_map);
22c213
     lo_map_destroy(&lo.dirp_map);
22c213
     lo_map_destroy(&lo.ino_map);
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213