Skip to contentSkip to conversation

null_blk: validate configfs values before power-on

PATCH In review Yingjie Wang 2 participants 9 messages Nov 27 – Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover null_blk: validate configfs values before power-on

This series fixes three bugs found while fuzzing null_blk's configfs interface. Setting size or home_node to the values below and writing 1 to power triggers a crash:

2/4 size=2^44 MiB with zoned=1 wraps the byte capacity to zero.
The resulting zero zone count causes zone_nr_conv to underflow.
3/4 size=2^40 MiB with zoned=1 and the default 256 MiB zone size
requires 2^32 zones. Storing this count in nr_zones truncates it to zero, leading to the same failure.
4/4 home_node=2147483647 is passed to kzalloc_node() without
validation, causing an out-of-bounds access in SLUB.

Patch 1/4 moves BLK_DEV_MAX_SECTORS to <linux/blkdev.h> so that 2/4 can check the configured size against it. The zone count overflow still occurs with only patch 2/4 applied.

Testing used x86_64 with KASAN, CONFIG_NUMA=y, a two-node QEMU guest, oops=panic panic_on_warn=1, and null_blk built as a module:
- The unpatched module crashes for all three cases above.
- With only the size check applied, the size=2^40 MiB zoned case still crashes.
- With the full series, writing 1 to power fails with EINVAL for all three cases and leaves power at 0. Correcting the value and retrying succeeds. Sizes of 2^43, 2^44 and 2^44 + 1 MiB and ULONG_MAX are rejected.
- Valid non-zoned devices were tested at 0, 1 and 2^43 - 1 MiB; valid zoned devices were tested at 256, 257 and 512 MiB. Tests also covered memory-backed read/write, zone reports and a conventional zone.
- home_node values NUMA_NO_NODE, 0 and 1 succeed. Values 2, 63, 64 and 2147483647 are rejected on power-on.
- Each patch was build-tested with W=1 on i386 and x86_64, using GCC and clang 18.

The v3 runtime test ran the v3 null_blk sources as an external module on an unpatched kernel; for that, the test copy defined BLK_DEV_MAX_SECTORS itself. Sparse NUMA node numbering and other architectures were not tested. The reproducers are available on request.

BLK_DEV_MAX_SECTORS was added in v6.14 by commit 3d9a9e9a77c5 ("block:
limit disk max sectors to (LLONG_MAX >> 9)"), so 1/4 and 2/4 will not apply to older stable kernels as is. There, the same limit can be written as LLONG_MAX >> SECTOR_SHIFT.

---
Changes in v3:
- New patch 1/4: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>.
- 2/4: Compute the size in sectors with check_mul_overflow() and compare it against BLK_DEV_MAX_SECTORS instead of using LLONG_MAX directly (Damien). This also avoids the clang -Wtautological-constant-out-of-range-compare warning on 32-bit that the kernel test robot reported:
https://lore.kernel.org/oe-kbuild-all/[email protected]/
- 2/4: Explain in the commit message why checking only for the wrap is not enough.
- 3/4: Use unsigned long long instead of sector_t for the zone count (Damien).
- 4/4: No changes.

v2: https://lore.kernel.org/linux-block/[email protected]/

Changes in v2:
- Add the missing Signed-off-by tags.
- State in each commit message that the bug was found by a fuzzer.
- No code changes.

v1: https://lore.kernel.org/linux-block/[email protected]/

Yingjie Wang (4):
block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>
null_blk: reject device size overflow
null_blk: reject zone counts that overflow unsigned int
null_blk: validate the configfs home_node attribute

 block/blk.h                    |  1 -
 drivers/block/null_blk/main.c  | 15 +++++++++++++++
 drivers/block/null_blk/zoned.c |  8 +++++++-
 include/linux/blkdev.h         |  6 ++++++
 4 files changed, 28 insertions(+), 2 deletions(-)

base-commit: 5a29cfaf51105039d156dd8a61d2783e8b653667

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/4 block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>

BLK_DEV_MAX_SECTORS is the largest disk capacity the block layer
supports: set_capacity() truncates larger capacities and add_disk() rejects them. It is defined in block/blk.h, so drivers cannot use it to validate a capacity they get from the user before calling set_capacity().

Move it to <linux/blkdev.h>. The next patch uses it in null_blk to reject a configured device size that is too large. No functional change.

---
 block/blk.h            | 1 -
 include/linux/blkdev.h | 6 ++++++
 2 files changed, 6 insertions(+), 1 deletion(-)
block/blk.h +0 -1
index 105fff157bfb..13fa24aa05ea 100644
@@ -23,7 +23,6 @@ struct elv_change_ctx;
  */
 #define BLK_DEF_MAX_SECTORS_CAP	(SZ_4M >> SECTOR_SHIFT)
 
-#define	BLK_DEV_MAX_SECTORS	(LLONG_MAX >> 9)
 #define	BLK_MIN_SEGMENT_SIZE	4096
 
 /* Max future timer expiry for timeouts */
include/linux/blkdev.h +6 -0
index 215753171cef..75d640cc0e9a 100644
@@ -834,6 +834,12 @@ static inline bool bdev_read_only(struct block_device *bdev)
 	return bdev_test_flag(bdev, BD_READ_ONLY) || get_disk_ro(bdev->bd_disk);
 }
 
+/*
+ * Largest supported disk capacity in sectors: the capacity in bytes must fit
+ * in a loff_t.
+ */
+#define	BLK_DEV_MAX_SECTORS	(LLONG_MAX >> 9)
+
 bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
 void disk_force_media_change(struct gendisk *disk);
 void bdev_mark_dead(struct block_device *bdev, bool surprise);

base-commit: 5a29cfaf51105039d156dd8a61d2783e8b653667

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>

Quoted 21 lines · Yingjie Wang

BLK_DEV_MAX_SECTORS is the largest disk capacity the block layer
supports: set_capacity() truncates larger capacities and add_disk() rejects them. It is defined in block/blk.h, so drivers cannot use it to validate a capacity they get from the user before calling set_capacity().

Move it to <linux/blkdev.h>. The next patch uses it in null_blk to reject a configured device size that is too large. No functional change.

---
 block/blk.h            | 1 -
 include/linux/blkdev.h | 6 ++++++
 2 files changed, 6 insertions(+), 1 deletion(-)
block/blk.h +0 -0
index 105fff157bfb..13fa24aa05ea 100644

block/blk.h struct elv_change_ctx; · line 26

Quoted 4 lines
block/blk.h +0 -1
@@ -23,7 +23,6 @@ struct elv_change_ctx;
  */
 #define BLK_DEF_MAX_SECTORS_CAP	(SZ_4M >> SECTOR_SHIFT)
 
-#define	BLK_DEV_MAX_SECTORS	(LLONG_MAX >> 9)

Please use SECTOR_SHIFT instead of 9.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw

PATCH v3 2/4 null_blk: reject device size overflow

In reply to Yingjie Wang

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")
---
 drivers/block/null_blk/main.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/block/null_blk/main.c +9 -0
index ec0d7ba4d938..eb7617fd0964 100644
@@ -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;
Signature · 1 line
2.43.0
Reviewed-by on v3 2/4
#4 lore raw

Re: [PATCH v3 2/4] null_blk: reject device size overflow

Quoted 35 lines · Yingjie Wang

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.

Quoted 5 lines

This bug was found by a fuzzer.

Fixes cedcafad8277 ("nullb: add interface to power on disk")

Looks OK to me.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#5 lore raw

PATCH v3 3/4 null_blk: reject zone counts that overflow unsigned int

In reply to Yingjie Wang

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")
---
 drivers/block/null_blk/zoned.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/null_blk/zoned.c +7 -1
index b271d0a88ff2..28689f15a01a 100644
@@ -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);
Signature · 1 line
2.43.0
Reviewed-by on v3 3/4
#6 lore raw

Re: [PATCH v3 3/4] null_blk: reject zone counts that overflow unsigned int

Quoted 15 lines · Yingjie Wang

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.

Quoted 5 lines

This bug was found by a fuzzer.

Fixes ca4b2a011948 ("null_blk: add zone support")

Looks good.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#7 lore raw

PATCH v3 4/4 null_blk: validate the configfs home_node attribute

In reply to Yingjie Wang

The home_node module parameter is checked at module initialization, but the configfs home_node attribute is stored without any validation. Powering on the device then passes the value to kzalloc_node() and uses it as the tag set NUMA node.

With CONFIG_NUMA enabled, writing 2147483647 to home_node and then 1 to power makes SLUB use that node ID as an index into kmem_cache's per_node[] array, causing an out-of-bounds read:

  BUG: unable to handle page fault for address: ffff88880b042e80
  #PF: supervisor read access in kernel mode
  RIP: 0010:___slab_alloc+0x5ad/0x760
  Call Trace:
   __kmalloc_cache_node_noprof+0x2dc/0x600
   null_add_dev+0x588/0x1e70 [null_blk]
   nullb_device_power_store+0x29d/0x350 [null_blk]
   configfs_write_iter+0x306/0x500
   vfs_write+0x6a4/0x1030
   ksys_write+0x12a/0x250

Reject home_node values other than NUMA_NO_NODE that do not name an online node in null_validate_conf(), before any allocation uses the value. Checking against nr_node_ids alone is not sufficient, because node IDs below it need not be possible when node numbering is sparse. This matches the validation done for the PCI numa_node sysfs attribute.

This bug was found by a fuzzer.

Fixes cedcafad8277 ("nullb: add interface to power on disk")
---
 drivers/block/null_blk/main.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/block/null_blk/main.c +6 -0
index eb7617fd0964..726c8c58aff4 100644
@@ -1902,6 +1902,12 @@ static int null_validate_conf(struct nullb_device *dev)
 		return -EINVAL;
 	}
 
+	if (dev->home_node != NUMA_NO_NODE &&
+	    (dev->home_node >= nr_node_ids || !node_online(dev->home_node))) {
+		pr_err("invalid home_node value\n");
+		return -EINVAL;
+	}
+
 	if (dev->queue_mode == NULL_Q_RQ) {
 		pr_err("legacy IO path is no longer available\n");
 		return -EINVAL;
Signature · 1 line
2.43.0
Reviewed-by on v3 4/4
#8 lore raw

Re: [PATCH v3 4/4] null_blk: validate the configfs home_node attribute

Quoted 26 lines · Yingjie Wang

The home_node module parameter is checked at module initialization, but the configfs home_node attribute is stored without any validation. Powering on the device then passes the value to kzalloc_node() and uses it as the tag set NUMA node.

With CONFIG_NUMA enabled, writing 2147483647 to home_node and then 1 to power makes SLUB use that node ID as an index into kmem_cache's per_node[] array, causing an out-of-bounds read:

  BUG: unable to handle page fault for address: ffff88880b042e80
  #PF: supervisor read access in kernel mode
  RIP: 0010:___slab_alloc+0x5ad/0x760
  Call Trace:
   __kmalloc_cache_node_noprof+0x2dc/0x600
   null_add_dev+0x588/0x1e70 [null_blk]
   nullb_device_power_store+0x29d/0x350 [null_blk]
   configfs_write_iter+0x306/0x500
   vfs_write+0x6a4/0x1030
   ksys_write+0x12a/0x250

Reject home_node values other than NUMA_NO_NODE that do not name an online node in null_validate_conf(), before any allocation uses the value. Checking against nr_node_ids alone is not sufficient, because node IDs below it need not be possible when node numbering is sparse. This matches the validation done for the PCI numa_node sysfs attribute.

Quoted 5 lines

This bug was found by a fuzzer.

Fixes cedcafad8277 ("nullb: add interface to power on disk")
Signature · 2 lines
Damien Le Moal
Western Digital Research