[PATCH 8/9] fs: don't allow kernel reads and writes without iter ops

Christoph Hellwig
2020-06-26T07:59:29Z

Don't allow calling ->read or ->write with set_fs as a preparation for
killing off set_fs. While I've not triggered any of these cases in my
setups as all the usual suspect (file systems, pipes, sockets, block
devices, system character devices) use the iter ops this is almost
going to be guaranteed to eventuall break something, so print a detailed
error message helping to debug such cases. The fix will be to switch the
affected driver to use the iter ops.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
fs/read_write.c | 33 ++++++++++++++++++---------------
1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index e765c95ff3440d..ae463bcadb6906 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -420,6 +420,18 @@ ssize_t iter_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos,
return ret;
}

+static void warn_unsupported(struct file *file, const char *op)
+{
+ char pathname[128], *path;
+
+ path = file_path(file, pathname, sizeof(pathname));
+ if (IS_ERR(path))
+ path = "(unknown)";
+ pr_warn_ratelimited(
+ "kernel %s not supported for file %s (pid: %d comm: %.20s)\n",
+ op, path, current->pid, current->comm);
+}
+
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
{
ssize_t ret;
@@ -431,13 +443,7 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)

if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
- if (file->f_op->read) {
- mm_segment_t old_fs = get_fs();
-
- set_fs(KERNEL_DS);
- ret = file->f_op->read(file, (void __user *)buf, count, pos);
- set_fs(old_fs);
- } else if (file->f_op->read_iter) {
+ if (file->f_op->read_iter) {
struct kvec iov = { .iov_base = buf, .iov_len = count };
struct kiocb kiocb;
struct iov_iter iter;
@@ -448,6 +454,8 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
ret = file->f_op->read_iter(&kiocb, &iter);
*pos = kiocb.ki_pos;
} else {
+ if (file->f_op->read)
+ warn_unsupported(file, "read");
ret = -EINVAL;
}
if (ret > 0) {
@@ -532,14 +540,7 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count,

if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
- if (file->f_op->write) {
- mm_segment_t old_fs = get_fs();
-
- set_fs(KERNEL_DS);
- ret = file->f_op->write(file, (__force const char __user *)buf,
- count, pos);
- set_fs(old_fs);
- } else if (file->f_op->write_iter) {
+ if (file->f_op->write_iter) {
struct kvec iov = { .iov_base = (void *)buf, .iov_len = count };
struct kiocb kiocb;
struct iov_iter iter;
@@ -551,6 +552,8 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count,
if (ret > 0)
*pos = kiocb.ki_pos;
} else {
+ if (file->f_op->write)
+ warn_unsupported(file, "write");
ret = -EINVAL;
}
if (ret > 0) {
--
2.26.2