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 v2] md: fix use-after-free of mddev in md_start_sync workqueue
Date: Wed, 25 Nov 2026 10:52:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-233172@kernel.org> (raw)
In-Reply-To: <tencent_72E3228BE54C78B91F4B5BB1A4C84B34090A@qq.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] md: stop_sync_thread() skips mddev_unlock() when locked is
  false
- [Medium] md: clearing MD_RECOVERY_RUNNING unlocked in md_start_sync()
  loses recovery
- [Medium] md: stop_sync_thread() sets MD_RECOVERY_INTR when idle,
  breaking dm-raid
--

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

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

This commit takes an mddev reference when queueing sync_work, flushes
sync_work unconditionally in stop_sync_thread(), moves the clearing of
MD_RECOVERY_RUNNING to the end of md_start_sync(), and checks
MD_RECOVERY_FROZEN in the read-only path of md_check_recovery().

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..29b8eb3082ff5 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5210,13 +5210,13 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
>  {
>  	int sync_seq = atomic_read(&mddev->sync_seq);
>  
> -	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
> -		if (!locked)
> -			mddev_unlock(mddev);
> -		return;
> -	}
> -
> -	mddev_unlock(mddev);
> +	/*
> +	 * Release reconfig_mutex before flushing sync_work or waiting on
> +	 * resync_wait. md_start_sync() needs reconfig_mutex via
> +	 * mddev_lock_nointr(), so holding it here would deadlock.
> +	 */
> +	if (locked)
> +		mddev_unlock(mddev);

[Severity: High]
Does gating mddev_unlock(mddev) on locked leave reconfig_mutex held when
locked is false?

All callers of stop_sync_thread() enter with reconfig_mutex held; the
locked parameter indicates whether reconfig_mutex should be re-acquired
before returning or left unlocked.  When md_set_readonly() calls
stop_sync_thread(mddev, false), reconfig_mutex is never released:

drivers/md/md.c:md_set_readonly() {
    ...
	stop_sync_thread(mddev, false);
	wait_event(mddev->sb_wait,
		   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
	mddev_lock_nointr(mddev);
    ...
}

Besides holding reconfig_mutex across flush_work(&mddev->sync_work) and
wait_event(resync_wait, ...), stop_sync_thread(mddev, false) returns with
reconfig_mutex still held and md_set_readonly() immediately deadlocks on
mddev_lock_nointr(mddev).  mddev_unlock(mddev) needs to be unconditional
here.

>  
>  	set_bit(MD_RECOVERY_INTR, &mddev->recovery);
>  	/*
> @@ -5224,8 +5224,18 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
>  	 * never happen
>  	 */
>  	md_wakeup_thread_directly(&mddev->sync_thread);
> -	if (work_pending(&mddev->sync_work))
> -		flush_work(&mddev->sync_work);
> +	/*
> +	 * Always flush sync_work so that even an in-flight invocation
> +	 * (already removed from the workqueue's pending list but not yet
> +	 * returned from md_start_sync()) is drained. Previously this was
> +	 * gated on work_pending() which is false for running work, and the
> +	 * whole path could be short-circuited by MD_RECOVERY_RUNNING being
> +	 * already clear — but md_start_sync() now only clears RUNNING after
> +	 * its full tail, and unconditionally flushing handles any remaining
> +	 * race where stop_sync_thread() was entered while sync_work was
> +	 * already in flight.
> +	 */
> +	flush_work(&mddev->sync_work);

[Severity: Medium]
Does removing the !MD_RECOVERY_RUNNING early return leave
MD_RECOVERY_INTR set on an idle array?

In dm-raid, rs_get_progress() checks MD_RECOVERY_INTR when the array is
idle to distinguish an in-sync array from an interrupted sync:

drivers/md/dm-raid.c:rs_get_progress() {
    ...
		if (state == st_idle && !test_bit(MD_RECOVERY_INTR, &recovery))
			r = mddev->resync_offset;
		else
			r = mddev->curr_resync_completed;
    ...
}

When an in-sync, idle dm-raid array is suspended via raid_postsuspend() ->
md_stop_writes() -> stop_sync_thread(), setting MD_RECOVERY_INTR with no
sync running leaves the bit set and causes dm-raid status to report
curr_resync_completed (0 if no sync ran since assembly) instead of
resync_offset.

[ ... ]

> @@ -10368,7 +10379,6 @@ static void md_start_sync(struct work_struct *ws)
>  	clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
>  	clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
>  	clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
> -	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
>  	mddev_unlock(mddev);
>  	/*
>  	 * md_start_sync was triggered by MD_RECOVERY_NEEDED, so we should
> @@ -10381,10 +10391,18 @@ static void md_start_sync(struct work_struct *ws)
>  		__mddev_resume(mddev, false);
>  	}
>  
> -	wake_up(&resync_wait);
>  	if (test_and_clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
>  	    mddev->sysfs_action)
>  		sysfs_notify_dirent_safe(mddev->sysfs_action);
> +	/*
> +	 * Drop RUNNING only after the full tail of md_start_sync()
> +	 * completes so that stop_sync_thread() can't observe !RUNNING and
> +	 * return while md_start_sync() is still mid-execution. The put
> +	 * follows so mddev remains alive while the flag is cleared.
> +	 */
> +	clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
> +	wake_up(&resync_wait);
> +	mddev_put(mddev);
>  }

[Severity: Medium]
Can clearing MD_RECOVERY_RUNNING after mddev_unlock() and
__mddev_resume() race with md_check_recovery() and drop
MD_RECOVERY_NEEDED?

Commit db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
cleared MD_RECOVERY_RUNNING before mddev_unlock() in the not_running path
so that it was cleared under reconfig_mutex.  With clear_bit() moved to
the end of md_start_sync(), both mddev_unlock() and __mddev_resume() wake
mddev->thread while MD_RECOVERY_RUNNING is still set:

kworker (md_start_sync)           mddev->thread (md_check_recovery)
not_running:
  mddev_unlock(mddev)
    mutex_unlock(&reconfig_mutex)
    md_wakeup_thread(mddev->thread)
                                  mddev_trylock(mddev)
                                  if (test_bit(MD_RECOVERY_RUNNING, ...))
                                    unregister_sync_thread(mddev)
                                      if (!test_bit(MD_RECOVERY_DONE, ...))
                                        clear_bit(MD_RECOVERY_NEEDED, ...)
                                  mddev_unlock(mddev)
  clear_bit(MD_RECOVERY_RUNNING, ...)

Because MD_RECOVERY_DONE is clear, unregister_sync_thread() assumes a
sync thread is running and clears MD_RECOVERY_NEEDED, expecting
md_reap_sync_thread() to set it again when that thread finishes.  Since
md_start_sync() took the not_running path and never started a sync
thread, the pending recovery is lost.  Since stop_sync_thread() now
calls flush_work(&mddev->sync_work) before waiting on
MD_RECOVERY_RUNNING, MD_RECOVERY_RUNNING can stay cleared before
mddev_unlock().

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

      reply	other threads:[~2026-11-25 10:52 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
2026-11-25 10:34 ` [PATCH v2] " Shuai Liu
2026-11-25 10:52   ` 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-233172@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