mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] md: bound raid_disks by max_disks for v0.90 metadata
@ 2026-11-19  9:01 Yang Luohan
  0 siblings, 0 replies; only message in thread
From: Yang Luohan @ 2026-11-19  9:01 UTC (permalink / raw)
  To: song; +Cc: yukuai3, linux-raid, linux-kernel

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;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19  9:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19  9:01 [PATCH] md: bound raid_disks by max_disks for v0.90 metadata Yang Luohan

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®