null_init_zoned_dev() calculates the zone count from sector_t values but stores it in the unsigned int nr_zones field without checking the range. For example, a size of 2^40 MiB with the default 256 MiB zone size requires 2^32 zones. This count is truncated to zero when stored in nr_zones. The resulting zero-length allocation returns ZERO_SIZE_PTR. Adjusting the number of conventional zones then underflows zone_nr_conv to UINT_MAX, and initializing those zones accesses invalid memory. This happens even when the device capacity itself fits in a signed 64-bit byte count. Keep the calculated zone count in an unsigned long long temporary and reject values above UINT_MAX before assigning nr_zones or allocating the zone array. This bug was found by a fuzzer. Fixes: ca4b2a011948 ("null_blk: add zone support") Cc: stable@vger.kernel.org Signed-off-by: Yingjie Wang --- drivers/block/null_blk/zoned.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/block/null_blk/zoned.c b/drivers/block/null_blk/zoned.c index b271d0a88ff2..28689f15a01a 100644 --- a/drivers/block/null_blk/zoned.c +++ b/drivers/block/null_blk/zoned.c @@ -54,6 +54,7 @@ int null_init_zoned_dev(struct nullb_device *dev, struct queue_limits *lim) { sector_t dev_capacity_sects, zone_capacity_sects; + unsigned long long nr_zones; struct nullb_zone *zone; sector_t sector = 0; unsigned int i; @@ -94,8 +95,13 @@ int null_init_zoned_dev(struct nullb_device *dev, pr_err("zone_size too large or too small, leads to zero sectors\n"); return -EINVAL; } - dev->nr_zones = round_up(dev_capacity_sects, dev->zone_size_sects) + nr_zones = round_up(dev_capacity_sects, dev->zone_size_sects) >> ilog2(dev->zone_size_sects); + if (nr_zones > UINT_MAX) { + pr_err("Too many zones\n"); + return -EINVAL; + } + dev->nr_zones = nr_zones; dev->zones = kvmalloc_objs(struct nullb_zone, dev->nr_zones, GFP_KERNEL | __GFP_ZERO); -- 2.43.0