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: PCM close frees runtime while  playback/feedback URB completion and capture work still use it (UAF)
Date: Wed, 25 Nov 2026 13:13:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-233470@kernel.org> (raw)
In-Reply-To: <7b5aed28.578.1a212c08a97.Coremail.xuhx@hust.edu.cn>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] ALSA: usb-audio: us144mkii: missing Signed-off-by trailer in
  commit
- [High] ALSA: usb-audio: us144mkii: cancelling stop_work wedges
  active_urbs
- [High] ALSA: usb-audio: us144mkii: killing feedback URBs on close
  stalls stream
- [High] ALSA: usb-audio: us144mkii: DMA buffer freed in do_hw_free()
  before close()
- [High] ALSA: usb-audio: us144mkii: unlocked resubmit in
  capture_urb_complete()
- [Medium] ALSA: usb-audio: us144mkii: stop_work cancelled before
  stop_pcm_work
- [Low] ALSA: usb-audio: us144mkii: missing Fixes trailer on UAF bug fix
- [Low] ALSA: usb-audio: us144mkii: missing kernel-doc for new
  tascam_card fields
--

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

> ALSA: usb-audio: us144mkii: PCM close frees runtime while playback/feedback URB completion and capture work still use it (UAF)
>
> tascam_playback_close() and tascam_capture_close() only cleared the
> substream pointer; the ALSA release path then detached the substream
> and freed the snd_pcm_runtime while a playback/feedback URB
> completion or the capture work still used it.
>
> Make close() a real synchronous barrier:
>
>  - clear the active flag and the substream pointer under
>    tascam->lock, and load them under the same lock in the
>    completions, so a completion cannot observe a stale substream of
>    a stream being quiesced;
>  - usb_kill_urb() every playback/feedback URB on playback close and
>    every capture/feedback URB on capture close (the feedback
>    completion consumes BOTH runtimes); usb_kill_urb() waits for each
>    in-flight completion, including one already inside the giveback
>    path;
>  - serialize the completions' final usb_submit_urb() with the close
>    under tascam->lock, so a completion that raced the close cannot
>    resubmit a killed URB;
>  - cancel_work_sync() for capture_work/stop_work/stop_pcm_work;
>  - serialize the whole quiesce with tascam_disconnect()'s
>    tascam_free_urbs() through a new cleanup_lock plus a
>    tascam->disconnected flag, released before snd_card_free() waits
>    for the open PCM files.

[Severity: High]
The commit message is missing a Signed-off-by: trailer, which is required
under the Developer's Certificate of Origin.

[Severity: Low]
Since this fixes a use-after-free in the playback/feedback and capture
paths, should it include Fixes: trailers for those commits?

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

> diff --git a/sound/usb/usx2y/us144mkii.c b/sound/usb/usx2y/us144mkii.c
> index 58ef23146f209..d2f2cd8bb3305 100644
> --- a/sound/usb/usx2y/us144mkii.c
> +++ b/sound/usb/usx2y/us144mkii.c
[ ... ]
> @@ -605,6 +617,10 @@ static void tascam_disconnect(struct usb_interface *intf)
>  		cancel_work_sync(&tascam->stop_pcm_work);
>  		timer_delete_sync(&tascam->error_timer);
>  		tascam_free_urbs(tascam);
> +		/* release before snd_card_free(): it waits for the open PCM
> +		 * files, and a close running during that wait takes this
> +		 * lock (and sees tascam->disconnected) */
> +		mutex_unlock(&tascam->cleanup_lock);
>  		snd_card_free(tascam->card);
>  		dev_idx--;
>  	}

[Severity: Medium]
Can tascam_stop_pcm_work_handler() re-arm tascam->stop_work after
cancel_work_sync(&tascam->stop_work) has already returned here and in
tascam_playback_close() / tascam_capture_close()?

tascam_stop_pcm_work_handler() calls snd_pcm_stop(), which invokes
tascam_pcm_trigger(SNDRV_PCM_TRIGGER_STOP) and calls
schedule_work(&tascam->stop_work).  If stop_pcm_work is already running
when stop_work is cancelled first, it can queue stop_work again before
cancel_work_sync(&tascam->stop_pcm_work) runs, leaving stop_work pending
after close() or after snd_card_free() frees tascam_card.  Cancelling
stop_pcm_work before stop_work avoids this.

> diff --git a/sound/usb/usx2y/us144mkii.h b/sound/usb/usx2y/us144mkii.h
> index 95c4341f038ad..4d049db675ef6 100644
> --- a/sound/usb/usx2y/us144mkii.h
> +++ b/sound/usb/usx2y/us144mkii.h
> @@ -229,6 +229,9 @@ struct tascam_card {
>  
>  	/* --- Stream State --- */
>  	spinlock_t lock;
> +	struct mutex cleanup_lock;	/* serializes the close-time URB quiesce
> +					 * with disconnect()'s free path */
> +	bool disconnected;		/* disconnect() freed the URBs */
>  	atomic_t playback_active;
>  	atomic_t capture_active;
>  	atomic_t active_urbs;

[Severity: Low]
struct tascam_card has a kernel-doc comment header above it, so adding
cleanup_lock and disconnected without @cleanup_lock and @disconnected
entries in that comment block will trigger W=1 kernel-doc warnings.

> diff --git a/sound/usb/usx2y/us144mkii_capture.c b/sound/usb/usx2y/us144mkii_capture.c
> index d7cdf9e0f6851..0eba34cea5d1d 100644
> --- a/sound/usb/usx2y/us144mkii_capture.c
> +++ b/sound/usb/usx2y/us144mkii_capture.c
> @@ -35,8 +35,43 @@ static int tascam_capture_open(struct snd_pcm_substream *substream)
>  static int tascam_capture_close(struct snd_pcm_substream *substream)
>  {
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
> +	int i;
> +
> +	/* Serialize the quiesce below with disconnect()'s tascam_free_urbs()
> +	 * via the cleanup lock; after disconnect() has run there is nothing
> +	 * left to kill. */
> +	guard(mutex)(&tascam->cleanup_lock);
> +	if (!tascam->disconnected) {
> +		/* Clear the state under tascam->lock first (ordered against
> +		 * the completions' locked reads) before killing. */
> +		scoped_guard(spinlock_irqsave, &tascam->lock) {
> +			atomic_set(&tascam->capture_active, 0);
> +			tascam->capture_substream = NULL;
> +		}
> +
> +		/*
> +		 * Kill the capture URBs, and the feedback URBs, too: the
> +		 * feedback completion consumes the capture runtime
> +		 * (capture_rt->period_size, snd_pcm_period_elapsed()), so
> +		 * closing capture while playback stays open must quiesce
> +		 * the feedback path as well.  usb_kill_urb() waits for each
> +		 * in-flight completion -- the explicit synchronous barrier.
> +		 */
> +		for (i = 0; i < NUM_CAPTURE_URBS; i++)
> +			if (tascam->capture_urbs[i])
> +				usb_kill_urb(tascam->capture_urbs[i]);
> +		for (i = 0; i < NUM_FEEDBACK_URBS; i++)
> +			if (tascam->feedback_urbs[i])
> +				usb_kill_urb(tascam->feedback_urbs[i]);
> +	}

[Severity: High]
Does killing tascam->feedback_urbs[] here without restarting them stall an
active playback stream when capture is closed?

When usb_kill_urb() unlinks the feedback URBs, feedback_urb_complete()
bails out on -ENOENT without resubmitting (and any in-flight completion
with status 0 fails usb_submit_urb() with -EPERM while urb->reject is
set).  Because tascam_capture_close() never resubmits feedback_urbs[],
feedback_urb_complete() stops running altogether, halting
tascam->playback_frames_consumed updates and snd_pcm_period_elapsed() for
playback.  Symmetrically, closing playback while capture is running clears
tascam->playback_active and kills feedback_urbs[] in
tascam_playback_close(), which freezes capture_frames_processed and causes
capture's later tascam_pcm_trigger(SNDRV_PCM_TRIGGER_STOP) to do nothing
because it checks atomic_read(&tascam->playback_active).

[Severity: High]
Is capture_urb_complete() missing the tascam->lock serialization that this
patch adds to playback_urb_complete() and feedback_urb_complete()?

sound/usb/usx2y/us144mkii_capture.c:capture_urb_complete() {
    ...
	if (!tascam || !atomic_read(&tascam->capture_active))
		goto out;
    ...
	usb_get_urb(urb);
	usb_anchor_urb(urb, &tascam->capture_anchor);
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret < 0) {
		dev_err_ratelimited(tascam->card->dev,
				    "Failed to resubmit capture URB: %d\n",
				    ret);
		usb_unanchor_urb(urb);
		usb_put_urb(urb);
		atomic_dec(
			&tascam->active_urbs); /* Decrement on failed resubmission */
		return;
	}
out:
	usb_put_urb(urb);
}

If capture_urb_complete() passes the unlocked capture_active check just
before tascam_capture_close() clears capture_active, it will still call
schedule_work(&tascam->capture_work) and usb_submit_urb().  If
tascam_capture_close() has already finished usb_kill_urb() on that URB
index, the URB is resubmitted after capture_active is 0; if usb_kill_urb()
is currently running on that URB, usb_submit_urb() fails with -EPERM and
the early return skips out: usb_put_urb(urb), leaking the URB reference
acquired when the completed transfer was submitted.
tascam_capture_work_handler() also still reads tascam->capture_substream
and substream->runtime at entry without holding tascam->lock.

>  
> -	tascam->capture_substream = NULL;
> +	/* the capture work caches substream->runtime at entry and writes
> +	 * runtime->dma_area; it must not run after the detach below */
> +	cancel_work_sync(&tascam->capture_work);
> +	cancel_work_sync(&tascam->stop_work);
> +	/* 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]
Does cancelling tascam->stop_work here and in tascam_playback_close()
leave tascam->active_urbs permanently non-zero after closing a running
stream?

When a running stream is closed, snd_pcm_release_substream() calls
snd_pcm_drop() -> tascam_pcm_trigger(SNDRV_PCM_TRIGGER_STOP), which queues
tascam->stop_work, and then immediately calls .close().  None of the URB
completions decrement tascam->active_urbs when an URB is killed with
-ENOENT; tascam_stop_work_handler() is the only place that resets
atomic_set(&tascam->active_urbs, 0) (and kills all three URB anchors,
including capture_anchor on playback close and playback_anchor on capture
close).  When cancel_work_sync(&tascam->stop_work) cancels the pending
work item before it runs, tascam->active_urbs stays at 16 and every
subsequent SNDRV_PCM_TRIGGER_START fails with -EAGAIN at:

sound/usb/usx2y/us144mkii_pcm.c:tascam_pcm_trigger() {
    ...
	if (do_start) {
		if (atomic_read(&tascam->active_urbs) > 0) {
			dev_warn(tascam->card->dev,
				 "Cannot start, URBs still active.\n");
			return -EAGAIN;
		}
    ...
}

> diff --git a/sound/usb/usx2y/us144mkii_playback.c b/sound/usb/usx2y/us144mkii_playback.c
> index 7efaca0a64899..58de4721ee859 100644
> --- a/sound/usb/usx2y/us144mkii_playback.c
> +++ b/sound/usb/usx2y/us144mkii_playback.c
> @@ -35,8 +35,40 @@ static int tascam_playback_open(struct snd_pcm_substream *substream)
>  static int tascam_playback_close(struct snd_pcm_substream *substream)
>  {
>  	struct tascam_card *tascam = snd_pcm_substream_chip(substream);
> +	int i;
> +
> +	/* Serialize the quiesce below with disconnect()'s tascam_free_urbs()
> +	 * via the cleanup lock; after disconnect() has run there is nothing
> +	 * left to kill. */
> +	guard(mutex)(&tascam->cleanup_lock);
> +	if (!tascam->disconnected) {
> +		/* Clear the state under tascam->lock first, so a completion
> +		 * that races this close observes the cleared flag before its
> +		 * final resubmit (see the completions) and cannot bring an
> +		 * URB back to life after the kill. */
> +		scoped_guard(spinlock_irqsave, &tascam->lock) {
> +			atomic_set(&tascam->playback_active, 0);
> +			tascam->playback_substream = NULL;
> +		}
> +
> +		/*
> +		 * Per-URB usb_kill_urb() is the explicit synchronous close
> +		 * barrier: it waits for each in-flight completion (including
> +		 * one that already passed the active/pointer checks).  The
> +		 * feedback completion also consumes the playback runtime.
> +		 */
> +		for (i = 0; i < NUM_PLAYBACK_URBS; i++)
> +			if (tascam->playback_urbs[i])
> +				usb_kill_urb(tascam->playback_urbs[i]);
> +		for (i = 0; i < NUM_FEEDBACK_URBS; i++)
> +			if (tascam->feedback_urbs[i])
> +				usb_kill_urb(tascam->feedback_urbs[i]);
> +	}

[Severity: High]
Is .close() too late to quiesce playback_urb_complete() and
tascam_capture_work_handler() against runtime->dma_area teardown?

CPU0                                  CPU1 (<softirq> / kworker)
snd_pcm_release_substream()
  snd_pcm_drop()
    tascam_pcm_trigger(STOP)
      schedule_work(&stop_work)
                                      playback_urb_complete()
                                        /* or tascam_capture_work_handler() */
  do_hw_free()
    tascam_pcm_hw_free() /* no-op */
    snd_pcm_lib_free_pages()
      runtime->dma_area = NULL;
                                        memcpy(..., runtime->dma_area + ...)
                                          <-- NULL dereference / UAF
  substream->ops->close()
    tascam_playback_close()

Because the driver uses snd_pcm_set_managed_buffer_all() and has neither a
.sync_stop callback nor URB/work quiescing in tascam_pcm_hw_free(),
do_hw_free() frees the DMA buffer and sets runtime->dma_area = NULL before
tascam_playback_close() or tascam_capture_close() is reached (and userspace
can also trigger do_hw_free() directly via SNDRV_PCM_IOCTL_HW_FREE without
closing).  Stop synchronization needs to happen in .sync_stop or .hw_free
before snd_pcm_lib_free_pages() runs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/7b5aed28.578.1a212c08a97.Coremail.xuhx@hust.edu.cn?part=1

  reply	other threads:[~2026-11-25 13:13 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 [this message]
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-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-233470@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