* [PATCH] md/raid1: reject oversized raid_disks values
@ 2026-11-17 23:57 Aibing Chen
2026-11-18 0:11 ` sashiko-bot
0 siblings, 1 reply; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-18 0:11 UTC | newest]
Thread overview: 2+ 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
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.