Skip to contentSkip to conversation

md/raid5: reject an invalid chunk size when continuing a reshape

PATCH Superseded Vasiliy Kovalev 2 participants 2 messages Nov 20 MD RAID

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md/raid5: reject an invalid chunk size when continuing a reshape

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 chunk_sectors is
not: the md/chunk_size sysfs attribute and a v1.x superblock can set it 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.

Reject a chunk size that is not positive or 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")
---
 drivers/md/raid5.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/md/raid5.c +7 -0
index d52adf81065a..e31ae5d6eece 100644
@@ -8113,6 +8113,13 @@ 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 (chunk_sectors <= 0 ||
+		    (u64)chunk_sectors * new_data_disks > INT_MAX ||
+		    (u64)chunk_sectors * (old_disks - max_degraded) > INT_MAX) {
+			pr_warn("md/raid:%s: invalid chunk size 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));
Signature · 1 line
2.50.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] md/raid5: unvalidated mddev->chunk_sectors in raid5_run() masked by max() It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] md/raid5: unvalidated mddev->chunk_sectors in raid5_run() masked by max()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] md/raid5: sysfs leak in raid5_run() leading to type confusion [1]
--

commit 9d4cf0a64af3fa8dfc560d2c2354bd784b6f5097
Author: Vasiliy Kovalev <[email protected]>

md/raid5: reject an invalid chunk size when continuing a reshape

This commit validates chunk_sectors and its products with the old and new data disk counts in raid5_run() when continuing an in-progress reshape.

Quoted 8 lines
drivers/md/raid5.c +0 -0
index d52adf81065a7..e31ae5d6eece8 100644
@@ -8113,6 +8113,13 @@ 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;

drivers/md/raid5.c static int raid5_run(struct mddev *mddev) · line 8125

Quoted 10 lines
drivers/md/raid5.c +7 -0
+		if (chunk_sectors <= 0 ||
+		    (u64)chunk_sectors * new_data_disks > INT_MAX ||
+		    (u64)chunk_sectors * (old_disks - max_degraded) > INT_MAX) {
+			pr_warn("md/raid:%s: invalid chunk size 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));

[Severity: Medium]
Can an invalid mddev->chunk_sectors slip past this check when mddev->new_chunk_sectors is positive and valid?

Because chunk_sectors is max(mddev->chunk_sectors, mddev->new_chunk_sectors), a valid mddev->new_chunk_sectors masks a zero, negative, or non-power-of-two mddev->chunk_sectors (which chunk_size_store() leaves untouched once reshape_position != MaxSector, and which v1.x/v0.91 superblocks load from a separate on-disk field). setup_conf() only validates mddev->new_chunk_sectors before copying mddev->chunk_sectors into conf->prev_chunk_sectors:

drivers/md/raid5.c:setup_conf() {
    ...
	if (!mddev->new_chunk_sectors ||
	    (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
	    !is_power_of_2(mddev->new_chunk_sectors)) {
		pr_warn("md/raid:%s: invalid chunk size %d\n",
			mdname(mddev), mddev->new_chunk_sectors << 9);
		return ERR_PTR(-EINVAL);
	}
    ...
	if (conf->reshape_progress != MaxSector) {
		conf->prev_chunk_sectors = mddev->chunk_sectors;
		conf->prev_algo = mddev->layout;
	}
    ...
}

An unvalidated conf->prev_chunk_sectors (for example -2^30 with 4 old data disks) can then overflow sectors_per_chunk to 0 and trigger a divide error in raid5_bitmap_sector_map(), or violate power-of-two chunk alignment assumptions in raid5_set_limits(), in_chunk_boundary(), and raid5_size(). Should mddev->chunk_sectors be validated with the same rules as mddev->new_chunk_sectors?

[Severity: Critical]
This problem wasn't introduced by this patch, but if a later setup step in raid5_run() jumps to abort [1], does it leak raid5_attrs_group on mddev->kobj?

drivers/md/raid5.c:raid5_run() {
    ...
	if (mddev->to_remove == &raid5_attrs_group)
		mddev->to_remove = NULL;
	else if (mddev->kobj.sd &&
	    sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
		pr_warn("raid5: failed to create sysfs attributes for %s\n",
			mdname(mddev));
    ...
	ret = log_init(conf, journal_dev, raid5_has_ppl(conf));
	if (ret)
		goto abort;

	return 0;
abort:
	md_unregister_thread(mddev, &mddev->thread);
	print_raid5_conf(conf);
	free_conf(conf);
	mddev->private = NULL;
	pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
	return ret;
}

When raid5_set_limits(), raid5_create_ctx_pool(), or log_init() fails, abort frees conf and clears mddev->private without removing raid5_attrs_group or setting mddev->to_remove. If the array is then started with another personality such as RAID1, accessing the leftover RAID5 sysfs attributes (for example raid5_show_group_thread_cnt()) casts the new personality's mddev->private to struct r5conf * and dereferences it, causing type confusion and out-of-bounds memory accesses.

[1] https://sashiko.dev/#/bug/linux-799de59e-8448-4ba5-a36c-4b6d949b8ba3

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1