tpm_common_read() uses *off as an index into priv->data_buffer. nonseekable_open() makes pread() fail, but an IORING_OP_READ request still reaches tpm_common_read() through loop_rw_iter() with *off set to sqe->off, and a plain read() racing a write() reaches it with a stale f_pos. Both allow out-of-bounds reads and zeroing of kernel memory.
Patch 1 moves the read position into struct file_priv, under buffer_mutex, and ignores *off. Patch 2 adds a kselftest: an io_uring read with sqe->off inside the response buffer must return the whole response from its start. Blocking io_uring, suggested in the v1 review, is no longer needed for safety with patch 1; I discuss it in the v1 thread. Tested in a QEMU guest with swtpm and KASAN: the tpm2 selftests pass with patch 1, and the new one fails without it. Claude Opus 5.5 was used in preparing this series. Changes in v2: - commit message reworked per review, read()/write() race trace added - add a kselftest v1: https://lore.kernel.org/all/[email protected]/ Rustam Mustafin (2): tpm: Track the response read position internally selftests: tpm2: test that io_uring reads ignore the file offset drivers/char/tpm/tpm-dev-common.c | 11 +- drivers/char/tpm/tpm-dev.h | 1 + tools/testing/selftests/tpm2/.gitignore | 1 + tools/testing/selftests/tpm2/Makefile | 4 + tools/testing/selftests/tpm2/tpm_uring.c | 131 +++++++++++++++++++++++ 5 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/tpm2/tpm_uring.c base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4 -- 2.52.0
