Re: [PATCH] tpm: track the response read position internally, not via the file offset
From: Jarkko Sakkinen
Date: Wed Nov 18 2026 - 09:34:14 EST
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@xxxxxxxxxx> 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