linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: Fix WARNING in _copy_from_iter
@ 2024-01-21 20:26 Christian A. Ehrhardt
  2024-01-23  8:51 ` Christoph Hellwig
  2024-01-23 15:57 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Christian A. Ehrhardt @ 2024-01-21 20:26 UTC (permalink / raw)
  To: linux-block, linux-kernel
  Cc: Christian A. Ehrhardt, syzbot+a532b03fdfee2c137666,
	syzbot+63dec323ac56c28e644f, Jens Axboe, Andrew Morton,
	Alexander Viro

Syzkaller reports a warning in _copy_from_iter because an
iov_iter is supposedly used in the wrong direction. The reason
is that syzcaller managed to generate a request with
a transfer direction of SG_DXFER_TO_FROM_DEV. This instructs
the kernel to copy user buffers into the kernel, read into
the copied buffers and then copy the data back to user space.

Thus the iovec is used in both directions.

Detect this situation in the block layer and construct a new
iterator with the correct direction for the copy-in.

Reported-by: syzbot+a532b03fdfee2c137666@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/0000000000009b92c10604d7a5e9@google.com/t/
Reported-by: syzbot+63dec323ac56c28e644f@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/0000000000003faaa105f6e7c658@google.com/T/
Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
---
 block/blk-map.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/block/blk-map.c b/block/blk-map.c
index 8584babf3ea0..71210cdb3442 100644
--- a/block/blk-map.c
+++ b/block/blk-map.c
@@ -205,12 +205,19 @@ static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data,
 	/*
 	 * success
 	 */
-	if ((iov_iter_rw(iter) == WRITE &&
-	     (!map_data || !map_data->null_mapped)) ||
-	    (map_data && map_data->from_user)) {
+	if (iov_iter_rw(iter) == WRITE &&
+	     (!map_data || !map_data->null_mapped)) {
 		ret = bio_copy_from_iter(bio, iter);
 		if (ret)
 			goto cleanup;
+	} else if (map_data && map_data->from_user) {
+		struct iov_iter iter2 = *iter;
+
+		/* This is the copy-in part of SG_DXFER_TO_FROM_DEV. */
+		iter2.data_source = ITER_SOURCE;
+		ret = bio_copy_from_iter(bio, &iter2);
+		if (ret)
+			goto cleanup;
 	} else {
 		if (bmd->is_our_pages)
 			zero_fill_bio(bio);
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Fix WARNING in _copy_from_iter
  2024-01-21 20:26 [PATCH] block: Fix WARNING in _copy_from_iter Christian A. Ehrhardt
@ 2024-01-23  8:51 ` Christoph Hellwig
  2024-01-23 15:57 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2024-01-23  8:51 UTC (permalink / raw)
  To: Christian A. Ehrhardt
  Cc: linux-block, linux-kernel, syzbot+a532b03fdfee2c137666,
	syzbot+63dec323ac56c28e644f, Jens Axboe, Andrew Morton,
	Alexander Viro

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

(although I really wish we could kill the whole map-data crap..)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] block: Fix WARNING in _copy_from_iter
  2024-01-21 20:26 [PATCH] block: Fix WARNING in _copy_from_iter Christian A. Ehrhardt
  2024-01-23  8:51 ` Christoph Hellwig
@ 2024-01-23 15:57 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2024-01-23 15:57 UTC (permalink / raw)
  To: linux-block, linux-kernel, Christian A. Ehrhardt
  Cc: syzbot+a532b03fdfee2c137666, syzbot+63dec323ac56c28e644f,
	Andrew Morton, Alexander Viro


On Sun, 21 Jan 2024 21:26:34 +0100, Christian A. Ehrhardt wrote:
> Syzkaller reports a warning in _copy_from_iter because an
> iov_iter is supposedly used in the wrong direction. The reason
> is that syzcaller managed to generate a request with
> a transfer direction of SG_DXFER_TO_FROM_DEV. This instructs
> the kernel to copy user buffers into the kernel, read into
> the copied buffers and then copy the data back to user space.
> 
> [...]

Applied, thanks!

[1/1] block: Fix WARNING in _copy_from_iter
      commit: 13f3956eb5681a4045a8dfdef48df5dc4d9f58a6

Best regards,
-- 
Jens Axboe




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-23 15:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-21 20:26 [PATCH] block: Fix WARNING in _copy_from_iter Christian A. Ehrhardt
2024-01-23  8:51 ` Christoph Hellwig
2024-01-23 15:57 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).