* [PATCH v2 1/3] brd: protect page with rcu
2025-04-27 8:39 [PATCH v2 0/3] brd: discard bugfix Yu Kuai
@ 2025-04-27 8:39 ` Yu Kuai
2025-04-28 12:52 ` Christoph Hellwig
2025-04-27 8:39 ` [PATCH v2 2/3] brd: fix aligned_sector from brd_do_discard() Yu Kuai
2025-04-27 8:39 ` [PATCH v2 3/3] brd: fix discard end sector Yu Kuai
2 siblings, 1 reply; 6+ messages in thread
From: Yu Kuai @ 2025-04-27 8:39 UTC (permalink / raw)
To: hch, axboe, kbusch
Cc: linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang, yangerkun,
johnny.chenyi
From: Yu Kuai <yukuai3@huawei.com>
Currently, after fetching the page by xa_load() in IO path, there is no
protection and page can be freed concurrently by discard:
cpu0
brd_submit_bio
brd_do_bvec
page = brd_lookup_page
cpu1
brd_submit_bio
brd_do_discard
page = __xa_erase()
__free_page()
// page UAF
Fix the problem by protecting page with rcu.
Meanwhile, if page is already freed, also prevent BUG_ON() by skipping
the write, and user will get zero data later if there is no page.
Fixes: 9ead7efc6f3f ("brd: implement discard support")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
drivers/block/brd.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 79e96221f887..98c9297beafe 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -132,23 +132,32 @@ static bool brd_rw_bvec(struct brd_device *brd, struct bio *bio)
}
}
+ rcu_read_lock();
page = brd_lookup_page(brd, sector);
kaddr = bvec_kmap_local(&bv);
if (op_is_write(opf)) {
- BUG_ON(!page);
- memcpy_to_page(page, offset, kaddr, bv.bv_len);
+ if (page)
+ memcpy_to_page(page, offset, kaddr, bv.bv_len);
} else if (page) {
memcpy_from_page(kaddr, page, offset, bv.bv_len);
} else {
memset(kaddr, 0, bv.bv_len);
}
kunmap_local(kaddr);
+ rcu_read_unlock();
bio_advance_iter_single(bio, &bio->bi_iter, bv.bv_len);
return true;
}
+static void brd_free_one_page(struct rcu_head *head)
+{
+ struct page *page = container_of(head, struct page, rcu_head);
+
+ __free_page(page);
+}
+
static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
{
sector_t aligned_sector = (sector + PAGE_SECTORS) & ~PAGE_SECTORS;
@@ -159,7 +168,7 @@ static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
if (page) {
- __free_page(page);
+ call_rcu(&page->rcu_head, brd_free_one_page);
brd->brd_nr_pages--;
}
aligned_sector += PAGE_SECTORS;
--
2.39.2
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 2/3] brd: fix aligned_sector from brd_do_discard()
2025-04-27 8:39 [PATCH v2 0/3] brd: discard bugfix Yu Kuai
2025-04-27 8:39 ` [PATCH v2 1/3] brd: protect page with rcu Yu Kuai
@ 2025-04-27 8:39 ` Yu Kuai
2025-04-27 8:39 ` [PATCH v2 3/3] brd: fix discard end sector Yu Kuai
2 siblings, 0 replies; 6+ messages in thread
From: Yu Kuai @ 2025-04-27 8:39 UTC (permalink / raw)
To: hch, axboe, kbusch
Cc: linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang, yangerkun,
johnny.chenyi
From: Yu Kuai <yukuai3@huawei.com>
The calculation is just wrong, fix it by round_up().
Fixes: 9ead7efc6f3f ("brd: implement discard support")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
drivers/block/brd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 98c9297beafe..567aee2f882c 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -160,7 +160,7 @@ static void brd_free_one_page(struct rcu_head *head)
static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
{
- sector_t aligned_sector = (sector + PAGE_SECTORS) & ~PAGE_SECTORS;
+ sector_t aligned_sector = round_up(sector, PAGE_SECTORS);
struct page *page;
size -= (aligned_sector - sector) * SECTOR_SIZE;
--
2.39.2
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 3/3] brd: fix discard end sector
2025-04-27 8:39 [PATCH v2 0/3] brd: discard bugfix Yu Kuai
2025-04-27 8:39 ` [PATCH v2 1/3] brd: protect page with rcu Yu Kuai
2025-04-27 8:39 ` [PATCH v2 2/3] brd: fix aligned_sector from brd_do_discard() Yu Kuai
@ 2025-04-27 8:39 ` Yu Kuai
2 siblings, 0 replies; 6+ messages in thread
From: Yu Kuai @ 2025-04-27 8:39 UTC (permalink / raw)
To: hch, axboe, kbusch
Cc: linux-block, linux-kernel, yukuai3, yukuai1, yi.zhang, yangerkun,
johnny.chenyi
From: Yu Kuai <yukuai3@huawei.com>
brd_do_discard() just aligned start sector to page, this can only work
if the discard size if at least one page. For example:
blkdiscard /dev/ram0 -o 5120 -l 1024
In this case, size = (1024 - (8192 - 5120)), which is a huge value.
Fix the problem by round_down() the end sector.
Fixes: 9ead7efc6f3f ("brd: implement discard support")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
---
drivers/block/brd.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 567aee2f882c..10381a1dd44e 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -161,18 +161,21 @@ static void brd_free_one_page(struct rcu_head *head)
static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size)
{
sector_t aligned_sector = round_up(sector, PAGE_SECTORS);
+ sector_t aligned_end = round_down(
+ sector + (size >> SECTOR_SHIFT), PAGE_SECTORS);
struct page *page;
- size -= (aligned_sector - sector) * SECTOR_SIZE;
+ if (aligned_end <= aligned_sector)
+ return;
+
xa_lock(&brd->brd_pages);
- while (size >= PAGE_SIZE && aligned_sector < rd_size * 2) {
+ while (aligned_sector < aligned_end && aligned_sector < rd_size * 2) {
page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT);
if (page) {
call_rcu(&page->rcu_head, brd_free_one_page);
brd->brd_nr_pages--;
}
aligned_sector += PAGE_SECTORS;
- size -= PAGE_SIZE;
}
xa_unlock(&brd->brd_pages);
}
--
2.39.2
^ permalink raw reply [flat|nested] 6+ messages in thread