yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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