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 1/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0
Date: Wed, 25 Nov 2026 13:54:28 +0700 [thread overview]
Message-ID: <20261125065429.1483868-1-rmustafin@cloudlinux.com> (raw)
In-Reply-To: <awXHzblUfp05jZSQ@kernel.org>
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
next prev parent reply other threads:[~2026-11-25 6:54 UTC|newest]
Thread overview: 12+ 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 17:22 ` Jarkko Sakkinen
2026-11-25 6:54 ` Rustam Mustafin [this message]
2026-11-25 6:54 ` [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected Rustam Mustafin
2026-11-25 16:57 ` Jarkko Sakkinen
2026-11-25 16:58 ` [PATCH v3 1/2] tpm: Do not allow io_uring for /dev/tpm0 and /dev/tpmrm0 Jarkko Sakkinen
2026-11-25 17:12 ` Jarkko Sakkinen
2026-11-25 17:18 ` Jarkko Sakkinen
2026-11-26 14:08 ` Rustam Mustafin
2026-11-26 17:27 ` Jarkko Sakkinen
2026-11-24 14:24 ` [PATCH v3 2/2] selftests: tpm2: test that io_uring reads are rejected Rustam Mustafin
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=20261125065429.1483868-1-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.