Linux RAID subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Aibing Chen" <chenaibing@cmss.chinamobile.com>
Cc: linux-raid@vger.kernel.org, yukuai@fygo.io
Subject: Re: [PATCH] md/raid1: reject oversized raid_disks values
Date: Wed, 18 Nov 2026 00:11:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-219833@kernel.org> (raw)
In-Reply-To: <20261117235714.1101-1-chenaibing@cmss.chinamobile.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] md/raid1: raid1_run() upper bound fails to bound
  conf->mirrors on 64K pages
- [Medium] md/raid1: missing raid_disks upper-bound check in
  raid1_reshape()
--

commit 377f5560edb8ce6de0f74acd8535bb4eba6e1103
Author: Aibing Chen <chenaibing@cmss.chinamobile.com>

md/raid1: reject oversized raid_disks values

This commit rejects negative, zero, or oversized mddev->raid_disks values in
raid1_run() before allocating the RAID1 configuration structures and resync
buffer pool.

> diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
> index 894efb50c6d58..875c985526928 100644
> --- a/drivers/md/raid1.c
> +++ b/drivers/md/raid1.c
> @@ -3169,6 +3169,18 @@ static int raid1_run(struct mddev *mddev)
>  		return -EIO;
>  	}
>  
> +	/*
> +	 * The resync and r1bio pools are sized from raid_disks * 2.  A
> +	 * raid_disks value large enough to make those allocations exceed
> +	 * KMALLOC_MAX_SIZE would only trigger a kmalloc WARN_ON() when the
> +	 * resync starts, see r1buf_pool_alloc(), and the resync would not
> +	 * run at all.  Reject such a value before anything is allocated.
> +	 */
> +	if (mddev->raid_disks <= 0 ||
> +	    mddev->raid_disks > KMALLOC_MAX_SIZE /
> +				(2 * sizeof(struct resync_pages)))
> +		return -EINVAL;

[Severity: Medium]
Can conf->mirrors in setup_conf() still exceed KMALLOC_MAX_SIZE on 64 KiB page
kernels?

When PAGE_SIZE is 64 KiB, RESYNC_PAGES is 1, so sizeof(struct resync_pages) is
16 bytes while sizeof(struct raid1_info) is 32 bytes.  A raid_disks value in
(KMALLOC_MAX_SIZE / 64, KMALLOC_MAX_SIZE / 32] passes this check and then
triggers the WARN_ON_ONCE_GFP() in setup_conf() when allocating conf->mirrors:

drivers/md/raid1.c:setup_conf() {
	...
	conf->mirrors = kzalloc(array3_size(sizeof(struct raid1_info),
					    mddev->raid_disks, 2),
				GFP_KERNEL);
	...
}

Dividing by 2 * max(sizeof(struct resync_pages), sizeof(struct raid1_info))
would cover both allocations across all page sizes.

[Severity: Medium]
Does raid1_reshape() also need this upper-bound check on raid_disks?

On a running non-persistent or external-metadata RAID1 array where
mddev->max_disks is 0, updating raid_disks via the SET_ARRAY_INFO ioctl or
sysfs raid_disks calls update_raid_disks() -> raid1_reshape() without
re-running raid1_run():

drivers/md/raid1.c:raid1_reshape() {
	...
	raid_disks = mddev->raid_disks + mddev->delta_disks;
	...
	new_r1bio_size = offsetof(struct r1bio, bios[raid_disks * 2]);
	newpool = mempool_create_kmalloc_pool(NR_RAID_BIOS, new_r1bio_size);
	if (!newpool) {
		return -ENOMEM;
	}
	newmirrors = kzalloc(array3_size(sizeof(struct raid1_info),
					 raid_disks, 2),
			     GFP_KERNEL);
	...
}

Depending on the new raid_disks value, this either updates conf->raid_disks
and hits the same WARN_ON_ONCE_GFP() in r1buf_pool_alloc() when recovery
starts, triggers the kmalloc warning directly in raid1_reshape(), or overflows
raid_disks * 2 when computing new_r1bio_size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117235714.1101-1-chenaibing@cmss.chinamobile.com?part=1

  reply	other threads:[~2026-11-18  0:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 23:57 [PATCH] md/raid1: reject oversized raid_disks values Aibing Chen
2026-11-18  0:11 ` sashiko-bot [this message]
2026-11-18  8:14 ` [PATCH v2] " Aibing Chen

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-219833@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=chenaibing@cmss.chinamobile.com \
    --cc=linux-raid@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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