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