Linux RAID subsystem development
 help / color / mirror / Atom feed
From: Vasiliy Kovalev <kovalev@altlinux.org>
To: Song Liu <song@kernel.org>, Yu Kuai <yukuai@fygo.io>,
	Li Nan <magiclinan@didiglobal.com>, Xiao Ni <xiao@kernel.org>,
	linux-raid@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org,
	kovalev@altlinux.org
Subject: [PATCH v2] md/raid5: reject an invalid chunk size when continuing a reshape
Date: Sat, 21 Nov 2026 00:08:33 +0300	[thread overview]
Message-ID: <20261120210833.625156-1-kovalev@altlinux.org> (raw)

raid5_run() handles an array that claims an in-progress reshape
(reshape_position != MaxSector) before any later chunk-size validation.
It computes

	chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
	new_data_disks = mddev->raid_disks - max_degraded;
	if (sector_div(here_new, chunk_sectors * new_data_disks)) {
	...
	sector_div(here_old, chunk_sectors * (old_disks-max_degraded));

The disk counts are checked earlier in this block, but the chunk sizes
are not: the md/chunk_size sysfs attribute and a v1.x superblock can set
them to any int value. The divisor is then zero for a zero chunk size,
and for any product that is a multiple of 2^32, since sector_div()
truncates the divisor to 32 bits, e.g. 2^30 sectors with four data
disks:

md/raid:md0: not clean -- starting background reconstruction
Oops: divide error: 0000 [#1] SMP KASAN NOPTI
CPU: 3 UID: 0 PID: 1414 Comm: syz.1.18 Not tainted 7.4.0-rc1-next-20261104-alt-syz-box-drivers_md_bcache #1 PREEMPT(lazy)
RIP: 0010:raid5_run+0x5a8/0x27b0 drivers/md/raid5.c:8116
Call Trace:
 <TASK>
 md_run+0xea5/0x2a00 drivers/md/md.c:6808
 do_md_run+0x32/0x660 drivers/md/md.c:6909
 md_ioctl+0x2ab2/0x4040 drivers/md/md.c:8541
 blkdev_ioctl+0x5a9/0x6e0 block/ioctl.c:798
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl fs/ioctl.c:583 [inline]
 __x64_sys_ioctl+0x18a/0x210 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x112/0x630 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x00007f562441a40b
 </TASK>

Writing 8 (bytes) to md/chunk_size, a non-MaxSector reshape_position and
then RUN_ARRAY on a raid5 array is enough to trigger it.

check_reshape() and raid5_start_reshape() already reject a zero chunk
size, but an interrupted reshape is resumed by raid5_run() itself and
goes through neither of them.

Require mddev->chunk_sectors to be a power of two of at least a page,
as setup_conf() does for new_chunk_sectors: max() lets a valid
new_chunk_sectors hide it, and setup_conf() copies it to
prev_chunk_sectors unchecked. Also reject a chunk size that makes either
product overflow int.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: f67055780caa ("[PATCH] md: Checkpoint and allow restart of raid5 reshape")
Cc: stable@vger.kernel.org
Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
---
v2: also validate mddev->chunk_sectors, which max() could hide behind a
    valid new_chunk_sectors (Sashiko review).

v1: https://lore.kernel.org/all/20261120184801.614123-1-kovalev@altlinux.org

 drivers/md/raid5.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
index d52adf81065a..b62657b410d4 100644
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -8103,6 +8103,12 @@ static int raid5_run(struct mddev *mddev)
 				min_disks);
 			return -EINVAL;
 		}
+		if (!is_power_of_2(mddev->chunk_sectors) ||
+		    mddev->chunk_sectors < PAGE_SECTORS) {
+			pr_warn("md/raid:%s: invalid chunk size for reshape - aborting.\n",
+				mdname(mddev));
+			return -EINVAL;
+		}
 		/* reshape_position must be on a new-stripe boundary, and one
 		 * further up in new geometry must map after here in old
 		 * geometry.
@@ -8113,6 +8119,12 @@ static int raid5_run(struct mddev *mddev)
 		here_new = mddev->reshape_position;
 		chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
 		new_data_disks = mddev->raid_disks - max_degraded;
+		if ((u64)chunk_sectors * new_data_disks > INT_MAX ||
+		    (u64)chunk_sectors * (old_disks - max_degraded) > INT_MAX) {
+			pr_warn("md/raid:%s: chunk size too large for reshape - aborting.\n",
+				mdname(mddev));
+			return -EINVAL;
+		}
 		if (sector_div(here_new, chunk_sectors * new_data_disks)) {
 			pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
 				mdname(mddev));
-- 
2.50.1


                 reply	other threads:[~2026-11-20 21:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261120210833.625156-1-kovalev@altlinux.org \
    --to=kovalev@altlinux.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=magiclinan@didiglobal.com \
    --cc=song@kernel.org \
    --cc=xiao@kernel.org \
    --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