ALSA: core: serialize card disconnect/free and quiesce codecs before device release (UAF)

From: 徐晗翔

Date: Tue Dec 01 2026 - 08:13:06 EST


The card lifecycle protocol treats card->shutdown purely as a re-entry
guard and as an "already disconnected" indicator, but nothing makes a
disconnect *complete* before others act on the flag, and nothing pins
the card while a disconnect is in flight. Three consequences on
mainline 7.3, re-checked and still present on 7.4.0-rc5, all reachable
from plain admin-level sysfs operations (reconfig write x PCI unbind),
all verified in a KASAN QEMU guest:

1. Hang + card leak: snd_hda_lock_devices() reuses card->shutdown as
a reversible pseudo-lock while a reconfig detaches the codec
driver (reproduced identically on 7.4-rc5). A PCI unbind that enters snd_card_free() inside that
window gets snd_card_disconnect() early-returned at the
"shutdown" check, skips the entire disconnect phase (minors stay
registered, snd_cards[] stays populated, no device_del), and then
blocks FOREVER in snd_card_free()'s wait_for_completion() - the
completion is only fired from snd_card_do_free(), which requires
the card reference count to reach zero, which cannot happen while
the minors are still registered. Observed with a sysrq-t dump:
task in state D with "__wait_for_common <- snd_card_free+0xc8 <-
azx_remove+0x8c <- pci_device_remove <- unbind_store", while
/proc/asound/cards, /sys/class/sound/card0 and /dev/snd/* all
still listed the half-dead card. The reconfig writers then spin
in "The codec is being used, can't reconfigure." (678 messages in
the captured run) and the console flood wedges the guest.

2. Free-phase UAF: when a disconnect IS in flight (verb-timeout
fallback path calls snd_card_disconnect() from jackpoll/DRM-
notify context), a concurrent snd_card_free() early-returns from
the disconnect and its put_device() drops the last reference the
moment the disconnecting task deletes the last minor: the
release (snd_card_do_free -> snd_device_free_all -> list_del +
kfree(dev), then kfree(card)) nests into the deleting task's own
call chain and races the still-running disconnect loop over
card->devices. Hammering reconfig x codec unbind/bind x PCI
churn x PCM open/close produced 6 slab-use-after-free reports in
hda_free_jack_priv (free_all's JACK pass writing the codec's
already-freed jack table) and a wild-pointer
"Read of size 4 at addr dead000000000118" in
snd_device_register_all <- snd_card_register (reported in its own
mail).

3. Release-order window for the codec quiesce: cancel of the codec
poll work and the jack-table release only happen when the codec
device is freed (auto-detach inside snd_device_free_all's CODEC
pass) - after the JACK/HWDEP/PCM devices were already freed in
the same pass, so an in-flight poll work item or a concurrent
HDMI hotplug notification can still walk the just-freed objects.

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. The
audited files (sound/core/init.c, sound/hda/common/codec.c,
sound/hda/controllers/intel.c, sound/hda/core/bus.c,
drivers/base/core.c, drivers/base/bus.c) are byte-identical between
the tested trees and the report baseline. The relevant protocol
(early return at init.c:503 / rc5:497, put_device at init.c:631 /
rc5:625, wait_for_completion at init.c:666 / rc5:660, pseudo-lock at
codec.c:1754->1761 / 1785->1791) is unchanged for many years and
unchanged in 7.4-rc5; line numbers below are from 7.3 with the rc5
anchors in parentheses where they drifted.

* Mechanism

The reference-count ledger
(Line numbers below are from 7.3; all rc5 anchors are within a dozen
lines of these - the functions, logic and control flow are unchanged,
verified by direct comparison of the two trees; rc5-specific anchors
are given inline in the re-check note above.)
that governs when snd_card_do_free() may
run: card->card_dev starts at 1 (device_initialize); every
registered minor (ctl/hwdep/pcm devices are children of card->card_dev,
sound/core/init.c:137) holds one parent reference taken by
device_add(); every open ALSA file holds one (snd_card_file_add,
init.c:1111). References are returned by device_del() during the
disconnect phase (snd_unregister_device, sound/core/sound.c:315),
by file closes (snd_card_file_remove), and by the single
put_device() in snd_card_free_when_closed() (init.c:631 / rc5:625). The
release (release_card_device -> snd_card_do_free ->
snd_device_free_all + kfree(card)) fires synchronously in whatever
task drops the last reference.

Given that ledger:

- normal sequential teardown works: disconnect deletes all minors
(count 1+C -> 1), put_device reaches zero, do_free runs;

- an in-flight disconnect (call it A) plus a concurrent
snd_card_free (B) that early-returns and put_device()s leaves
count = C (+F); A's minor deletions then cross zero *inside* A's
dev_disconnect callback, so snd_card_do_free() nests into the
callback's own stack and frees the very snd_device whose
dev_disconnect is executing; A continues on freed objects
(writes dev->state at sound/core/device.c:65 after the kfree,
keeps iterating freed list nodes) and later touches freed card
fields at init.c:540-560;

- with shutdown set by the pseudo-lock instead, B's disconnect is
skipped entirely: the minors are never deleted, B's put_device
cannot reach zero, and B waits forever (form 1 above).

The pseudo-lock itself (snd_hda_lock_devices,
sound/hda/common/codec.c:1746-1773) sets card->shutdown = 1 for the
duration of snd_hda_codec_reset()'s device_release_driver() and
clears it afterwards (codec.c:1785) - a *reversible* state driving
an *irreversible* release decision in snd_card_disconnect()
(init.c:503-505 / rc5:497-499).

* Trigger model

root/admin-level sysfs operations only: one writer looping
"echo 1 > /sys/class/sound/hwC0D0/reconfig", another doing
"echo <bdf> > /sys/bus/pci/drivers/snd_hda_intel/unbind", with no
ALSA files open. Reproductions on 7.3: hang + leak + EBUSY storm
(form 1, one-shot with sysrq dump), free-phase UAF family (form 2),
and four independent full-guest freezes at 33-48s across plan
variants (the post-hang EBUSY spin floods dmesg and livelocks the
console). Consequence: permanent uninterruptible hang of the PCI
removal path, permanent card/chip/IRQ/DMA leak with a still-open
user-visible card, console livelock (system-level DoS); form 2 adds
kernel memory corruption.

* Reproducer and results on 7.3 (still present as of 7.4-rc5)

Pure userspace (no test modules): the hangs, the sysrq-t task dump
with the stack quoted above, the leaked-card observations and the
EBUSY flood counts are captured in full serial logs; the form-2 UAF
reports are quoted in the companion mail for the register re-entry
and were captured during the same hammer series. Full serial logs
retained; available on request.

* Fix (full 3-patch series below)

1/3: core: serialize disconnect and pin the card during teardown.
New card->disconnect_mutex held for the whole
snd_card_disconnect() body; a concurrent caller (including
snd_card_free()'s disconnect) now WAITS for the in-flight
disconnect to fully finish instead of acting on a half-state;
disconnect pins the card with get_device()/put_device() so the
teardown body cannot race snd_card_do_free().

2/3: hda: split the lock_devices pseudo-lock from card->shutdown.
New card->pseudo_locked flag for snd_hda_lock_devices()/
snd_hda_unlock_devices(); snd_card_disconnect() no longer
mistakes the reversible pseudo-lock for a completed teardown,
which removes the skip-and-hang form entirely.

3/3: hda: quiesce the codec at codec-device disconnect. A
.dev_disconnect callback for the codec snd_device that cancels
the jackpoll work, waits out in-flight PCM users and detaches
the codec driver (running the standard
hda_codec_driver_remove() cleanup: jack table release, control
list removal, PCM release). All of that now happens in the
disconnect phase - while every other device is still alive -
instead of in the release phase, where snd_device_free_all()
frees JACK/HWDEP/PCM and codec devices in one pass.

The patches are written against 7.3 and reviewed by inspection;
they have not been runtime-validated (the reported reproduction
used the unpatched kernel). Happy to test any counter-proposal.

* Patch

[PATCH 1/3] ALSA: core: serialize card disconnect and pin the card
during teardown

--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -139,6 +139,8 @@
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 */
+ bool pseudo_locked; /* hda pseudo device lock, not shutdown */
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
@@ -499,24 +500,37 @@
if (!card)
return;

- scoped_guard(spinlock, &card->files_lock) {
- if (card->shutdown)
- return;
- card->shutdown = 1;
-
- /* replace file->f_op with special dummy operations */
- list_for_each_entry(mfile, &card->files_list, list) {
- /* it's critical part, use endless loop */
- /* we have no room to fail */
- mfile->disconnected_f_op = mfile->file->f_op;
-
- scoped_guard(spinlock, &shutdown_lock)
- list_add(&mfile->shutdown_list, &shutdown_files);
-
- mfile->file->f_op = &snd_shutdown_f_ops;
- fops_get(mfile->file->f_op);
- }
- }

+ mutex_lock(&card->disconnect_mutex);
+ get_device(&card->card_dev);
+
+ spin_lock(&card->files_lock);
+ if (card->shutdown) {
+ /* already fully disconnected by a previous call that this
+ * call waited on via disconnect_mutex */
+ spin_unlock(&card->files_lock);
+ put_device(&card->card_dev);
+ mutex_unlock(&card->disconnect_mutex);
+ return;
+ }
+ card->shutdown = 1;
+
+ /* replace file->f_op with special dummy operations */
+ list_for_each_entry(mfile, &card->files_list, list) {
+ /* it's critical part, use endless loop */
+ /* we have no room to fail */
+ mfile->disconnected_f_op = mfile->file->f_op;
+
+ spin_lock(&shutdown_lock);
+ list_add(&mfile->shutdown_list, &shutdown_files);
+ spin_unlock(&shutdown_lock);
+
+ mfile->file->f_op = &snd_shutdown_f_ops;
+ fops_get(mfile->file->f_op);
+ }
+ spin_unlock(&card->files_lock);

#ifdef CONFIG_PM
/* wake up sleepers here before other callbacks for avoiding potential
@@ -558,6 +572,9 @@
}

snd_power_sync_ref(card);
+
+ put_device(&card->card_dev); /* drop the teardown pin */
+ mutex_unlock(&card->disconnect_mutex);
}
EXPORT_SYMBOL(snd_card_disconnect);


--
2.47.1


[PATCH 2/3] ALSA: hda: split the lock_devices pseudo-lock from
card->shutdown

--- a/sound/hda/common/codec.c
+++ b/sound/hda/common/codec.c
@@ -1751,7 +1751,9 @@
guard(spinlock)(&card->files_lock);
if (card->shutdown)
return -EINVAL;
- card->shutdown = 1;
+ if (card->pseudo_locked)
+ return -EINVAL;
+ card->pseudo_locked = 1;
if (!list_empty(&card->ctl_files))
goto err_clear;

@@ -1768,7 +1770,7 @@
return 0;

err_clear:
- card->shutdown = 0;
+ card->pseudo_locked = 0;
return -EINVAL;
}
EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
@@ -1782,7 +1784,7 @@
struct snd_card *card = bus->card;

guard(spinlock)(&card->files_lock);
- card->shutdown = 0;
+ card->pseudo_locked = 0;
}
EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);


--
2.47.1


[PATCH 3/3] ALSA: hda: quiesce the codec at codec-device disconnect

--- a/sound/hda/common/codec.c
+++ b/sound/hda/common/codec.c
@@ -826,6 +826,26 @@
return 0;
}

+static int snd_hda_codec_dev_disconnect(struct snd_device *device)
+{
+ struct hda_codec *codec = device->device_data;
+ cancel_delayed_work_sync(&codec->jackpoll_work);
+ snd_refcount_sync(&codec->pcm_ref);
+ device_release_driver(hda_codec_dev(codec));
+ return 0;
+}
+
/**
* snd_hda_codec_unregister - Unregister specified codec device
* @codec: codec device to unregister
@@ -970,6 +990,7 @@
int err;
static const struct snd_device_ops dev_ops = {
.dev_register = snd_hda_codec_dev_register,
+ .dev_disconnect = snd_hda_codec_dev_disconnect,
.dev_free = snd_hda_codec_dev_free,
};


--
2.47.1

Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx