Linux RAID subsystem development
 help / color / mirror / Atom feed
* [PATCH] md/raid1: reject oversized raid_disks values
@ 2026-11-17 23:57 Aibing Chen
  2026-11-18  0:11 ` sashiko-bot
  2026-11-18  8:14 ` [PATCH v2] " Aibing Chen
  0 siblings, 2 replies; 3+ messages in thread
From: Aibing Chen @ 2026-11-17 23:57 UTC (permalink / raw)
  To: Song Liu, Yu Kuai; +Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel

r1buf_pool_alloc() allocates an array of struct resync_pages with
conf->raid_disks * 2 entries.  raid_disks is set from the user through
the SET_ARRAY_INFO ioctl and is unbounded, so a large value makes that
allocation exceed KMALLOC_MAX_SIZE: kmalloc emits a WARN_ON_ONCE_GFP()
splat (syzbot: WARNING in r1buf_pool_alloc) and returns NULL, after
which the resync fails to start.

The same disk count is used to size the mirrors array and the r1bio
pool during setup_conf(), so reject it in raid1_run() before any of
those allocations happen.

Reported-by: syzbot+c0e700c08ee88f60e54d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c0e700c08ee88f60e54d
Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
 drivers/md/raid1.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 894efb50c..875c98552 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;
+
 	/*
 	 * copy the already verified devices into our private RAID1
 	 * bookkeeping area. [whatever we allocate in run(),



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] md/raid1: reject oversized raid_disks values
  2026-11-17 23:57 [PATCH] md/raid1: reject oversized raid_disks values Aibing Chen
@ 2026-11-18  0:11 ` sashiko-bot
  2026-11-18  8:14 ` [PATCH v2] " Aibing Chen
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-18  0:11 UTC (permalink / raw)
  To: Aibing Chen; +Cc: linux-raid, yukuai

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] md/raid1: reject oversized raid_disks values
  2026-11-17 23:57 [PATCH] md/raid1: reject oversized raid_disks values Aibing Chen
  2026-11-18  0:11 ` sashiko-bot
@ 2026-11-18  8:14 ` Aibing Chen
  1 sibling, 0 replies; 3+ messages in thread
From: Aibing Chen @ 2026-11-18  8:14 UTC (permalink / raw)
  To: Song Liu, Yu Kuai; +Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel

setup_conf(), raid1_reshape() and r1buf_pool_alloc() size the mirrors
array, the r1bio pool and the resync buffer pool by raid_disks * 2.
raid_disks is set from the user through the SET_ARRAY_INFO ioctl and is
unbounded, so a large value makes one of those allocations exceed
KMALLOC_MAX_SIZE: kmalloc emits a WARN_ON_ONCE_GFP() splat (syzbot:
WARNING in r1buf_pool_alloc) and returns NULL.

Cap raid_disks by the largest element size involved, and check the cap
both in raid1_run(), before the pools are allocated, and in
raid1_reshape(), which updates raid_disks on a running array through
update_raid_disks().

Reported-by: syzbot+c0e700c08ee88f60e54d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c0e700c08ee88f60e54d
Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
v2:
- cap by max(sizeof(struct resync_pages), sizeof(struct raid1_info)) so
  the mirrors array is covered on 64K page kernels as well.
- add the same check to raid1_reshape(), which changes raid_disks on a
  running array without going through raid1_run().

 drivers/md/raid1.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 894efb50c..609c64d85 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -3152,6 +3152,18 @@ static int raid1_set_limits(struct mddev *mddev)
 	return queue_limits_set(mddev->gendisk->queue, &lim);
 }
 
+/*
+ * setup_conf(), raid1_reshape() and r1buf_pool_alloc() size the mirrors
+ * array, the r1bio pool and the resync buffer pool by raid_disks * 2.
+ * Reject a disk count for which those allocations would exceed
+ * KMALLOC_MAX_SIZE instead of letting kmalloc warn and fail later.
+ */
+static int raid1_max_disks(void)
+{
+	return KMALLOC_MAX_SIZE /
+		(2 * max(sizeof(struct resync_pages), sizeof(struct raid1_info)));
+}
+
 static int raid1_run(struct mddev *mddev)
 {
 	struct r1conf *conf;
@@ -3169,6 +3181,9 @@ static int raid1_run(struct mddev *mddev)
 		return -EIO;
 	}
 
+	if (mddev->raid_disks <= 0 || mddev->raid_disks > raid1_max_disks())
+		return -EINVAL;
+
 	/*
 	 * copy the already verified devices into our private RAID1
 	 * bookkeeping area. [whatever we allocate in run(),
@@ -3318,6 +3333,9 @@ static int raid1_reshape(struct mddev *mddev)
 
 	raid_disks = mddev->raid_disks + mddev->delta_disks;
 
+	if (raid_disks <= 0 || raid_disks > raid1_max_disks())
+		return -EINVAL;
+
 	if (raid_disks < conf->raid_disks) {
 		cnt=0;
 		for (d= 0; d < conf->raid_disks; d++)



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18  8:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 23:57 [PATCH] md/raid1: reject oversized raid_disks values Aibing Chen
2026-11-18  0:11 ` sashiko-bot
2026-11-18  8:14 ` [PATCH v2] " Aibing Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox