peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0056-virtiofsd-prevent-.-escape-in-lo_do_readdir.patch

1d442b
From: Stefan Hajnoczi <stefanha@redhat.com>
1d442b
Date: Mon, 27 Jan 2020 19:01:25 +0000
1d442b
Subject: [PATCH] virtiofsd: prevent ".." escape in lo_do_readdir()
1d442b
1d442b
Construct a fake dirent for the root directory's ".." entry.  This hides
1d442b
the parent directory from the FUSE client.
1d442b
1d442b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
1d442b
Reviewed-by: Sergio Lopez <slp@redhat.com>
1d442b
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1d442b
(cherry picked from commit 752272da2b68a2312f0e11fc5303015a6c3ee1ac)
1d442b
---
1d442b
 tools/virtiofsd/passthrough_ll.c | 36 +++++++++++++++++++-------------
1d442b
 1 file changed, 22 insertions(+), 14 deletions(-)
1d442b
1d442b
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
1d442b
index 79d5966eea..e3d65c3676 100644
1d442b
--- a/tools/virtiofsd/passthrough_ll.c
1d442b
+++ b/tools/virtiofsd/passthrough_ll.c
1d442b
@@ -1149,19 +1149,25 @@ out_err:
1d442b
 static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1d442b
                           off_t offset, struct fuse_file_info *fi, int plus)
1d442b
 {
1d442b
+    struct lo_data *lo = lo_data(req);
1d442b
     struct lo_dirp *d;
1d442b
+    struct lo_inode *dinode;
1d442b
     char *buf = NULL;
1d442b
     char *p;
1d442b
     size_t rem = size;
1d442b
-    int err = ENOMEM;
1d442b
+    int err = EBADF;
1d442b
 
1d442b
-    (void)ino;
1d442b
+    dinode = lo_inode(req, ino);
1d442b
+    if (!dinode) {
1d442b
+        goto error;
1d442b
+    }
1d442b
 
1d442b
     d = lo_dirp(req, fi);
1d442b
     if (!d) {
1d442b
         goto error;
1d442b
     }
1d442b
 
1d442b
+    err = ENOMEM;
1d442b
     buf = calloc(1, size);
1d442b
     if (!buf) {
1d442b
         goto error;
1d442b
@@ -1192,15 +1198,21 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1d442b
         }
1d442b
         nextoff = d->entry->d_off;
1d442b
         name = d->entry->d_name;
1d442b
+
1d442b
         fuse_ino_t entry_ino = 0;
1d442b
+        struct fuse_entry_param e = (struct fuse_entry_param){
1d442b
+            .attr.st_ino = d->entry->d_ino,
1d442b
+            .attr.st_mode = d->entry->d_type << 12,
1d442b
+        };
1d442b
+
1d442b
+        /* Hide root's parent directory */
1d442b
+        if (dinode == &lo->root && strcmp(name, "..") == 0) {
1d442b
+            e.attr.st_ino = lo->root.ino;
1d442b
+            e.attr.st_mode = DT_DIR << 12;
1d442b
+        }
1d442b
+
1d442b
         if (plus) {
1d442b
-            struct fuse_entry_param e;
1d442b
-            if (is_dot_or_dotdot(name)) {
1d442b
-                e = (struct fuse_entry_param){
1d442b
-                    .attr.st_ino = d->entry->d_ino,
1d442b
-                    .attr.st_mode = d->entry->d_type << 12,
1d442b
-                };
1d442b
-            } else {
1d442b
+            if (!is_dot_or_dotdot(name)) {
1d442b
                 err = lo_do_lookup(req, ino, name, &e);
1d442b
                 if (err) {
1d442b
                     goto error;
1d442b
@@ -1210,11 +1222,7 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
1d442b
 
1d442b
             entsize = fuse_add_direntry_plus(req, p, rem, name, &e, nextoff);
1d442b
         } else {
1d442b
-            struct stat st = {
1d442b
-                .st_ino = d->entry->d_ino,
1d442b
-                .st_mode = d->entry->d_type << 12,
1d442b
-            };
1d442b
-            entsize = fuse_add_direntry(req, p, rem, name, &st, nextoff);
1d442b
+            entsize = fuse_add_direntry(req, p, rem, name, &e.attr, nextoff);
1d442b
         }
1d442b
         if (entsize > rem) {
1d442b
             if (entry_ino != 0) {