io_uring: fix dead-hung for non-iter fixed rw
authorPavel Begunkov <asml.silence@gmail.com>
Sun, 24 Nov 2019 08:58:24 +0000 (11:58 +0300)
committerJens Axboe <axboe@kernel.dk>
Tue, 26 Nov 2019 02:56:11 +0000 (19:56 -0700)
Read/write requests to devices without implemented read/write_iter
using fixed buffers can cause general protection fault, which totally
hangs a machine.

io_import_fixed() initialises iov_iter with bvec, but loop_rw_iter()
accesses it as iovec, dereferencing random address.

kmap() page by page in this case

Cc: stable@vger.kernel.org
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/io_uring.c

index 0225492..fc83216 100644 (file)
@@ -1622,9 +1622,19 @@ static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
                return -EAGAIN;
 
        while (iov_iter_count(iter)) {
-               struct iovec iovec = iov_iter_iovec(iter);
+               struct iovec iovec;
                ssize_t nr;
 
+               if (!iov_iter_is_bvec(iter)) {
+                       iovec = iov_iter_iovec(iter);
+               } else {
+                       /* fixed buffers import bvec */
+                       iovec.iov_base = kmap(iter->bvec->bv_page)
+                                               + iter->iov_offset;
+                       iovec.iov_len = min(iter->count,
+                                       iter->bvec->bv_len - iter->iov_offset);
+               }
+
                if (rw == READ) {
                        nr = file->f_op->read(file, iovec.iov_base,
                                              iovec.iov_len, &kiocb->ki_pos);
@@ -1633,6 +1643,9 @@ static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
                                               iovec.iov_len, &kiocb->ki_pos);
                }
 
+               if (iov_iter_is_bvec(iter))
+                       kunmap(iter->bvec->bv_page);
+
                if (nr < 0) {
                        if (!ret)
                                ret = nr;