* [PATCH v2] md: reject inconsistent reshape state during assembly
@ 2026-12-04 19:51 Kyuwon Cho
2026-12-04 20:10 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-04 19:51 UTC (permalink / raw)
To: Song Liu, Yu Kuai; +Cc: Li Nan, Xiao Ni, NeilBrown, linux-raid, linux-kernel
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@gmail.com/
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
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@kernel.org/
v1: https://lore.kernel.org/all/179631974158.3729010.8619704387637000613@gmail.com/
The bug was found with an LLM-guided test generator. The original report is at
https://lore.kernel.org/linux-raid/179489219553.3099317.5403557298223708081@gmail.com/.
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);
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] md: reject inconsistent reshape state during assembly
2026-12-04 19:51 [PATCH v2] md: reject inconsistent reshape state during assembly Kyuwon Cho
@ 2026-12-04 20:10 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 20:10 UTC (permalink / raw)
To: Kyuwon Cho; +Cc: linux-raid, yukuai
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] md: v1.x raid_disks == 0 bypasses
inactive_reshape_is_consistent()
- [Low] md: inactive_reshape_is_consistent() misses v1.x flags and RAID0
new_layout
- [Low] md: Fixes trailer cites ba1b41b6b4e3 instead of f67055780caa
--
commit 500726eaee6390956b12674623d4a55c32568a00
Author: Kyuwon Cho <kyuwoncho18@gmail.com>
> md: reject inconsistent reshape state during assembly
>
> 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@gmail.com/
> Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
[Severity: Low]
Does the Fixes tag point to the right commit?
Commit ba1b41b6b4e3 ("md: range check slot number when manually adding a
spare.") added the upper bound in slot_store()'s active-array path rather
than widening it. Unvalidated v0.91/v1.x reshape loading came from
f67055780caa ("[PATCH] md: Checkpoint and allow restart of raid5 reshape"):
Fixes: f67055780caa ("[PATCH] md: Checkpoint and allow restart of raid5 reshape")
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..c1e64a2aa15a8 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -3921,6 +3921,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)
> +{
[ ... ]
> + 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;
> + }
[Severity: Low]
Can a v1.x superblock with MD_FEATURE_RESHAPE_ACTIVE and reshape_position
== MaxSector still leave inconsistent reshape state in mddev or rdev?
This check misses MD_FEATURE_RESHAPE_BACKWARDS and MD_FEATURE_NEW_OFFSET
with nonzero sb->new_offset. super_1_validate() then sets
mddev->reshape_backwards = 1, and super_1_load() shifts
rdev->new_data_offset, breaking later checks in update_raid_disks().
For implicit-layout RAID0 on v0.91 or v1.x without MD_FEATURE_RAID0_LAYOUT,
canonicalizing new_layout to -1 accepts any sb->new_layout. However,
super_90_validate() and super_1_validate() copy raw sb->new_layout into
mddev->new_layout while resetting only mddev->layout to -1.
[ ... ]
> @@ -3946,6 +3997,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);
[Severity: Medium]
Can a second v1.x member disk bypass inactive_reshape_is_consistent() when
freshest has sb->raid_disks == 0?
Unlike super_90_load(), super_1_load() does not reject sb->raid_disks == 0
and only compares set_uuid, level, layout, and chunksize across members.
If freshest has raid_disks == 0 without MD_FEATURE_RESHAPE_ACTIVE,
super_1_validate() leaves mddev->raid_disks == 0.
The rdev_for_each_safe loop then passes the next rdev to super_1_validate(),
which re-enters its mddev->raid_disks == 0 branch and commits that disk's
unchecked reshape_position == MaxSector and nonzero delta_disks, allowing
slot_store() to trigger the same slab-out-of-bounds in raid10_add_disk().
Hardcoding mddev->major_version checks in analyze_sbs() also bypasses
super_types[]. Moving the check into super_90_validate() and
super_1_validate() (and rejecting sb->raid_disks == 0 in super_1_load())
would fix both.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179637501342.1425433.17124866246346683813@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 20:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 19:51 [PATCH v2] md: reject inconsistent reshape state during assembly Kyuwon Cho
2026-12-04 20:10 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox