cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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