All of lore.kernel.org
 help / color / mirror / Atom feed
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: [RFC PATCH 1/1] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0
Date: Fri, 20 Nov 2026 21:21:16 +0700	[thread overview]
Message-ID: <20261120142116.1630010-2-rmustafin@cloudlinux.com> (raw)
In-Reply-To: <20261120142116.1630010-1-rmustafin@cloudlinux.com>

Add tpm_common_read_iter() and tpm_common_write_iter(), which fail
io_uring and AIO requests with -EIO. read() and write() are not
affected, as the VFS still calls ->read() and ->write() for them.
readv() and writev() now reach the new callbacks instead of the VFS
loop over ->read() and ->write(), so the callbacks keep that loop and
call tpm_common_read() and tpm_common_write() once per iovec.

Update the tpm_uring kselftest to expect -EIO, and check that the
rejected request leaves the whole response for the following read().

Suggested-by: Jarkko Sakkinen <jarkko@kernel.org>
Link: https://lore.kernel.org/r/av23L5HBpy2gbm3m@kernel.org
Signed-off-by: Rustam Mustafin <rmustafin@cloudlinux.com>
---
 drivers/char/tpm/tpm-dev-common.c        | 72 ++++++++++++++++++++++++
 drivers/char/tpm/tpm-dev.c               |  4 ++
 drivers/char/tpm/tpm-dev.h               |  4 ++
 drivers/char/tpm/tpmrm-dev.c             |  4 ++
 tools/testing/selftests/tpm2/tpm_uring.c | 14 ++---
 5 files changed, 91 insertions(+), 7 deletions(-)

diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c
index 553fc6aca94d..e40a251315f3 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,76 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
 	return ret;
 }
 
+#ifdef CONFIG_IO_URING
+/*
+ * 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;
+}
+#endif /* CONFIG_IO_URING */
+
 __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..56751be46d90 100644
--- a/drivers/char/tpm/tpm-dev.c
+++ b/drivers/char/tpm/tpm-dev.c
@@ -62,6 +62,10 @@ const struct file_operations tpm_fops = {
 	.open = tpm_open,
 	.read = tpm_common_read,
 	.write = tpm_common_write,
+#ifdef CONFIG_IO_URING
+	.read_iter = tpm_common_read_iter,
+	.write_iter = tpm_common_write_iter,
+#endif
 	.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..22ea88156833 100644
--- a/drivers/char/tpm/tpm-dev.h
+++ b/drivers/char/tpm/tpm-dev.h
@@ -28,6 +28,10 @@ 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);
+#ifdef CONFIG_IO_URING
+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);
+#endif
 __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..95788c6bdd4c 100644
--- a/drivers/char/tpm/tpmrm-dev.c
+++ b/drivers/char/tpm/tpmrm-dev.c
@@ -49,6 +49,10 @@ const struct file_operations tpmrm_fops = {
 	.open = tpmrm_open,
 	.read = tpm_common_read,
 	.write = tpm_common_write,
+#ifdef CONFIG_IO_URING
+	.read_iter = tpm_common_read_iter,
+	.write_iter = tpm_common_write_iter,
+#endif
 	.poll = tpm_common_poll,
 	.release = tpmrm_release,
 };
diff --git a/tools/testing/selftests/tpm2/tpm_uring.c b/tools/testing/selftests/tpm2/tpm_uring.c
index 9caa3cba5bf0..37b41c7d295b 100644
--- a/tools/testing/selftests/tpm2/tpm_uring.c
+++ b/tools/testing/selftests/tpm2/tpm_uring.c
@@ -1,7 +1,6 @@
 // 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.
+ * The TPM character devices must reject io_uring reads.
  */
 
 #include <endian.h>
@@ -39,7 +38,7 @@ static void test_read(struct io_uring *ring, const char *path)
 	struct pollfd pfd = { .events = POLLIN };
 	struct io_uring_sqe *sqe;
 	struct io_uring_cqe *cqe;
-	int fd, ret, res;
+	int fd, ret, res, n;
 
 	fd = open(path, O_RDWR);
 	if (fd < 0) {
@@ -86,10 +85,11 @@ static void test_read(struct io_uring *ring, const char *path)
 	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);
+	/* the rejected read must leave the whole response in place */
+	n = read(fd, buf, BUF_LEN);
+	ksft_test_result(res == -EIO && n >= (int)sizeof(*hdr) &&
+			 be32toh(hdr->size) == n && !hdr->return_code,
+			 "%s: io_uring read %d, read() %d\n", path, res, n);
 out:
 	close(fd);
 }
-- 
2.52.0


  reply	other threads:[~2026-11-20 14:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 14:21 [RFC PATCH 0/1] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 Rustam Mustafin
2026-11-20 14:21 ` Rustam Mustafin [this message]
2026-11-20 14:55   ` [RFC PATCH 1/1] " Jarkko Sakkinen

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=20261120142116.1630010-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.