All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yang Luohan" <ylh440104@gmail.com>
Cc: linux-raid@vger.kernel.org, yukuai@fygo.io
Subject: Re: [PATCH] md: bound raid_disks by max_disks for v0.90 metadata
Date: Thu, 19 Nov 2026 09:15:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-222802@kernel.org> (raw)
In-Reply-To: <179507886219.2370.1301636898303569514@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] md: out-of-bounds access to sb->disks[] in super_90_sync()
- [Medium] md: missing raid_disks check and max_disks init in
  md_set_array_info()
- [Medium] md: max_disks check bypass and int overflow in
  raid_disks_store()
- [Low] md: incomplete max_disks check and stale max_disks in
  metadata_store()
- [Low] md: 10-char SHA in Fixes trailer and duplicated subject in
  changelog
--

commit 0588d69c6a4d85b9a67f6669d8e19860ad381e67
Author: Yang Luohan <ylh440104@gmail.com>

> md: bound raid_disks by max_disks for v0.90 metadata
>
> 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>

[Severity: Low]
The Fixes: tag uses a 10-character SHA instead of at least 12 characters:

  Fixes: 1a33a379e678 ("md: reject v0.90 arrays with too many devices")

The subject line is also duplicated at the top of the commit message body.

[Severity: Medium]
The commit message notes that MD_SET_ARRAY_INFO writes mddev->raid_disks
directly and leaves max_disks cleared, but md_set_array_info() is not
updated in this patch.

When info->raid_disks != 0, md_set_array_info() assigns mddev->raid_disks
= info->raid_disks without checking it against 0 or MD_SB_DISKS.  When
info->raid_disks == 0, it sets major_version, minor_version and persistent
while leaving mddev->max_disks == 0, so a subsequent sysfs write to
raid_disks_store() still bypasses the max_disks check.

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..788521a55a13c 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -1630,7 +1630,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..
>  	 *
> @@ -1663,7 +1663,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);
> @@ -1747,7 +1747,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;

[Severity: High]
Can super_90_sync() still access sb->disks[] out of bounds in the
rdev_for_each() loop and at sb->this_disk = sb->disks[rdev->desc_nr]?

sb->disks has MD_SB_DISKS (27) entries (indices 0..26), followed by
sb->this_disk at the end of the 4 KiB rdev->sb_page:

drivers/md/md.c:super_90_sync() {
	...
	rdev_for_each(rdev2, mddev) {
		...
		if (is_active)
			desc_nr = rdev2->raid_disk;
		else
			desc_nr = next_spare++;
		rdev2->desc_nr = desc_nr;
		d = &sb->disks[rdev2->desc_nr];
	...
	sb->this_disk = sb->disks[rdev->desc_nr];
}

When mddev->raid_disks >= MD_SB_DISKS (or when mddev->raid_disks plus the
number of spare or faulty devices in mddev->disks exceeds MD_SB_DISKS),
next_spare++ reaches >= MD_SB_DISKS, and rdev2->raid_disk for active
devices is not bounded against MD_SB_DISKS either.  Indexing
sb->disks[rdev2->desc_nr] and sb->disks[rdev->desc_nr] then reads and
writes past sb->disks[] and past the 4 KiB superblock page.

> @@ -4473,6 +4473,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:

[Severity: Medium]
Can raid_disks_store() bypass the max_disks check or overflow
mddev->raid_disks?

Because the new check is placed after the mddev->reshape_position !=
MaxSector branch, writing to raid_disks while mddev->pers == NULL and
reshape_position != MaxSector assigns mddev->raid_disks = n without
checking mddev->max_disks.

In addition, n is parsed as an unsigned int while mddev->raid_disks is a
signed int.  If n > INT_MAX is written while mddev->max_disks == 0,
mddev->raid_disks becomes negative, which then passes the signed
mddev->raid_disks >= MD_SB_DISKS check in metadata_store() and causes
min_t(int, mddev->raid_disks, MD_SB_DISKS) in super_90_sync() to evaluate
to a negative number.

> @@ -5067,6 +5069,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;

[Severity: Low]
Should metadata_store() also validate mddev->raid_disks against the new
max_disks when major != 0, and reset mddev->max_disks = 0 in the "none"
and "external:" branches?

Right now, writing raid_disks >= (4096 - 256) / 2 before selecting v1.x
metadata is not rejected here, and switching metadata_version from 0.90 or
1.x to "none" or "external:" leaves the previous max_disks limit set on
the array.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179507886219.2370.1301636898303569514@gmail.com?part=1

      reply	other threads:[~2026-11-19  9:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  9:01 [PATCH] md: bound raid_disks by max_disks for v0.90 metadata Yang Luohan
2026-11-19  9:15 ` sashiko-bot [this message]

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=sashiko-outbox-222802@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=ylh440104@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.