[PATCH v2] md: reject inconsistent reshape state during assembly
From: Kyuwon Cho
Date: Fri Dec 04 2026 - 14:51:30 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 the freshest superblock's raw reshape tuple before
validate_super() commits it to mddev. This makes a failed RUN_ARRAY retry
metadata again. Treat an implicit RAID0 layout as the canonical -1 on both
sides of the comparison so ordinary RAID0 arrays remain valid.
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 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/
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 | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index c71c3b043496..519695040e6c 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -3916,6 +3916,57 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe
* Check a full RAID array for plausibility
*/
+static bool inactive_reshape_is_consistent(struct mddev *mddev,
+ struct md_rdev *rdev)
+{
+ int level, layout, new_level, new_layout;
+
+ if (mddev->major_version == 0) {
+ mdp_super_t *sb = page_address(rdev->sb_page);
+
+ if (sb->minor_version < 91 ||
+ sb->reshape_position != (sector_t)-1)
+ return true;
+
+ level = sb->level;
+ layout = level == 0 ? -1 : sb->layout;
+ new_level = sb->new_level;
+ new_layout = new_level == 0 ? -1 : sb->new_layout;
+
+ return level == new_level &&
+ layout == new_layout &&
+ sb->chunk_size == sb->new_chunk &&
+ sb->delta_disks == 0;
+ }
+
+ if (mddev->major_version == 1) {
+ struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
+ u32 features = le32_to_cpu(sb->feature_map);
+
+ if (!(features & MD_FEATURE_RESHAPE_ACTIVE) ||
+ le64_to_cpu(sb->reshape_position) != (sector_t)-1)
+ return true;
+
+ level = le32_to_cpu(sb->level);
+ layout = le32_to_cpu(sb->layout);
+ new_level = le32_to_cpu(sb->new_level);
+ new_layout = le32_to_cpu(sb->new_layout);
+ if (!(features & MD_FEATURE_RAID0_LAYOUT)) {
+ if (level == 0)
+ layout = -1;
+ if (new_level == 0)
+ new_layout = -1;
+ }
+
+ return level == new_level &&
+ layout == new_layout &&
+ sb->chunksize == sb->new_chunk &&
+ sb->delta_disks == 0;
+ }
+
+ return true;
+}
+
static int analyze_sbs(struct mddev *mddev)
{
struct md_rdev *rdev, *freshest, *tmp;
@@ -3941,6 +3992,11 @@ static int analyze_sbs(struct mddev *mddev)
return -EINVAL;
}
+ if (!inactive_reshape_is_consistent(mddev, freshest)) {
+ pr_warn("md: %s: inconsistent reshape state\n", mdname(mddev));
+ return -EINVAL;
+ }
+
super_types[mddev->major_version].
validate_super(mddev, NULL/*freshest*/, freshest);