Skip to content

Commit 211ff16

Browse files
ming1axboe
authored andcommitted
ublk: verify all pages in multi-page bvec fall within registered range
rq_for_each_bvec() yields multi-page bvecs where bv_page is only the first page. ublk_try_buf_match() only validated the start PFN against the maple tree, but a bvec can span multiple pages past the end of a registered range. Use mas_walk() instead of mtree_load() to obtain the range boundaries stored in the maple tree, and check that the bvec's end PFN does not exceed the range. Also remove base_pfn from struct ublk_buf_range since mas.index already provides the range start PFN. Reported-by: Caleb Sander Mateos <csander@purestorage.com> Signed-off-by: Ming Lei <tom.leiming@gmail.com> Link: https://patch.msgid.link/20260409133020.3780098-3-tom.leiming@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 23b3b6f commit 211ff16

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ struct ublk_buf {
304304

305305
/* Maple tree value: maps a PFN range to buffer location */
306306
struct ublk_buf_range {
307-
unsigned long base_pfn;
308307
unsigned short buf_index;
309308
unsigned short flags;
310309
unsigned int base_offset; /* byte offset within buffer */
@@ -5306,7 +5305,6 @@ static int __ublk_ctrl_reg_buf(struct ublk_device *ub,
53065305
}
53075306
range->buf_index = index;
53085307
range->flags = flags;
5309-
range->base_pfn = pfn;
53105308
range->base_offset = start << PAGE_SHIFT;
53115309

53125310
ret = mtree_insert_range(&ub->buf_tree, pfn,
@@ -5451,8 +5449,8 @@ static void __ublk_ctrl_unreg_buf(struct ublk_device *ub,
54515449
if (range->buf_index != buf_index)
54525450
continue;
54535451

5454-
base = range->base_pfn;
5455-
nr = mas.last - mas.index + 1;
5452+
base = mas.index;
5453+
nr = mas.last - base + 1;
54565454
mas_erase(&mas);
54575455

54585456
for (off = 0; off < nr; ) {
@@ -5531,15 +5529,22 @@ static bool ublk_try_buf_match(struct ublk_device *ub,
55315529

55325530
rq_for_each_bvec(bv, rq, iter) {
55335531
unsigned long pfn = page_to_pfn(bv.bv_page);
5532+
unsigned long end_pfn = pfn +
5533+
((bv.bv_offset + bv.bv_len - 1) >> PAGE_SHIFT);
55345534
struct ublk_buf_range *range;
55355535
unsigned long off;
5536+
MA_STATE(mas, &ub->buf_tree, pfn, pfn);
55365537

5537-
range = mtree_load(&ub->buf_tree, pfn);
5538+
range = mas_walk(&mas);
55385539
if (!range)
55395540
return false;
55405541

5542+
/* verify all pages in this bvec fall within the range */
5543+
if (end_pfn > mas.last)
5544+
return false;
5545+
55415546
off = range->base_offset +
5542-
(pfn - range->base_pfn) * PAGE_SIZE + bv.bv_offset;
5547+
(pfn - mas.index) * PAGE_SIZE + bv.bv_offset;
55435548

55445549
if (first) {
55455550
/* Read-only buffer can't serve READ (kernel writes) */

0 commit comments

Comments
 (0)