[PATCH v2] md: validate v1 dev_number while loading superblock

From: Kyuwon Cho

Date: Thu Dec 03 2026 - 14:50:39 EST


super_1_load() copies the v1 dev_number into rdev->desc_nr without checking
that the descriptor exists in dev_roles. A sysfs-configured array can reach
super_1_sync() without super_1_validate() and index beyond the role array.

Reject an out-of-range descriptor before publishing it in the rdev. This
also avoids treating a normalized negative descriptor as a spare and later
indexing dev_roles[-1].

Fixes: 31b65a0d3894 ("[PATCH] md: set desc_nr correctly for version-1 superblocks")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v2:
- Reject an invalid dev_number before assigning rdev->desc_nr instead of
normalizing it after publication, as suggested by the Sashiko review.
- Correct the Fixes tag to the commit that moved desc_nr assignment into
super_1_load().

v1: https://lore.kernel.org/all/179621082761.2397410.3913367575512805215@xxxxxxxx/

The issue was found by an LLM-guided test generator that we are
developing as a research prototype.

A reproducer is available on request.

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

diff --git a/drivers/md/md.c b/drivers/md/md.c
index c71c3b043496..fe97ae811bc8 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1902,8 +1902,10 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
&& rdev->new_data_offset < sb_start + (rdev->sb_size/512))
return -EINVAL;

- rdev->desc_nr = le32_to_cpu(sb->dev_number);
+ if (le32_to_cpu(sb->dev_number) >= le32_to_cpu(sb->max_dev))
+ return -EINVAL;

+ rdev->desc_nr = le32_to_cpu(sb->dev_number);
if (!rdev->bb_page) {
rdev->bb_page = alloc_page(GFP_KERNEL);
if (!rdev->bb_page)
--
2.43.0