ALSA: aloop: Fix racy access at PCM trigger
Subsystems (2)
PATCH 1/1 ALSA: aloop: Fix racy access at PCM trigger
From: Takashi Iwai <[email protected]>
[ Upstream commit 826af7fa62e347464b1b4e0ba2fe19a92438084f ]
The PCM trigger callback of aloop driver tries to check the PCM state and stop the stream of the tied substream in the corresponding cable. Since both check and stop operations are performed outside the cable lock, this may result in UAF when a program attempts to trigger frequently while opening/closing the tied stream, as spotted by fuzzers.
For addressing the UAF, this patch changes two things:
- It covers the most of code in loopback_check_format() with cable->lock spinlock, and add the proper NULL checks. This avoids already some racy accesses.
- In addition, now we try to check the state of the capture PCM stream that may be stopped in this function, which was the major pain point leading to UAF.
[ Karl Mehltretter: open-coded spin_lock_irqsave() instead of scoped_guard();
used snd_pcm_running() instead of cruntime->state; dropped the access-mode comparison and notification (462494565c27, e299a9fd433f, cdac6e1f7164); kept the stop_count handling from the e5c33cdc6f40 backport. ]
---e5c33cdc6f40 ("ALSA: aloop: Fix peer runtime UAF during format-change stop") went into 5.10.y (83bd62fa9620) and 5.15.y (345c24b2bcf0) without this prerequisite, so stop_count is only taken after the unlocked peer lookup and format checks.
The 5.10.y and 5.15.y backports are submitted as separate mails with identical source diffs. The 6.1.y and 6.6.y branches need both upstream fixes and are handled by separate two-patch submissions.
Tested on v5.10.268 with KASAN under QEMU; a kprobe widened the race. Vulnerable kernel, probe at loopback_trigger+0x679 after the peer-runtime
load: KASAN UAF, then a NULL dereference and Oops. Fixed kernel, probe at +0x93, placed just inside the new lock boundary so the close contends on cable->lock: no KASAN report or Oops.
Functional A/B, same initramfs: all 4096 captured frames matched byte-for-byte and control/mismatch handling passed. A prepared-but-idle capture remained PREPARED when mismatched playback started instead of being forced to DRAINING, matching upstream.
aloop.o builds on v5.10.268 and v5.15.219. The patch also applies unchanged to v5.10.269-rc1 and v5.15.220-rc1.
sound/drivers/aloop.c | 99 ++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 40 deletions(-)
sound/drivers/aloop.c +59 -40
@@ -323,56 +323,75 @@ static int loopback_snd_timer_close_cable(struct loopback_pcm *dpcm)static int loopback_check_format (struct loopback_cable *cable,int stream) { +struct loopback_pcm *dpcm_play, *dpcm_capt;struct snd_pcm_runtime *runtime, *cruntime;struct loopback_setup *setup;struct snd_card *card; -int check; +unsigned long flags; +bool stop_capture = false; +int check, err =0 ; + +spin_lock_irqsave (&cable->lock, flags); + dpcm_play = cable->streams[SNDRV_PCM_STREAM_PLAYBACK ]; + dpcm_capt = cable->streams[SNDRV_PCM_STREAM_CAPTURE ];if (cable->valid !=CABLE_VALID_BOTH ) { -if (stream ==SNDRV_PCM_STREAM_PLAYBACK ) - goto __notify; -return 0 ; - } - runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK ]-> - substream->runtime; - cruntime = cable->streams[SNDRV_PCM_STREAM_CAPTURE ]-> - substream->runtime; - check = runtime->format != cruntime->format || - runtime->rate != cruntime->rate || - runtime->channels != cruntime->channels; -if (!check) -return 0 ; -if (stream ==SNDRV_PCM_STREAM_CAPTURE ) { -return -EIO ; +if (stream ==SNDRV_PCM_STREAM_CAPTURE || !dpcm_play) + goto unlock; }else { -/* close must not free the peer runtime below */ -atomic_inc (&cable->stop_count); -snd_pcm_stop (cable->streams[SNDRV_PCM_STREAM_CAPTURE ]-> - substream,SNDRV_PCM_STATE_DRAINING ); -if (atomic_dec_and_test (&cable->stop_count)) -wake_up (&cable->stop_wait); - __notify: - runtime = cable->streams[SNDRV_PCM_STREAM_PLAYBACK ]-> - substream->runtime; - setup =get_setup (cable->streams[SNDRV_PCM_STREAM_PLAYBACK ]); - card = cable->streams[SNDRV_PCM_STREAM_PLAYBACK ]->loopback->card; -if (setup->format != runtime->format) { -snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , - &setup->format_id); - setup->format = runtime->format; +if (!dpcm_play || !dpcm_capt) { + err = -EIO ; + goto unlock; } -if (setup->rate != runtime->rate) { -snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , - &setup->rate_id); - setup->rate = runtime->rate; + runtime = dpcm_play->substream->runtime; + cruntime = dpcm_capt->substream->runtime; +if (!runtime || !cruntime) { + err = -EIO ; + goto unlock; } -if (setup->channels != runtime->channels) { -snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , - &setup->channels_id); - setup->channels = runtime->channels; + check = runtime->format != cruntime->format || + runtime->rate != cruntime->rate || + runtime->channels != cruntime->channels; +if (!check) + goto unlock; +if (stream ==SNDRV_PCM_STREAM_CAPTURE ) { + err = -EIO ; + goto unlock; + }else if (snd_pcm_running (dpcm_capt->substream)) { +/* close must not free the peer runtime below */ +atomic_inc (&cable->stop_count); + stop_capture = true; } } -return 0 ; + + setup =get_setup (dpcm_play); + card = dpcm_play->loopback->card; + runtime = dpcm_play->substream->runtime; +if (setup->format != runtime->format) { +snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , + &setup->format_id); + setup->format = runtime->format; + } +if (setup->rate != runtime->rate) { +snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , + &setup->rate_id); + setup->rate = runtime->rate; + } +if (setup->channels != runtime->channels) { +snd_ctl_notify (card,SNDRV_CTL_EVENT_MASK_VALUE , + &setup->channels_id); + setup->channels = runtime->channels; + } + +unlock: +spin_unlock_irqrestore (&cable->lock, flags); +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); + } + +return err; }static void loopback_active_notify (struct loopback_pcm *dpcm)
Signature · 1 line
2.53.0
Quoted 6 lines
From: Takashi Iwai <[email protected]>
[ Upstream commit
826af7fa62e347464b1b4e0ba2fe19a92438084f]The PCM trigger callback of aloop driver tries to check the PCM state and stop the stream of the tied substream in the corresponding cable.
Queued for 5.10, thanks.
Signature · 2 lines
Thanks, Sasha