Re: [PATCH v3] selftests: tpm2: test that io_uring reads ignore the file offset
From: Jarkko Sakkinen
Date: Mon Nov 23 2026 - 14:05:57 EST
On Mon, Nov 23, 2026 at 11:56:38PM +0700, Rustam Mustafin wrote:
> 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.
>
> Signed-off-by: Rustam Mustafin <rmustafin@xxxxxxxxxxxxxx>
> ---
> 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
>
> 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..bb6e2e10162e
> --- /dev/null
> +++ b/tools/testing/selftests/tpm2/test_uring.sh
> @@ -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
> diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
> index ffe98b5c8d22..dc4e642450d6 100644
> --- a/tools/testing/selftests/tpm2/tpm2_tests.py
> +++ b/tools/testing/selftests/tpm2/tpm2_tests.py
> @@ -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)
> --
> 2.52.0
>
Thank you.
It's just best not to spread tests to multiple languages without
any special reasons, that's all.
Reviewed-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
Br, Jarkko