* [PATCH] ALSA: usx2y: Fix use-after-free of playback URBs at close
@ 2026-12-05 16:43 YuanZhe Liu
2026-12-05 17:06 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-05 16:43 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela
Cc: linux-sound, linux-kernel, stable, Bin Yu, Cheng Wen
The capture completion handler prepares and submits the playback URBs:
when a capture URB completes, usx2y_usbframe_complete() takes the URB
stored in playbacksubs->completed_urb and passes it to
usx2y_urb_play_prepare(). snd_usx2y_pcm_hw_free() frees the playback
URBs first, and only releases the capture URBs afterwards, and only
when the capture substream is not open and prepared. Nothing waits for
a capture completion that is still running, so it can write the ISO
descriptors of a playback URB that was already freed:
BUG: KASAN: slab-use-after-free in usx2y_urb_play_prepare
Write of size 4 at addr ffff8880108620c0
Call Trace:
usx2y_urb_play_prepare sound/usb/usx2y/usbusx2yaudio.c:129
usx2y_usbframe_complete sound/usb/usx2y/usbusx2yaudio.c:235
i_usx2y_urb_complete sound/usb/usx2y/usbusx2yaudio.c:332
__usb_hcd_giveback_urb
usb_giveback_urb_bh
usb_kill_urb() only waits for the completion of the URB it is called
on, so killing the playback URBs does not wait for the capture
completion that uses them. Release the capture stream before the
playback URBs instead: its state is set to STATE_STOPPED first, so a
completion that runs later does not touch the playback path, and
usb_kill_urb() on the capture URBs waits for one that is still in
flight. The same order also covers the full-duplex case where the
capture substream is open and prepared: the playback URBs cannot be
freed while the capture completion is driving them.
usx2y_usbframe_complete() also ignores playbacksubs->completed_urb once
the playback stream was stopped, so a completion that is already
running cannot use the URB after it was freed.
A KASAN slab-use-after-free write was observed when an OSS PCM stream
was closed while a capture transfer was completing. The change is
compile-tested only, as no US-122 hardware was available.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
sound/usb/usx2y/usbusx2yaudio.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
index ef24c3ec690f..6cee877833a9 100644
--- a/sound/usb/usx2y/usbusx2yaudio.c
+++ b/sound/usb/usx2y/usbusx2yaudio.c
@@ -214,6 +214,8 @@ static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs,
struct urb *urb = playbacksubs->completed_urb;
state = atomic_read(&playbacksubs->state);
+ if (state == STATE_STOPPED)
+ urb = NULL;
if (urb) {
if (state == STATE_RUNNING)
usx2y_urb_play_retire(playbacksubs, urb);
@@ -824,14 +826,14 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
+ /*
+ * The capture completion feeds the playback URBs; release
+ * the capture stream first so usb_kill_urb() waits for it.
+ */
atomic_set(&subs->state, STATE_STOPPED);
+ atomic_set(&cap_subs->state, STATE_STOPPED);
+ usx2y_urbs_release(cap_subs);
usx2y_urbs_release(subs);
- if (!cap_subs->pcm_substream ||
- !cap_subs->pcm_substream->runtime ||
- cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) {
- atomic_set(&cap_subs->state, STATE_STOPPED);
- usx2y_urbs_release(cap_subs);
- }
} else {
playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ALSA: usx2y: Fix use-after-free of playback URBs at close
2026-12-05 16:43 [PATCH] ALSA: usx2y: Fix use-after-free of playback URBs at close YuanZhe Liu
@ 2026-12-05 17:06 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 17:06 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: usx2y: UAF of capture URBs in snd_usx2y_pcm_hw_free()
- [High] ALSA: usx2y: playback hw_free() unconditionally kills active
capture
- [High] ALSA: usx2y: hw_free() vs trigger() race deadlocks
pcm_prepare()
--
commit 829b732cac6599f15afd45198cef24c7c11ba1e6
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
ALSA: usx2y: Fix use-after-free of playback URBs at close
This commit ignores playbacksubs->completed_urb in usx2y_usbframe_complete()
once the playback stream is stopped, and changes playback
snd_usx2y_pcm_hw_free() to unconditionally stop and release the capture URBs
before releasing the playback URBs.
> diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
> index ef24c3ec690f5..6cee877833a98 100644
> --- a/sound/usb/usx2y/usbusx2yaudio.c
> +++ b/sound/usb/usx2y/usbusx2yaudio.c
[ ... ]
> @@ -824,14 +826,14 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
>
> if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
> cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
> + /*
> + * The capture completion feeds the playback URBs; release
> + * the capture stream first so usb_kill_urb() waits for it.
> + */
> atomic_set(&subs->state, STATE_STOPPED);
> + atomic_set(&cap_subs->state, STATE_STOPPED);
> + usx2y_urbs_release(cap_subs);
> usx2y_urbs_release(subs);
> - if (!cap_subs->pcm_substream ||
> - !cap_subs->pcm_substream->runtime ||
> - cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) {
> - atomic_set(&cap_subs->state, STATE_STOPPED);
> - usx2y_urbs_release(cap_subs);
> - }
[Severity: High]
Can releasing cap_subs before subs cause a use-after-free of capture URBs?
Both capture and playback URBs share i_usx2y_urb_complete(). When a
capture URB completes before its paired playback URB, it stores itself in
capsubs->completed_urb and returns, so its use_count drops to zero. If
playback snd_usx2y_pcm_hw_free() then races with the playback URB
completion, usb_kill_urb() on the idle capture URB returns immediately and
usx2y_urbs_release(cap_subs) frees it while the playback completion is
still using it:
CPU0 CPU1
i_usx2y_urb_complete(play_urb)
usx2y_usbframe_complete()
read playbacksubs->state
snd_usx2y_pcm_hw_free()
set subs/cap_subs STATE_STOPPED
usx2y_urbs_release(cap_subs)
usb_kill_urb(cap_urb)
kfree(cap_urb->transfer_buffer)
usb_free_urb(cap_urb)
usx2y_urb_play_prepare()
cap_urb->iso_frame_desc[] <-- use-after-free
Because capture and playback completions cross-reference both streams,
killing all URBs on both streams before freeing any URB or transfer buffer
would avoid the use-after-free in either direction.
[Severity: High]
Does removing the capture state check break active full-duplex capture
streams?
Unconditionally setting cap_subs->state to STATE_STOPPED and calling
usx2y_urbs_release(cap_subs) tears down capture URBs whenever playback
closes or fails hw_params(), since snd_pcm_hw_params() calls hw_free() on
error. A running capture stream then silently stalls with no further URB
completions or xrun notification, and a prepared capture stream fails
SNDRV_PCM_TRIGGER_START with -EPIPE in snd_usx2y_pcm_trigger().
[Severity: High]
Can racing playback snd_usx2y_pcm_hw_free() with capture
snd_usx2y_pcm_trigger() deadlock snd_usx2y_pcm_prepare()?
snd_usx2y_pcm_trigger() does not hold usx2y->pcm_mutex and updates
cap_subs->state with a non-atomic read-then-set. Now that playback
hw_free() unconditionally stops and frees capture URBs even while capture
is prepared or running, a concurrent capture trigger can overwrite
STATE_STOPPED back to STATE_PRERUNNING or STATE_PREPARED after the capture
URBs have been freed:
CPU0 CPU1
snd_usx2y_pcm_trigger(capture)
atomic_read(&subs->state)
== STATE_PREPARED
snd_usx2y_pcm_hw_free(playback)
atomic_set(&cap_subs->state,
STATE_STOPPED)
usx2y_urbs_release(cap_subs)
atomic_set(&subs->state,
STATE_PRERUNNING)
snd_usx2y_pcm_prepare(playback)
capsubs->state >= STATE_PREPARED
usx2y_urbs_start(playback)
wait_event(prepare_wait_queue, ...)
<-- hangs forever
Because no capture URBs exist to drive playback startup and wake
prepare_wait_queue, usx2y_urbs_start() blocks permanently in
uninterruptible sleep while holding usx2y->pcm_mutex, wedging all
subsequent PCM operations on the card.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205164316.3665-1-25031212351@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 17:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 16:43 [PATCH] ALSA: usx2y: Fix use-after-free of playback URBs at close YuanZhe Liu
2026-12-05 17:06 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox