ALSA: core: pin and serialize snd_card_register() against disconnect/free (UAF)
From: 徐晗翔
Date: Tue Dec 01 2026 - 08:14:46 EST
snd_card_register() is re-entered on an already-registered card by the
HDA reconfig path (reconfig_codec() -> snd_card_register(),
sound/hda/common/sysfs.c) and by hda_codec_driver_probe()
(sound/hda/common/bind.c: "if (!codec->bus->bus_probing &&
codec->card->registered) snd_card_register(codec->card)"). On that
path it performs four entirely unlocked operations:
- reads card->registered and, on the !registered branch, calls
device_add(&card->card_dev) (init.c:890-894) - device_add() does
not check the "dead" flag that device_del()/kill_device() set, so
this fully re-registers a card that device_del() already tore out
of sysfs;
- walks card->devices with snd_device_register_all() (init.c:906 / rc5:900 ->
sound/core/device.c: list_for_each_entry, dereferencing
dev->state and calling dev->ops->dev_register);
- re-inserts snd_cards[card->number] = card under snd_card_mutex
(init.c:928 / rc5:922), which can land AFTER snd_card_disconnect() cleared
the same slot (init.c:556) - the two critical sections have no
ordering;
- calls snd_info_card_register(card) (init.c:930 / rc5:926), touching
card->id / card->proc_root / card->proc_root_link.
Concurrently, snd_card_free() -> snd_card_disconnect() + put_device()
tears the same card down: device_del, minor removals,
snd_device_free_all() (list_del + kfree of every snd_device) and
finally kfree(card). No lock, reference or barrier is shared between
the register side and the teardown side. The release fires
synchronously in whichever task drops the last card reference - the
register side holds no reference - so the two traversals (register vs
free_all) and the register-side late writes all race freed memory.
Verified on mainline 7.3 with a KASAN kernel, QEMU/KVM x86_64 guest
and an emulated Intel HDA controller, hammering sysfs reconfig x
PCI unbind/bind: "BUG: KASAN: wild-memory-access in
snd_device_register_all+0x51/0xb0, Read of size 4 at addr
dead000000000118" (a LIST_POISON-derived pointer) with the call chain
"snd_device_register_all <- snd_card_register <- azx_probe_work",
followed within the same second by
"Oops: general protection fault, probably for non-canonical address
0xdead000000000118" with "RIP: 0010:snd_device_register_all+0x51/0xb0"
- i.e. the collision escalates from a UAF read to a hard kernel Oops
(the task died; the guest survived because panic_on_oops=0). The
register-side traversal needs no privileges beyond admin-level sysfs
access to trigger; consequence: kernel Oops / memory corruption.
The detailed description:
* Test environment / affected version (still present as of 7.4-rc5)
Verified on Linux 7.3.0 (gcc 13.3.0, GNU ld 2.42) #3 SMP
PREEMPT_DYNAMIC and RE-CHECKED on Linux 7.4.0-rc5 (#1, 2026-12-01) -
both vanilla mainline trees, unmodified sources, KASAN enabled,
QEMU/KVM x86_64 guest with -device intel-hda + hda-duplex,
CONFIG_SND_HDA_RECONFIG=y. The audited files
(sound/core/init.c, sound/core/device.c,
sound/hda/common/sysfs.c, sound/hda/common/bind.c,
drivers/base/core.c) are byte-identical between the tested tree and
the report baseline; line numbers below are from 7.3, with the
7.4-rc5 anchors in parentheses where they drifted (device_add is at
drivers/base/core.c:3645 in rc5, kill_device at 3890/3930 - logic
unchanged). Re-check result on 7.4-rc5: snd_card_register() and
snd_device_register_all() are unchanged, device_add() still has no
"dead" check, and the same hammering on a 7.4-rc5 KASAN guest
produced free-phase use-after-free reports in
snd_device_free_all()/__snd_device_free() ending in an
"Oops: stack segment" - the lifecycle gap of the companion
card-lifecycle mail is confirmed on rc5 as well (the targeted run to
land the exact register_all wild-pointer form on rc5 is queued; the
7.3 capture above remains the reference evidence for that form).
* Mechanism
card->devices has no lock of its own; its safety rests entirely on
the create/register/disconnect/free phases not overlapping. The
HDA reconfig path breaks that: reconfig_codec() runs
device_reprobe() (creating new snd_devices with snd_device_new()
mid-list) and then calls snd_card_register() on the live card,
while an unrelated PCI unbind can be anywhere inside
snd_card_free()'s disconnect or release phase for the same card.
Concretely:
- snd_device_register_all() (sound/core/device.c:189-202) reads
dev->list.next and dev->state for every entry; the release side
does list_del() + kfree(dev) per entry (device.c:222-239) - a
register-side traversal that lands on a deleted (poisoned) or
freed node is the captured wild-memory-access/Oops;
- card->registered: snd_card_disconnect() clears it after
device_del (init.c:551 / rc5:545); snd_card_register() reading false
afterwards calls device_add(&card->card_dev) on a card whose
teardown is already running - device_add() does not check the
kill_device() "dead" flag (drivers/base/core.c:3643-3724 vs
3888-3904), so sysfs/kobject/PM registrations are rebuilt for
an object that kfree(card) then releases;
- snd_cards[]: the "already registered" check and the re-insert
(init.c:910/928) sit in their own snd_card_mutex section while
the disconnect side clears the slot in its own (init.c:556) -
re-insert after clear leaves a permanent dangling global entry,
and every later snd_card_ref() (init.c:389-399) does
get_device(&card->card_dev) on it: either resurrecting a
zero-count card that kfree(card) releases anyway (callers left
holding references into freed memory) or atomically updating a
freed object;
- snd_info_card_register(card) (sound/core/info.c:501-519) reads
card->id / card->proc_root / card->proc_root_link on the same
racing object.
* Trigger model
root/admin-level sysfs: one writer looping
"echo 1 > /sys/class/sound/hwC0D0/reconfig", another doing PCI
unbind/bind rounds on the HDA controller, no ALSA files open. The
collision windows are the whole disconnect/free duration (multiple
device_del calls plus synchronize_irq), well over a millisecond per
round. Consequence: kernel Oops (observed), poisoned-list
dereference, dangling global table entry.
* Reproducer and results on 7.3 (still present as of 7.4-rc5)
Pure userspace hammering (reconfig x unbind/bind). Captured: the
wild-memory-access KASAN report and the follow-up GPF Oops quoted
above (both in full serial logs, retained); plus independent full
guest freezes from the same interleaving family (the companion
lifecycle race mail covers that face with a sysrq dump).
* Fix (full patch below)
Pin the card (get_device/put_device) and hold a new
card->disconnect_mutex for the whole snd_card_register() body, with
an explicit bail-out when card->shutdown is set (the check sits
under the same mutex as the disconnect-side flag write, so the two
are ordered): the register side can then never walk card->devices
against the release phase, never re-add a card that is going away,
and never resurrect snd_cards[] for a dying card. The mutex is the
same one the companion card-lifecycle series introduces; if that
series is applied first, the two hunks touching the mutex
field/init here are redundant and only the snd_card_register() hunk
is needed. The patch below is self-contained.
The patch is regenerated against 7.4-rc5 (patch --dry-run passes
cleanly) and reviewed by inspection; it has not been
runtime-validated (the reported reproduction used the unpatched
kernel). Happy to test any counter-proposal.
* Patch
snd_card_register() re-entry on a live card (HDA reconfig path and
hda_codec_driver_probe()) walks card->devices, re-reads/writes
card->registered, re-inserts snd_cards[] and registers info entries
with no serialization against a concurrent snd_card_disconnect()/
snd_card_free(). The release fires as soon as the last card
reference drops, so the release-phase list_del()+kfree() of the
snd_devices races the register-side traversal: observed as
"wild-memory-access in snd_device_register_all" reading a
LIST_POISON-derived pointer (dead000000000118) and escalating to a
general protection fault Oops in the same second. The registered
race also allows device_add() on a card that device_del() already
marked dead (device_add() checks nothing), and a late snd_cards[]
re-insert leaves a dangling global entry that later snd_card_ref()
callers dereference.
Pin the card and serialize the whole function with a new
card->disconnect_mutex (also held by snd_card_disconnect(); see the
companion lifecycle series), bail out when card->shutdown is set, and
drop our reference on every exit.
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -139,6 +139,7 @@
state */
spinlock_t files_lock; /* lock the files for this card */
int shutdown; /* this card is going down */
+ struct mutex disconnect_mutex; /* serializes disconnect/free/register */
struct completion *release_completion;
struct device *dev; /* device assigned to this card */
struct device card_dev; /* cardX object for sysfs */
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -324,6 +324,7 @@
xa_init(&card->ctl_hash);
#endif
spin_lock_init(&card->files_lock);
+ mutex_init(&card->disconnect_mutex);
INIT_LIST_HEAD(&card->files_list);
mutex_init(&card->memory_mutex);
#ifdef CONFIG_PM
@@ -887,10 +888,23 @@
if (snd_BUG_ON(!card))
return -EINVAL;
+ /* pin the card and serialize against disconnect/free: the re-entry
+ * below must neither walk card->devices against the release phase
+ * nor resurrect snd_cards[] for a card that is going away */
+ get_device(&card->card_dev);
+ guard(mutex)(&card->disconnect_mutex);
+
+ if (card->shutdown) {
+ put_device(&card->card_dev);
+ return -ENODEV;
+ }
+
if (!card->registered) {
err = device_add(&card->card_dev);
- if (err < 0)
+ if (err < 0) {
+ put_device(&card->card_dev);
return err;
+ }
card->registered = true;
} else {
if (card->managed)
@@ -899,17 +913,23 @@
if (card->managed) {
err = devm_add_action(card->dev, trigger_card_free, card);
- if (err < 0)
+ if (err < 0) {
+ put_device(&card->card_dev);
return err;
+ }
}
err = snd_device_register_all(card);
- if (err < 0)
+ if (err < 0) {
+ put_device(&card->card_dev);
return err;
+ }
scoped_guard(mutex, &snd_card_mutex) {
if (snd_cards[card->number]) {
/* already registered */
- return snd_info_card_register(card); /* register pending info */
+ err = snd_info_card_register(card); /* register pending info */
+ put_device(&card->card_dev);
+ return err < 0 ? err : 0;
}
if (*card->id) {
/* make a unique id name from the given string */
@@ -928,13 +948,16 @@
snd_cards[card->number] = card;
}
err = snd_info_card_register(card);
- if (err < 0)
+ if (err < 0) {
+ put_device(&card->card_dev);
return err;
+ }
#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
if (snd_mixer_oss_notify_callback)
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
#endif
+ put_device(&card->card_dev);
return 0;
}
EXPORT_SYMBOL(snd_card_register);
--
2.47.1
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx