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

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