From c4adfa3e57470d6005554e4e7dacec14cbc127e4 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 9 Aug 2022 19:58:33 -0400 Subject: [PATCH] swtpm: cuse: Extend usage of FILE_OPS_LOCK to protect a reading thread Extend usage of the FILE_OPS_LOCK to prevent other threads from reading or writing commands or doing ioctls while the current thread is reading a response. This prevents a race condition where ptm_read_offset is set to 0 by a thread writing a new command to the device while the current thread is reading a response from the device and needs this offset. Resolves: https://github.com/stefanberger/swtpm/issues/725 Signed-off-by: Stefan Berger --- src/swtpm/cuse_tpm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/swtpm/cuse_tpm.c b/src/swtpm/cuse_tpm.c index 0ce992c..e69500b 100644 --- a/src/swtpm/cuse_tpm.c +++ b/src/swtpm/cuse_tpm.c @@ -545,6 +545,9 @@ static void ptm_read_result(fuse_req_t req, size_t size) { size_t len = 0; + /* prevent other threads from reading or writing cmds or doing ioctls */ + g_mutex_lock(FILE_OPS_LOCK); + if (tpm_running) { /* wait until results are ready */ worker_thread_wait_done(); @@ -553,12 +556,14 @@ static void ptm_read_result(fuse_req_t req, size_t size) if (ptm_read_offset < ptm_res_len) { len = ptm_res_len - ptm_read_offset; if (size < len) - len = size; + len = size; } fuse_reply_buf(req, (const char *)&ptm_response[ptm_read_offset], len); ptm_read_offset += len; + + g_mutex_unlock(FILE_OPS_LOCK); } /* @@ -869,7 +874,7 @@ static void ptm_write_cmd(fuse_req_t req, const char *buf, size_t size, ptm_req_len = size; ptm_res_len = 0; - /* prevent other threads from writing or doing ioctls */ + /* prevent other threads from reading or writing cmds or doing ioctls */ g_mutex_lock(FILE_OPS_LOCK); if (tpm_running) {