[PATCH v3] md: reject inconsistent reshape state during assembly

From: Kyuwon Cho

Date: Sat Dec 05 2026 - 23:47:01 EST


slot_store() permits a slot below raid_disks + delta_disks while a
reshape prepares additional slots. The v0.90 and v1.x validators can
copy a nonzero delta_disks from a superblock whose reshape_position is
MaxSector. The stale delta lets slot_store() pass an out-of-range slot
to a personality whose table was allocated for raid_disks only.

KASAN reports:

BUG: KASAN: slab-out-of-bounds in raid10_add_disk (drivers/md/raid10.c:2106)
Call Trace:
raid10_add_disk (drivers/md/raid10.c:2106)
slot_store (drivers/md/md.c:3370)
rdev_attr_store (drivers/md/md.c:3788)

Validate each metadata format's raw reshape state while loading the
superblock, before it can be copied into rdev or mddev. Reject zero-disk
v1.x metadata and inactive reshape state that carries changed geometry,
direction, or data-offset fields.

Fixes: ba1b41b6b4e3 ("md: range check slot number when manually adding a spare.")
Closes: https://lore.kernel.org/linux-raid/179489219553.3099317.5403557298223708081@xxxxxxxxx/
Signed-off-by: Kyuwon Cho <kyuwoncho18@xxxxxxxxx>
---
Changes in v3:
- Validate inactive reshape state in each format's load path, reject zero
raid_disks and stale NEW_OFFSET/RESHAPE_BACKWARDS flags in v1.x, and
compare the raw new_layout that super_1_validate() copies.
- Keep Fixes: ba1b41b6b4e3: the suggested f67055780caa introduced reshape
loading, but ba1b41b6b4e3 made stale delta_disks admit the out-of-range
slot involved in this OOB. These changes address the Sashiko AI review:
https://lore.kernel.org/all/sashiko-outbox-250602@xxxxxxxxxx/

Changes in v2:
- Validate the raw superblock before committing its geometry to mddev, so a
failed RUN_ARRAY retry cannot skip the check.
- Canonicalize an implicit RAID0 layout to -1 on both sides of the comparison.
- Keep Fixes: ba1b41b6b4e3 because that commit made stale delta_disks expand
slot_store()'s accepted index range. These changes address the Sashiko AI
review:
https://lore.kernel.org/all/sashiko-outbox-248433@xxxxxxxxxx/

v2: https://lore.kernel.org/all/179637501342.1425433.17124866246346683813@xxxxxxxxx/
v1: https://lore.kernel.org/all/179631974158.3729010.8619704387637000613@xxxxxxxxx/

The bug was found with an LLM-guided test generator. The original report is at
https://lore.kernel.org/linux-raid/179489219553.3099317.5403557298223708081@xxxxxxxxx/.
A reproducer is available on request.

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

diff --git a/drivers/md/md.c b/drivers/md/md.c
index c71c3b043496..076992ec8fc5 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1415,6 +1415,13 @@ static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor
goto abort;
}

+ if (sb->minor_version >= 91 && sb->reshape_position == MaxSector &&
+ (sb->delta_disks != 0 || sb->new_level != sb->level ||
+ sb->new_layout != sb->layout || sb->new_chunk != sb->chunk_size)) {
+ pr_warn("md: inconsistent reshape state on %pg\n", rdev->bdev);
+ goto abort;
+ }
+
rdev->preferred_minor = sb->md_minor;
rdev->data_offset = 0;
rdev->new_data_offset = 0;
@@ -1817,6 +1824,7 @@ static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
{
struct mdp_superblock_1 *sb;
+ u32 features;
int ret;
sector_t sb_start;
sector_t sectors;
@@ -1867,6 +1875,21 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
rdev->bdev);
return -EINVAL;
}
+
+ features = le32_to_cpu(sb->feature_map);
+ if (!le32_to_cpu(sb->raid_disks))
+ return -EINVAL;
+
+ if ((features & MD_FEATURE_RESHAPE_ACTIVE) &&
+ le64_to_cpu(sb->reshape_position) == MaxSector &&
+ (le32_to_cpu(sb->delta_disks) != 0 ||
+ sb->new_level != sb->level || sb->new_layout != sb->layout ||
+ sb->new_chunk != sb->chunksize ||
+ (features & (MD_FEATURE_NEW_OFFSET |
+ MD_FEATURE_RESHAPE_BACKWARDS)))) {
+ pr_warn("md: inconsistent reshape state on %pg\n", rdev->bdev);
+ return -EINVAL;
+ }
if (le64_to_cpu(sb->data_size) < 10) {
pr_warn("md: data_size too small on %pg\n",
rdev->bdev);
@@ -3915,7 +3938,6 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe
/*
* Check a full RAID array for plausibility
*/
-
static int analyze_sbs(struct mddev *mddev)
{
struct md_rdev *rdev, *freshest, *tmp;
--
2.43.0