tpm: Track the response read position internally
Subsystems (4)
v2 cover tpm: Track the response read position internally
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
Signature · 1 line
2.52.0
PATCH v2 1/2 tpm: Track the response read position internally
tpm_common_read() indexes data_buffer with the caller supplied *off. A read issued through io_uring reaches the driver with that offset:loop_rw_iter() passes &ki_pos to ->read(). This allows out-of-bounds reads and zeroing of kernel memory:
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
A plain read() racing a write() reaches the same access: read() samples f_pos before taking buffer_mutex, so it can consume a new response from the offset left by the previous one:
BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x4b/0x70 Read of size 75 by task trigger/151 Call Trace: _copy_to_user+0x4b/0x70 tpm_common_read+0x16e/0x2c0 vfs_read+0x189/0xc00 ksys_read+0xf9/0x1d0
Replace *off with a read_pos member of struct file_priv. It is only accessed under buffer_mutex, so the offset passed by the caller no longer matters.
--- drivers/char/tpm/tpm-dev-common.c | 11 ++++++----- drivers/char/tpm/tpm-dev.h | 1 + 2 files changed, 7 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm-dev-common.c +6 -5
@@ -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
drivers/char/tpm/tpm-dev.h +1 -0
@@ -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;
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerQuoted 26 lines · Rustam Mustafin
tpm_common_read()indexesdata_bufferwith the caller supplied *off. A read issued throughio_uringreaches the driver with that offset:loop_rw_iter()passes&ki_posto ->read(). This allows out-of-bounds reads and zeroing of kernel memory: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/0x180A plain
read()racing awrite()reaches the same access:read()samplesf_posbefore takingbuffer_mutex, so it can consume a new response from the offset left by the previous one:BUG: KASAN: slab-out-of-bounds in _copy_to_user+0x4b/0x70 Read of size 75 by task trigger/151 Call Trace: _copy_to_user+0x4b/0x70 tpm_common_read+0x16e/0x2c0 vfs_read+0x189/0xc00 ksys_read+0xf9/0x1d0
Quoted 8 lines
Replace *off with a
read_posmember of structfile_priv. It is only accessed underbuffer_mutex, so the offset passed by the caller no longer matters.Fixes 9488585b21be ("tpm: add support for partial reads")Signed-off-by Rustam Mustafin <[email protected]>---
So... why does not this block uring? You are implementing a feature as a bug fix, or so that it appears.
Quoted 66 lines
drivers/char/tpm/tpm-dev-common.c | 11 ++++++----- drivers/char/tpm/tpm-dev.h | 1 + 2 files changed, 7 insertions(+), 5 deletions(-)drivers/char/tpm/tpm-dev-common.c +6
-5@@ -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 drivers/char/tpm/tpm-dev.h +1
-0@@ -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;Signature · 1 line
2.52.0
Br, Jarkko
Quoted 1 line · Jarkko Sakkinen
On Fri, Nov 20, 2026 at 07:16:13PM +0700, Rustam Mustafin wrote:
[...]
Quoted 2 lines
So... why does not this block uring? You are implementing a feature as a bug fix, or so that it appears.
Patch 1 also fixes a plain read() racing a write() (the second trace), which does not involve io_uring, so blocking io_uring alone would not cover it. Patch 1 is small, so it might be easier to backport.
The io_uring block is posted on top of this series:
https://lore.kernel.org/all/[email protected]/
It depends on this series, because the new ->read_iter() would let vfs_iter_read() callers such as Coda reach tpm_common_read() with their own offset.
Br,
Rustam
Jarkko Sakkinen
maintainerQuoted 10 lines · Rustam Mustafin
Quoted 1 line · Jarkko Sakkinen
On Fri, Nov 20, 2026 at 07:16:13PM +0700, Rustam Mustafin wrote:
[...]
Quoted 2 lines
So... why does not this block uring? You are implementing a feature as a bug fix, or so that it appears.
Patch 1 also fixes a plain
read()racing awrite()(the second trace), which does not involveio_uring, so blockingio_uringalone would not cover it. Patch 1 is small, so it might be easier to backport.
Quoted 6 lines
The
io_uringblock is posted on top of this series:
https://lore.kernel.org/all/[email protected]/It depends on this series, because the new ->read_iter() would let
vfs_iter_read()callers such as Coda reachtpm_common_read()with their own offset.
My mishap.
Sorry. I'll look into this soon.
Quoted 3 lines
Br,
Rustam
Br, Jarkko
PATCH v2 2/2 selftests: tpm2: test that io_uring reads ignore the file offset
In reply to Rustam MustafinTest that an IORING_OP_READ request on /dev/tpm0 and /dev/tpmrm0 returns the whole pending response from its start, although sqe->off points past the response header. The offset stays inside priv->data_buffer, so an unpatched kernel fails the test on the returned header without an out-of-bounds access.
--- tools/testing/selftests/tpm2/.gitignore | 1 + tools/testing/selftests/tpm2/Makefile | 4 + tools/testing/selftests/tpm2/tpm_uring.c | 131 +++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/tpm2/tpm_uring.c
tools/testing/selftests/tpm2/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only AsyncTest.log SpaceTest.log +tpm_uring
tools/testing/selftests/tpm2/Makefile +4 -0
@@ -1,4 +1,8 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +CFLAGS += $(KHDR_INCLUDES) -idirafter ../../../include -Wall + +TEST_GEN_PROGS := tpm_uring + include ../lib.mk TEST_PROGS := test_smoke.sh test_space.sh test_async.sh
tools/testing/selftests/tpm2/tpm_uring.c new file +131 -0
131 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v2 2/2] selftests: tpm2: test that io_uring reads ignore the file offset
Quoted 176 lines · Rustam Mustafin
Test that an
IORING_OP_READrequest on/dev/tpm0and/dev/tpmrm0returns the whole pending response from its start, althoughsqe->offpoints past the response header. The offset stays insidepriv->data_buffer, so an unpatched kernel fails the test on the returned header without an out-of-bounds access.Signed-off-by Rustam Mustafin <[email protected]>--- tools/testing/selftests/tpm2/.gitignore | 1 + tools/testing/selftests/tpm2/Makefile | 4 + tools/testing/selftests/tpm2/tpm_uring.c | 131 +++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/tpm2/tpm_uring.ctools/testing/selftests/tpm2/.gitignore +1
-0@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only AsyncTest.log SpaceTest.log +tpm_uringtools/testing/selftests/tpm2/Makefile +4
-0@@ -1,4 +1,8 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +CFLAGS += $(KHDR_INCLUDES) -idirafter ../../../include -Wall + +TEST_GEN_PROGS := tpm_uring + include ../lib.mk TEST_PROGS := test_smoke.sh test_space.sh test_async.shtools/testing/selftests/tpm2/tpm_uring.c new file +131
-0@@ -0,0 +1,131 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +/* +* An io_uring read chooses its own file offset. The TPM character devices +* must ignore it and return the response from its start. +*/ + +#include <endian.h> +#include <errno.h> +#include <fcntl.h> +#include <poll.h> +#include <signal.h> +#include <io_uring/mini_liburing.h> +#include "../kselftest.h" + +#define RING_ENTRIES 4 +#define BUF_LEN 4096 +#define POLL_TIMEOUT_MS 10000 +#define TPM_VERSION_FILE "/sys/class/tpm/tpm0/tpm_version_major" +struct tpm_header { +uint16_t tag; +uint32_t size; +uint32_t return_code; +} __attribute__((packed)); + +/* TPM2_GetCapability(TPM_CAP_TPM_PROPERTIES, TPM_PT_FIXED, 1) */ +static const unsigned char cmd[] = { +0x80 ,0x01 ,/* TPM_ST_NO_SESSIONS */ +0x00 ,0x00 ,0x00 ,0x16 ,/* command size */ +0x00 ,0x00 ,0x01 ,0x7a ,/* TPM_CC_GetCapability */ +0x00 ,0x00 ,0x00 ,0x06 ,/* TPM_CAP_TPM_PROPERTIES */ +0x00 ,0x00 ,0x01 ,0x00 ,/* TPM_PT_FIXED */ +0x00 ,0x00 ,0x00 ,0x01 ,/* property count */ +}; + +static void test_read (struct io_uring *ring,const char *path) +{ +static char buf[BUF_LEN ]; +const struct tpm_header *hdr = (const void *)buf; +struct pollfd pfd = { .events =POLLIN }; +struct io_uring_sqe *sqe; +struct io_uring_cqe *cqe; +int fd, ret, res; + + fd =open (path,O_RDWR ); +if (fd <0 ) { +if (errno ==ENOENT ) +ksft_test_result_skip ("%s: not present\n" , path); +else +ksft_test_result_fail ("%s: open: %s\n" , path, +strerror (errno)); +return ; + } + +if (write (fd, cmd,sizeof (cmd)) !=sizeof (cmd)) { +ksft_test_result_fail ("%s: write: %s\n" , path,strerror (errno)); + goto out; + } + + pfd.fd = fd; +if (poll (&pfd,1 ,POLL_TIMEOUT_MS ) !=1 ) { +ksft_test_result_fail ("%s: no response to the command\n" , path); + goto out; + } + + sqe =io_uring_get_sqe (ring); +memset (sqe,0 ,sizeof (*sqe)); + sqe->opcode =IORING_OP_READ ; + sqe->fd = fd; +/* +* Inside the response buffer: an unpatched kernel returns the response +* without its header instead of reading out of bounds +*/ + sqe->off =sizeof (*hdr); + sqe->addr = (unsigned long )buf; + sqe->len =BUF_LEN ; + ret =io_uring_submit (ring); +if (ret !=1 ) { +ksft_test_result_fail ("%s: io_uring_submit: %d\n" , path, ret); + goto out; + } + ret =io_uring_wait_cqe (ring, &cqe); +if (ret) { +ksft_test_result_fail ("%s: io_uring_wait_cqe: %d\n" , path, ret); + goto out; + } + res = cqe->res; +io_uring_cqe_seen (ring); + +/* the offset is ignored: the whole response, from its start */ +ksft_test_result (res >= (int )sizeof (*hdr) && +be32toh (hdr->size) == res && !hdr->return_code, +"%s: io_uring read %d\n" , path, res); +out: +close (fd); +} + +/* the command is TPM2 only; test_smoke.sh skips the same way */ +static int tpm_version_major (void ) +{ +FILE *f =fopen (TPM_VERSION_FILE ,"r" ); +int major =0 ; + +if (f) { +if (fscanf (f,"%d" , &major) !=1 ) + major =0 ; +fclose (f); + } +return major; +} + +int main (void ) +{ +struct io_uring ring; + +ksft_print_header (); +if (tpm_version_major () !=2 ) +ksft_exit_skip ("no TPM 2.0 device\n" ); +if (io_uring_queue_init (RING_ENTRIES , &ring,0 )) { +/* not built in, or disabled by kernel.io_uring_disabled */ +if (errno ==ENOSYS || errno ==EPERM ) +ksft_exit_skip ("io_uring_setup: %s\n" ,strerror (errno)); +ksft_exit_fail_msg ("io_uring_queue_init: %s\n" ,strerror (errno)); + } +ksft_set_plan (2 ); + +test_read (&ring,"/dev/tpm0" ); +test_read (&ring,"/dev/tpmrm0" ); + +io_uring_queue_exit (&ring); +ksft_finished (); +}Signature · 1 line
2.52.0
Thanks. I applied them.
Sorry for few igorant responses I was doing too many things.
Br, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] selftests: tpm2: test that io_uring reads ignore the file offset
Quoted 180 lines · Jarkko Sakkinen
Quoted 176 lines · Rustam Mustafin
Test that an
IORING_OP_READrequest on/dev/tpm0and/dev/tpmrm0returns the whole pending response from its start, althoughsqe->offpoints past the response header. The offset stays insidepriv->data_buffer, so an unpatched kernel fails the test on the returned header without an out-of-bounds access.Signed-off-by Rustam Mustafin <[email protected]>--- tools/testing/selftests/tpm2/.gitignore | 1 + tools/testing/selftests/tpm2/Makefile | 4 + tools/testing/selftests/tpm2/tpm_uring.c | 131 +++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/tpm2/tpm_uring.ctools/testing/selftests/tpm2/.gitignore +1
-0@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only AsyncTest.log SpaceTest.log +tpm_uringtools/testing/selftests/tpm2/Makefile +4
-0@@ -1,4 +1,8 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +CFLAGS += $(KHDR_INCLUDES) -idirafter ../../../include -Wall + +TEST_GEN_PROGS := tpm_uring + include ../lib.mk TEST_PROGS := test_smoke.sh test_space.sh test_async.shtools/testing/selftests/tpm2/tpm_uring.c new file +131
-0@@ -0,0 +1,131 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +/* +* An io_uring read chooses its own file offset. The TPM character devices +* must ignore it and return the response from its start. +*/ + +#include <endian.h> +#include <errno.h> +#include <fcntl.h> +#include <poll.h> +#include <signal.h> +#include <io_uring/mini_liburing.h> +#include "../kselftest.h" + +#define RING_ENTRIES 4 +#define BUF_LEN 4096 +#define POLL_TIMEOUT_MS 10000 +#define TPM_VERSION_FILE "/sys/class/tpm/tpm0/tpm_version_major" +struct tpm_header { +uint16_t tag; +uint32_t size; +uint32_t return_code; +} __attribute__((packed)); + +/* TPM2_GetCapability(TPM_CAP_TPM_PROPERTIES, TPM_PT_FIXED, 1) */ +static const unsigned char cmd[] = { +0x80 ,0x01 ,/* TPM_ST_NO_SESSIONS */ +0x00 ,0x00 ,0x00 ,0x16 ,/* command size */ +0x00 ,0x00 ,0x01 ,0x7a ,/* TPM_CC_GetCapability */ +0x00 ,0x00 ,0x00 ,0x06 ,/* TPM_CAP_TPM_PROPERTIES */ +0x00 ,0x00 ,0x01 ,0x00 ,/* TPM_PT_FIXED */ +0x00 ,0x00 ,0x00 ,0x01 ,/* property count */ +}; + +static void test_read (struct io_uring *ring,const char *path) +{ +static char buf[BUF_LEN ]; +const struct tpm_header *hdr = (const void *)buf; +struct pollfd pfd = { .events =POLLIN }; +struct io_uring_sqe *sqe; +struct io_uring_cqe *cqe; +int fd, ret, res; + + fd =open (path,O_RDWR ); +if (fd <0 ) { +if (errno ==ENOENT ) +ksft_test_result_skip ("%s: not present\n" , path); +else +ksft_test_result_fail ("%s: open: %s\n" , path, +strerror (errno)); +return ; + } + +if (write (fd, cmd,sizeof (cmd)) !=sizeof (cmd)) { +ksft_test_result_fail ("%s: write: %s\n" , path,strerror (errno)); + goto out; + } + + pfd.fd = fd; +if (poll (&pfd,1 ,POLL_TIMEOUT_MS ) !=1 ) { +ksft_test_result_fail ("%s: no response to the command\n" , path); + goto out; + } + + sqe =io_uring_get_sqe (ring); +memset (sqe,0 ,sizeof (*sqe)); + sqe->opcode =IORING_OP_READ ; + sqe->fd = fd; +/* +* Inside the response buffer: an unpatched kernel returns the response +* without its header instead of reading out of bounds +*/ + sqe->off =sizeof (*hdr); + sqe->addr = (unsigned long )buf; + sqe->len =BUF_LEN ; + ret =io_uring_submit (ring); +if (ret !=1 ) { +ksft_test_result_fail ("%s: io_uring_submit: %d\n" , path, ret); + goto out; + } + ret =io_uring_wait_cqe (ring, &cqe); +if (ret) { +ksft_test_result_fail ("%s: io_uring_wait_cqe: %d\n" , path, ret); + goto out; + } + res = cqe->res; +io_uring_cqe_seen (ring); + +/* the offset is ignored: the whole response, from its start */ +ksft_test_result (res >= (int )sizeof (*hdr) && +be32toh (hdr->size) == res && !hdr->return_code, +"%s: io_uring read %d\n" , path, res); +out: +close (fd); +} + +/* the command is TPM2 only; test_smoke.sh skips the same way */ +static int tpm_version_major (void ) +{ +FILE *f =fopen (TPM_VERSION_FILE ,"r" ); +int major =0 ; + +if (f) { +if (fscanf (f,"%d" , &major) !=1 ) + major =0 ; +fclose (f); + } +return major; +} + +int main (void ) +{ +struct io_uring ring; + +ksft_print_header (); +if (tpm_version_major () !=2 ) +ksft_exit_skip ("no TPM 2.0 device\n" ); +if (io_uring_queue_init (RING_ENTRIES , &ring,0 )) { +/* not built in, or disabled by kernel.io_uring_disabled */ +if (errno ==ENOSYS || errno ==EPERM ) +ksft_exit_skip ("io_uring_setup: %s\n" ,strerror (errno)); +ksft_exit_fail_msg ("io_uring_queue_init: %s\n" ,strerror (errno)); + } +ksft_set_plan (2 ); + +test_read (&ring,"/dev/tpm0" ); +test_read (&ring,"/dev/tpmrm0" ); + +io_uring_queue_exit (&ring); +ksft_finished (); +}Signature · 1 line
2.52.0Thanks. I applied them.
Quoted 6 lines
Sorry for few igorant responses I was doing too many things.
Reviewed-by Jarkko Sakkinen <[email protected]>Br, Jarkko
Second thoughts on 2/2 but I'll apply 1/2.
checkpatch.pl --strict:
WARNING: Prefer __packed over __attribute__((packed)) #74: FILE: tools/testing/selftests/tpm2/tpm_uring.c:23:
+} __attribute__((packed));
WARNING: ENOSYS means 'invalid syscall nr' and nothing else #171: FILE: tools/testing/selftests/tpm2/tpm_uring.c:120:
+ if (errno == ENOSYS || errno == EPERM)
Also, this cannot be in C.
Br, Jarkko
PATCH v3 1/1 selftests: tpm2: test that io_uring reads ignore the file offset
Test that an IORING_OP_READ request on /dev/tpm0 and /dev/tpmrm0 returns the whole pending response from its start, although sqe->off points past the response header. The offset stays inside priv->data_buffer, so an unpatched kernel fails the test on the returned header without an out-of-bounds access.
---
Changes in v3:
- rewrite in Python as tpm2_tests.UringTest, run by test_uring.sh (Jarkko)
- io_uring through liburing-ffi (liburing 2.4+)
- test_uring.sh skips when liburing-ffi is missing or io_uring_queue_init() fails
tools/testing/selftests/tpm2/Makefile | 2 +- tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ tools/testing/selftests/tpm2/tpm2_tests.py | 71 ++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/tpm2/test_uring.sh
tools/testing/selftests/tpm2/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) include ../lib.mk -TEST_PROGS := test_smoke.sh test_space.sh test_async.sh +TEST_PROGS := test_smoke.sh test_space.sh test_async.sh test_uring.sh TEST_PROGS_EXTENDED := tpm2.py tpm2_tests.py
tools/testing/selftests/tpm2/test_uring.sh new file +15 -0
@@ -0,0 +1,15 @@ +#!/bin/sh +# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + +[ -e /dev/tpm0 ] || exit $ksft_skip +[ -e /dev/tpmrm0 ] || exit $ksft_skip +read tpm_version < /sys/class/tpm/tpm0/tpm_version_major +[ "$tpm_version" = 2 ] || exit $ksft_skip + +python3 -c 'import sys, tpm2_tests +sys.exit(not tpm2_tests.uring_available())' || exit $ksft_skip + +python3 -m unittest -v tpm2_tests.UringTest 2>&1
tools/testing/selftests/tpm2/tpm2_tests.py +71 -0
74 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v3] selftests: tpm2: test that io_uring reads ignore the file offset
tools/testing/selftests/tpm2/test_uring.sh line 2
Quoted 2 lines
tools/testing/selftests/tpm2/test_uring.sh new file +2
-0+#!/bin/sh +# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
This should be GPL-2.0-only, as you asked on the RFC; I copied the line from the neighbouring scripts. I can send a v4 with only that change, or you can fix it up when applying, whichever you prefer.
Br,
Rustam
Jarkko Sakkinen
maintainerRe: [PATCH v3] selftests: tpm2: test that io_uring reads ignore the file offset
Quoted 6 lines · Rustam Mustafin
tools/testing/selftests/tpm2/test_uring.sh line 2
Quoted 2 lines
tools/testing/selftests/tpm2/test_uring.sh new file +2
-0+#!/bin/sh +# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)This should be GPL-2.0-only, as you asked on the RFC; I copied the line from the neighbouring scripts. I can send a v4 with only that change, or you can fix it up when applying, whichever you prefer.
NP, I can do it.
Quoted 3 lines
Br,
Rustam
Br, Jarkko
Re: [PATCH v3] selftests: tpm2: test that io_uring reads ignore the file offset
In reply to Rustam MustafinQuoted 135 lines · Rustam Mustafin
Test that an
IORING_OP_READrequest on/dev/tpm0and/dev/tpmrm0returns the whole pending response from its start, althoughsqe->offpoints past the response header. The offset stays insidepriv->data_buffer, so an unpatched kernel fails the test on the returned header without an out-of-bounds access.Signed-off-by Rustam Mustafin <[email protected]>---
Changes in v3:
- rewrite in Python as tpm2_tests.UringTest, run bytest_uring.sh(Jarkko)
-io_uringthrough liburing-ffi (liburing 2.4+)
-test_uring.shskips when liburing-ffi is missing orio_uring_queue_init()failstools/testing/selftests/tpm2/Makefile | 2 +- tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ tools/testing/selftests/tpm2/tpm2_tests.py | 71 ++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/tpm2/test_uring.shtools/testing/selftests/tpm2/Makefile +1
-1@@ -1,5 +1,5 @@ # SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) include ../lib.mk -TEST_PROGS := test_smoke.sh test_space.sh test_async.sh +TEST_PROGS := test_smoke.sh test_space.sh test_async.sh test_uring.sh TEST_PROGS_EXTENDED := tpm2.py tpm2_tests.pytools/testing/selftests/tpm2/test_uring.sh new file +15
-0@@ -0,0 +1,15 @@ +#!/bin/sh +# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + +[ -e /dev/tpm0 ] || exit $ksft_skip +[ -e /dev/tpmrm0 ] || exit $ksft_skip +read tpm_version < /sys/class/tpm/tpm0/tpm_version_major +[ "$tpm_version" = 2 ] || exit $ksft_skip + +python3 -c 'import sys, tpm2_tests +sys.exit(not tpm2_tests.uring_available())' || exit $ksft_skip + +python3 -m unittest -v tpm2_tests.UringTest 2>&1tools/testing/selftests/tpm2/tpm2_tests.py +71
-0@@ -331,3 +331,74 @@ class AsyncTest(unittest.TestCase): self.assertEqual(rc, 22) async_client.close() + +def uring_available(): + import ctypes.util + + name = ctypes.util.find_library('uring-ffi') + if not name: + return False + lib = ctypes.CDLL(name) + ring = ctypes.create_string_buffer(1024) + if lib.io_uring_queue_init(1, ring, 0) < 0: + return False + lib.io_uring_queue_exit(ring) + return True + +def uring_read(fd, size, off): + import ctypes.util + + lib = ctypes.CDLL(ctypes.util.find_library('uring-ffi')) + lib.io_uring_get_sqe.restype = ctypes.c_void_p + lib.io_uring_prep_read.argtypes = [ctypes.c_void_p, ctypes.c_int, + ctypes.c_void_p, ctypes.c_uint, + ctypes.c_uint64] + ring = ctypes.create_string_buffer(1024) + ret = lib.io_uring_queue_init(1, ring, 0) + if ret < 0: + raise OSError(-ret, 'io_uring_queue_init') + try: + buf = ctypes.create_string_buffer(size) + lib.io_uring_prep_read(lib.io_uring_get_sqe(ring), fd, buf, size, off) + ret = lib.io_uring_submit(ring) + if ret < 0: + raise OSError(-ret, 'io_uring_submit') + cqe = ctypes.c_void_p() + ret = lib.io_uring_wait_cqe(ring, ctypes.byref(cqe)) + if ret < 0: + raise OSError(-ret, 'io_uring_wait_cqe') + # cqe->res follows the 64-bit user_data + res = ctypes.c_int.from_address(cqe.value + 8).value + finally: + lib.io_uring_queue_exit(ring) + + return res, buf.raw + +class UringTest(unittest.TestCase): + def read_response(self, flags): + client = tpm2.Client(flags) + fmt = '>HII III' + cmd = struct.pack(fmt, + tpm2.TPM2_ST_NO_SESSIONS, + struct.calcsize(fmt), + tpm2.TPM2_CC_GET_CAPABILITY, + tpm2.TPM2_CAP_TPM_PROPERTIES, + tpm2.TPM2_PT_FIXED, 1) + try: + client.tpm.write(cmd) + # Offset 10 stays inside the response, so an unpatched kernel + # skips the header instead of reading out of bounds + res, rsp = uring_read(client.tpm.fileno(), 4096, 10) + finally: + client.close() + + self.assertGreaterEqual(res, 10) + sz, rc = struct.unpack('>II', rsp[2:10]) + self.assertEqual(sz, res) + self.assertEqual(rc, 0) + + def test_read_tpm(self): + self.read_response(0) + + def test_read_tpmrm(self): + self.read_response(tpm2.Client.FLAG_SPACE)Signature · 1 line
2.52.0
Thank you.
It's just best not to spread tests to multiple languages without any special reasons, that's all.
Br, Jarkko