The configfs size attribute accepts an unsigned long value in MiB without checking whether it can be converted to bytes. Both null_add_dev() and mb_to_sects() multiply that value by SZ_1M before converting it to sectors, so on 64-bit systems a size of 2^44 MiB wraps the byte count to zero. For a zoned device with the default zone size, this also produces zero zones. The zero-length zone allocation returns ZERO_SIZE_PTR, and the conventional-zone adjustment underflows zone_nr_conv to UINT_MAX. The subsequent zone initialization then accesses invalid memory: null_blk: changed the number of conventional zones to 4294967295 BUG: kernel NULL pointer dereference, address: 0000000000000030 #PF: supervisor write access in kernel mode RIP: 0010:lockdep_init_map_type+0x1b/0x230 Call Trace: __raw_spin_lock_init+0x3a/0x110 null_init_zoned_dev+0x5a2/0x1270 [null_blk] null_add_dev+0x15f5/0x1e70 [null_blk] nullb_device_power_store+0x29d/0x350 [null_blk] configfs_write_iter+0x306/0x500 vfs_write+0x6a4/0x1030 ksys_write+0x12a/0x250 Non-zoned devices can instead be registered with a wrapped capacity. In null_validate_conf(), convert the size to sectors with check_mul_overflow() and reject it if the conversion overflows or the result exceeds BLK_DEV_MAX_SECTORS. This happens before any device resources are allocated or zones are initialized. Only checking for the wrap would not be enough: set_capacity() truncates capacities above BLK_DEV_MAX_SECTORS, so null_blk would register a disk smaller than the configured size. This check runs for both configfs power-on and module initialization. This bug was found by a fuzzer. Fixes: cedcafad8277 ("nullb: add interface to power on disk") Cc: stable@vger.kernel.org Signed-off-by: Yingjie Wang --- drivers/block/null_blk/main.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c index ec0d7ba4d938..eb7617fd0964 100644 --- a/drivers/block/null_blk/main.c +++ b/drivers/block/null_blk/main.c @@ -1893,6 +1893,15 @@ static int null_setup_tagset(struct nullb *nullb) static int null_validate_conf(struct nullb_device *dev) { + sector_t nr_sectors; + + if (check_mul_overflow((sector_t)dev->size, + (sector_t)(SZ_1M >> SECTOR_SHIFT), &nr_sectors) || + nr_sectors > BLK_DEV_MAX_SECTORS) { + pr_err("Device size is too large\n"); + return -EINVAL; + } + if (dev->queue_mode == NULL_Q_RQ) { pr_err("legacy IO path is no longer available\n"); return -EINVAL; -- 2.43.0