yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-virtiofsd-prevent-FUSE_INIT-FUSE_DESTROY-races.patch

ddf19c
From 249c02ae54739dc5894ee1b2905bbe8f1e79e909 Mon Sep 17 00:00:00 2001
ddf19c
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
ddf19c
Date: Mon, 27 Jan 2020 19:02:20 +0100
ddf19c
Subject: [PATCH 109/116] virtiofsd: prevent FUSE_INIT/FUSE_DESTROY races
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-106-dgilbert@redhat.com>
ddf19c
Patchwork-id: 93562
ddf19c
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 105/112] virtiofsd: prevent FUSE_INIT/FUSE_DESTROY races
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
When running with multiple threads it can be tricky to handle
ddf19c
FUSE_INIT/FUSE_DESTROY in parallel with other request types or in
ddf19c
parallel with themselves.  Serialize FUSE_INIT and FUSE_DESTROY so that
ddf19c
malicious clients cannot trigger race conditions.
ddf19c
ddf19c
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
ddf19c
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
ddf19c
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
ddf19c
(cherry picked from commit cdc497c6925be745bc895355bd4674a17a4b2a8b)
ddf19c
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ddf19c
---
ddf19c
 tools/virtiofsd/fuse_i.h        |  1 +
ddf19c
 tools/virtiofsd/fuse_lowlevel.c | 18 ++++++++++++++++++
ddf19c
 2 files changed, 19 insertions(+)
ddf19c
ddf19c
diff --git a/tools/virtiofsd/fuse_i.h b/tools/virtiofsd/fuse_i.h
ddf19c
index a20854f..1447d86 100644
ddf19c
--- a/tools/virtiofsd/fuse_i.h
ddf19c
+++ b/tools/virtiofsd/fuse_i.h
ddf19c
@@ -61,6 +61,7 @@ struct fuse_session {
ddf19c
     struct fuse_req list;
ddf19c
     struct fuse_req interrupts;
ddf19c
     pthread_mutex_t lock;
ddf19c
+    pthread_rwlock_t init_rwlock;
ddf19c
     int got_destroy;
ddf19c
     int broken_splice_nonblock;
ddf19c
     uint64_t notify_ctr;
ddf19c
diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
index dab6a31..79a4031 100644
ddf19c
--- a/tools/virtiofsd/fuse_lowlevel.c
ddf19c
+++ b/tools/virtiofsd/fuse_lowlevel.c
ddf19c
@@ -2428,6 +2428,19 @@ void fuse_session_process_buf_int(struct fuse_session *se,
ddf19c
     req->ctx.pid = in->pid;
ddf19c
     req->ch = ch;
ddf19c
 
ddf19c
+    /*
ddf19c
+     * INIT and DESTROY requests are serialized, all other request types
ddf19c
+     * run in parallel.  This prevents races between FUSE_INIT and ordinary
ddf19c
+     * requests, FUSE_INIT and FUSE_INIT, FUSE_INIT and FUSE_DESTROY, and
ddf19c
+     * FUSE_DESTROY and FUSE_DESTROY.
ddf19c
+     */
ddf19c
+    if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT ||
ddf19c
+        in->opcode == FUSE_DESTROY) {
ddf19c
+        pthread_rwlock_wrlock(&se->init_rwlock);
ddf19c
+    } else {
ddf19c
+        pthread_rwlock_rdlock(&se->init_rwlock);
ddf19c
+    }
ddf19c
+
ddf19c
     err = EIO;
ddf19c
     if (!se->got_init) {
ddf19c
         enum fuse_opcode expected;
ddf19c
@@ -2485,10 +2498,13 @@ void fuse_session_process_buf_int(struct fuse_session *se,
ddf19c
     } else {
ddf19c
         fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter);
ddf19c
     }
ddf19c
+
ddf19c
+    pthread_rwlock_unlock(&se->init_rwlock);
ddf19c
     return;
ddf19c
 
ddf19c
 reply_err:
ddf19c
     fuse_reply_err(req, err);
ddf19c
+    pthread_rwlock_unlock(&se->init_rwlock);
ddf19c
 }
ddf19c
 
ddf19c
 #define LL_OPTION(n, o, v)                     \
ddf19c
@@ -2531,6 +2547,7 @@ void fuse_session_destroy(struct fuse_session *se)
ddf19c
             se->op.destroy(se->userdata);
ddf19c
         }
ddf19c
     }
ddf19c
+    pthread_rwlock_destroy(&se->init_rwlock);
ddf19c
     pthread_mutex_destroy(&se->lock);
ddf19c
     free(se->cuse_data);
ddf19c
     if (se->fd != -1) {
ddf19c
@@ -2610,6 +2627,7 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
ddf19c
     list_init_req(&se->list);
ddf19c
     list_init_req(&se->interrupts);
ddf19c
     fuse_mutex_init(&se->lock);
ddf19c
+    pthread_rwlock_init(&se->init_rwlock, NULL);
ddf19c
 
ddf19c
     memcpy(&se->op, op, op_size);
ddf19c
     se->owner = getuid();
ddf19c
-- 
ddf19c
1.8.3.1
ddf19c