Re: ALSA: pcm: struct snd_pcm and its substreams are kfree()d while open fds and in-flight ioctls still use them (UAF)

From: 徐晗翔

Date: Tue Dec 01 2026 - 07:01:28 EST


Update: still present in 7.4-rc5 -- snd_pcm_free() still kfree()s
every substream and the pcm itself with open fds around (the
disconnect path gained no ref wait and no f_op swap), and the same
reproducer produces 36 use-after-free reports at the same sites
(snd_pcm_release, snd_pcm_common_ioctl, snd_power_ref_and_wait).

Thanks.


> -----原始邮件-----
> 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> 发送时间:2026-12-01 12:33:54 (星期二)
> 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: ALSA: pcm: struct snd_pcm and its substreams are kfree()d while open fds and in-flight ioctls still use them (UAF)
>
> An ALSA sub-driver that registered a pcm on a live card frees it with
> snd_device_free(card, pcm) on its own remove path -- for example
> snd_emu8000_remove() runs exactly that call (emu8000_synth.c:92) for
> the Emu8000 playback pcm. snd_pcm_dev_disconnect() stops the streams,
> marks them DISCONNECTED and unregisters the minor, but it neither
> replaces the open files' f_op nor waits for substream->ref_count to
> drop; snd_pcm_free() then kfrees every substream (pcm.c:826) and
> struct snd_pcm itself (pcm.c:863). Each open /dev/snd/pcmC*D* fd
> keeps pcm_file->substream, substream->pcm and pcm->open_mutex pointing
> at the freed memory: in-flight ioctls dereference it, and the
> eventual close() runs snd_pcm_release() on it -- pcm_native.c:2976
> pcm_file->substream, 2979 substream->pcm, 2982 snd_power_wait(pcm->card),
> 2984 mutex_lock(&pcm->open_mutex), 2985 snd_pcm_release_substream()
> -> substream->ops->close. When the removal was an rmmod, those ops
> point into the unmapped module text. We verified the audited release
> chain on vanilla 7.3 with the byte-identical core code
> (snd_pcm_new/open/dev_disconnect/free) driven by a module-owned ops
> table in the exact shape of snd_emu8000_pcm_new()/snd_emu8000_remove()
> -- the AWE64 ISA hardware itself cannot be attached to a QEMU x86
> guest -- and obtained 16 use-after-free reports (snd_pcm_release
> +0x3a/+0x4d, the snd_pcm_get_state family reached through
> snd_pcm_common_ioctl, snd_power_ref_and_wait on the freed pcm->card,
> snd_refcount_put, __wake_up_common on the freed pcm's open_wait), with
> ops->close demonstrably reached on the post-free release path.
> Removing the sub-driver requires root; using the device does not.
>
> The detailed description:
>
> * Test environment / affected version
>
> Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC
> -- vanilla mainline 7.3 release tarball, KASAN enabled, QEMU/KVM
> x86_64 guest. sound/core/pcm.c and sound/core/pcm_native.c are
> byte-identical to mainline 7.3 (the audited free sites are
> pcm.c:826/863 and the disconnect at pcm.c:1085-1120); the emu8000
> driver shape (emu8000_synth.c:83-97, emu8000_pcm.c:637-651) is
> byte-identical too; line numbers below are from 7.3.
>
> * Mechanism
>
> - Open side: snd_pcm_open() pins the card (sound.c:111) and the
> card module only (pcm_native.c:2924); nothing pins the pcm object
> or prevents the registering sub-driver module from being removed
> while the fd is open. There is also a lockless window between
> the minor lookup (pcm_native.c:2889) and taking pcm->open_mutex
> (2930), and a re-lock window inside the open retry loop
> (2942-2949) that reads pcm->card->shutdown on a possibly freed pcm.
>
> - Remove side: snd_device_free() -> snd_pcm_dev_disconnect()
> (pcm.c:1085-1120) stops running streams and unregisters the
> device but has no ref_count wait and no f_op replacement;
> snd_pcm_free() -> snd_pcm_free_stream() kfrees each substream
> (826) and kfree(pcm) (863). Note the substream runtime is not
> even freed there (only snd_pcm_detach_substream() frees it on the
> release path), so the dangling fd keeps a leaked runtime plus two
> freed back-pointers.
>
> - Whole-card removal nests the same free inside
> snd_device_disconnect_all() via the seq device's synchronous
> device_del() unbind; with f_ops already swapped only in-flight
> syscalls can hit the freed objects.
>
> - The HDA codec path is the protected counter-example:
> hda_codec_driver_remove() waits snd_refcount_sync(&codec->pcm_ref)
> + snd_power_sync_ref() (bind.c:163-166) BEFORE codec_release_pcms()
> runs snd_device_free(); the emu8000/AOA removal paths have no
> equivalent and emu8k_pcm_open() pins no sub-driver module.
>
> * Trigger model
>
> Root runs `rmmod snd_emu8000_synth` (or sysfs unbind; on PowerMac
> the analogous i2sbus codec detach runs automatically) while a
> /dev/snd/pcmC*D1p fd is open or a pcm syscall is in flight. The
> hardware requirement is an AWE64-class card with the emu8000 synth
> sub-driver (mem_size > 0). Consequence: use-after-free on
> snd_pcm/snd_pcm_substream (mutex_lock on freed open_mutex,
> snd_power_wait on freed card pointer, ops function-pointer loads)
> and, after an rmmod, indirect calls into the unmapped module text
> from the release path.
>
> * Reproducer (attached: fwmr04.c, fwmr_test.c) and results on 7.3
>
> fwmr04.c creates a real pcm through snd_pcm_new() with a
> module-owned ops table, registers it with snd_device_register()
> (emu8000_pcm.c:637-651 shape) and implements the remove side as the
> verbatim emu8000_synth.c:92 snd_device_free(card, pcm); fwmr_test
> mr04pcm opens /dev/snd/pcmC1D0p through the real snd_pcm_open()
> path (ref_count=1), probes with PVERSION/INFO, holds the fd across
> the free, then issues INFO and closes.
>
> Results (7.3, 2 vCPU, KASAN): 16 slab-use-after-free reports --
> snd_pcm_release+0x3a/+0x4d (pcm_file->substream / substream->pcm),
> snd_pcm_get_state+0x12..0x6f via snd_pcm_common_ioctl,
> snd_power_ref_and_wait+0x87/+0x9d (pcm->card), snd_refcount_put,
> __wake_up_common (wake_up(&pcm->open_wait) on the freed pcm) --
> plus "ops->close reached on post-free release path" from the
> module. Reopen after the free returns -ENODEV as expected.
>
> * Suggested patch
>
> Give struct snd_pcm a kref: the card device entry holds the initial
> reference (dropped by snd_pcm_free()), each successful open takes a
> reference in snd_pcm_attach_substream() and each close puts it at
> the tail of snd_pcm_release() (after the last pcm use); the whole
> free body -- notify, private_free, preallocation cleanup, both
> stream frees and kfree(pcm) -- moves behind kref release. A
> concurrent snd_device_free() then cannot free a pcm that still has
> open files, which is exactly the audited defect. The complete
> patch against 7.3 (include/sound/pcm.h, sound/core/pcm.c,
> sound/core/pcm_native.c) follows (also kept as
> patch/0001-fw-mr04.patch); happy to rework per review.
>
> ---- patch begins ----
> ---
> --- a/include/sound/pcm.h
> +++ b/include/sound/pcm.h
> @@ -539,6 +539,7 @@
> char id[64];
> char name[80];
> struct snd_pcm_str streams[2];
> + struct kref ref; /* device + open files; frees the whole struct */
> struct mutex open_mutex;
> wait_queue_head_t open_wait;
> void *private_data;
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -717,6 +717,7 @@
> pcm->card = card;
> pcm->device = device;
> pcm->internal = internal;
> + kref_init(&pcm->ref);
> mutex_init(&pcm->open_mutex);
> init_waitqueue_head(&pcm->open_wait);
> INIT_LIST_HEAD(&pcm->list);
> @@ -849,10 +850,16 @@
> #define pcm_call_notify(pcm, call) do {} while (0)
> #endif
>
> -static int snd_pcm_free(struct snd_pcm *pcm)
> -{
> - if (!pcm)
> - return 0;
>
>
> +static void snd_pcm_free_up(struct kref *ref)
> +{
> + struct snd_pcm *pcm = container_of(ref, struct snd_pcm, ref);
> +
> if (!pcm->internal)
> pcm_call_notify(pcm, n_unregister);
> if (pcm->private_free)
> @@ -861,6 +868,13 @@
> snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
> snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
> kfree(pcm);
> +}
> +
> +static int snd_pcm_free(struct snd_pcm *pcm)
> +{
> + if (!pcm)
> + return 0;
> + kref_put(&pcm->ref, snd_pcm_free_up);
> return 0;
> }
>
> @@ -968,6 +982,7 @@
> substream->f_flags = file->f_flags;
> substream->pid = get_pid(task_pid(current));
> pstr->substream_opened++;
> + kref_get(&pcm->ref);
> *rsubstream = substream;
> #ifdef CONFIG_SND_PCM_XRUN_DEBUG
> substream->xrun_counter = 0;
> --- a/sound/core/pcm_native.c
> +++ b/sound/core/pcm_native.c
> @@ -2960,6 +2960,7 @@
>
> __error:
> module_put(pcm->card->module);
> + kref_put(&pcm->ref, snd_pcm_free_up);
> __error2:
> snd_card_file_remove(pcm->card, file);
> __error1:
> @@ -2988,6 +2989,7 @@
> wake_up(&pcm->open_wait);
> module_put(pcm->card->module);
> snd_card_file_remove(pcm->card, file);
> + kref_put(&pcm->ref, snd_pcm_free_up);
> return 0;
> }
>
> ---- patch ends ----
>
>
> Please keep me in Cc; I am not subscribed to the lists.
>
> Regards,
> Hanxiang Xu
> xuhx@xxxxxxxxxxx