Linux RAID subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vasiliy Kovalev" <kovalev@altlinux.org>, linux-raid@vger.kernel.org
Cc: yukuai@fygo.io
Subject: Re: [PATCH] md/raid5: reject an invalid chunk size when continuing a reshape
Date: Fri, 20 Nov 2026 19:06:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-225891@kernel.org> (raw)
In-Reply-To: <20261120184801.614123-1-kovalev@altlinux.org>

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 <kovalev@altlinux.org>

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.

> diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
> index d52adf81065a7..e31ae5d6eece8 100644
> --- a/drivers/md/raid5.c
> +++ b/drivers/md/raid5.c
> @@ -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));

[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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120184801.614123-1-kovalev@altlinux.org?part=1

      reply	other threads:[~2026-11-20 19:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 18:48 [PATCH] md/raid5: reject an invalid chunk size when continuing a reshape Vasiliy Kovalev
2026-11-20 19:06 ` sashiko-bot [this message]

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=sashiko-outbox-225891@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kovalev@altlinux.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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