Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: 徐晗翔 <xuhx@hust.edu.cn>
Cc: "Krzysztof Kozlowski" <krzk@kernel.org>,
	perex@perex.cz, tiwai@suse.com, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Re: Re: sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF)
Date: Wed, 25 Nov 2026 15:37:13 +0100	[thread overview]
Message-ID: <87ecc96kpy.wl-tiwai@suse.de> (raw)
In-Reply-To: <5a5b96e3.569.1a212bd09ac.Coremail.xuhx@hust.edu.cn>

On Wed, 25 Nov 2026 13:45:08 +0100,
徐晗翔 wrote:
> 
> close_substream() cancelled the input event work before clearing
> substream->opened, but snd_rawmidi_receive() serializes only against
> the opened store: a driver that delivers input in that window
> re-queued event_work after the cancellation, and the work then ran
> on the runtime freed by snd_rawmidi_runtime_free() (KASAN UAF in
> snd_rawmidi_input_event_work()).
> 
> Cancel the work a second time after opened is cleared and before
> the runtime is freed.  Also restore the per-substream disconnected
> flag: snd_rawmidi_dev_disconnect() marks input substreams
> disconnected under substream->lock and drains the event work, so a
> driver that keeps calling snd_rawmidi_receive() after the
> disconnect cannot re-queue the work either (snd_rawmidi_receive()
> and snd_rawmidi_input_trigger() check the flag).

This should have been already addressed in the recent upstream code.
Please verify whether you still see the issue.


thanks,

Takashi

> ---
>  sound/core/rawmidi.c    | 23 +++++++++++++++++++++--
>  include/sound/rawmidi.h |  1 +
>  2 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
> --- a/sound/core/rawmidi.c
> +++ b/sound/core/rawmidi.c
> @@ -215,7 +215,7 @@
>  
>  static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
>  {
> -	if (!substream->opened)
> +	if (!substream->opened || substream->disconnected)
>  		return;
>  	substream->ops->trigger(substream, up);
>  	if (!up)
> @@ -557,6 +557,14 @@
>  		substream->opened = 0;
>  		substream->append = 0;
>  	}
> +	/* Drain the input event work that a racing snd_rawmidi_receive()
> +	 * may have queued after the earlier cancellation in
> +	 * snd_rawmidi_input_trigger(): receive() serializes only against
> +	 * the opened=0 store above, not against the earlier cancel, so the
> +	 * work can be pending again here.  It must not outlive the runtime
> +	 * free below. */
> +	if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
> +		cancel_work_sync(&substream->runtime->event_work);
>  	substream->ops->close(substream);
>  	if (substream->runtime->private_free)
>  		substream->runtime->private_free(substream);
> @@ -1151,7 +1159,7 @@
>  	struct snd_rawmidi_runtime *runtime;
>  
>  	guard(spinlock_irqsave)(&substream->lock);
> -	if (!substream->opened)
> +	if (!substream->opened || substream->disconnected)
>  		return -EBADFD;
>  	runtime = substream->runtime;
>  	if (!runtime || !runtime->buffer) {
> @@ -2065,6 +2073,17 @@
>  		list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
>  			if (s->runtime)
>  				wake_up(&s->runtime->sleep);
> +			/* mark input substreams disconnected and drain the
> +			 * event work: a driver that keeps calling
> +			 * snd_rawmidi_receive() after the disconnect must
> +			 * not re-queue the work on a runtime that the close
> +			 * path is about to free */
> +			if (s->stream == SNDRV_RAWMIDI_STREAM_INPUT) {
> +				scoped_guard(spinlock_irqsave, &s->lock)
> +					s->disconnected = true;
> +				if (s->runtime)
> +					cancel_work_sync(&s->runtime->event_work);
> +			}
>  		}
>  	}
>  
> diff --git a/include/sound/rawmidi.h b/include/sound/rawmidi.h
> --- a/include/sound/rawmidi.h
> +++ b/include/sound/rawmidi.h
> @@ -84,6 +84,7 @@
>  	int stream;			/* direction */
>  	int number;			/* substream number */
>  	bool opened;			/* open flag */
> +	bool disconnected;		/* device disconnected flag */
>  	bool append;			/* append flag (merge more streams) */
>  	bool active_sensing;		/* send active sensing when close */
>  	unsigned int framing;		/* whether to frame input data */
> -- 
> 
> 
> > -----原始邮件-----
> > 发件人: "Krzysztof Kozlowski" <krzk@kernel.org>
> > 发送时间:2026-11-24 22:52:58 (星期二)
> > 收件人: 徐晗翔 <xuhx@hust.edu.cn>, perex@perex.cz, tiwai@suse.com
> > 抄送: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
> > 主题: Re: sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF)
> > 
> > On 22/11/2026 08:50, 徐晗翔 wrote:
> > > 
> > >   Please keep me in Cc; I am not subscribed to the lists.
> > 
> > 
> > Why? You never responded to any other query we asked for the last month!
> > 
> > 
> > Best regards,
> > Krzysztof

  reply	other threads:[~2026-11-25 14:37 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4c7f9772.192e1.1a17d5c2ed5.Coremail.xuhx@hust.edu.cn>
     [not found] ` <2026102727-data-sequel-ce41@gregkh>
2026-11-13 11:58   ` ALSA: ctxfi: free atimer only after the timer IRQ is quiesced 徐晗翔
2026-11-13 16:30     ` Takashi Iwai
2026-11-25 12:51       ` [PATCH] Re: " 徐晗翔
2026-11-25 13:00         ` sashiko-bot
2026-11-25 14:33         ` Takashi Iwai
2026-11-13 12:03   ` ALSA: ctxfi: do not free a running system timer when timer_delete_sync_try() returns -1 (UAF) 徐晗翔
2026-11-25 12:51     ` [PATCH] " 徐晗翔
2026-11-25 13:05       ` sashiko-bot
2026-11-25 14:35       ` Takashi Iwai
2026-11-26  2:13         ` 徐晗翔
2026-11-26  2:27           ` sashiko-bot
2026-11-26  7:54           ` Takashi Iwai
2026-11-13 12:04   ` ALSA: asihpi: free IRQ before snd_card_free()/HPI delete on HPI6205 removal (UAF) 徐晗翔
2026-11-25 12:50     ` [PATCH] " 徐晗翔
2026-11-25 13:03       ` sashiko-bot
2026-11-25 14:43       ` Takashi Iwai
2026-11-13 12:06   ` ALSA: asihpi: quiesce PCM timer and IRQ before freeing dpcm on close (UAF) 徐晗翔
2026-11-25 12:50     ` [PATCH] " 徐晗翔
2026-11-25 13:06       ` sashiko-bot
2026-11-25 14:46       ` Takashi Iwai
2026-11-13 12:15   ` ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF) 徐晗翔
2026-11-25 12:55     ` [PATCH] " 徐晗翔
2026-11-25 13:12       ` sashiko-bot
2026-11-25 13:27       ` Takashi Iwai
2026-11-13 12:28   ` ALSA: usb-audio: us144mkii: PCM close frees runtime while playback/feedback URB completion and capture work still use it (UAF) 徐晗翔
2026-11-25 12:48     ` [PATCH] " 徐晗翔
2026-11-25 13:13       ` sashiko-bot
2026-11-25 16:25       ` Takashi Iwai
2026-11-13 12:29   ` ALSA: usb-audio: us144mkii: queued stop_pcm_work calls snd_pcm_stop() after PCM close and dereferences NULL runtime 徐晗翔
2026-11-25 12:49     ` [PATCH] " 徐晗翔
2026-11-25 13:03       ` sashiko-bot
2026-11-22  7:43   ` sound: core: compress offload fd does not pin card or module, so card removal leaves a live fd pointing at freed snd_compr (KASAN UAF) 徐晗翔
2026-11-22  8:07     ` Takashi Iwai
2026-11-25 12:44       ` [PATCH] Re: " 徐晗翔
2026-11-25 14:21         ` Takashi Iwai
2026-11-22  7:48   ` sound: core: jack kcontrol is published before jack_kctl allocation, and the failure path frees it without removing it from card->controls (null-ptr-deref) 徐晗翔
2026-11-24  9:39     ` Sreeraj S Kurup
2026-11-25 12:43       ` 徐晗翔
2026-11-22  7:50   ` sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF) 徐晗翔
2026-11-24 14:52     ` Krzysztof Kozlowski
2026-11-25 12:45       ` [PATCH] Re: " 徐晗翔
2026-11-25 14:37         ` Takashi Iwai [this message]
2026-11-22  7:52   ` sound: core: hwdep DSP_LOAD check-then-act is unlocked, allowing concurrent DSP_LOAD callbacks (UAF) 徐晗翔
2026-11-25 12:46     ` [PATCH] " 徐晗翔
2026-11-25 14:14       ` Takashi Iwai
2026-11-22  7:55   ` sound: core: get_available_index() calls snd_ctl_find_id() without controls_rwsem (UAF risk) 徐晗翔
2026-11-25 12:46     ` [PATCH] " 徐晗翔
2026-11-25 12:51       ` sashiko-bot
2026-11-25 14:18       ` Takashi Iwai
2026-11-22  7:56   ` sound: core: OSS mixer notify callback is invoked without module pin or mutex, racing with rmmod 徐晗翔
2026-11-25 12:47     ` [PATCH] " 徐晗翔
2026-11-25 14:16       ` Takashi Iwai
2026-11-22  7:57   ` sound: timer: snd_utimer_fops lacks .owner, so an open user timer fd does not pin snd-timer 徐晗翔
2026-11-25 12:48     ` [PATCH] " 徐晗翔
2026-11-25 14:17       ` Takashi Iwai

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=87ecc96kpy.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=krzk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    --cc=xuhx@hust.edu.cn \
    /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