From: Takashi Iwai <tiwai@suse.de>
To: 徐晗翔 <xuhx@hust.edu.cn>
Cc: perex@perex.cz, tiwai@suse.com, linux-sound@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Re: ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF)
Date: Wed, 25 Nov 2026 14:27:48 +0100 [thread overview]
Message-ID: <8733sp82i3.wl-tiwai@suse.de> (raw)
In-Reply-To: <49f1dede.596.1a212c69040.Coremail.xuhx@hust.edu.cn>
On Wed, 25 Nov 2026 13:55:32 +0100,
徐晗翔 wrote:
>
> snd_pcm_group_for_each_entry() is a plain list_for_each_entry() over
> substream->group->substreams. A driver timer callback (asihpi's
> snd_card_asihpi_timer_function()) walked that list from softirq with
> no group reference and no link rwsem, while the close of another
> linked substream could unlink the members, reassign the survivors
> and kfree() the group object -- the callback kept dereferencing a
> stale s/runtime pointer and freed group memory (KASAN UAF).
>
> Core: free the group object only after an RCU grace period (new
> rcu_head in struct snd_pcm_group, kfree_rcu() at both free sites),
> so a group pointer read without the link rwsem stays memory-safe
> while a concurrent unlink tears the group down.
>
> Driver: walk the group one member at a time under the group lock
> with rcu_read_lock() held -- the lock makes membership (and each
> member's runtime) stable while the member is processed, and RCU
> keeps the group object alive. snd_pcm_stop_xrun()/
> snd_pcm_period_elapsed() are deferred out of the locked section:
> they enter snd_pcm_action(), which re-takes the group lock. A
> substream object outlives its fd's close and both helpers recheck
> substream->runtime under the stream lock, so the deferred calls are
> safe against a concurrent close of the member.
I don't think we want to add a RCU there. It can open a can of worms.
IMO, an easier and safer fix would be to keep the ASIHPI's timer
callback alive only during the PCM stream running, i.e. assure to kill
the timer callback at PCM stop and sync_stop. During the PCM running
state, PCM links can't be changed, hence the list is safe to traverse.
thanks,
Takashi
> ---
> sound/core/pcm_native.c | 12 ++++++++++--
> include/sound/pcm.h | 6 ++++++
> sound/pci/asihpi/asihpi.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
> 3 files changed, 145 insertions(+), 17 deletions(-)
>
> diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -1368,7 +1368,11 @@
> do_free = refcount_dec_and_test(&group->refs);
> snd_pcm_group_unlock(group, substream->pcm->nonatomic);
> if (do_free)
> - kfree(group);
> + /* free only after an RCU grace period: a driver timer
> + * callback (softirq context) may hold group->lock under
> + * rcu_read_lock() without the link rwsem and must not
> + * touch freed group memory */
> + kfree_rcu(group, rcu_head);
> }
>
> /*
> @@ -2401,7 +2405,11 @@
>
> snd_pcm_group_unlock_irq(group, nonatomic);
> if (do_free)
> - kfree(group);
> + /* free only after an RCU grace period: a driver timer
> + * callback (softirq context) may hold group->lock under
> + * rcu_read_lock() without the link rwsem and must not
> + * touch freed group memory */
> + kfree_rcu(group, rcu_head);
> return 0;
> }
>
> diff --git a/include/sound/pcm.h b/include/sound/pcm.h
> --- a/include/sound/pcm.h
> +++ b/include/sound/pcm.h
> @@ -16,6 +16,7 @@
> #include <linux/bitops.h>
> #include <linux/pm_qos.h>
> #include <linux/refcount.h>
> +#include <linux/rcupdate.h>
> #include <linux/uio.h>
>
> #define snd_pcm_substream_chip(substream) ((substream)->private_data)
> @@ -455,6 +456,11 @@
> struct mutex mutex;
> struct list_head substreams;
> refcount_t refs;
> + /* a group object is freed only after an RCU grace period, so a
> + * lockless/atomic group walker (e.g. a driver timer callback that
> + * takes group->lock under rcu_read_lock()) cannot touch freed
> + * group memory while a concurrent unlink tears the group down */
> + struct rcu_head rcu_head;
> };
>
> struct pid;
> diff --git a/sound/pci/asihpi/asihpi.c b/sound/pci/asihpi/asihpi.c
> --- a/sound/pci/asihpi/asihpi.c
> +++ b/sound/pci/asihpi/asihpi.c
> @@ -136,6 +136,8 @@
> unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
> unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
> unsigned int drained_count;
> + bool timer_walked; /* one-shot mark for the timer
> + * callback's group walk */
> struct snd_pcm_substream *substream;
> u32 h_stream;
> struct hpi_format format;
> @@ -705,6 +707,31 @@
> return result;
> }
>
> +/* Pick the next group member this timer callback has not processed yet.
> + * Must be called with the group lock held and under rcu_read_lock(): the
> + * group list cannot change under us, so every member's runtime is still
> + * alive. */
> +static struct snd_pcm_substream *
> +asihpi_timer_next_member(struct snd_pcm_substream *substream,
> + struct snd_card_asihpi *card)
> +{
> + struct snd_pcm_substream *s;
> +
> + snd_pcm_group_for_each_entry(s, substream) {
> + struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
> +
> + if (snd_pcm_substream_chip(s) != card)
> + continue;
> + /* don't link Cap and Play */
> + if (substream->stream != s->stream)
> + continue;
> + if (ds->timer_walked)
> + continue;
> + return s;
> + }
> + return NULL;
> +}
> +
> /** Timer function, equivalent to interrupt service routine for cards
> */
> static void snd_card_asihpi_timer_function(struct timer_list *t)
> @@ -713,10 +740,12 @@
> struct snd_pcm_substream *substream = dpcm->substream;
> struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
> struct snd_pcm_runtime *runtime;
> + struct snd_pcm_group *group;
> struct snd_pcm_substream *s;
> unsigned int newdata = 0;
> unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
> unsigned int remdata, xfercount, next_jiffies;
> + unsigned long flags;
> int first = 1;
> u16 state;
> u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
> @@ -725,17 +754,58 @@
>
> snd_pcm_debug_name(substream, name, sizeof(name));
>
> - /* find minimum newdata and buffer pos in group */
> + /*
> + * Walk the PCM link group one member at a time, under the group
> + * lock and an RCU read lock. This callback runs from a timer
> + * (softirq) with no group reference and no link rwsem, so a close
> + * of another linked substream could previously unlink the members,
> + * free the group object and free that member's runtime while this
> + * walk was holding a stale s/runtime pointer.
> + *
> + * The group lock keeps the membership (and each member's runtime)
> + * stable while the member is processed; the RCU read lock keeps
> + * the group object itself alive (snd_pcm_unlink() frees it only
> + * after a grace period). snd_pcm_stop_xrun() and
> + * snd_pcm_period_elapsed() must run WITHOUT the group lock -- they
> + * enter snd_pcm_action(), which re-takes the group lock -- so they
> + * are deferred to after the unlocked section. A substream object
> + * outlives the close of its fd, and both helpers recheck
> + * substream->runtime under the stream lock.
> + */
> +
> + /* clear the one-shot walk marks of the previous invocation */
> + rcu_read_lock();
> + group = READ_ONCE(substream->group); /* kept alive by RCU */
> + spin_lock_irqsave(&group->lock, flags);
> snd_pcm_group_for_each_entry(s, substream) {
> - struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
> - runtime = s->runtime;
> + if (snd_pcm_substream_chip(s) == card) {
> + struct snd_card_asihpi_pcm *ds =
> + s->runtime->private_data;
>
> - if (snd_pcm_substream_chip(s) != card)
> - continue;
> + ds->timer_walked = false;
> + }
> + }
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
>
> - /* don't link Cap and Play */
> - if (substream->stream != s->stream)
> - continue;
> + /* find minimum newdata and buffer pos in group */
> + for (;;) {
> + struct snd_card_asihpi_pcm *ds;
> + struct snd_pcm_substream *xrun = NULL;
> +
> + rcu_read_lock();
> + group = READ_ONCE(substream->group); /* kept alive by RCU */
> + spin_lock_irqsave(&group->lock, flags);
> +
> + s = asihpi_timer_next_member(substream, card);
> + if (!s) {
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
> + break;
> + }
> + ds = s->runtime->private_data;
> + runtime = s->runtime;
> + ds->timer_walked = true;
>
> hpi_handle_error(hpi_stream_get_info_ex(
> ds->h_stream, &state,
> @@ -762,8 +832,9 @@
> "P%d drained\n", s->number);
> ds->drained_count++;
> if (ds->drained_count > 20) {
> - snd_pcm_stop_xrun(s);
> - continue;
> + /* defer to after the unlock */
> + xrun = s;
> + goto member_done;
> }
> } else {
> ds->drained_count = 0;
> @@ -799,6 +870,13 @@
> (unsigned long)frames_to_bytes(runtime,
> runtime->control->appl_ptr)
> );
> +
> +member_done:
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
> +
> + if (xrun)
> + snd_pcm_stop_xrun(xrun);
> }
> pcm_buf_dma_ofs = min_buf_pos;
>
> @@ -818,12 +896,41 @@
> asihpi_dbg("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
> next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
>
> + /* clear the one-shot walk marks again for the second pass */
> + rcu_read_lock();
> + group = READ_ONCE(substream->group); /* kept alive by RCU */
> + spin_lock_irqsave(&group->lock, flags);
> snd_pcm_group_for_each_entry(s, substream) {
> - struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
> + if (snd_pcm_substream_chip(s) == card) {
> + struct snd_card_asihpi_pcm *ds =
> + s->runtime->private_data;
>
> - /* don't link Cap and Play */
> - if (substream->stream != s->stream)
> - continue;
> + ds->timer_walked = false;
> + }
> + }
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
> +
> + /* second pass, again one member at a time under the group lock
> + * (see above); snd_pcm_period_elapsed() is deferred to after the
> + * unlock for the same reason as snd_pcm_stop_xrun() */
> + for (;;) {
> + struct snd_card_asihpi_pcm *ds;
> + struct snd_pcm_substream *elapsed = NULL;
> +
> + rcu_read_lock();
> + group = READ_ONCE(substream->group); /* kept alive by RCU */
> + spin_lock_irqsave(&group->lock, flags);
> +
> + s = asihpi_timer_next_member(substream, card);
> + if (!s) {
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
> + break;
> + }
> + ds = s->runtime->private_data;
> + runtime = s->runtime;
> + ds->timer_walked = true;
>
> /* Store dma offset for use by pointer callback */
> ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
> @@ -887,8 +994,15 @@
> /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
> ds->pcm_buf_host_rw_ofs += xfercount;
> ds->pcm_buf_elapsed_dma_ofs += xfercount;
> - snd_pcm_period_elapsed(s);
> + /* defer to after the unlock */
> + elapsed = s;
> }
> +
> + spin_unlock_irqrestore(&group->lock, flags);
> + rcu_read_unlock();
> +
> + if (elapsed)
> + snd_pcm_period_elapsed(elapsed);
> }
>
> if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
>
>
> > -----原始邮件-----
> > 发件人: 徐晗翔 <xuhx@hust.edu.cn>
> > 发送时间:2026-11-13 20:15:08 (星期五)
> > 收件人: perex@perex.cz, tiwai@suse.com
> > 抄送: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
> > 主题: ALSA: pcm: link-group walk in timer callback can use a closed substream's freed runtime (UAF)
> >
> > `snd_card_asihpi_timer_function()` walks the PCM link group with
> > `snd_pcm_group_for_each_entry()` and dereferences each member's
> > `s->runtime->private_data`. That macro is a plain `list_for_each_entry()`;
> > the callback holds no group reference and no group lock. Close of another
> > linked substream can unlink/free the group and detach/free that substream's
> > runtime and `dpcm`, leaving the timer callback with a stale `s`/runtime
> > pointer. This is reachable from user space through
> > `SNDRV_PCM_IOCTL_LINK` plus close, on hardware whose driver supports PCM
> > linking (asihpi does).
> >
> > The detailed description:
> >
> > * Test environment / affected version
> >
> > Linux 7.3.0 source plus a QEMU/KASAN fake-PCM harness. No ASI6205 hardware
> > was available; the harness reproduces the core group traversal and close
> > ordering, with the asihpi callback's exact
> > `snd_pcm_group_for_each_entry()` pattern.
> >
> > * Mechanism
> >
> > The group loops are at `sound/pci/asihpi/asihpi.c:729-735` and `821-827`.
> > They read `s->runtime->private_data` (and in one path `dma_area`) and call
> > `snd_pcm_period_elapsed(s)`. The macro is
> > `include/sound/pcm.h:694`:
> >
> > #define snd_pcm_group_for_each_entry(s, substream) \
> > list_for_each_entry(s, &substream->group->substreams, link_list)
> >
> > `snd_pcm_stream_lock_irq()` (`sound/core/pcm_native.c:140-144`) locks
> > `substream->self_group`, not the group that the timer callback walks.
> > `snd_pcm_unlink()` (`pcm_native.c:2370-2402`) takes
> > `snd_pcm_link_rwsem` and the group lock, removes the closing substream,
> > relinks the remaining streams, and may `kfree(group)`.
> > `snd_pcm_release_substream()` (`pcm_native.c:2785-2805`) calls
> > `pcm_release_private()` -> `snd_pcm_unlink()` at 2779-2783 before
> > `snd_pcm_detach_substream()` frees the runtime and `dpcm`.
> >
> > A timer callback that already loaded member `s` or `s->runtime` can run
> > after B's unlink/detach. It may then read freed `runtime`, freed
> > `private_data`, or a freed group list node; `snd_pcm_period_elapsed(s)`
> > additionally takes the stale substream.
> >
> > * Why existing synchronization does not close the window
> >
> > `snd_pcm_stop()` does transiently take a group reference through
> > `snd_pcm_action()` -> `snd_pcm_stream_group_ref()` (`pcm_native.c:1378-1427`)
> > and group lock, but it drops the reference after the STOP action. The
> > asihpi timer callback is not part of that action and holds no group
> > reference. Taking `snd_pcm_stream_lock_irq(A)` in the callback would only
> > lock A's `self_group`; it would not pin B or the group. The group can be
> > freed while the callback holds a raw member pointer.
> >
> > * Trigger model
> >
> > Two capture substreams A and B, same PCM and runtime state; user does
> > `SNDRV_PCM_IOCTL_LINK` then starts A. While A's timer callback is walking
> > the group, user closes B. `SNDRV_PCM_IOCTL_LINK` is available to a local
> > user with the PCM device open; no special privilege is needed beyond access
> > to the PCM. Consequence: UAF in timer context, crash or memory corruption.
> >
> >
> > * How to run the PoC
> >
> > The harness is self-contained and does not need ASI6205 hardware: it creates
> > a fake ALSA card with two capture substreams. The tested configuration was
> > 7.3.0 with KASAN and 4 vCPU; a uniprocessor run may not overlap.
> >
> > Build against the target kernel build tree:
> >
> > printf 'obj-m := sndpci_race.o\n' > Kbuild
> > make -C <kernel-build-dir> M=$PWD modules
> > gcc -static -O2 -I<kernel-source>/include/uapi -o pcmtest pcmtest.c
> >
> > Put `sndpci_race.ko` and `pcmtest` into the guest initramfs/rootfs, boot the
> > target kernel (QEMU/KVM recommended), then run as root:
> >
> > insmod sndpci_race.ko mode=pcm
> > ./pcmtest link 200
> > dmesg | grep -E 'BUG: KASAN|race_pcm_timer|victim use'
> >
> > In the minimal guest the fake card enumerates as card 0 and `pcmtest` opens
> > `/dev/snd/pcmC0D0c`. If the test machine already has another sound card,
> > adjust `DEV` in `pcmtest.c` or use the QEMU initramfs. Expected result:
> > 32 KASAN reports with `slab-use-after-free in race_pcm_timer` and about 114
> > `victim use` lines.
> >
> >
> > * Suggested direction
> >
> > This needs a core/lifetime fix, not just an asihpi timer fix. Possible
> > directions: make group traversal safe against concurrent unlink (group
> > reference or RCU-protected/atomic-safe iterator that pins each member's
> > runtime), or provide a driver API that snapshots/refcounts members before
> > the callback walks them. On the asihpi side, quiesce the timer/IRQ before
> > allowing the linked substream to detach. Adding only
> > `snd_pcm_stream_lock_irq(A)` is not sufficient because it locks A's
> > `self_group`, not the group being walked.
> >
> >
> >
> > Please keep me in Cc; I am not subscribed to the lists.
> >
> >
> > Regards,
> > Hanxiang Xu
> > xuhx@hust.edu.cn
next prev parent reply other threads:[~2026-11-25 13:27 UTC|newest]
Thread overview: 41+ 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-25 13:27 ` Takashi Iwai [this message]
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
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=8733sp82i3.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=tiwai@suse.com \
--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