* [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free
@ 2026-12-02 2:12 Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02 2:12 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel
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@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free
2026-12-02 2:12 [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free Hanxiang Xu
@ 2026-12-02 3:23 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 3:23 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: core: self-deadlock on devices_mutex in
snd_device_register_all()
- [Medium] ALSA: core: use-after-free race in
snd_device_disconnect_all()
- [Low] ALSA: core: duplicate __snd_device_free() logic in
snd_device_free()
--
commit be22969e90c9ca2d2e7f9f8946b975be9cdbafcd
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: core: protect card->devices against concurrent disconnect and free
This commit adds card->devices_mutex to serialize manipulations and walks of
the card->devices list against concurrent sub-driver removal and card
disconnection.
> diff --git a/sound/core/device.c b/sound/core/device.c
> index 5f2aa476d89b3..8aca99857582b 100644
> --- a/sound/core/device.c
> +++ b/sound/core/device.c
[ ... ]
> @@ -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))
[ ... ]
> @@ -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);
[Severity: Low]
This isn't a bug, but since __snd_device_free() now expects an already
unlinked node, snd_device_free() can still call __snd_device_free(dev)
after dropping card->devices_mutex instead of duplicating its body.
> @@ -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)
[Severity: High]
Does holding card->devices_mutex across __snd_device_register() cause a
self-deadlock when a dev_register callback creates or registers a child
device on the same card?
snd_card_register()
snd_device_register_all()
mutex_lock(&card->devices_mutex)
__snd_device_register()
snd_pcm_dev_register()
snd_pcm_timer_init()
snd_timer_new()
snd_device_new()
mutex_lock(&card->devices_mutex) <-- deadlocks
With CONFIG_SND_PCM_TIMER enabled, registering any card with a PCM device
hits this path (as does snd_rawmidi_dev_register() -> snd_seq_device_new()),
hanging card probe and mutating card->devices while
snd_device_register_all() is iterating it.
> @@ -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);
> + }
> }
[Severity: Medium]
Can dev be freed by a concurrent snd_device_free() after
snd_device_disconnect_all() drops card->devices_mutex?
CPU0 CPU1
snd_device_disconnect_all()
mutex_lock(&card->devices_mutex)
dev = iter
mutex_unlock(&card->devices_mutex)
snd_device_free()
mutex_lock(&card->devices_mutex)
list_del(&dev->list)
mutex_unlock(&card->devices_mutex)
__snd_device_disconnect(dev)
dev->ops->dev_free(dev)
kfree(dev)
__snd_device_disconnect(dev) <-- use-after-free
Unlike snd_device_free_all(), snd_device_disconnect_all() leaves dev on
card->devices with dev->state still set to SNDRV_DEV_REGISTERED when it
unlocks card->devices_mutex, so a concurrent sub-driver removal calling
snd_device_free() can still find, unlink, and free dev before
__snd_device_disconnect(dev) runs on CPU0.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202101200.1-1-xuhx@hust.edu.cn?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 3:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 2:12 [PATCH] ALSA: core: protect card->devices against concurrent disconnect and free Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox