Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: 徐晗翔 <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] Re: ALSA: usb-audio: us144mkii: queued stop_pcm_work calls snd_pcm_stop() after PCM close and dereferences NULL runtime
Date: Wed, 25 Nov 2026 13:03:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-233433@kernel.org> (raw)
In-Reply-To: <753f2e86.57a.1a212c1052c.Coremail.xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] ALSA: usb-audio: us144mkii: missing Signed-off-by trailer in
  commit
- [High] ALSA: usb-audio: us144mkii: feedback_urb_complete() re-arms
  work after close
- [Medium] ALSA: usb-audio: us144mkii: unlocked TOCTOU substream read in
  stop_pcm_work
- [Medium] ALSA: usb-audio: us144mkii: cancel_work_sync() in close drops
  sibling XRUN
- [Low] ALSA: usb-audio: us144mkii: missing Fixes trailer in commit
  message
--

commit b36c55bb30946f2fb9b8c0b478d61532334be47c
Author: 徐晗翔 <xuhx@hust.edu.cn>

> ALSA: usb-audio: us144mkii: queued stop_pcm_work calls snd_pcm_stop() after PCM close and dereferences NULL runtime
>
> A stop_pcm_work queued by feedback_urb_complete() after a feedback
> sync loss read tascam->playback_substream/capture_substream without
> any lock and called snd_pcm_stop() on them; PCM close did not
> cancel the work, so after snd_pcm_detach_substream() cleared
> substream->runtime the handler's snd_pcm_stop() oopsed in
> snd_pcm_pre_stop() on the NULL runtime.
>
> Cancel the work in both .close() callbacks (cancel_work_sync() is
> the lifecycle guarantee; it waits for a running handler), and make
> the handler use snd_pcm_stop_xrun(), which takes the PCM stream
> lock itself and rechecks substream->runtime -- fixing both the
> missing close-cancel and the ALSA convention that snd_pcm_action()
> must run under the stream lock.  This patch touches the same .close()
> functions as the separate PCM-close UAF report; the two fixes are
> complementary, not interchangeable.

[Severity: High]
This commit message is missing a Signed-off-by: trailer.

[Severity: Low]
Since this fixes a bug introduced when playback and feedback handling were
added, should it include a Fixes: tag?

  Fixes: a2a2210f2c2e ("ALSA: usb-audio: us144mkii: Implement audio playback and feedback")

> diff --git a/sound/usb/usx2y/us144mkii_capture.c b/sound/usb/usx2y/us144mkii_capture.c
> index d7cdf9e0f6851..1f6d87a9421a5 100644
> --- a/sound/usb/usx2y/us144mkii_capture.c
> +++ b/sound/usb/usx2y/us144mkii_capture.c
> @@ -37,6 +37,9 @@ static int tascam_capture_close(struct snd_pcm_substream *substream)
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
>  
>  	tascam->capture_substream = NULL;
> +	/* a stop_pcm_work queued on feedback sync loss must not run after
> +	 * ALSA detached the substream and freed its runtime */
> +	cancel_work_sync(&tascam->stop_pcm_work);
>  
>  	return 0;
>  }

[Severity: Medium]
Can cancelling tascam->stop_pcm_work here drop a pending feedback-sync-loss
XRUN stop for a playback substream that is still running?

tascam->stop_pcm_work is shared by both playback and capture.  When
feedback_urb_complete() queues tascam->stop_pcm_work, it immediately sets
tascam->feedback_synced = false:

sound/usb/usx2y/us144mkii_playback.c:feedback_urb_complete() {
	...
				if (tascam->feedback_synced) {
					tascam->feedback_consecutive_errors++;
					if (tascam->feedback_consecutive_errors >
					    FEEDBACK_SYNC_LOSS_THRESHOLD) {
						...
						schedule_work(
								&tascam->stop_pcm_work);
						tascam->feedback_synced = false;
						break;
					}
				}
	...
}

If capture is opened and closed (for example by an application probing the
device) while playback is running and losing feedback sync,
tascam_capture_close() cancels the pending work item and
feedback_urb_complete() will never re-queue it because feedback_synced is
already false, leaving playback running unsynchronized instead of entering
SNDRV_PCM_STATE_XRUN.

> diff --git a/sound/usb/usx2y/us144mkii_playback.c b/sound/usb/usx2y/us144mkii_playback.c
> index 7efaca0a64899..1bdc68006baf1 100644
> --- a/sound/usb/usx2y/us144mkii_playback.c
> +++ b/sound/usb/usx2y/us144mkii_playback.c
> @@ -37,6 +37,9 @@ static int tascam_playback_close(struct snd_pcm_substream *substream)
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
>  
>  	tascam->playback_substream = NULL;
> +	/* a stop_pcm_work queued on feedback sync loss must not run after
> +	 * ALSA detached the substream and freed its runtime */
> +	cancel_work_sync(&tascam->stop_pcm_work);
>  
>  	return 0;
>  }

[Severity: High]
Can an in-flight feedback_urb_complete() schedule tascam->stop_pcm_work
again after cancel_work_sync() returns?

During close, tascam_pcm_trigger() with SNDRV_PCM_TRIGGER_STOP only queues
tascam->stop_work asynchronously to kill the anchored URBs, and neither
tascam_playback_close() nor tascam_capture_close() flushes tascam->stop_work
or kills tascam->feedback_anchor before calling
cancel_work_sync(&tascam->stop_pcm_work):

CPU0 (close)                      CPU1 (<softirq>)
tascam_pcm_trigger(STOP)
  playback_active = 0
  schedule_work(&stop_work)
                                  feedback_urb_complete()
                                    /* entered before URBs killed */
tascam_playback_close()
  cancel_work_sync(&stop_pcm_work)
                                    schedule_work(&stop_pcm_work)  <-- re-armed

In addition, snd_pcm_stop_xrun() does not serialize against
snd_pcm_detach_substream(), because snd_pcm_detach_substream() frees
runtime->status, clears substream->runtime, and frees runtime without
taking pcm_stream_lock.

> @@ -449,8 +452,13 @@ void tascam_stop_pcm_work_handler(struct work_struct *work)
>  	struct tascam_card *tascam =
>  		container_of(work, struct tascam_card, stop_pcm_work);
>  
> +	/* snd_pcm_stop() requires the PCM stream lock and dereferences
> +	 * substream->runtime unconditionally; after a concurrent close the
> +	 * runtime is NULL and snd_pcm_pre_stop() oopses.  Use
> +	 * snd_pcm_stop_xrun(), which takes the stream lock itself and
> +	 * rechecks substream->runtime before touching it. */
>  	if (tascam->playback_substream)
> -		snd_pcm_stop(tascam->playback_substream, SNDRV_PCM_STATE_XRUN);
> +		snd_pcm_stop_xrun(tascam->playback_substream);
>  	if (tascam->capture_substream)
> -		snd_pcm_stop(tascam->capture_substream, SNDRV_PCM_STATE_XRUN);
> +		snd_pcm_stop_xrun(tascam->capture_substream);
>  }

[Severity: Medium]
Can tascam_stop_pcm_work_handler() pass a NULL substream pointer to
snd_pcm_stop_xrun() when racing with tascam_playback_close() or
tascam_capture_close()?

The close callbacks clear tascam->playback_substream and
tascam->capture_substream without a lock before calling
cancel_work_sync(&tascam->stop_pcm_work), while
tascam_stop_pcm_work_handler() reads each field unlocked in the condition
and can reload it when passing the argument to snd_pcm_stop_xrun():

CPU0 (kworker)                    CPU1 (close)
tascam_stop_pcm_work_handler()
  if (tascam->playback_substream)
                                  tascam_playback_close()
                                    tascam->playback_substream = NULL;
                                    cancel_work_sync(&tascam->stop_pcm_work)
    snd_pcm_stop_xrun(tascam->playback_substream)
      guard(pcm_stream_lock_irqsave)(NULL)  <-- NULL pointer dereference

snd_pcm_stop_xrun() immediately dereferences substream when acquiring
pcm_stream_lock_irqsave.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/753f2e86.57a.1a212c1052c.Coremail.xuhx@hust.edu.cn?part=1

  reply	other threads:[~2026-11-25 13:03 UTC|newest]

Thread overview: 40+ 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-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-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-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-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-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-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 [this message]
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-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-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-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-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-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] " 徐晗翔

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-233433@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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