* [PATCH] ALSA: aloop: Fix peer runtime UAF during format-change stop
@ 2026-04-23 3:22 Cássio Gabriel
2026-04-24 11:14 ` Takashi Iwai
0 siblings, 1 reply; 3+ messages in thread
From: Cássio Gabriel @ 2026-04-23 3:22 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela
Cc: linux-sound, linux-kernel, syzbot+8fa95c41eafbc9d2ff6f, stable,
Cássio Gabriel
loopback_check_format() may stop the capture side when playback starts
with parameters that no longer match a running capture stream. Commit
826af7fa62e3 ("ALSA: aloop: Fix racy access at PCM trigger") moved
the peer lookup under cable->lock, but the actual snd_pcm_stop() still
runs after dropping that lock.
A concurrent close can clear the capture entry from cable->streams[] and
detach or free its runtime while the playback trigger path still holds a
stale peer substream pointer.
Keep a per-cable count of in-flight peer stops before dropping
cable->lock, make free_cable() wait for those stops before detaching the
runtime, and take the peer stream lock around snd_pcm_stop(). This
preserves the existing behavior while making the peer runtime lifetime
explicit.
Reported-by: syzbot+8fa95c41eafbc9d2ff6f@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8fa95c41eafbc9d2ff6f
Fixes: 597603d615d2 ("ALSA: introduce the snd-aloop module for the PCM loopback")
Cc: stable@vger.kernel.org
Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>
---
sound/drivers/aloop.c | 56 ++++++++++++++++++++++++++++++++++++---------------
1 file changed, 40 insertions(+), 16 deletions(-)
diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c
index aa0d2fcb1a18..a997ee262740 100644
--- a/sound/drivers/aloop.c
+++ b/sound/drivers/aloop.c
@@ -99,6 +99,9 @@ struct loopback_ops {
struct loopback_cable {
spinlock_t lock;
struct loopback_pcm *streams[2];
+ /* in-flight peer stops running outside cable->lock */
+ atomic_t stop_count;
+ wait_queue_head_t stop_wait;
struct snd_pcm_hardware hw;
/* flags */
unsigned int valid;
@@ -337,10 +340,10 @@ static bool is_access_interleaved(snd_pcm_access_t access)
static int loopback_check_format(struct loopback_cable *cable, int stream)
{
struct loopback_pcm *dpcm_play, *dpcm_capt;
+ struct loopback_pcm *stop_dpcm = NULL;
struct snd_pcm_runtime *runtime, *cruntime;
struct loopback_setup *setup;
struct snd_card *card;
- bool stop_capture = false;
int check;
scoped_guard(spinlock_irqsave, &cable->lock) {
@@ -366,8 +369,11 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)
return 0;
if (stream == SNDRV_PCM_STREAM_CAPTURE)
return -EIO;
- else if (cruntime->state == SNDRV_PCM_STATE_RUNNING)
- stop_capture = true;
+ else if (cruntime->state == SNDRV_PCM_STATE_RUNNING) {
+ /* close must not free the peer runtime below */
+ atomic_inc(&cable->stop_count);
+ stop_dpcm = dpcm_capt;
+ }
}
setup = get_setup(dpcm_play);
@@ -396,8 +402,18 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)
}
}
- if (stop_capture)
- snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING);
+ if (stop_dpcm) {
+ struct snd_pcm_substream *stop_substream = stop_dpcm->substream;
+ unsigned long flags;
+
+ snd_pcm_stream_lock_irqsave_nested(stop_substream, flags);
+ if (stop_substream->runtime && snd_pcm_running(stop_substream))
+ snd_pcm_stop(stop_substream, SNDRV_PCM_STATE_DRAINING);
+ snd_pcm_stream_unlock_irqrestore(stop_substream, flags);
+
+ if (atomic_dec_and_test(&cable->stop_count))
+ wake_up(&cable->stop_wait);
+ }
return 0;
}
@@ -1049,23 +1065,29 @@ static void free_cable(struct snd_pcm_substream *substream)
struct loopback *loopback = substream->private_data;
int dev = get_cable_index(substream);
struct loopback_cable *cable;
+ struct loopback_pcm *dpcm;
+ bool other_alive;
cable = loopback->cables[substream->number][dev];
if (!cable)
return;
- if (cable->streams[!substream->stream]) {
- /* other stream is still alive */
- guard(spinlock_irq)(&cable->lock);
- cable->streams[substream->stream] = NULL;
- } else {
- struct loopback_pcm *dpcm = substream->runtime->private_data;
- if (cable->ops && cable->ops->close_cable && dpcm)
- cable->ops->close_cable(dpcm);
- /* free the cable */
- loopback->cables[substream->number][dev] = NULL;
- kfree(cable);
+ scoped_guard(spinlock_irq, &cable->lock) {
+ cable->streams[substream->stream] = NULL;
+ other_alive = cable->streams[!substream->stream];
}
+
+ /* Pair with the stop_count increment in loopback_check_format(). */
+ wait_event(cable->stop_wait, !atomic_read(&cable->stop_count));
+ if (other_alive)
+ return;
+
+ dpcm = substream->runtime->private_data;
+ if (cable->ops && cable->ops->close_cable && dpcm)
+ cable->ops->close_cable(dpcm);
+ /* free the cable */
+ loopback->cables[substream->number][dev] = NULL;
+ kfree(cable);
}
static int loopback_jiffies_timer_open(struct loopback_pcm *dpcm)
@@ -1260,6 +1282,8 @@ static int loopback_open(struct snd_pcm_substream *substream)
goto unlock;
}
spin_lock_init(&cable->lock);
+ atomic_set(&cable->stop_count, 0);
+ init_waitqueue_head(&cable->stop_wait);
cable->hw = loopback_pcm_hardware;
if (loopback->timer_source)
cable->ops = &loopback_snd_timer_ops;
---
base-commit: 03ef04a70af3ddfa925d78015713fdb4a15e4e88
change-id: 20260422-alsa-aloop-peer-stop-uaf-004de2120b52
Best regards,
--
Cássio Gabriel <cassiogabrielcontato@gmail.com>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] ALSA: aloop: Fix peer runtime UAF during format-change stop
2026-04-23 3:22 [PATCH] ALSA: aloop: Fix peer runtime UAF during format-change stop Cássio Gabriel
@ 2026-04-24 11:14 ` Takashi Iwai
2026-04-24 12:14 ` Cássio Gabriel Monteiro Pires
0 siblings, 1 reply; 3+ messages in thread
From: Takashi Iwai @ 2026-04-24 11:14 UTC (permalink / raw)
To: Cássio Gabriel
Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel,
syzbot+8fa95c41eafbc9d2ff6f, stable
On Thu, 23 Apr 2026 05:22:22 +0200,
Cássio Gabriel wrote:
>
> loopback_check_format() may stop the capture side when playback starts
> with parameters that no longer match a running capture stream. Commit
> 826af7fa62e3 ("ALSA: aloop: Fix racy access at PCM trigger") moved
> the peer lookup under cable->lock, but the actual snd_pcm_stop() still
> runs after dropping that lock.
>
> A concurrent close can clear the capture entry from cable->streams[] and
> detach or free its runtime while the playback trigger path still holds a
> stale peer substream pointer.
>
> Keep a per-cable count of in-flight peer stops before dropping
> cable->lock, make free_cable() wait for those stops before detaching the
> runtime, and take the peer stream lock around snd_pcm_stop(). This
> preserves the existing behavior while making the peer runtime lifetime
> explicit.
>
> Reported-by: syzbot+8fa95c41eafbc9d2ff6f@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=8fa95c41eafbc9d2ff6f
> Fixes: 597603d615d2 ("ALSA: introduce the snd-aloop module for the PCM loopback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>
> ---
> sound/drivers/aloop.c | 56 ++++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 40 insertions(+), 16 deletions(-)
>
> diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c
> index aa0d2fcb1a18..a997ee262740 100644
> --- a/sound/drivers/aloop.c
> +++ b/sound/drivers/aloop.c
> @@ -99,6 +99,9 @@ struct loopback_ops {
> struct loopback_cable {
> spinlock_t lock;
> struct loopback_pcm *streams[2];
> + /* in-flight peer stops running outside cable->lock */
> + atomic_t stop_count;
> + wait_queue_head_t stop_wait;
> struct snd_pcm_hardware hw;
> /* flags */
> unsigned int valid;
> @@ -337,10 +340,10 @@ static bool is_access_interleaved(snd_pcm_access_t access)
> static int loopback_check_format(struct loopback_cable *cable, int stream)
> {
> struct loopback_pcm *dpcm_play, *dpcm_capt;
> + struct loopback_pcm *stop_dpcm = NULL;
> struct snd_pcm_runtime *runtime, *cruntime;
> struct loopback_setup *setup;
> struct snd_card *card;
> - bool stop_capture = false;
> int check;
>
> scoped_guard(spinlock_irqsave, &cable->lock) {
> @@ -366,8 +369,11 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)
> return 0;
> if (stream == SNDRV_PCM_STREAM_CAPTURE)
> return -EIO;
> - else if (cruntime->state == SNDRV_PCM_STATE_RUNNING)
> - stop_capture = true;
> + else if (cruntime->state == SNDRV_PCM_STATE_RUNNING) {
> + /* close must not free the peer runtime below */
> + atomic_inc(&cable->stop_count);
> + stop_dpcm = dpcm_capt;
> + }
> }
>
> setup = get_setup(dpcm_play);
> @@ -396,8 +402,18 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)
> }
> }
>
> - if (stop_capture)
> - snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING);
> + if (stop_dpcm) {
> + struct snd_pcm_substream *stop_substream = stop_dpcm->substream;
> + unsigned long flags;
> +
> + snd_pcm_stream_lock_irqsave_nested(stop_substream, flags);
> + if (stop_substream->runtime && snd_pcm_running(stop_substream))
> + snd_pcm_stop(stop_substream, SNDRV_PCM_STATE_DRAINING);
> + snd_pcm_stream_unlock_irqrestore(stop_substream, flags);
> +
> + if (atomic_dec_and_test(&cable->stop_count))
> + wake_up(&cable->stop_wait);
> + }
Do we need to complicate this handling? IOW, can it be simply be like
below?
if (stop_capture) {
snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING);
if (atomic_dec_and_test(&cable->stop_count))
wake_up(&cable->stop_wait);
}
thanks,
Takashi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] ALSA: aloop: Fix peer runtime UAF during format-change stop
2026-04-24 11:14 ` Takashi Iwai
@ 2026-04-24 12:14 ` Cássio Gabriel Monteiro Pires
0 siblings, 0 replies; 3+ messages in thread
From: Cássio Gabriel Monteiro Pires @ 2026-04-24 12:14 UTC (permalink / raw)
To: Takashi Iwai
Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel,
syzbot+8fa95c41eafbc9d2ff6f, stable
[-- Attachment #1.1: Type: text/plain, Size: 516 bytes --]
On 4/24/26 08:14, Takashi Iwai wrote:
> Do we need to complicate this handling? IOW, can it be simply be like
> below?
>
> if (stop_capture) {
> snd_pcm_stop(dpcm_capt->substream, SNDRV_PCM_STATE_DRAINING);
> if (atomic_dec_and_test(&cable->stop_count))
> wake_up(&cable->stop_wait);
> }
That makes sense.
The lifetime fix only needs the stop_count/stop_wait
serialization, so I can simplify the stop path as you suggested.
I'll respin and send a v2 patch.
--
Thanks,
Cássio
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-04-24 12:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-23 3:22 [PATCH] ALSA: aloop: Fix peer runtime UAF during format-change stop Cássio Gabriel
2026-04-24 11:14 ` Takashi Iwai
2026-04-24 12:14 ` Cássio Gabriel Monteiro Pires
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox