From: Christoph Hellwig <hch@lst.de>
To: Al Viro <viro@zeniv.linux.org.uk>,
Linus Torvalds <torvalds@linux-foundation.org>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
Matthew Wilcox <willy@infradead.org>,
Kees Cook <keescook@chromium.org>,
Iurii Zaikin <yzaikin@google.com>,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: [PATCH 9/9] fs: don't allow splice read/write without explicit ops
Date: Fri, 26 Jun 2020 09:58:36 +0200 [thread overview]
Message-ID: <20200626075836.1998185-10-hch@lst.de> (raw)
In-Reply-To: <20200626075836.1998185-1-hch@lst.de>
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/splice.c | 117 +++++++++++-----------------------------------------
1 file changed, 24 insertions(+), 93 deletions(-)
diff --git a/fs/splice.c b/fs/splice.c
index d1efc53875bd93..f93bbfc131d5ec 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -342,66 +342,6 @@ const struct pipe_buf_operations nosteal_pipe_buf_ops = {
};
EXPORT_SYMBOL(nosteal_pipe_buf_ops);
-static ssize_t default_file_splice_read(struct file *in, loff_t *ppos,
- struct pipe_inode_info *pipe, size_t len,
- unsigned int flags)
-{
- struct iov_iter to;
- struct page **pages;
- unsigned int nr_pages;
- unsigned int mask;
- size_t offset, base, copied = 0;
- loff_t pos;
- ssize_t res;
- int i;
-
- if (pipe_full(pipe->head, pipe->tail, pipe->max_usage))
- return -EAGAIN;
-
- res = rw_verify_area(READ, in, ppos, len);
- if (res < 0)
- return res;
-
- /*
- * Try to keep page boundaries matching to source pagecache ones -
- * it probably won't be much help, but...
- */
- offset = *ppos & ~PAGE_MASK;
-
- iov_iter_pipe(&to, READ, pipe, len + offset);
-
- res = iov_iter_get_pages_alloc(&to, &pages, len + offset, &base);
- if (res <= 0)
- return -ENOMEM;
-
- mask = pipe->ring_size - 1;
- pipe->bufs[to.head & mask].offset = offset;
- pipe->bufs[to.head & mask].len -= offset;
-
- nr_pages = DIV_ROUND_UP(res + base, PAGE_SIZE);
-
- pos = *ppos;
- for (i = 0; i < nr_pages; i++) {
- size_t this_len = min_t(size_t, len, PAGE_SIZE - offset);
-
- res = __kernel_read(in, page_address(pages[i]) + offset,
- this_len, &pos);
- if (res < 0)
- goto out;
- len -= this_len;
- offset = 0;
- }
- copied = pos - *ppos;
- *ppos = pos;
-
-out:
- for (i = 0; i < nr_pages; i++)
- put_page(pages[i]);
- kvfree(pages);
- iov_iter_advance(&to, copied); /* truncates and discards */
- return res;
-}
-
/*
* Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos'
* using sendpage(). Return the number of bytes sent.
@@ -765,33 +705,6 @@ iter_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
EXPORT_SYMBOL(iter_file_splice_write);
-static int write_pipe_buf(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
- struct splice_desc *sd)
-{
- int ret;
- void *data;
- loff_t tmp = sd->pos;
-
- data = kmap(buf->page);
- ret = __kernel_write(sd->u.file, data + buf->offset, sd->len, &tmp);
- kunmap(buf->page);
-
- return ret;
-}
-
-static ssize_t default_file_splice_write(struct pipe_inode_info *pipe,
- struct file *out, loff_t *ppos,
- size_t len, unsigned int flags)
-{
- ssize_t ret;
-
- ret = splice_from_pipe(pipe, out, ppos, len, flags, write_pipe_buf);
- if (ret > 0)
- *ppos += ret;
-
- return ret;
-}
-
/**
* generic_splice_sendpage - splice data from a pipe to a socket
* @pipe: pipe to splice from
@@ -813,15 +726,30 @@ ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe, struct file *out,
EXPORT_SYMBOL(generic_splice_sendpage);
+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(
+ "splice %s not supported for file %s (pid: %d comm: %.20s)\n",
+ op, path, current->pid, current->comm);
+}
+
/*
* Attempt to initiate a splice from pipe to file.
*/
static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
loff_t *ppos, size_t len, unsigned int flags)
{
- if (out->f_op->splice_write)
- return out->f_op->splice_write(pipe, out, ppos, len, flags);
- return default_file_splice_write(pipe, out, ppos, len, flags);
+ if (!out->f_op->splice_write) {
+ warn_unsupported(out, "write");
+ return -EINVAL;
+ }
+
+ return out->f_op->splice_write(pipe, out, ppos, len, flags);
}
/*
@@ -843,9 +771,12 @@ static long do_splice_to(struct file *in, loff_t *ppos,
if (unlikely(len > MAX_RW_COUNT))
len = MAX_RW_COUNT;
- if (in->f_op->splice_read)
- return in->f_op->splice_read(in, ppos, pipe, len, flags);
- return default_file_splice_read(in, ppos, pipe, len, flags);
+ if (!in->f_op->splice_read) {
+ warn_unsupported(in, "read");
+ return -EINVAL;
+ }
+
+ return in->f_op->splice_read(in, ppos, pipe, len, flags);
}
/**
--
2.26.2
next prev parent reply other threads:[~2020-06-26 7:59 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-26 7:58 [RFC] stop using ->read and ->write for kernel access v2 Christoph Hellwig
2020-06-26 7:58 ` [PATCH 1/9] fs: refactor new_sync_read Christoph Hellwig
2020-06-26 7:58 ` [PATCH 2/9] proc: add a read_iter method to proc proc_ops Christoph Hellwig
2020-06-26 12:06 ` Luis Chamberlain
2020-06-26 13:36 ` Christoph Hellwig
2020-06-26 7:58 ` [PATCH 3/9] seq_file: add seq_read_iter Christoph Hellwig
2020-06-26 7:58 ` [PATCH 4/9] seq_file: switch over direct seq_read method calls to seq_read_iter Christoph Hellwig
2020-06-26 7:58 ` [PATCH 5/9] proc: " Christoph Hellwig
2020-06-26 7:58 ` [PATCH 6/9] sysctl: Call sysctl_head_finish on error Christoph Hellwig
2020-06-26 12:17 ` Luis Chamberlain
2020-06-26 12:27 ` Matthew Wilcox
2020-06-26 7:58 ` [PATCH 7/9] sysctl: Convert to iter interfaces Christoph Hellwig
2020-06-26 7:58 ` [PATCH 8/9] fs: don't allow kernel reads and writes without iter ops Christoph Hellwig
2020-06-26 12:27 ` Luis Chamberlain
2020-06-26 13:37 ` Christoph Hellwig
2020-06-26 13:51 ` Matthew Wilcox
2020-06-26 21:05 ` Kees Cook
2020-06-27 7:10 ` Christoph Hellwig
2020-06-26 7:58 ` Christoph Hellwig [this message]
2020-06-27 22:15 ` [RFC] stop using ->read and ->write for kernel access v2 Linus Torvalds
2020-06-28 7:20 ` Christoph Hellwig
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=20200626075836.1998185-10-hch@lst.de \
--to=hch@lst.de \
--cc=keescook@chromium.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mcgrof@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=yzaikin@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®