From: Yang Luohan <ylh440104@gmail.com>
To: song@kernel.org
Cc: yukuai3@huawei.com, linux-raid@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] md: bound raid_disks by max_disks for v0.90 metadata
Date: Thu, 19 Nov 2026 09:01:02 +0000 [thread overview]
Message-ID: <179507886219.2370.1301636898303569514@gmail.com> (raw)
md: bound raid_disks by max_disks for v0.90 metadata
commit 1a33a379e6 ("md: reject v0.90 arrays with too many devices")
bounded sb->raid_disks in super_90_load(), so a crafted superblock can
no longer describe more devices than a v0.90 superblock is able to hold.
mddev->raid_disks however is not only loaded from the superblock.
Both the raid_disks sysfs attribute and MD_SET_ARRAY_INFO write it
directly while the array has no personality, and neither enforces any
upper bound. super_90_sync() then lays out the in-superblock device
table using mddev->raid_disks and walks past the end of the 4 KiB
superblock page.
mddev->max_disks already carries the limit that the selected metadata
format can represent, and update_raid_disks() already enforces it, but
it is only initialized when a superblock is read from a member device.
An array that has just been created through sysfs or
MD_SET_ARRAY_INFO has max_disks cleared, so the check is skipped.
Set max_disks when v0.90 metadata is selected in metadata_store(), and
reject values the selected format cannot represent in
raid_disks_store(). Both orderings are then covered: the limit is
installed either before the value is written or together with the
metadata choice. Clamp super_90_sync() as well, so an array which
already carries a larger value cannot walk past sb->disks[].
v1.x metadata sets max_disks to (4096-256)/2 and is therefore not
affected by the v0.90 limit.
Fixes: 1a33a379e6 ("md: reject v0.90 arrays with too many devices")
Signed-off-by: Yang Luohan <ylh440104@gmail.com>
diff --git a/drivers/md/md.c b/drivers/md/md.c
index d1465bcd8..769ed1f09 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1610,7 +1610,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
{
mdp_super_t *sb;
struct md_rdev *rdev2;
- int next_spare = mddev->raid_disks;
+ int next_spare = min_t(int, mddev->raid_disks, MD_SB_DISKS);
/* make rdev->sb match mddev data..
*
@@ -1643,7 +1643,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
sb->level = mddev->level;
sb->size = mddev->dev_sectors / 2;
- sb->raid_disks = mddev->raid_disks;
+ sb->raid_disks = min_t(int, mddev->raid_disks, MD_SB_DISKS);
sb->md_minor = mddev->md_minor;
sb->not_persistent = 0;
sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
@@ -1727,7 +1727,7 @@ static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
d->state |= (1<<MD_DISK_FAILFAST);
}
/* now set the "removed" and "faulty" bits on any missing devices */
- for (i=0 ; i < mddev->raid_disks ; i++) {
+ for (i=0 ; i < min_t(int, mddev->raid_disks, MD_SB_DISKS) ; i++) {
mdp_disk_t *d = &sb->disks[i];
if (d->state == 0 && d->number == 0) {
d->number = i;
@@ -4441,6 +4441,8 @@ raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
mddev->delta_disks = n - olddisks;
mddev->raid_disks = n;
mddev->reshape_backwards = (mddev->delta_disks < 0);
+ } else if (mddev->max_disks && n >= mddev->max_disks) {
+ err = -EINVAL;
} else
mddev->raid_disks = n;
out_unlock:
@@ -5029,6 +5031,13 @@ metadata_store(struct mddev *mddev, const char *buf, size_t len)
err = -ENOENT;
if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
goto out_unlock;
+ err = -EINVAL;
+ if (major == 0 && mddev->raid_disks >= MD_SB_DISKS)
+ goto out_unlock;
+ if (major == 0)
+ mddev->max_disks = MD_SB_DISKS;
+ else
+ mddev->max_disks = (4096 - 256) / 2;
mddev->major_version = major;
mddev->minor_version = minor;
mddev->persistent = 1;
reply other threads:[~2026-11-19 9:01 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=179507886219.2370.1301636898303569514@gmail.com \
--to=ylh440104@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=song@kernel.org \
--cc=yukuai3@huawei.com \
/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®