[PATCH 1/2] ubi: block: Fix use-after-free of gendisk

From: Vincent Whitchurch
Date: Tue May 23 2023 - 09:12:37 EST


Do not touch the gendisk after put_disk() to fix this use-after-free:

==================================================
BUG: KASAN: slab-use-after-free in ubiblock_remove
Read of size 4 by task ubiblock/361

Call Trace:
ubiblock_remove (drivers/mtd/ubi/block.c:459 drivers/mtd/ubi/block.c:483)
vol_cdev_ioctl
...

Allocated by task 358:
__alloc_disk_node (block/genhd.c:1377)
__blk_mq_alloc_disk (block/blk-mq.c:4093)
ubiblock_create (drivers/mtd/ubi/block.c:397)
vol_cdev_ioctl
...

Freed by task 0:
bdev_free_inode (block/bdev.c:337)
i_callback
rcu_core
__do_softirq
...

Signed-off-by: Vincent Whitchurch <vincent.whitchurch@xxxxxxxx>
---
drivers/mtd/ubi/block.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index 3711d7f74600..70caec4606cd 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -448,13 +448,15 @@ int ubiblock_create(struct ubi_volume_info *vi)

static void ubiblock_cleanup(struct ubiblock *dev)
{
+ int first_minor = dev->gd->first_minor;
+
/* Stop new requests to arrive */
del_gendisk(dev->gd);
/* Finally destroy the blk queue */
dev_info(disk_to_dev(dev->gd), "released");
put_disk(dev->gd);
blk_mq_free_tag_set(&dev->tag_set);
- idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
+ idr_remove(&ubiblock_minor_idr, first_minor);
}

int ubiblock_remove(struct ubi_volume_info *vi)

--
2.34.1