Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] tpm: track the response read position internally, not via the file offset
       [not found] <alvD5nOP15O5B8Ai@kernel.org>
@ 2026-11-17 10:48 ` Rustam Mustafin
  2026-11-17 10:59   ` Jarkko Sakkinen
  0 siblings, 1 reply; 6+ messages in thread
From: Rustam Mustafin @ 2026-11-17 10:48 UTC (permalink / raw)
  To: Peter Huewe, Jarkko Sakkinen
  Cc: Jason Gunthorpe, Jaewon Yang, Tadeusz Struk, linux-integrity,
	linux-kernel, Rustam Mustafin, stable

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: stable@vger.kernel.org
Link: https://lore.kernel.org/r/CAHk-=wi8aXM9=Y8othMb1oxTv-PDw4isj-FPbXu0MXsm_EPE1g@mail.gmail.com/
Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com>
---
- 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] tpm: track the response read position internally, not via the file offset
  2026-11-17 10:48 ` [PATCH] tpm: track the response read position internally, not via the file offset Rustam Mustafin
@ 2026-11-17 10:59   ` Jarkko Sakkinen
  2026-11-17 12:35     ` Jarkko Sakkinen
  0 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 10:59 UTC (permalink / raw)
  To: Rustam Mustafin
  Cc: Peter Huewe, Jason Gunthorpe, Jaewon Yang, Tadeusz Struk,
	linux-integrity, linux-kernel, stable


Please remove that claude'ism from short summary (, not.. uaargh).


On Tue, Nov 17, 2026 at 05:48:38PM +0700, Rustam Mustafin wrote:
> Commit f20d61c22bca ("tpm: Make the TPM character devices non-seekable")

Duplicate information to fixes tag.

> 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

io_uring is a data structure, not an active actor and IORING_OP_READ
is a constant.

> 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

Why open code variable names in English?

> 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: stable@vger.kernel.org
> Link: https://lore.kernel.org/r/CAHk-=wi8aXM9=Y8othMb1oxTv-PDw4isj-FPbXu0MXsm_EPE1g@mail.gmail.com/
> Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com>
> ---
> - 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.

Why weren't you able to realize this in the kselftest?

> 
>  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;

I did search read_pos but it had zero hits in the commit message.


>  	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
> 

Br, Jarkko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] tpm: track the response read position internally, not via the file offset
  2026-11-17 10:59   ` Jarkko Sakkinen
@ 2026-11-17 12:35     ` Jarkko Sakkinen
  2026-11-17 12:38       ` Jarkko Sakkinen
  0 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 12:35 UTC (permalink / raw)
  To: Rustam Mustafin
  Cc: Peter Huewe, Jason Gunthorpe, Jaewon Yang, Tadeusz Struk,
	linux-integrity, linux-kernel, stable

On Tue, Nov 17, 2026 at 12:59:25PM +0200, Jarkko Sakkinen wrote:
>  ...

I think this would be more feasible:

        if (current->flags & PF_IO_WORKER)
                return -EIO;

        if ((current->flags & PF_KTHREAD) && current->io_uring)
                return -EIO;

I.e., block io_uring entirely.

Br, Jarkko



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] tpm: track the response read position internally, not via the file offset
  2026-11-17 12:35     ` Jarkko Sakkinen
@ 2026-11-17 12:38       ` Jarkko Sakkinen
  2026-11-18  9:55         ` Rustam Mustafin
       [not found]         ` <CAG6=z5sxUo1x6mpotkNZXk=nQYvD2AyMLSDhzD3B3kqxJ1=iaw@mail.gmail.com>
  0 siblings, 2 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-17 12:38 UTC (permalink / raw)
  To: Rustam Mustafin
  Cc: Peter Huewe, Jason Gunthorpe, Jaewon Yang, Tadeusz Struk,
	linux-integrity, linux-kernel, stable

On Tue, Nov 17, 2026 at 02:35:04PM +0200, Jarkko Sakkinen wrote:
> On Tue, Nov 17, 2026 at 12:59:25PM +0200, Jarkko Sakkinen wrote:
> >  ...
> 
> I think this would be more feasible:
> 
>         if (current->flags & PF_IO_WORKER)
>                 return -EIO;
> 
>         if ((current->flags & PF_KTHREAD) && current->io_uring)
>                 return -EIO;
> 
> I.e., block io_uring entirely.

And kselftest can then be extended to address this case (as a
separate patch) making this two-patch patch set.

Short summary would be e.g. (or use your imagination):

"tpm: Do not allow io_uring for /dev/tpm{,rm0}"

Br, Jarkko

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] tpm: track the response read position internally, not via the file offset
  2026-11-17 12:38       ` Jarkko Sakkinen
@ 2026-11-18  9:55         ` Rustam Mustafin
       [not found]         ` <CAG6=z5sxUo1x6mpotkNZXk=nQYvD2AyMLSDhzD3B3kqxJ1=iaw@mail.gmail.com>
  1 sibling, 0 replies; 6+ messages in thread
From: Rustam Mustafin @ 2026-11-18  9:55 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: Peter Huewe, Jason Gunthorpe, Jaewon Yang, Tadeusz Struk,
	linux-integrity, linux-kernel, stable

On Tue, Nov 17, 2026 at 7:38 PM Jarkko Sakkinen <jarkko@kernel.org> wrote:
> On Tue, Nov 17, 2026 at 02:35:04PM +0200, Jarkko Sakkinen wrote:
> > I think this would be more feasible:
> >
> >         if (current->flags & PF_IO_WORKER)
> >                 return -EIO;
> >
> >         if ((current->flags & PF_KTHREAD) && current->io_uring)
> >                 return -EIO;
> >
> > I.e., block io_uring entirely.

Hi Jarkko, thanks for the review.

These checks do not fully block io_uring. With O_NONBLOCK set and a
response ready, io_uring calls ->read() inline from the caller's own
task, where neither PF_IO_WORKER nor PF_KTHREAD is set.
loop_rw_iter() declines the inline attempt only when the file lacks
O_NONBLOCK:

    if ((kiocb->ki_flags & IOCB_NOWAIT) &&
        !(kiocb->ki_filp->f_flags & O_NONBLOCK))
        return -EAGAIN;

I built a kernel with both checks applied and the injected offset
still reaches tpm_common_read():

    BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x4b/0x70
    Read of size 16 at addr ffff88810c3455d2 by task python3/134
    Call Trace:
    _copy_to_user+0x4b/0x70
    tpm_common_read+0x1e6/0x360
    loop_rw_iter+0x235/0x5a0
    __io_read+0xc6e/0x11a0
    io_read+0x8a/0x180

Reproducer:
- open("/dev/tpm0", O_RDWR | O_NONBLOCK)
- write() a command, then poll() until POLLIN
- submit IORING_OP_READ with sqe->off = 0x1400, without IOSQE_ASYNC
(this is how tpm2-tss opens the device O_RDWR | O_NONBLOCK).

Checking current->io_uring alone would catch the inline path, but
then a process that uses io_uring for anything else would also lose
plain read()/write() on the device.

For v2 I propose:

1. The read position fix, with your review comments applied.
2. stream_open() instead of nonseekable_open(), on top of 1. With
   FMODE_STREAM both the VFS and io_uring pass a NULL offset to the
   driver. This follows c5bf68fe0c86 ("*: convert stream-like files
   from nonseekable_open -> stream_open").
3. A kselftest: lseek() and pread() fail with -ESPIPE, chunked reads
   of a TPM2_GetCapability(TPM_PT_FAMILY_INDICATOR) response match a
   one-shot read, and an io_uring read with an explicit sqe->off
   returns the start of the response.

Br,
Rustam

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] tpm: track the response read position internally, not via the file offset
       [not found]         ` <CAG6=z5sxUo1x6mpotkNZXk=nQYvD2AyMLSDhzD3B3kqxJ1=iaw@mail.gmail.com>
@ 2026-11-18 14:33           ` Jarkko Sakkinen
  0 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2026-11-18 14:33 UTC (permalink / raw)
  To: Rustam Mustafin
  Cc: Peter Huewe, Jason Gunthorpe, Jaewon Yang, Tadeusz Struk,
	linux-integrity, linux-kernel, stable

On Wed, Nov 18, 2026 at 04:49:46PM +0700, Rustam Mustafin wrote:
> On Tue, Nov 17, 2026 at 7:38 PM Jarkko Sakkinen <jarkko@kernel.org> wrote:
> > On Tue, Nov 17, 2026 at 02:35:04PM +0200, Jarkko Sakkinen wrote:
> > > I think this would be more feasible:
> > >
> > >         if (current->flags & PF_IO_WORKER)
> > >                 return -EIO;
> > >
> > >         if ((current->flags & PF_KTHREAD) && current->io_uring)
> > >                 return -EIO;
> > >
> > > I.e., block io_uring entirely.
> 
> Hi Jarkko, thanks for the review.
> 
> These checks do not fully block io_uring. With O_NONBLOCK set and a
> response ready, io_uring calls ->read() inline from the caller's own
> task, where neither PF_IO_WORKER nor PF_KTHREAD is set.
> loop_rw_iter() declines the inline attempt only when the file lacks
> O_NONBLOCK:
> 
>     if ((kiocb->ki_flags & IOCB_NOWAIT) &&
>         !(kiocb->ki_filp->f_flags & O_NONBLOCK))
>         return -EAGAIN;
> 
> I built a kernel with both checks applied and the injected offset
> still reaches tpm_common_read():
> 
>     BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x4b/0x70
>     Read of size 16 at addr ffff88810c3455d2 by task python3/134
>     Call Trace:
>     _copy_to_user+0x4b/0x70
>     tpm_common_read+0x1e6/0x360
>     loop_rw_iter+0x235/0x5a0
>     __io_read+0xc6e/0x11a0
>     io_read+0x8a/0x180
> 
> Reproducer:
> - open("/dev/tpm0", O_RDWR | O_NONBLOCK)
> - write() a command, then poll() until POLLIN
> - submit IORING_OP_READ with sqe->off = 0x1400, without IOSQE_ASYNC
> (this is how tpm2-tss opens the device O_RDWR | O_NONBLOCK).
> 
> Checking current->io_uring alone would catch the inline path, but
> then a process that uses io_uring for anything else would also lose
> plain read()/write() on the device.
> 
> For v2 I propose:
> 
> 1. The read position fix, with your review comments applied.
> 2. stream_open() instead of nonseekable_open(), on top of 1. With
>     FMODE_STREAM both the VFS and io_uring pass a NULL offset to the
>     driver. This follows c5bf68fe0c86 ("*: convert stream-like files
>     from nonseekable_open -> stream_open").
> 3. A kselftest: lseek() and pread() fail with -ESPIPE, chunked reads
>     of a TPM2_GetCapability(TPM_PT_FAMILY_INDICATOR) response match a
>     one-shot read, and an io_uring read with an explicit sqe->off
>     returns the start of the response.
> 
> Br,
> Rustam

I wrote that out of my memory. I did not expect to be exactly out.

Block uring.

Br, Jarkko

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-18 14:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <alvD5nOP15O5B8Ai@kernel.org>
2026-11-17 10:48 ` [PATCH] tpm: track the response read position internally, not via the file offset Rustam Mustafin
2026-11-17 10:59   ` Jarkko Sakkinen
2026-11-17 12:35     ` Jarkko Sakkinen
2026-11-17 12:38       ` Jarkko Sakkinen
2026-11-18  9:55         ` Rustam Mustafin
     [not found]         ` <CAG6=z5sxUo1x6mpotkNZXk=nQYvD2AyMLSDhzD3B3kqxJ1=iaw@mail.gmail.com>
2026-11-18 14:33           ` Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox