mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Song Liu <song@kernel.org>, Yu Kuai <yukuai@fygo.io>
Cc: Li Nan <magiclinan@didiglobal.com>, Xiao Ni <xiao@kernel.org>,
	NeilBrown <neil@brown.name>,
	linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3] md: reject inconsistent reshape state during assembly
Date: Sun,  6 Dec 2026 13:46:50 +0900	[thread overview]
Message-ID: <179643369147.1996725.14454060339934285854@gmail.com> (raw)

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@gmail.com/
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
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@kernel.org/

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/

v2: https://lore.kernel.org/all/179637501342.1425433.17124866246346683813@gmail.com/
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 | 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


                 reply	other threads:[~2026-12-06  4:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179643369147.1996725.14454060339934285854@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=magiclinan@didiglobal.com \
    --cc=neil@brown.name \
    --cc=song@kernel.org \
    --cc=xiao@kernel.org \
    --cc=yukuai@fygo.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®