[PATCH] ALSA: core: protect card->devices against concurrent disconnect and free

From: Hanxiang Xu

Date: Tue Dec 01 2026 - 22:15:20 EST


Currently the snd_device nodes on card->devices are created, walked,
unlinked and freed with no lock at all. That is only safe while every
mutation happens inside the single-threaded snd_card_free() sequence;
it breaks once a loadable sub-driver (e.g. snd_emu10k1_synth) is
removed independently (rmmod or unbind) while the card is concurrently
being disconnected by a hot-unplug: snd_emux_free() unlinks and frees
its own snd_device nodes while snd_device_disconnect_all() is iterating
the same list, resulting in use-after-free of struct snd_device and
struct snd_hwdep (dynamically confirmed on v7.4-rc5).

Serialize the list with card->devices_mutex. snd_device_free() and
snd_device_disconnect_all()/snd_device_free_all() unlink nodes under
the lock and run the dev_disconnect/dev_free callbacks with the lock
dropped, since those callbacks may nest snd_device_free() for sibling
nodes (sub-driver remove cascade).

Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---

include/sound/core.h | 1 +
sound/core/device.c | 74 +++++++++++++++++++++++++++++++++++++++-------------
sound/core/init.c | 1 +
3 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/include/sound/core.h b/include/sound/core.h
index d658496..d7eb12b 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -117,6 +117,7 @@ struct snd_card {
void (*private_free) (struct snd_card *card); /* callback for freeing of
private data */
struct list_head devices; /* devices */
+ struct mutex devices_mutex; /* protects the devices list */

struct device *ctl_dev; /* control device */
unsigned int last_numid; /* last used numeric ID */
diff --git a/sound/core/device.c b/sound/core/device.c
index 5f2aa47..8aca998 100644
--- a/sound/core/device.c
+++ b/sound/core/device.c
@@ -45,6 +45,7 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
dev->ops = ops;

/* insert the entry in an incrementally sorted list */
+ guard(mutex)(&card->devices_mutex);
list_for_each_prev(p, &card->devices) {
struct snd_device *pdev = list_entry(p, struct snd_device, list);
if ((unsigned int)pdev->type <= (unsigned int)type)
@@ -66,11 +67,9 @@ static void __snd_device_disconnect(struct snd_device *dev)
}
}

+/* the node must already be unlinked from card->devices */
static void __snd_device_free(struct snd_device *dev)
{
- /* unlink */
- list_del(&dev->list);
-
__snd_device_disconnect(dev);
if (dev->ops->dev_free) {
if (dev->ops->dev_free(dev))
@@ -109,6 +108,7 @@ void snd_device_disconnect(struct snd_card *card, void *device_data)

if (snd_BUG_ON(!card || !device_data))
return;
+ guard(mutex)(&card->devices_mutex);
dev = look_for_dev(card, device_data);
if (dev)
__snd_device_disconnect(dev);
@@ -133,12 +133,23 @@ void snd_device_free(struct snd_card *card, void *device_data)

if (snd_BUG_ON(!card || !device_data))
return;
+ mutex_lock(&card->devices_mutex);
dev = look_for_dev(card, device_data);
- if (dev)
- __snd_device_free(dev);
- else
+ if (!dev) {
+ mutex_unlock(&card->devices_mutex);
dev_dbg(card->dev, "device free %p (from %pS), not found\n",
device_data, __builtin_return_address(0));
+ return;
+ }
+ list_del(&dev->list);
+ mutex_unlock(&card->devices_mutex);
+
+ __snd_device_disconnect(dev);
+ if (dev->ops->dev_free) {
+ if (dev->ops->dev_free(dev))
+ dev_err(dev->card->dev, "device free failure\n");
+ }
+ kfree(dev);
}
EXPORT_SYMBOL(snd_device_free);

@@ -174,6 +185,7 @@ int snd_device_register(struct snd_card *card, void *device_data)

if (snd_BUG_ON(!card || !device_data))
return -ENXIO;
+ guard(mutex)(&card->devices_mutex);
dev = look_for_dev(card, device_data);
if (dev)
return __snd_device_register(dev);
@@ -193,6 +205,7 @@ int snd_device_register_all(struct snd_card *card)

if (snd_BUG_ON(!card))
return -ENXIO;
+ guard(mutex)(&card->devices_mutex);
list_for_each_entry(dev, &card->devices, list) {
err = __snd_device_register(dev);
if (err < 0)
@@ -211,8 +224,23 @@ void snd_device_disconnect_all(struct snd_card *card)

if (snd_BUG_ON(!card))
return;
- list_for_each_entry_reverse(dev, &card->devices, list)
+
+ for (;;) {
+ struct snd_device *iter;
+
+ dev = NULL;
+ mutex_lock(&card->devices_mutex);
+ list_for_each_entry_reverse(iter, &card->devices, list) {
+ if (iter->state == SNDRV_DEV_REGISTERED) {
+ dev = iter;
+ break;
+ }
+ }
+ mutex_unlock(&card->devices_mutex);
+ if (!dev)
+ return;
__snd_device_disconnect(dev);
+ }
}

/*
@@ -221,19 +249,29 @@ void snd_device_disconnect_all(struct snd_card *card)
*/
void snd_device_free_all(struct snd_card *card)
{
- struct snd_device *dev, *next;
+ struct snd_device *dev, *iter;
+ int pass;

if (snd_BUG_ON(!card))
return;
- list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
- /* exception: free ctl and lowlevel stuff later */
- if (dev->type == SNDRV_DEV_CONTROL ||
- dev->type == SNDRV_DEV_LOWLEVEL)
- continue;
- __snd_device_free(dev);
- }

- /* free all */
- list_for_each_entry_safe_reverse(dev, next, &card->devices, list)
- __snd_device_free(dev);
+ for (pass = 0; pass < 2; pass++) {
+ for (;;) {
+ dev = NULL;
+ mutex_lock(&card->devices_mutex);
+ list_for_each_entry_reverse(iter, &card->devices, list) {
+ if (pass == 0 && (iter->type == SNDRV_DEV_CONTROL ||
+ iter->type == SNDRV_DEV_LOWLEVEL))
+ continue;
+ dev = iter;
+ break;
+ }
+ if (dev)
+ list_del(&dev->list);
+ mutex_unlock(&card->devices_mutex);
+ if (!dev)
+ break;
+ __snd_device_free(dev);
+ }
+ }
}
diff --git a/sound/core/init.c b/sound/core/init.c
index d518cb8..04862e5 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -309,6 +309,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
WARN_ON(IS_MODULE(CONFIG_SND) && !module);
card->module = module;
INIT_LIST_HEAD(&card->devices);
+ mutex_init(&card->devices_mutex);
init_rwsem(&card->controls_rwsem);
rwlock_init(&card->controls_rwlock);
INIT_LIST_HEAD(&card->controls);
--
2.43.0