Linux RAID subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shuai Liu" <847807867@qq.com>
Cc: linux-raid@vger.kernel.org, yukuai@fygo.io
Subject: Re: [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue
Date: Wed, 25 Nov 2026 10:28:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-233122@kernel.org> (raw)
In-Reply-To: <tencent_FD9B3C56D33F6E8298C044857BB7342DCB06@qq.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] md: stop_sync_thread() still races with in-flight
  md_start_sync()
--

commit 956e002b3dd32e5ec98ffbeb74b91b8f0d27187f
Author: Shuai Liu <847807867@qq.com>

md: fix use-after-free of mddev in md_start_sync workqueue

This commit increments mddev->active before queueing mddev->sync_work in
md_check_recovery() and drops the reference via mddev_put() when
md_start_sync() finishes.

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..4b9d7ba63efa6 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -10361,6 +10361,7 @@ static void md_start_sync(struct work_struct *ws)
>  	md_wakeup_thread(mddev->sync_thread);
>  	sysfs_notify_dirent_safe(mddev->sysfs_action);
>  	md_new_event();
> +	mddev_put(mddev);
>  	return;
>  
>  not_running:
> @@ -10385,6 +10386,7 @@ static void md_start_sync(struct work_struct *ws)
>  	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
>  	    mddev->sysfs_action)
>  		sysfs_notify_dirent_safe(mddev->sysfs_action);
> +	mddev_put(mddev);
>  }

[Severity: High]
Can stop_sync_thread() still return while md_start_sync() is executing its
post-unlock tail?

drivers/md/md.c:stop_sync_thread() {
	...
	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
		if (!locked)
			mddev_unlock(mddev);
		return;
	}

	mddev_unlock(mddev);

	set_bit(MD_RECOVERY_INTR, &mddev->recovery);\
	...
	if (work_pending(&mddev->sync_work))
		flush_work(&mddev->sync_work);
	...
}

Because the workqueue core clears WORK_STRUCT_PENDING_BIT before invoking
md_start_sync(), work_pending(&mddev->sync_work) is false once the work
callback is already running.  When md_start_sync() takes the not_running
path, it clears MD_RECOVERY_RUNNING before calling mddev_unlock(mddev), and
stop_sync_thread() also returns early without flushing sync_work whenever
MD_RECOVERY_RUNNING is already clear:

CPU0                                  CPU1 (md_misc_wq)
                                      md_start_sync()
                                        mddev_lock_nointr(mddev)
stop_sync_thread()
  mddev_unlock(mddev)
  set_bit(MD_RECOVERY_INTR, ...)
  work_pending(&mddev->sync_work)
    -> false
  wait_event(resync_wait,
    !test_bit(MD_RECOVERY_RUNNING))
                                        md_choose_sync_action() -> false
                                        goto not_running
                                        clear_bit(MD_RECOVERY_RUNNING, ...)
                                        mddev_unlock(mddev)
                                          mutex_unlock(&reconfig_mutex)
  mddev_lock_nointr(mddev)
  /* returns to caller */
  raid_dtr() / do_md_stop()
    raid_set_free(rs)
                                        wake_up(&resync_wait)
                                        mddev_put(mddev)  <-- use-after-free

In dm-raid, struct mddev is embedded in struct raid_set, and raid_dtr()
frees the raid_set via raid_set_free() without waiting on mddev->active, so
the in-flight md_start_sync() and the added mddev_put() access freed memory.
Even on standard md arrays where mddev->active delays freeing struct mddev
itself, do_md_stop() proceeds while md_start_sync() is still running, so
md_start_sync() can race with do_md_stop() tearing down mddev->sysfs_action
or setting MD_DELETED before md_start_sync() finishes mddev_unlock() and
__mddev_resume().  In addition, on a read-only array, md_check_recovery()
queues sync_work when md_spares_need_change() is true without checking
MD_RECOVERY_FROZEN, allowing a new sync_work to be queued while
stop_sync_thread() drops reconfig_mutex.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_FD9B3C56D33F6E8298C044857BB7342DCB06@qq.com?part=1

  reply	other threads:[~2026-11-25 10:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  1:28 [syzbot] [raid?] KASAN: slab-use-after-free Read in mddev_unlock syzbot
2026-11-25 10:12 ` [PATCH] md: fix use-after-free of mddev in md_start_sync workqueue Shuai Liu
2026-11-25 10:28   ` sashiko-bot [this message]
2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
2026-11-25 10:52   ` sashiko-bot

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-233122@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=847807867@qq.com \
    --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