From: Rustam Mustafin <rmustafin@cloudlinux.com>
To: Peter Huewe <peterhuewe@gmx.de>, Jarkko Sakkinen <jarkko@kernel.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>, Shuah Khan <shuah@kernel.org>,
Jaewon Yang <yong010301@gmail.com>,
linux-integrity@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org,
Rustam Mustafin <rmustafin@cloudlinux.com>
Subject: [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected
Date: Tue, 24 Nov 2026 21:24:32 +0700 [thread overview]
Message-ID: <20261124142432.368943-3-rmustafin@cloudlinux.com> (raw)
In-Reply-To: <20261124142432.368943-1-rmustafin@cloudlinux.com>
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
prev parent reply other threads:[~2026-11-24 14:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124142432.368943-3-rmustafin@cloudlinux.com \
--to=rmustafin@cloudlinux.com \
--cc=jarkko@kernel.org \
--cc=jgg@ziepe.ca \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=peterhuewe@gmx.de \
--cc=shuah@kernel.org \
--cc=yong010301@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox