Commit f20d61c22bca ("tpm: Make the TPM character devices non-seekable")
made pread() fail with -ESPIPE, but tpm_common_read() still indexes
data_buffer with *off, and io_uring can still set it: IORING_OP_READ
copies sqe->off into ki_pos without checking FMODE_PREAD, and
loop_rw_iter() passes it to ->read(). With sqe->off = 0x1400 the read
copies heap memory past data_buffer to user space and then zeroes it:

  BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x4b/0x70
  Read of size 16 by task iou-wrk-148/149
  Call Trace:
   _copy_to_user+0x4b/0x70
   tpm_common_read+0x16e/0x2c0
   loop_rw_iter+0x235/0x5a0
   __io_read+0xc6e/0x11a0
   io_read+0x8a/0x180

Keep the read position in file_priv, protected by buffer_mutex, and
ignore *off. This also closes a race where read() samples f_pos before
taking buffer_mutex and a concurrent write() replaces the response in
between.

Fixes: 9488585b21be ("tpm: add support for partial reads")
Cc: [email protected]
Link: 
https://lore.kernel.org/r/CAHk-=wi8aXM9=y8othmb1oxtv-pdw4isj-fpbxu0mxsm_ep...@mail.gmail.com/
Signed-off-by: Rustam Mustafin <[email protected]>
---
- A C reproducer for the io_uring path is available on request.
- nonseekable_open() only closes the VFS doors (lseek(), pread()); io_uring
  bypasses both, so with this patch an IORING_OP_READ that passes an
  explicit sqe->off ignores the offset and reads sequentially.

 drivers/char/tpm/tpm-dev-common.c | 11 ++++++-----
 drivers/char/tpm/tpm-dev.h        |  1 +
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/char/tpm/tpm-dev-common.c 
b/drivers/char/tpm/tpm-dev-common.c
index ee91f456f975..553fc6aca94d 100644
--- a/drivers/char/tpm/tpm-dev-common.c
+++ b/drivers/char/tpm/tpm-dev-common.c
@@ -107,6 +107,7 @@ static void tpm_timeout_work(struct work_struct *work)
        mutex_lock(&priv->buffer_mutex);
        priv->response_read = true;
        priv->response_length = 0;
+       priv->read_pos = 0;
        memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
        mutex_unlock(&priv->buffer_mutex);
        wake_up_interruptible(&priv->async_wait);
@@ -149,21 +150,21 @@ ssize_t tpm_common_read(struct file *file, char __user 
*buf,
                        goto out;
                }
 
-               rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
+               rc = copy_to_user(buf, priv->data_buffer + priv->read_pos, 
ret_size);
                if (rc) {
                        memset(priv->data_buffer, 0, TPM_BUFSIZE);
                        priv->response_length = 0;
                        ret_size = -EFAULT;
                } else {
-                       memset(priv->data_buffer + *off, 0, ret_size);
+                       memset(priv->data_buffer + priv->read_pos, 0, ret_size);
                        priv->response_length -= ret_size;
-                       *off += ret_size;
+                       priv->read_pos += ret_size;
                }
        }
 
 out:
        if (!priv->response_length) {
-               *off = 0;
+               priv->read_pos = 0;
                timer_delete_sync(&priv->user_read_timer);
                flush = true;
        }
@@ -214,7 +215,7 @@ ssize_t tpm_common_write(struct file *file, const char 
__user *buf,
 
        priv->response_length = 0;
        priv->response_read = false;
-       *off = 0;
+       priv->read_pos = 0;
 
        /*
         * If in nonblocking mode schedule an async job to send
diff --git a/drivers/char/tpm/tpm-dev.h b/drivers/char/tpm/tpm-dev.h
index f3742bcc73e3..34efb1ffa491 100644
--- a/drivers/char/tpm/tpm-dev.h
+++ b/drivers/char/tpm/tpm-dev.h
@@ -15,6 +15,7 @@ struct file_priv {
        struct work_struct async_work;
        wait_queue_head_t async_wait;
        ssize_t response_length;
+       size_t read_pos;
        bool response_read;
        bool command_enqueued;
 
-- 
2.52.0


Reply via email to