peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0111-virtiofsd-prevent-FUSE_INIT-FUSE_DESTROY-races.patch

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