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

From: Kyuwon Cho

Date: Fri Dec 04 2026 - 00:05:42 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 as part of the initial superblock
header validation, before changing any rdev geometry. This also keeps a
failed clustered reload from leaving data_offset or sb_size inconsistent
with the restored superblock.

Fixes: 31b65a0d3894 ("[PATCH] md: set desc_nr correctly for version-1 superblocks")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v3:
- Move the dev_number check into the initial header validation so a failed
load cannot alter rdev geometry (Sashiko review).
- Rebase onto md-7.5.

Link to v2: https://lore.kernel.org/all/179632352675.3850376.17102584105921885328@xxxxxxxx/
Link to 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 | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index c71c3b043496..571fd3f2ba14 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1858,6 +1858,7 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
sb->major_version != cpu_to_le32(1) ||
le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
+ le32_to_cpu(sb->dev_number) >= le32_to_cpu(sb->max_dev) ||
le64_to_cpu(sb->super_offset) != rdev->sb_start ||
(le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
return -EINVAL;
--
2.43.0