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