Linux RAID subsystem development
 help / color / mirror / Atom feed
* [PATCH] md: validate v1 dev_number while loading superblock
@ 2026-12-03 16:48 Kyuwon Cho
  2026-12-03 17:46 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-03 16:48 UTC (permalink / raw)
  To: Song Liu; +Cc: Yu Kuai, Li Nan, Xiao Ni, linux-raid, linux-kernel

super_1_load() copies dev_number to rdev->desc_nr without checking it
against the dev_roles array described by max_dev. Assembly normally catches
this later in super_1_validate(), but an array configured through sysfs can
reach super_1_sync() without that validation and index past dev_roles.

Normalize an invalid descriptor number when the superblock is loaded. The
normal rdev binding path will then assign a valid free descriptor number.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
The issue was found by an LLM-guided test generator that we are
developing as a research prototype.

A crafted v1 component advertises dev_number 65536 with a much smaller
max_dev. Creating an array through the md sysfs interface binds that
component without calling super_1_validate(). Updating its state then
calls super_1_sync(), which writes through dev_roles[rdev->desc_nr].
Loop-device and md sysfs setup require root in the initial namespace.

The decoded trace is:

  BUG: KASAN: slab-out-of-bounds in super_1_sync
  Write of size 2
  super_1_sync (drivers/md/md.c:2353)
  md_update_sb (drivers/md/md.c:2522 drivers/md/md.c:2764
                drivers/md/md.c:2940)
  state_store (kernel/power/main.c:1052)
  rdev_attr_store (drivers/md/md.c:3782)
  kernfs_fop_write_iter (fs/kernfs/file.c:345)

The reproducer was tested on v7.4-rc5. The patch was also rebased onto
md commit c526b7cd32f7.

 drivers/md/md.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index c71c3b043496..172bc75fbff7 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1903,6 +1903,9 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
 		return -EINVAL;
 
 	rdev->desc_nr = le32_to_cpu(sb->dev_number);
+	if (rdev->desc_nr < 0 ||
+	    rdev->desc_nr >= le32_to_cpu(sb->max_dev))
+		rdev->desc_nr = -1;
 
 	if (!rdev->bb_page) {
 		rdev->bb_page = alloc_page(GFP_KERNEL);
-- 
2.43.0


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

* Re: [PATCH] md: validate v1 dev_number while loading superblock
  2026-12-03 16:48 [PATCH] md: validate v1 dev_number while loading superblock Kyuwon Cho
@ 2026-12-03 17:46 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 17:46 UTC (permalink / raw)
  To: Kyuwon Cho; +Cc: linux-raid, yukuai

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] md: super_1_load() desc_nr=-1 causes dev_roles[-1] OOB and
  stale dev_number
- [Low] md: Fixes tag cites 1da177e4c3f4 instead of introducing commit
  31b65a0d3894
- [Low] md: two-step write to rdev->desc_nr in super_1_load() races with
  RCU readers
--

commit 141b0781ca4e4bf71f443a15d7441d9cc5894d2a
Author: Kyuwon Cho <kyuwon.cho@skku.edu>

> md: validate v1 dev_number while loading superblock
>
> super_1_load() copies dev_number to rdev->desc_nr without checking it
> against the dev_roles array described by max_dev. Assembly normally catches
> this later in super_1_validate(), but an array configured through sysfs can
> reach super_1_sync() without that validation and index past dev_roles.
>
> Normalize an invalid descriptor number when the superblock is loaded. The
> normal rdev binding path will then assign a valid free descriptor number.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>

[Severity: Low]
In 1da177e4c3f4 ("Linux-2.6.12-rc2"), super_1_load() did not assign
rdev->desc_nr; that assignment was moved there later.  Should this tag
instead be:

  Fixes: 31b65a0d3894 ("[PATCH] md: set desc_nr correctly for version-1 superblocks")

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..5a6c87942bfd8 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -1914,6 +1914,9 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
>  		return -EINVAL;
>  
>  	rdev->desc_nr = le32_to_cpu(sb->dev_number);
> +	if (rdev->desc_nr < 0 ||
> +	    rdev->desc_nr >= le32_to_cpu(sb->max_dev))
> +		rdev->desc_nr = -1;
>  
>  	if (!rdev->bb_page) {
>  		rdev->bb_page = alloc_page(GFP_KERNEL);

[Severity: High]
Can setting rdev->desc_nr to -1 here cause an out-of-bounds write to
sb->dev_roles[-1] in super_1_sync()?

super_1_load() is also called on already-bound rdevs in analyze_sbs() and
read_rdev(), which do not call bind_rdev_to_array() afterward.  During
assembly of a device with an out-of-range sb->dev_number, analyze_sbs()
reloads the superblock and resets rdev->desc_nr back to -1.

Because rdev->desc_nr is signed, -1 bypasses the rdev->desc_nr >=
mddev->max_disks check in analyze_sbs() that previously kicked the device,
and super_1_validate() leaves it bound with rdev->desc_nr == -1.

When super_1_sync() runs, sb->dev_roles[-1] overwrites sb->pad3 on every
member disk, causing later super_1_load() calls to reject even healthy
disks when check_new_feature is enabled.  does_sb_need_changing() and
check_sb_changes() also read sb->dev_roles[-1] out of bounds.

Separately, super_1_sync() never writes sb->dev_number, so an invalid
on-disk dev_number stays stale even on the sysfs path after
bind_rdev_to_array() assigns a valid descriptor.  Returning -EINVAL from
super_1_load() on an out-of-range sb->dev_number avoids both issues.

[Severity: Low]
Can writing the unvalidated sb->dev_number directly into rdev->desc_nr
before clamping it to -1 race with concurrent RCU readers?

analyze_sbs() and read_rdev() call super_1_load() on already-bound rdevs
while holding only mddev->reconfig_mutex, whereas md_seq_show() reads
rdev->desc_nr under mddev->lock and rcu_read_lock():

CPU0                                CPU1
analyze_sbs()
  super_1_load()
    rdev->desc_nr = le32_to_cpu(...)
                                    md_seq_show()
                                      rcu_read_lock()
                                      seq_printf(..., rdev->desc_nr)
                                        <-- reads unvalidated value
    rdev->desc_nr = -1

This transiently exposes the out-of-range descriptor number to /proc/mdstat
(and similarly to GET_DISK_INFO via md_find_rdev_nr_rcu() during clustered
reloads) and triggers a KCSAN data race.  Validating into a local variable
before updating rdev->desc_nr avoids publishing the invalid value.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179621082761.2397410.3913367575512805215@skku.edu?part=1

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

end of thread, other threads:[~2026-12-03 17:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:48 [PATCH] md: validate v1 dev_number while loading superblock Kyuwon Cho
2026-12-03 17:46 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox