null_blk: validate configfs values before power-on
Subsystems (2)
v2 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.
---
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 (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 v2 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.
This bug was found by a fuzzer.
--- 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 ;
Re: [PATCH v2 1/3] null_blk: reject device size overflow
Quoted 24 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()andmb_to_sects()multiply that value bySZ_1Mbefore 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 underflowszone_nr_convtoUINT_MAX. The subsequent zone initialization then accesses invalid memory:
null_blk: changed the number of conventional zones to 4294967295BUG: 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
Quoted 3 lines
Non-zoned devices can instead be registered with a wrapped capacity.
Reject sizes above
LLONG_MAX/SZ_1Minnull_validate_conf(), before
Why not ULLONG_MAX / SZ_1M ?
Any particular reason?
Quoted 30 lines
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.
This bug was found by a fuzzer.
Fixes cedcafad8277 ("nullb: add interface to power on disk")Signed-off-by Yingjie Wang <[email protected]>--- 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 · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v2 1/3] null_blk: reject device size overflow
Quoted 4 lines · Damien Le Moal
Quoted 1 line
Reject sizes above
LLONG_MAX/SZ_1Minnull_validate_conf(), beforeWhy not
ULLONG_MAX/SZ_1M?
Any particular reason?
ULLONG_MAX / SZ_1M would avoid the wrap, but the block layer cannot represent a disk that large. A block device's size in bytes is a loff_t, and set_capacity() truncates anything above BLK_DEV_MAX_SECTORS (LLONG_MAX >> SECTOR_SHIFT) with a warning. With a ULLONG_MAX / SZ_1M limit, null_blk would register a disk smaller than the configured size. LLONG_MAX / SZ_1M is the largest size in MiB that fits.
I will add this to the commit message in v3. v3 will also compare the size as u64, because the kernel test robot reported that the comparison is always false on 32-bit.
Thanks,
Yingjie
Re: [PATCH v2 1/3] null_blk: reject device size overflow
Quoted 6 lines · Yingjie Wang
Quoted 4 lines · Damien Le Moal
Quoted 1 line
Reject sizes above
LLONG_MAX/SZ_1Minnull_validate_conf(), beforeWhy not
ULLONG_MAX/SZ_1M?
Any particular reason?
Quoted 6 lines
ULLONG_MAX/SZ_1Mwould avoid the wrap, but the block layer cannot represent a disk that large. A block device's size in bytes is aloff_t, andset_capacity()truncates anything aboveBLK_DEV_MAX_SECTORS(LLONG_MAX>>SECTOR_SHIFT) with a warning. With aULLONG_MAX/SZ_1Mlimit,null_blkwould register a disk smaller than the configured size.LLONG_MAX/SZ_1Mis the largest size in MiB that fits.
Then compare everything as a number of sectors against BLK_DEV_MAX_SECTORS. That will avoid using directly LLONG_MAX. BLK_DEV_MAX_SECTORS is ever changed, the code will still work.
Signature · 2 lines
Damien Le Moal Western Digital Research
kernel test robot
build botRe: [PATCH v2 1/3] null_blk: reject device size overflow
In reply to Yingjie Wangkernel test robot noticed the following build warnings:
Hi Yingjie,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 5a29cfaf51105039d156dd8a61d2783e8b653667]
url: https://github.com/intel-lab-lkp/linux/commits/Yingjie-Wang/null_blk-reject-device-size-overflow/20261126-074752 base: 5a29cfaf51105039d156dd8a61d2783e8b653667 patch link: https://lore.kernel.org/r/20261126074748.1076774-2-x-professor%40outlook.com patch subject: [PATCH v2 1/3] null_blk: reject device size overflow config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20261127/[email protected]/config) compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project fb29ca8163c3309160711e5a14eb46bc6d4fdad2) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261127/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/block/null_blk/main.c:1896:16: warning: result of comparison of constant 8796093022207 with expression of type 'unsigned long' is always false [-Wtautological-constant-out-of-range-compare]
1896 | if (dev->size > LLONG_MAX / SZ_1M) {
| ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~1 warning generated.
vim +1896 drivers/block/null_blk/main.c
1893
1894 static int null_validate_conf(struct nullb_device *dev)
1895 {
> 1896 if (dev->size > LLONG_MAX / SZ_1M) { 1897 pr_err("Device size is too large\n");
1898 return -EINVAL;
1899 }
1900
1901 if (dev->queue_mode == NULL_Q_RQ) {
1902 pr_err("legacy IO path is no longer available\n");
1903 return -EINVAL;
1904 }
1905 if (dev->queue_mode == NULL_Q_BIO) {
1906 pr_err("BIO-based IO path is no longer available, using blk-mq instead.\n");
1907 dev->queue_mode = NULL_Q_MQ;
1908 }
1909
1910 if (dev->use_per_node_hctx) {
1911 if (dev->submit_queues != nr_online_nodes)
1912 dev->submit_queues = nr_online_nodes;
1913 } else if (dev->submit_queues > nr_cpu_ids)
1914 dev->submit_queues = nr_cpu_ids;
1915 else if (dev->submit_queues == 0)
1916 dev->submit_queues = 1;
1917 dev->prev_submit_queues = dev->submit_queues;
1918
1919 if (dev->poll_queues > g_poll_queues)
1920 dev->poll_queues = g_poll_queues;
1921 dev->prev_poll_queues = dev->poll_queues;
1922 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
1923
1924 /* Do memory allocation, so set blocking */
1925 if (dev->memory_backed)
1926 dev->blocking = true;
1927 else /* cache is meaningless */
1928 dev->cache_size = 0;
1929 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1930 dev->cache_size);
1931 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1932
1933 if (dev->zoned &&
1934 (!dev->zone_size || !is_power_of_2(dev->zone_size))) {
1935 pr_err("zone_size must be power-of-two\n");
1936 return -EINVAL;
1937 }
1938
1939 return 0;
1940 }
1941
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 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.
This bug was found by a fuzzer.
--- 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);
Re: [PATCH v2 2/3] null_blk: reject zone counts that overflow unsigned int
Quoted 28 lines · Yingjie Wang
null_init_zoned_dev()calculates the zone count fromsector_tvalues but stores it in the unsigned intnr_zonesfield 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 innr_zones.The resulting zero-length allocation returns
ZERO_SIZE_PTR. Adjusting the number of conventional zones then underflowszone_nr_convtoUINT_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_ttemporary and reject values aboveUINT_MAXbefore assigningnr_zonesor allocating the zone array.This bug was found by a fuzzer.
Fixes ca4b2a011948 ("null_blk: add zone support")Signed-off-by Yingjie Wang <[email protected]>--- drivers/block/null_blk/zoned.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/block/null_blk/zoned.c +0
-0
drivers/block/null_blk/zoned.c int null_init_zoned_dev(struct nullb_device *dev, · line 57
Quoted 4 lines
drivers/block/null_blk/zoned.c +1
-0@@ -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;
Please use unsigned long long here. sector_t is for, well, sectors :)
Other than this, looks OK to me.
Quoted 18 lines
drivers/block/null_blk/zoned.c +6
-1struct 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 · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 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.
This bug was found by a fuzzer.
--- 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 ;