Commit 77325912 authored by Matthew Wilcox (Oracle)'s avatar Matthew Wilcox (Oracle) Committed by Li Nan
Browse files

block: Fix iterating over an empty bio with bio_for_each_folio_all

mainline inclusion
from mainline-v6.8-rc1
commit 7bed6f3d08b7af27b7015da8dc3acf2b9c1f21d7
category: bugfix
bugzilla: https://gitee.com/src-openeuler/kernel/issues/I99JYB
CVE: CVE-2024-26632

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7bed6f3d08b7af27b7015da8dc3acf2b9c1f21d7



--------------------------------

If the bio contains no data, bio_first_folio() calls page_folio() on a
NULL pointer and oopses.  Move the test that we've reached the end of
the bio from bio_next_folio() to bio_first_folio().

Reported-by: default avatar <syzbot+8b23309d5788a79d3eea@syzkaller.appspotmail.com>
Reported-by: default avatar <syzbot+004c1e0fced2b4bc3dcc@syzkaller.appspotmail.com>
Fixes: 640d1930 ("block: Add bio_for_each_folio_all()")
Cc: stable@vger.kernel.org
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Link: https://lore.kernel.org/r/20240116212959.3413014-1-willy@infradead.org


[axboe: add unlikely() to error case]
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
Signed-off-by: default avatarLi Nan <linan122@huawei.com>
parent 119aafed
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -287,6 +287,11 @@ static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
{
	struct bio_vec *bvec = bio_first_bvec_all(bio) + i;

	if (unlikely(i >= bio->bi_vcnt)) {
		fi->folio = NULL;
		return;
	}

	fi->folio = page_folio(bvec->bv_page);
	fi->offset = bvec->bv_offset +
			PAGE_SIZE * (bvec->bv_page - &fi->folio->page);
@@ -304,10 +309,8 @@ static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio)
		fi->offset = 0;
		fi->length = min(folio_size(fi->folio), fi->_seg_count);
		fi->_next = folio_next(fi->folio);
	} else if (fi->_i + 1 < bio->bi_vcnt) {
		bio_first_folio(fi, bio, fi->_i + 1);
	} else {
		fi->folio = NULL;
		bio_first_folio(fi, bio, fi->_i + 1);
	}
}