All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/4] null_blk: validate configfs values before power-on
@ 2026-12-01 14:01 Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h> Yingjie Wang
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 14:01 UTC (permalink / raw)
  To: axboe@kernel.dk; +Cc: linux-block@vger.kernel.org, Damien Le Moal

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 patches 1/4 and 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 1/4 and 2/4 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.

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 v4:
- 1/4: Use SECTOR_SHIFT instead of 9 (Damien).
- 2/4, 3/4, 4/4: Add Reviewed-by from Damien. No code changes.
- Rerun the runtime tests on a kernel with the series applied.

v3: https://lore.kernel.org/linux-block/20261127125738.1654012-1-x-professor@outlook.com/

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/20261127004636.Zw6bu9Dk-lkp@intel.com/
- 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/20261126074748.1076774-1-x-professor@outlook.com/

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/20261126063404.1051061-1-x-professor@outlook.com/

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
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>
  2026-12-01 14:01 [PATCH v4 0/4] null_blk: validate configfs values before power-on Yingjie Wang
@ 2026-12-01 14:02 ` Yingjie Wang
  2026-12-02  5:51   ` Damien Le Moal
  2026-12-01 14:02 ` [PATCH v4 2/4] null_blk: reject device size overflow Yingjie Wang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 14:02 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, Damien Le Moal,
	stable@vger.kernel.org

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>, using SECTOR_SHIFT instead of 9 while at
it. The next patch uses it in null_blk to reject a configured device
size that is too large. No functional change.

Cc: stable@vger.kernel.org # 6.14.x
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 block/blk.h            | 1 -
 include/linux/blkdev.h | 6 ++++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/block/blk.h b/block/blk.h
index 105fff157bfb..13fa24aa05ea 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -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 */
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef..1786fddd00ac 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -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 >> SECTOR_SHIFT)
+
 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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 2/4] null_blk: reject device size overflow
  2026-12-01 14:01 [PATCH v4 0/4] null_blk: validate configfs values before power-on Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h> Yingjie Wang
@ 2026-12-01 14:02 ` Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 3/4] null_blk: reject zone counts that overflow unsigned int Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 4/4] null_blk: validate the configfs home_node attribute Yingjie Wang
  3 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 14:02 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, Damien Le Moal,
	stable@vger.kernel.org

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 <x-professor@outlook.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 3/4] null_blk: reject zone counts that overflow unsigned int
  2026-12-01 14:01 [PATCH v4 0/4] null_blk: validate configfs values before power-on Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h> Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 2/4] null_blk: reject device size overflow Yingjie Wang
@ 2026-12-01 14:02 ` Yingjie Wang
  2026-12-01 14:02 ` [PATCH v4 4/4] null_blk: validate the configfs home_node attribute Yingjie Wang
  3 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 14:02 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, Damien Le Moal,
	stable@vger.kernel.org

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 <x-professor@outlook.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v4 4/4] null_blk: validate the configfs home_node attribute
  2026-12-01 14:01 [PATCH v4 0/4] null_blk: validate configfs values before power-on Yingjie Wang
                   ` (2 preceding siblings ...)
  2026-12-01 14:02 ` [PATCH v4 3/4] null_blk: reject zone counts that overflow unsigned int Yingjie Wang
@ 2026-12-01 14:02 ` Yingjie Wang
  3 siblings, 0 replies; 6+ messages in thread
From: Yingjie Wang @ 2026-12-01 14:02 UTC (permalink / raw)
  To: axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, Damien Le Moal,
	stable@vger.kernel.org

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")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/block/null_blk/main.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index eb7617fd0964..726c8c58aff4 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h>
  2026-12-01 14:02 ` [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h> Yingjie Wang
@ 2026-12-02  5:51   ` Damien Le Moal
  0 siblings, 0 replies; 6+ messages in thread
From: Damien Le Moal @ 2026-12-02  5:51 UTC (permalink / raw)
  To: Yingjie Wang, axboe@kernel.dk
  Cc: linux-block@vger.kernel.org, stable@vger.kernel.org

On 12/1/26 23:02, Yingjie Wang wrote:
> 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>, using SECTOR_SHIFT instead of 9 while at
> it. The next patch uses it in null_blk to reject a configured device
> size that is too large. No functional change.
> 
> Cc: stable@vger.kernel.org # 6.14.x
> Signed-off-by: Yingjie Wang <x-professor@outlook.com>

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-02  5:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 14:01 [PATCH v4 0/4] null_blk: validate configfs values before power-on Yingjie Wang
2026-12-01 14:02 ` [PATCH v4 1/4] block: move BLK_DEV_MAX_SECTORS to <linux/blkdev.h> Yingjie Wang
2026-12-02  5:51   ` Damien Le Moal
2026-12-01 14:02 ` [PATCH v4 2/4] null_blk: reject device size overflow Yingjie Wang
2026-12-01 14:02 ` [PATCH v4 3/4] null_blk: reject zone counts that overflow unsigned int Yingjie Wang
2026-12-01 14:02 ` [PATCH v4 4/4] null_blk: validate the configfs home_node attribute Yingjie Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.