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 <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2022-08-09 19:58:33 -04:00 committed by Stefan Berger
parent 54de243efd
commit c4adfa3e57

View File

@ -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) {