[PATCH] md/raid1: reject oversized raid_disks values

From: Aibing Chen

Date: Tue Nov 17 2026 - 18:57:32 EST


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@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=c0e700c08ee88f60e54d
Signed-off-by: Aibing Chen <chenaibing@xxxxxxxxxxxxxxxxxxxx>
---
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(),