yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
22c213
From 2e58ff6978f8433fc8672d2e357c6f0f5f36d24f Mon Sep 17 00:00:00 2001
22c213
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
22c213
Date: Mon, 27 Jan 2020 19:02:07 +0100
22c213
Subject: [PATCH 096/116] virtiofsd: prevent races with lo_dirp_put()
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-93-dgilbert@redhat.com>
22c213
Patchwork-id: 93546
22c213
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 092/112] virtiofsd: prevent races with lo_dirp_put()
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
Introduce lo_dirp_put() so that FUSE_RELEASEDIR does not cause
22c213
use-after-free races with other threads that are accessing lo_dirp.
22c213
22c213
Also make lo_releasedir() atomic to prevent FUSE_RELEASEDIR racing with
22c213
itself.  This prevents double-frees.
22c213
22c213
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
22c213
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
22c213
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
22c213
(cherry picked from commit acefdde73b403576a241ebd8dbe8431ddc0d9442)
22c213
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
22c213
---
22c213
 tools/virtiofsd/passthrough_ll.c | 41 ++++++++++++++++++++++++++++++++++------
22c213
 1 file changed, 35 insertions(+), 6 deletions(-)
22c213
22c213
diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c
22c213
index 690edbc..2d703b5 100644
22c213
--- a/tools/virtiofsd/passthrough_ll.c
22c213
+++ b/tools/virtiofsd/passthrough_ll.c
22c213
@@ -1284,11 +1284,28 @@ static void lo_readlink(fuse_req_t req, fuse_ino_t ino)
22c213
 }
22c213
 
22c213
 struct lo_dirp {
22c213
+    gint refcount;
22c213
     DIR *dp;
22c213
     struct dirent *entry;
22c213
     off_t offset;
22c213
 };
22c213
 
22c213
+static void lo_dirp_put(struct lo_dirp **dp)
22c213
+{
22c213
+    struct lo_dirp *d = *dp;
22c213
+
22c213
+    if (!d) {
22c213
+        return;
22c213
+    }
22c213
+    *dp = NULL;
22c213
+
22c213
+    if (g_atomic_int_dec_and_test(&d->refcount)) {
22c213
+        closedir(d->dp);
22c213
+        free(d);
22c213
+    }
22c213
+}
22c213
+
22c213
+/* Call lo_dirp_put() on the return value when no longer needed */
22c213
 static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi)
22c213
 {
22c213
     struct lo_data *lo = lo_data(req);
22c213
@@ -1296,6 +1313,9 @@ static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi)
22c213
 
22c213
     pthread_mutex_lock(&lo->mutex);
22c213
     elem = lo_map_get(&lo->dirp_map, fi->fh);
22c213
+    if (elem) {
22c213
+        g_atomic_int_inc(&elem->dirp->refcount);
22c213
+    }
22c213
     pthread_mutex_unlock(&lo->mutex);
22c213
     if (!elem) {
22c213
         return NULL;
22c213
@@ -1331,6 +1351,7 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino,
22c213
     d->offset = 0;
22c213
     d->entry = NULL;
22c213
 
22c213
+    g_atomic_int_set(&d->refcount, 1); /* paired with lo_releasedir() */
22c213
     pthread_mutex_lock(&lo->mutex);
22c213
     fh = lo_add_dirp_mapping(req, d);
22c213
     pthread_mutex_unlock(&lo->mutex);
22c213
@@ -1364,7 +1385,7 @@ 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_dirp *d = NULL;
22c213
     struct lo_inode *dinode;
22c213
     char *buf = NULL;
22c213
     char *p;
22c213
@@ -1454,6 +1475,8 @@ static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
22c213
 
22c213
     err = 0;
22c213
 error:
22c213
+    lo_dirp_put(&d);
22c213
+
22c213
     /*
22c213
      * If there's an error, we can only signal it if we haven't stored
22c213
      * any entries yet - otherwise we'd end up with wrong lookup
22c213
@@ -1484,22 +1507,25 @@ static void lo_releasedir(fuse_req_t req, fuse_ino_t ino,
22c213
                           struct fuse_file_info *fi)
22c213
 {
22c213
     struct lo_data *lo = lo_data(req);
22c213
+    struct lo_map_elem *elem;
22c213
     struct lo_dirp *d;
22c213
 
22c213
     (void)ino;
22c213
 
22c213
-    d = lo_dirp(req, fi);
22c213
-    if (!d) {
22c213
+    pthread_mutex_lock(&lo->mutex);
22c213
+    elem = lo_map_get(&lo->dirp_map, fi->fh);
22c213
+    if (!elem) {
22c213
+        pthread_mutex_unlock(&lo->mutex);
22c213
         fuse_reply_err(req, EBADF);
22c213
         return;
22c213
     }
22c213
 
22c213
-    pthread_mutex_lock(&lo->mutex);
22c213
+    d = elem->dirp;
22c213
     lo_map_remove(&lo->dirp_map, fi->fh);
22c213
     pthread_mutex_unlock(&lo->mutex);
22c213
 
22c213
-    closedir(d->dp);
22c213
-    free(d);
22c213
+    lo_dirp_put(&d); /* paired with lo_opendir() */
22c213
+
22c213
     fuse_reply_err(req, 0);
22c213
 }
22c213
 
22c213
@@ -1710,6 +1736,9 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
22c213
     } else {
22c213
         res = fsync(fd);
22c213
     }
22c213
+
22c213
+    lo_dirp_put(&d);
22c213
+
22c213
     fuse_reply_err(req, res == -1 ? errno : 0);
22c213
 }
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213