iomap, splice: Fix DIO/splice_read race memory corruptor and kill off ITER_PIPE
Subsystems (6)
cover iomap, splice: Fix DIO/splice_read race memory corruptor and kill off ITER_PIPE
Hi Jens, Christoph, Al,
Syzbot found a bug[1] that my bio/FOLL_PIN code[2] inadvertently introduced. The problem is that with my patches, pages obtained from kernel-backed iterators aren't ref'd or pinned when they're extracted and thus struct bio doesn't retain them. A DIO-read from a file through iomap that races with truncate may in __iomap_dio_rw() call iov_iter_revert() on the iov_iter it was given.
Unfortunately, if the iterator is an ITER_PIPE, the reversion has side
effects: the pages rolled back get released. Those pages, however, are not retained by the uncompleted bio and may get modified after their release.
The first patch fixes this by switching to bulk allocating all the necessary pages up front and adding them to an ITER_BVEC iterator, doing the I/O and only then trimming the excess pages. The remaining pages are then pushed into the pipe. This has the downside (as the code stands) of not handling any partial page lurking in the pipe - though that could be places as the first element in the bvec. OTOH, using the bulk allocation API should be more efficient.
As this is the only user of ITER_PIPE, the second patch removes ITER_PIPE and all its associated iov_iter helper functions.
Thanks to Hillf Danton for spotting that iov_iter_revert() was involved[3].
[!] Jens: Note that there's a window in the linux-block/for-next branch
with a memory corruptor bug that someone bisecting might hit. These two patches would be better pushed to the front of my iov-extract branch to eliminate the window. Would it be possible for you to replace my branch in your for-next branch at this point?
I've pushed the patches here also:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=iov-fixes
David
David Howells (2):
vfs, iomap: Fix generic_file_splice_read() to avoid reversion of
ITER_PIPE
iov_iter: Kill ITER_PIPE
fs/cifs/file.c | 8 +- fs/splice.c | 76 ++++++- include/linux/uio.h | 14 -- lib/iov_iter.c | 492 +------------------------------------------- mm/filemap.c | 3 - 5 files changed, 72 insertions(+), 521 deletions(-)
PATCH 1/2 vfs, iomap: Fix generic_file_splice_read() to avoid reversion of ITER_PIPE
With the new iov_iter_extract_pages() function, pages extracted from a non-user-backed iterator, such as ITER_PIPE, aren't pinned. __iomap_dio_rw(), however, calls iov_iter_revert() to shorten the iterator to just the data it is going to use - which causes the pipe buffers to be freed, even though they're attached to a bio and may get written to by DMA (thanks to Hillf Danton for spotting this[1]).
This then causes massive memory corruption that is particularly noticable when the syzbot test[2] is run. The test boils down to:
out = creat(argv[1], 0666); ftruncate(out, 0x800); lseek(out, 0x200, SEEK_SET); in = open(argv[1], O_RDONLY | O_DIRECT | O_NOFOLLOW); sendfile(out, in, NULL, 0x1dd00);
run repeatedly in parallel. What I think is happening is that ftruncate() occasionally shortens the DIO read that's about to be made by sendfile's splice core by reducing i_size.
Fix this by replacing the use of an ITER_PIPE iterator with an ITER_BVEC iterator for which reversion won't free the buffers. Bulk allocate all the buffers we think we're going to use in advance, do the read synchronously and only then trim the buffer down. The pages we did use get pushed into the pipe.
This is more efficient by virtue of doing a bulk page allocation, but slightly less efficient by ignoring any partial page in the pipe.
Note that this removes the only user of ITER_PIPE.
--- fs/splice.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 8 deletions(-)
fs/splice.c +68 -8
@@ -295,24 +295,62 @@ void splice_shrink_spd(struct splice_pipe_desc *spd)* used as long as it has more or less sane ->read_iter(). * */ -ssize_t generic_file_splice_read (struct file *in,loff_t *ppos, +ssize_t generic_file_splice_read (struct file *file,loff_t *ppos,struct pipe_inode_info *pipe,size_t len,unsigned int flags) { +LIST_HEAD (pages);struct iov_iter to; +struct bio_vec *bv;struct kiocb kiocb; -int ret; +struct page *page; +unsigned int head; +ssize_t ret; +size_t used, npages, chunk, remain, reclaim; +int i; + +/* Work out how much data we can actually add into the pipe */ + used =pipe_occupancy (pipe->head, pipe->tail); + npages =max_t (ssize_t, pipe->max_usage - used,0 ); + len =min_t (size_t, len, npages *PAGE_SIZE ); + npages =DIV_ROUND_UP (len,PAGE_SIZE ); + + bv =kmalloc (array_size (npages,sizeof (bv[0 ])),GFP_KERNEL ); +if (!bv) +return -ENOMEM ; + + npages =alloc_pages_bulk_list (GFP_USER , npages, &pages); +if (!npages) { +kfree (bv); +return -ENOMEM ; + } -iov_iter_pipe (&to,ITER_DEST , pipe, len); -init_sync_kiocb (&kiocb, in); + remain = len =min_t (size_t, len, npages *PAGE_SIZE ); + +for (i =0 ; i < npages; i++) { + chunk =min_t (size_t,PAGE_SIZE , remain); + page = list_first_entry(&pages, struct page, lru); +list_del_init (&page->lru); + bv[i].bv_page = page; + bv[i].bv_offset =0 ; + bv[i].bv_len = chunk; + remain -= chunk; + } + +/* Do the I/O */ +iov_iter_bvec (&to,ITER_DEST , bv, npages, len); +init_sync_kiocb (&kiocb, file); kiocb.ki_pos = *ppos; - ret =call_read_iter (in, &kiocb, &to); + ret =call_read_iter (file, &kiocb, &to); + + reclaim = npages *PAGE_SIZE ; + remain =0 ;if (ret >0 ) { + reclaim -= ret; + remain = ret; *ppos = kiocb.ki_pos; -file_accessed (in); +file_accessed (file); }else if (ret <0 ) { -/* free what was emitted */ -pipe_discard_from (pipe, to.start_head);/* * callers of ->splice_read() expect -EAGAIN on * "can't put anything in there", rather than -EFAULT.
@@ -321,6 +359,28 @@ ssize_t generic_file_splice_read(struct file *in, loff_t *ppos, ret = -EAGAIN ; } +/* Free any pages that didn't get touched at all. */ +for (; reclaim >=PAGE_SIZE ; reclaim -=PAGE_SIZE ) +__free_page (bv[--npages].bv_page); + +/* Push the remaining pages into the pipe. */ + head = pipe->head; +for (i =0 ; i < npages; i++) { +struct pipe_buffer *buf = &pipe->bufs[head & (pipe->ring_size -1 )]; + + chunk =min_t (size_t, remain,PAGE_SIZE ); + *buf = (struct pipe_buffer ) { + .ops = &default_pipe_buf_ops, + .page = bv[i].bv_page, + .offset =0 , + .len = chunk, + }; + head++; + remain -= chunk; + } + pipe->head = head; + +kfree (bv);return ret; }EXPORT_SYMBOL (generic_file_splice_read);
PATCH 2/2 iov_iter: Kill ITER_PIPE
In reply to David HowellsThe ITER_PIPE-type iterator was only used for generic_file_splice_read(), but that has now been switched to using ITER_BVEC instead, leaving ITER_PIPE unused - so get rid of it.
--- fs/cifs/file.c | 8 +- include/linux/uio.h | 14 -- lib/iov_iter.c | 492 +------------------------------------------- mm/filemap.c | 3 - 4 files changed, 4 insertions(+), 513 deletions(-)
fs/cifs/file.c +1 -7
@@ -3806,13 +3806,7 @@ cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)size_t copy =min_t (size_t, remaining,PAGE_SIZE );size_t written; -if (unlikely (iov_iter_is_pipe (iter))) { -void *addr =kmap_atomic (page); - - written =copy_to_iter (addr, copy, iter); -kunmap_atomic (addr); - }else - written =copy_page_to_iter (page,0 , copy, iter); + written =copy_page_to_iter (page,0 , copy, iter); remaining -= written;if (written < copy &&iov_iter_count (iter) >0 )break ;
include/linux/uio.h +0 -14
@@ -11,7 +11,6 @@#include <uapi/linux/uio.h> struct page ; -struct pipe_inode_info ;typedef unsigned int __bitwise iov_iter_extraction_t ;
@@ -25,7 +24,6 @@ enum iter_type {ITER_IOVEC ,ITER_KVEC ,ITER_BVEC , -ITER_PIPE ,ITER_XARRAY ,ITER_DISCARD , ITER_UBUF,
@@ -55,15 +53,10 @@ struct iov_iter {const struct kvec *kvec;const struct bio_vec *bvec;struct xarray *xarray; -struct pipe_inode_info *pipe;void __user *ubuf; };union {unsigned long nr_segs; -struct { -unsigned int head; -unsigned int start_head; - };loff_t xarray_start; }; };
@@ -101,11 +94,6 @@ static inline bool iov_iter_is_bvec(const struct iov_iter *i)return iov_iter_type (i) ==ITER_BVEC ; } -static inline bool iov_iter_is_pipe (const struct iov_iter *i) -{ -return iov_iter_type (i) ==ITER_PIPE ; -} -static inline bool iov_iter_is_discard (const struct iov_iter *i) {return iov_iter_type (i) ==ITER_DISCARD ;
@@ -247,8 +235,6 @@ void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec unsigned long nr_segs, size_t count);void iov_iter_bvec (struct iov_iter *i,unsigned int direction,const struct bio_vec *bvec,unsigned long nr_segs,size_t count); -void iov_iter_pipe (struct iov_iter *i,unsigned int direction,struct pipe_inode_info *pipe, -size_t count);void iov_iter_discard (struct iov_iter *i,unsigned int direction,size_t count);void iov_iter_xarray (struct iov_iter *i,unsigned int direction,struct xarray *xarray,loff_t start,size_t count);
lib/iov_iter.c +3 -489
636 lines not on this page · raw message
mm/filemap.c +0 -3
@@ -2446,9 +2446,6 @@ static bool filemap_range_uptodate(struct address_space *mapping,if (folio_test_uptodate (folio))return true; -/* pipes can't handle partially uptodate pages */ -if (iov_iter_is_pipe (iter)) -return false;if (!mapping->a_ops->is_partially_uptodate)return false;if (mapping->host->i_blkbits >=folio_shift (folio))
Jens Axboe
maintainerQuoted 5 lines · David Howells
[!] Jens: Note that there's a window in the linux-block/for-next branch
with a memory corruptor bug that someone bisecting might hit. These two patches would be better pushed to the front of my iov-extract branch to eliminate the window. Would it be possible for you to replace my branch in your for-next branch at this point?
I'll check in on these two patches later today, but just wanted to say that we can definitely just toss the existing branch, and setup a new one based on -rc7 that adds these two first, then pulls in the other branch on top to avoid this. Not a big deal, and warranted in this case.
Signature · 1 line
Jens Axboe
Hi Jens,
Quoted 5 lines
[!] Jens: Note that there's a window in the linux-block/for-next branch
with a memory corruptor bug that someone bisecting might hit. These two patches would be better pushed to the front of my iov-extract branch to eliminate the window. Would it be possible for you to replace my branch in your for-next branch at this point?
In case you would prefer to do this, I've updated my iov-extract[*] branch to put these two patches at the bottom and pushed the other patches back on top of it, dropping the pipe extraction bits from the iov_iter_extract_pages() patch in case you want to use that.
I've also removed the FOLL_PIN that was in that patch as it's implied by calling pin_user_pages_fast() and is being made private to mm/.
I can also repost the full set of patches if that would be useful.
David
[*] https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=iov-extract