render / rpms / qemu

Forked from rpms/qemu 9 months ago
Clone

Blame 0047-virtiofsd-passthrough_ll-add-fd_map-to-hide-file-des.patch

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