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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rustam Mustafin <[email protected]> --- 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
