[PATCH] ubi: block: KASAN null-ptr-deref in ubiblock_create()

From: Li Zetao
Date: Fri Oct 21 2022 - 02:54:19 EST


There is a null-ptr-deref when testing the ubiblock tools:

KASAN: null-ptr-deref in range [0x0000000000000040-0x0000000000000047]
CPU: 1 PID: 2299 Comm: ubiblock Not tainted
6.1.0-rc1-00002-gebb24af8c872-dirty #12
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),
BIOS 1.14.0-1.fc33 04/01/2014
RIP: 0010:ubiblock_create.cold+0x175/0x1b5 [ubi]
...
Call Trace:
vol_cdev_ioctl+0x6ce/0x870 [ubi]
__x64_sys_ioctl+0x11d/0x170
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x46/0xb0
RIP: 0033:0x7fd9141230ab

when blk_mq_alloc_tag_set() returns an error, the dev->gd is
uninitialized, that causing null-ptr-deref fault.

Fix it by replacing the disk_to_dev(dev->gd) with NULL, that
will printk "(NULL device *): ..." message if blk_mq_alloc_tag_set()
returns an error.

Fixes: 77567b25ab9f ("ubi: use blk_mq_alloc_disk and blk_cleanup_disk")
Signed-off-by: Li Zetao <lizetao1@xxxxxxxxxx>
---
drivers/mtd/ubi/block.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index 75eaecc8639f..9625dc2f45e2 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -408,7 +408,7 @@ int ubiblock_create(struct ubi_volume_info *vi)

ret = blk_mq_alloc_tag_set(&dev->tag_set);
if (ret) {
- dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
+ dev_err(NULL, "blk_mq_alloc_tag_set failed");
goto out_free_dev;
}

--
2.31.1