null_blk: validate configfs values before power-on
Subsystems (2)
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:
1/3 size=2^44 MiB with zoned=1 wraps the byte capacity to zero.
The resulting zero zone count causes zone_nr_conv to underflow.
2/3 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.
3/3 home_node=2147483647 is passed to kzalloc_node() without
validation, causing an out-of-bounds access in SLUB.
The zone count overflow still occurs with only patch 1 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 patch 1 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.
- Valid non-zoned devices were tested at 0, 1 and LLONG_MAX / SZ_1M 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.
Testing was limited to x86_64. Sparse NUMA node numbering was not tested. The reproducers are available on request.
Yingjie Wang (3):
null_blk: reject device size overflow
null_blk: reject zone counts that overflow unsigned int
null_blk: validate the configfs home_node attribute
drivers/block/null_blk/main.c | 11 +++++++++++ drivers/block/null_blk/zoned.c | 8 +++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
base-commit: 5a29cfaf51105039d156dd8a61d2783e8b653667
Signature · 1 line
2.43.0
PATCH 1/3 null_blk: reject device size overflow
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.
Reject sizes above LLONG_MAX / SZ_1M in null_validate_conf(), before allocating device resources or initializing zones. This also keeps the byte capacity within the signed 64-bit range supported by the block layer. This check runs for both configfs power-on and module initialization.
--- drivers/block/null_blk/main.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/block/null_blk/main.c +5 -0
@@ -1893,6 +1893,11 @@ static int null_setup_tagset(struct nullb *nullb)static int null_validate_conf (struct nullb_device *dev) { +if (dev->size >LLONG_MAX /SZ_1M ) { +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
PATCH 2/3 null_blk: reject zone counts that overflow unsigned int
In reply to Yingjie Wangnull_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 a sector_t temporary and reject values above UINT_MAX before assigning nr_zones or allocating the zone array.
--- drivers/block/null_blk/zoned.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/null_blk/zoned.c +7 -1
@@ -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; +sector_t 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
PATCH 3/3 null_blk: validate the configfs home_node attribute
In reply to Yingjie WangThe 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.
--- drivers/block/null_blk/main.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/block/null_blk/main.c +6 -0
@@ -1898,6 +1898,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
Sorry, this series went out without my Signed-off-by by mistake. Please disregard this version; I will resend it as v2 with the tag added.