yeahuh / rpms / qemu-kvm

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