* [PATCH v3 0/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 @ 2026-11-24 14:24 Rustam Mustafin 2026-11-24 14:24 ` [PATCH v3 1/2] " Rustam Mustafin 2026-11-24 14:24 ` Rustam Mustafin 0 siblings, 2 replies; 6+ messages in thread From: Rustam Mustafin @ 2026-11-24 14:24 UTC (permalink / raw) To: Peter Huewe, Jarkko Sakkinen Cc: Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel, Rustam Mustafin io_uring passes its own file offset to the TPM character devices despite nonseekable_open(), and its asynchronous requests do not fit the single command and response per open file. Patch 1 rejects io_uring and AIO with -EIO. Patch 2 adds the selftest and replaces the separate selftest patch [1]. The series applies on top of "tpm: Track the response read position internally" [2]. preadv2() and pwritev2() with RWF_SYNC, RWF_DSYNC, RWF_APPEND or RWF_NOAPPEND now succeed and ignore the flag, instead of failing with -EOPNOTSUPP. Tested with swtpm in QEMU: the tpm2 selftests pass, and the new one fails without patch 1. Changes in v3: - squash the selftest into this series, SPDX GPL-2.0-only (Jarkko) - keep tpm_common_read_iter() and tpm_common_write_iter() non-static: like tpm_common_read() and tpm_common_write(), they are used in tpm-dev.c and tpmrm-dev.c (Jarkko) Changes in v2: - open the message with the use case, rewrap (Jarkko) - drop the CONFIG_IO_URING guards (Jarkko) - drop Suggested-by [1] https://lore.kernel.org/all/20261123165638.2161305-1-rmustafin@cloudlinux.com/ [2] https://lore.kernel.org/all/20261120121614.978396-2-rmustafin@cloudlinux.com/ v2: https://lore.kernel.org/all/20261123173321.2363162-1-rmustafin@cloudlinux.com/ v1: https://lore.kernel.org/all/20261120142116.1630010-1-rmustafin@cloudlinux.com/ Rustam Mustafin (2): tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 selftests: tpm2: test that io_uring reads are rejected drivers/char/tpm/tpm-dev-common.c | 70 +++++++++++++++++++++ drivers/char/tpm/tpm-dev.c | 2 + drivers/char/tpm/tpm-dev.h | 2 + drivers/char/tpm/tpmrm-dev.c | 2 + tools/testing/selftests/tpm2/Makefile | 2 +- tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ tools/testing/selftests/tpm2/tpm2_tests.py | 72 ++++++++++++++++++++++ 7 files changed, 164 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/tpm2/test_uring.sh -- 2.52.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 1/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 2026-11-24 14:24 [PATCH v3 0/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 Rustam Mustafin @ 2026-11-24 14:24 ` Rustam Mustafin 2026-11-24 17:22 ` Jarkko Sakkinen 2026-11-24 14:24 ` Rustam Mustafin 1 sibling, 1 reply; 6+ messages in thread From: Rustam Mustafin @ 2026-11-24 14:24 UTC (permalink / raw) To: Peter Huewe, Jarkko Sakkinen Cc: Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel, Rustam Mustafin io_uring passes its own file offset to the TPM character devices, although they are opened with nonseekable_open(): IORING_OP_READ copies sqe->off into ki_pos without checking FMODE_PREAD. The devices also keep a single command and response per open file, which does not fit asynchronous requests. Add tpm_common_read_iter() and tpm_common_write_iter(), which fail io_uring and AIO requests with -EIO. A multishot read uses a synchronous kiocb, so also reject IOCB_NOWAIT and io-wq workers. The callbacks call tpm_common_read() and tpm_common_write() once per iovec, so readv() can still split a response into the header and the body. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com> --- drivers/char/tpm/tpm-dev-common.c | 70 +++++++++++++++++++++++++++++++ drivers/char/tpm/tpm-dev.c | 2 + drivers/char/tpm/tpm-dev.h | 2 + drivers/char/tpm/tpmrm-dev.c | 2 + 4 files changed, 76 insertions(+) diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c index 553fc6aca94d..5189c1d72058 100644 --- a/drivers/char/tpm/tpm-dev-common.c +++ b/drivers/char/tpm/tpm-dev-common.c @@ -13,8 +13,10 @@ * Device file system interface to the TPM */ #include <linux/poll.h> +#include <linux/sched.h> #include <linux/slab.h> #include <linux/uaccess.h> +#include <linux/uio.h> #include <linux/workqueue.h> #include "tpm.h" #include "tpm-dev.h" @@ -253,6 +255,74 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf, return ret; } +/* + * Reject io_uring and AIO. IORING_OP_READ_MULTISHOT requests use a + * synchronous kiocb, so also check for NOWAIT attempts and io-wq workers. + */ +static bool tpm_iter_is_async(struct kiocb *iocb) +{ + return !is_sync_kiocb(iocb) || + (iocb->ki_flags & IOCB_NOWAIT) || + (current->flags & PF_IO_WORKER); +} + +/* Preserve the VFS fallback's per-iovec read/write semantics. */ +ssize_t tpm_common_read_iter(struct kiocb *iocb, struct iov_iter *to) +{ + struct file *file = iocb->ki_filp; + ssize_t ret = 0; + + if (tpm_iter_is_async(iocb) || !user_backed_iter(to)) + return -EIO; + + while (iov_iter_count(to)) { + size_t len = iter_iov_len(to); + ssize_t nr; + + nr = tpm_common_read(file, iter_iov_addr(to), + len, &iocb->ki_pos); + if (nr < 0) { + if (!ret) + ret = nr; + break; + } + ret += nr; + iov_iter_advance(to, nr); + if (nr != len) + break; + } + + return ret; +} + +ssize_t tpm_common_write_iter(struct kiocb *iocb, struct iov_iter *from) +{ + struct file *file = iocb->ki_filp; + ssize_t ret = 0; + + if (tpm_iter_is_async(iocb) || !user_backed_iter(from)) + return -EIO; + + while (iov_iter_count(from)) { + size_t len = iter_iov_len(from); + ssize_t nr; + + nr = tpm_common_write(file, iter_iov_addr(from), + len, &iocb->ki_pos); + if (nr < 0) { + if (!ret) + ret = nr; + break; + } + ret += nr; + iov_iter_advance(from, nr); + if (nr != len) + break; + } + + return ret; +} + __poll_t tpm_common_poll(struct file *file, poll_table *wait) { struct file_priv *priv = file->private_data; diff --git a/drivers/char/tpm/tpm-dev.c b/drivers/char/tpm/tpm-dev.c index 74488f0a7b78..9c2f0ad826be 100644 --- a/drivers/char/tpm/tpm-dev.c +++ b/drivers/char/tpm/tpm-dev.c @@ -62,6 +62,8 @@ const struct file_operations tpm_fops = { .open = tpm_open, .read = tpm_common_read, .write = tpm_common_write, + .read_iter = tpm_common_read_iter, + .write_iter = tpm_common_write_iter, .poll = tpm_common_poll, .release = tpm_release, }; diff --git a/drivers/char/tpm/tpm-dev.h b/drivers/char/tpm/tpm-dev.h index 34efb1ffa491..2f2186e6f094 100644 --- a/drivers/char/tpm/tpm-dev.h +++ b/drivers/char/tpm/tpm-dev.h @@ -28,6 +28,8 @@ ssize_t tpm_common_read(struct file *file, char __user *buf, size_t size, loff_t *off); ssize_t tpm_common_write(struct file *file, const char __user *buf, size_t size, loff_t *off); +ssize_t tpm_common_read_iter(struct kiocb *iocb, struct iov_iter *to); +ssize_t tpm_common_write_iter(struct kiocb *iocb, struct iov_iter *from); __poll_t tpm_common_poll(struct file *file, poll_table *wait); void tpm_common_release(struct file *file, struct file_priv *priv); diff --git a/drivers/char/tpm/tpmrm-dev.c b/drivers/char/tpm/tpmrm-dev.c index 19e8f2779265..0c8c1a1b0b3b 100644 --- a/drivers/char/tpm/tpmrm-dev.c +++ b/drivers/char/tpm/tpmrm-dev.c @@ -49,6 +49,8 @@ const struct file_operations tpmrm_fops = { .open = tpmrm_open, .read = tpm_common_read, .write = tpm_common_write, + .read_iter = tpm_common_read_iter, + .write_iter = tpm_common_write_iter, .poll = tpm_common_poll, .release = tpmrm_release, }; -- 2.52.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 2026-11-24 14:24 ` [PATCH v3 1/2] " Rustam Mustafin @ 2026-11-24 17:22 ` Jarkko Sakkinen 2026-11-25 6:54 ` Rustam Mustafin 0 siblings, 1 reply; 6+ messages in thread From: Jarkko Sakkinen @ 2026-11-24 17:22 UTC (permalink / raw) To: Rustam Mustafin Cc: Peter Huewe, Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel On Tue, Nov 24, 2026 at 09:24:31PM +0700, Rustam Mustafin wrote: > Assisted-by: Claude:claude-opus-5-5 Drop this and send one more. Br, Jarkko ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 1/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 2026-11-24 17:22 ` Jarkko Sakkinen @ 2026-11-25 6:54 ` Rustam Mustafin 2026-11-25 6:54 ` [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected Rustam Mustafin 0 siblings, 1 reply; 6+ messages in thread From: Rustam Mustafin @ 2026-11-25 6:54 UTC (permalink / raw) To: Peter Huewe, Jarkko Sakkinen Cc: Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel, Rustam Mustafin io_uring passes its own file offset to the TPM character devices, although they are opened with nonseekable_open(): IORING_OP_READ copies sqe->off into ki_pos without checking FMODE_PREAD. The devices also keep a single command and response per open file, which does not fit asynchronous requests. Add tpm_common_read_iter() and tpm_common_write_iter(), which fail io_uring and AIO requests with -EIO. A multishot read uses a synchronous kiocb, so also reject IOCB_NOWAIT and io-wq workers. The callbacks call tpm_common_read() and tpm_common_write() once per iovec, so readv() can still split a response into the header and the body. Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com> --- drivers/char/tpm/tpm-dev-common.c | 70 +++++++++++++++++++++++++++++++ drivers/char/tpm/tpm-dev.c | 2 + drivers/char/tpm/tpm-dev.h | 2 + drivers/char/tpm/tpmrm-dev.c | 2 + 4 files changed, 76 insertions(+) diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c index 553fc6aca94d..5189c1d72058 100644 --- a/drivers/char/tpm/tpm-dev-common.c +++ b/drivers/char/tpm/tpm-dev-common.c @@ -13,8 +13,10 @@ * Device file system interface to the TPM */ #include <linux/poll.h> +#include <linux/sched.h> #include <linux/slab.h> #include <linux/uaccess.h> +#include <linux/uio.h> #include <linux/workqueue.h> #include "tpm.h" #include "tpm-dev.h" @@ -253,6 +255,74 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf, return ret; } +/* + * Reject io_uring and AIO. IORING_OP_READ_MULTISHOT requests use a + * synchronous kiocb, so also check for NOWAIT attempts and io-wq workers. + */ +static bool tpm_iter_is_async(struct kiocb *iocb) +{ + return !is_sync_kiocb(iocb) || + (iocb->ki_flags & IOCB_NOWAIT) || + (current->flags & PF_IO_WORKER); +} + +/* Preserve the VFS fallback's per-iovec read/write semantics. */ +ssize_t tpm_common_read_iter(struct kiocb *iocb, struct iov_iter *to) +{ + struct file *file = iocb->ki_filp; + ssize_t ret = 0; + + if (tpm_iter_is_async(iocb) || !user_backed_iter(to)) + return -EIO; + + while (iov_iter_count(to)) { + size_t len = iter_iov_len(to); + ssize_t nr; + + nr = tpm_common_read(file, iter_iov_addr(to), + len, &iocb->ki_pos); + if (nr < 0) { + if (!ret) + ret = nr; + break; + } + ret += nr; + iov_iter_advance(to, nr); + if (nr != len) + break; + } + + return ret; +} + +ssize_t tpm_common_write_iter(struct kiocb *iocb, struct iov_iter *from) +{ + struct file *file = iocb->ki_filp; + ssize_t ret = 0; + + if (tpm_iter_is_async(iocb) || !user_backed_iter(from)) + return -EIO; + + while (iov_iter_count(from)) { + size_t len = iter_iov_len(from); + ssize_t nr; + + nr = tpm_common_write(file, iter_iov_addr(from), + len, &iocb->ki_pos); + if (nr < 0) { + if (!ret) + ret = nr; + break; + } + ret += nr; + iov_iter_advance(from, nr); + if (nr != len) + break; + } + + return ret; +} + __poll_t tpm_common_poll(struct file *file, poll_table *wait) { struct file_priv *priv = file->private_data; diff --git a/drivers/char/tpm/tpm-dev.c b/drivers/char/tpm/tpm-dev.c index 74488f0a7b78..9c2f0ad826be 100644 --- a/drivers/char/tpm/tpm-dev.c +++ b/drivers/char/tpm/tpm-dev.c @@ -62,6 +62,8 @@ const struct file_operations tpm_fops = { .open = tpm_open, .read = tpm_common_read, .write = tpm_common_write, + .read_iter = tpm_common_read_iter, + .write_iter = tpm_common_write_iter, .poll = tpm_common_poll, .release = tpm_release, }; diff --git a/drivers/char/tpm/tpm-dev.h b/drivers/char/tpm/tpm-dev.h index 34efb1ffa491..2f2186e6f094 100644 --- a/drivers/char/tpm/tpm-dev.h +++ b/drivers/char/tpm/tpm-dev.h @@ -28,6 +28,8 @@ ssize_t tpm_common_read(struct file *file, char __user *buf, size_t size, loff_t *off); ssize_t tpm_common_write(struct file *file, const char __user *buf, size_t size, loff_t *off); +ssize_t tpm_common_read_iter(struct kiocb *iocb, struct iov_iter *to); +ssize_t tpm_common_write_iter(struct kiocb *iocb, struct iov_iter *from); __poll_t tpm_common_poll(struct file *file, poll_table *wait); void tpm_common_release(struct file *file, struct file_priv *priv); diff --git a/drivers/char/tpm/tpmrm-dev.c b/drivers/char/tpm/tpmrm-dev.c index 19e8f2779265..0c8c1a1b0b3b 100644 --- a/drivers/char/tpm/tpmrm-dev.c +++ b/drivers/char/tpm/tpmrm-dev.c @@ -49,6 +49,8 @@ const struct file_operations tpmrm_fops = { .open = tpmrm_open, .read = tpm_common_read, .write = tpm_common_write, + .read_iter = tpm_common_read_iter, + .write_iter = tpm_common_write_iter, .poll = tpm_common_poll, .release = tpmrm_release, }; -- 2.52.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected 2026-11-25 6:54 ` Rustam Mustafin @ 2026-11-25 6:54 ` Rustam Mustafin 0 siblings, 0 replies; 6+ messages in thread From: Rustam Mustafin @ 2026-11-25 6:54 UTC (permalink / raw) To: Peter Huewe, Jarkko Sakkinen Cc: Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel, Rustam Mustafin Test that IORING_OP_READ on /dev/tpm0 and /dev/tpmrm0 fails with -EIO and that the following read() returns the whole response. The test needs liburing-ffi (liburing 2.4+) and is skipped without it. Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com> --- tools/testing/selftests/tpm2/Makefile | 2 +- tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ tools/testing/selftests/tpm2/tpm2_tests.py | 72 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/tpm2/test_uring.sh diff --git a/tools/testing/selftests/tpm2/Makefile b/tools/testing/selftests/tpm2/Makefile index a9bf9459fb25..44630fb980fa 100644 --- a/tools/testing/selftests/tpm2/Makefile +++ b/tools/testing/selftests/tpm2/Makefile @@ -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 diff --git a/tools/testing/selftests/tpm2/test_uring.sh b/tools/testing/selftests/tpm2/test_uring.sh new file mode 100755 index 000000000000..516985ef20af --- /dev/null +++ b/tools/testing/selftests/tpm2/test_uring.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-only + +# 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 diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py index ffe98b5c8d22..6fe60e00ff06 100644 --- a/tools/testing/selftests/tpm2/tpm2_tests.py +++ b/tools/testing/selftests/tpm2/tpm2_tests.py @@ -9,6 +9,7 @@ from tpm2 import ProtocolError import unittest import logging import struct +import errno class SmokeTest(unittest.TestCase): def setUp(self): @@ -331,3 +332,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): + 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, 0) + 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 + +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) + res = uring_read(client.tpm.fileno(), 4096) + # The rejected read must leave the whole response in place + rsp = client.tpm.read(4096) + finally: + client.close() + + self.assertEqual(res, -errno.EIO) + sz, rc = struct.unpack('>II', rsp[2:10]) + self.assertEqual(sz, len(rsp)) + 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) -- 2.52.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected 2026-11-24 14:24 [PATCH v3 0/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 Rustam Mustafin 2026-11-24 14:24 ` [PATCH v3 1/2] " Rustam Mustafin @ 2026-11-24 14:24 ` Rustam Mustafin 1 sibling, 0 replies; 6+ messages in thread From: Rustam Mustafin @ 2026-11-24 14:24 UTC (permalink / raw) To: Peter Huewe, Jarkko Sakkinen Cc: Jason Gunthorpe, Shuah Khan, Jaewon Yang, linux-integrity, linux-kselftest, linux-kernel, Rustam Mustafin Test that IORING_OP_READ on /dev/tpm0 and /dev/tpmrm0 fails with -EIO and that the following read() returns the whole response. The test needs liburing-ffi (liburing 2.4+) and is skipped without it. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com> --- tools/testing/selftests/tpm2/Makefile | 2 +- tools/testing/selftests/tpm2/test_uring.sh | 15 +++++ tools/testing/selftests/tpm2/tpm2_tests.py | 72 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/tpm2/test_uring.sh diff --git a/tools/testing/selftests/tpm2/Makefile b/tools/testing/selftests/tpm2/Makefile index a9bf9459fb25..44630fb980fa 100644 --- a/tools/testing/selftests/tpm2/Makefile +++ b/tools/testing/selftests/tpm2/Makefile @@ -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 diff --git a/tools/testing/selftests/tpm2/test_uring.sh b/tools/testing/selftests/tpm2/test_uring.sh new file mode 100755 index 000000000000..516985ef20af --- /dev/null +++ b/tools/testing/selftests/tpm2/test_uring.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0-only + +# 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 diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py index ffe98b5c8d22..6fe60e00ff06 100644 --- a/tools/testing/selftests/tpm2/tpm2_tests.py +++ b/tools/testing/selftests/tpm2/tpm2_tests.py @@ -9,6 +9,7 @@ from tpm2 import ProtocolError import unittest import logging import struct +import errno class SmokeTest(unittest.TestCase): def setUp(self): @@ -331,3 +332,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): + 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, 0) + 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 + +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) + res = uring_read(client.tpm.fileno(), 4096) + # The rejected read must leave the whole response in place + rsp = client.tpm.read(4096) + finally: + client.close() + + self.assertEqual(res, -errno.EIO) + sz, rc = struct.unpack('>II', rsp[2:10]) + self.assertEqual(sz, len(rsp)) + 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) -- 2.52.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-25 6:54 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-24 14:24 [PATCH v3 0/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 Rustam Mustafin 2026-11-24 14:24 ` [PATCH v3 1/2] " Rustam Mustafin 2026-11-24 17:22 ` Jarkko Sakkinen 2026-11-25 6:54 ` Rustam Mustafin 2026-11-25 6:54 ` [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected Rustam Mustafin 2026-11-24 14:24 ` Rustam Mustafin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox