From: sashiko-bot@kernel.org
To: 徐晗翔 <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] Re: sound: core: card->devices is unlocked across the codec-side targeted free and the card-side bulk free (double free / UAF; KASAN confirmed on 7.3)
Date: Wed, 25 Nov 2026 13:00:25 +0000 [thread overview]
Message-ID: <sashiko-outbox-233427@kernel.org> (raw)
In-Reply-To: <107cbd83.556.1a212b8f680.Coremail.xuhx@hust.edu.cn>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Critical] ALSA: core: infinite loop in snd_device_register_all() on
card registration
- [High] ALSA: core: missing Signed-off-by trailer in commit message
- [High] ALSA: core: implicit declaration of look_for_dev() in
snd_device_claim()
- [High] ALSA: core: unlinking devices in snd_device_claim*() breaks
teardown ordering
- [Medium] ALSA: core: snd_device_insert() reorders same-type devices on
claim and restore
- [Medium] ALSA: control: linear scan in snd_ctl_remove() causes O(N^2)
bulk teardown
--
commit ba38a9fa39aebc163045817c433435b040527cc8
Author: 徐晗翔 <xuhx@hust.edu.cn>
> sound: core: card->devices is unlocked across the codec-side targeted free and the card-side bulk free (double free / UAF; KASAN confirmed on 7.3)
>
> card->devices was maintained entirely without locks while three
> paths with pairwise-disjoint lock sets mutate it: the HDA codec
> driver's targeted snd_device_free() of its PCM/JACK nodes, the
> card-side bulk snd_device_disconnect_all()/snd_device_free_all(),
> and the register_all() traversal. A codec unbind racing the card
> teardown produced double frees and use-after-frees on the list, the
> snd_device and the freed PCM/chmap kcontrol.
>
> Serialize every list operation with a per-card device_list_lock,
> but never hold it across a device callback: a node is *claimed*
> (unlinked under the lock) while its dev_disconnect/dev_register/
> dev_free callbacks run, so a concurrent targeted free can no longer
> find and double free the same node, and no traversal can touch a
> node being freed. For the chmap kcontrol form, snd_ctl_remove() now
> verifies the instance is still linked on card->controls before
> touching it, so a stale pointer can no longer be list_del()'d and
> double kfree()d by the PCM teardown racing the CONTROL teardown.
[Severity: High]
The commit message is missing a Signed-off-by trailer.
> diff --git a/sound/core/control.c b/sound/core/control.c
> index 4199342d4ffe0..82e794c4f8cd1 100644
> --- a/sound/core/control.c
> +++ b/sound/core/control.c
> @@ -620,10 +620,21 @@ static inline int snd_ctl_remove_locked(struct snd_card *card,
> */
> int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
> {
> + struct snd_kcontrol *ctl;
> +
> if (!kcontrol)
> return 0;
> guard(rwsem_write)(&card->controls_rwsem);
> - return snd_ctl_remove_locked(card, kcontrol);
> + /* Paranoia against stale pointers: a caller (e.g. the PCM chmap
> + * teardown) may race the card's CONTROL teardown, which has
> + * already removed and freed this very kcontrol. Verify the
> + * instance is still linked on the card's control list before
> + * touching its contents. */
> + list_for_each_entry(ctl, &card->controls, list) {
> + if (ctl == kcontrol)
> + return snd_ctl_remove_locked(card, kcontrol);
> + }
> + return -ENOENT;
> }
> EXPORT_SYMBOL(snd_ctl_remove);
[Severity: Medium]
Does scanning card->controls from the head on every snd_ctl_remove() make
bulk control removal quadratic?
During ASoC topology unload, snd_soc_tplg_component_remove() calls
soc_tplg_remove_kcontrol() and soc_tplg_remove_widget() in reverse
insertion order (from the tail of card->controls backward), so each
snd_ctl_remove() walks all remaining preceding controls on card->controls
under card->controls_rwsem.
> diff --git a/sound/core/device.c b/sound/core/device.c
> index 5f2aa476d89b3..ad0369db8f8fe 100644
> --- a/sound/core/device.c
> +++ b/sound/core/device.c
> @@ -10,6 +10,82 @@
[ ... ]
> +/* claim (unlink) the node for @device_data, or return NULL if it is not
> + * on the list (e.g. concurrently claimed and freed by another path) */
> +static struct snd_device *snd_device_claim(struct snd_card *card,
> + void *device_data)
> +{
> + struct snd_device *dev;
> +
> + guard(spinlock_irqsave)(&card->device_list_lock);
> +
> + dev = look_for_dev(card, device_data);
> + if (dev)
> + list_del(&dev->list);
> + return dev;
> +}
[Severity: High]
Does sound/core/device.c fail to build because look_for_dev() is called in
snd_device_claim() before its static definition later in the file?
[ ... ]
> @@ -171,14 +246,19 @@ static int __snd_device_register(struct snd_device *dev)
> int snd_device_register(struct snd_card *card, void *device_data)
> {
> struct snd_device *dev;
> + int err;
>
> if (snd_BUG_ON(!card || !device_data))
> return -ENXIO;
> - dev = look_for_dev(card, device_data);
> - if (dev)
> - return __snd_device_register(dev);
> - snd_BUG();
> - return -ENXIO;
> + dev = snd_device_claim(card, device_data);
> + if (!dev) {
> + snd_BUG();
> + return -ENXIO;
> + }
> +
> + err = __snd_device_register(dev);
> + snd_device_insert(card, dev);
> + return err;
> }
> EXPORT_SYMBOL(snd_device_register);
[Severity: Medium]
Can claiming a device and re-inserting it via snd_device_insert() reverse
the teardown order of same-type devices?
snd_device_insert() places dev at the tail of its dev->type group. When a
device's .dev_register callback creates and registers a child device of the
same snd_device_type (such as OPL3's SNDRV_DEV_SEQUENCER registration
calling snd_opl3_seq_probe() -> snd_opl3_init_seq_oss() to register
opl3->oss_seq_dev), the child is inserted while the parent is unlinked, and
snd_device_insert() then places the parent after the child. Reverse-order
teardown in snd_device_disconnect_all() and snd_device_free_all() will then
disconnect and free the parent before the child.
> @@ -190,11 +270,26 @@ int snd_device_register_all(struct snd_card *card)
> {
> struct snd_device *dev;
> int err;
> -
> +
> if (snd_BUG_ON(!card))
> return -ENXIO;
> - list_for_each_entry(dev, &card->devices, list) {
> +
> + /* register one device at a time: claim it off the list while its
> + * dev_register callback runs. A dev_register callback may itself
> + * register another (not yet processed) device, so the nodes stay
> + * visible on the list between the callbacks. */
> + for (;;) {
> + scoped_guard(spinlock_irqsave, &card->device_list_lock) {
> + dev = list_first_entry_or_null(&card->devices,
> + struct snd_device, list);
> + if (dev)
> + list_del(&dev->list);
> + }
> + if (!dev)
> + break;
> +
> err = __snd_device_register(dev);
> + snd_device_insert(card, dev);
> if (err < 0)
> return err;
> }
[Severity: Critical]
Does snd_device_register_all() loop forever whenever card->devices is
non-empty?
Each iteration pops the first device from card->devices, calls
__snd_device_register(dev), and immediately puts dev back onto
card->devices via snd_device_insert(card, dev). Because card->devices
never becomes empty and __snd_device_register() returns 0 once dev->state
is SNDRV_DEV_REGISTERED, the loop spins endlessly on the lowest-type
device and hangs snd_card_register().
> @@ -207,12 +302,23 @@ int snd_device_register_all(struct snd_card *card)
> */
> void snd_device_disconnect_all(struct snd_card *card)
> {
> - struct snd_device *dev;
> + LIST_HEAD(claimed);
> + struct snd_device *dev, *next;
>
> if (snd_BUG_ON(!card))
> return;
> - list_for_each_entry_reverse(dev, &card->devices, list)
> +
> + /* claim every node, then run the disconnect callbacks with the
> + * list lock released; a claimed node is invisible to concurrent
> + * targeted disconnect/free calls, so no callback races a free of
> + * its own device */
> + snd_device_claim_all(card, &claimed);
> + list_for_each_entry_safe(dev, next, &claimed, list)
> __snd_device_disconnect(dev);
> +
> + /* put the disconnected devices back in their original order; the
> + * card free path walks the list again */
> + snd_device_restore_all(card, &claimed);
> }
>
> /*
> @@ -221,19 +327,28 @@ void snd_device_disconnect_all(struct snd_card *card)
> */
> void snd_device_free_all(struct snd_card *card)
> {
> + LIST_HEAD(claimed);
> struct snd_device *dev, *next;
>
> if (snd_BUG_ON(!card))
> return;
> - list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
> - /* exception: free ctl and lowlevel stuff later */
> +
> + /* first pass: free everything but ctl and lowlevel stuff later;
> + * the claimed list is in reverse card-list order, i.e. the original
> + * free order */
> + snd_device_claim_all(card, &claimed);
> + list_for_each_entry_safe(dev, next, &claimed, list) {
> if (dev->type == SNDRV_DEV_CONTROL ||
> dev->type == SNDRV_DEV_LOWLEVEL)
> continue;
> + list_del(&dev->list);
> __snd_device_free(dev);
> }
>
> - /* free all */
> - list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
> + /* free all; the claimed nodes are off the card list, so a
> + * concurrent targeted free cannot find them again */
> + list_for_each_entry_safe(dev, next, &claimed, list) {
> + list_del(&dev->list);
> __snd_device_free(dev);
> + }
> }
[Severity: High]
Does moving all devices onto a local claimed list break nested and
concurrent snd_device_free() callers that free backing memory as soon as
snd_device_free() returns?
During single-threaded teardown, snd_device_disconnect_all() and
snd_device_free_all() empty card->devices onto claimed before running any
callbacks. When a callback performs a nested snd_device_free() on a child
device, snd_device_claim() cannot find the child on card->devices and
returns NULL:
- Disconnecting or freeing a SNDRV_DEV_SEQUENCER device calls
snd_emu8000_remove(), which calls snd_device_free(dev->card, hw->pcm)
and immediately frees hw->emu and hw->memhdr while hw->pcm is still on
claimed and runs its callbacks later against the freed structures.
- In snd_i2c_bus_free(), freeing a master bus loops on
while (!list_empty(&bus->buses)) calling snd_device_free(bus->card,
slave) to unlink each slave; because slave is on claimed instead of
card->devices, snd_device_free() is a no-op and the loop spins forever.
The same premature return happens in the concurrent HDA codec unbind path
that this patch targets:
CPU0 CPU1
snd_card_disconnect()
snd_device_disconnect_all()
snd_device_claim_all(..., &claimed)
snd_hda_codec_cleanup_for_unbind()
codec_release_pcms()
snd_device_free(..., pcm->pcm)
snd_device_claim() -> NULL
kfree(pcm)
snd_device_restore_all()
snd_card_do_free()
snd_device_free_all()
__snd_device_free()
...
azx_pcm_free()
apcm->info->pcm = NULL; <-- use-after-free
Conversely, if CPU1 claims a device in snd_device_free() (or
snd_device_disconnect() / snd_device_register()) just before CPU0 calls
snd_device_claim_all() in snd_device_free_all(), CPU0 does not wait for the
in-flight callback before freeing SNDRV_DEV_LOWLEVEL and struct snd_card.
--
Sashiko AI review · https://sashiko.dev/#/patchset/107cbd83.556.1a212b8f680.Coremail.xuhx@hust.edu.cn?part=1
prev parent reply other threads:[~2026-11-25 13:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 4:43 sound: core: card->devices is unlocked across the codec-side targeted free and the card-side bulk free (double free / UAF; KASAN confirmed on 7.3) 徐晗翔
2026-11-25 12:40 ` [PATCH] " 徐晗翔
2026-11-25 13:00 ` sashiko-bot [this message]
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=sashiko-outbox-233427@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--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