From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures
Date: Wed, 02 Dec 2026 03:26:25 +0000 [thread overview]
Message-ID: <sashiko-outbox-244777@kernel.org> (raw)
In-Reply-To: <20261202101231.1-1-xuhx@hust.edu.cn>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: emux: use-after-free in snd_emux_free() due to waiting
before disconnect
- [High] ALSA: emux: use-after-free in snd_emux_hwdep_release() on
premature wakeup
- [Medium] ALSA: emux: blocking wait in snd_emux_free() stalls
snd_card_disconnect()
--
commit fccddfea9a552c936c62cedc5b7a1b89754d19a2
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: emux: drain open hwdep users before freeing the emux structures
This commit registers open and release callbacks on the emux hwdep device to
track active users and waits in snd_emux_free() for hwdep_users to drop to
zero before tearing down the emux structures.
> diff --git a/sound/synth/emux/emux.c b/sound/synth/emux/emux.c
> index 2a42d041e1da0..4352c57aa9586 100644
> --- a/sound/synth/emux/emux.c
> +++ b/sound/synth/emux/emux.c
[ ... ]
> @@ -131,6 +133,7 @@ int snd_emux_free(struct snd_emux *emu)
> return -EINVAL;
>
> timer_shutdown_sync(&emu->tlist);
> + wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users));
>
> snd_emux_proc_free(emu);
> snd_emux_delete_virmidi(emu);
[Severity: Medium]
Does waiting unconditionally in snd_emux_free() stall snd_card_disconnect()
when a process holds the emux hwdep device open?
During whole-card teardown, snd_card_disconnect() calls
snd_device_disconnect_all(), which invokes snd_seq_device_dev_disconnect() ->
device_del() -> snd_emux_free() for the synth device before disconnecting
lower-ordered devices on the card. Unlike snd_card_disconnect_sync(),
snd_card_disconnect() and snd_card_free_when_closed() are meant to revoke file
operations and disconnect devices without waiting for open descriptors to
close, so blocking here prevents remaining card devices (such as rawmidi, PCM,
and lowlevel devices) from disconnecting until userspace closes the hwdep fd.
> diff --git a/sound/synth/emux/emux_hwdep.c b/sound/synth/emux/emux_hwdep.c
> index 14b990cf3b223..3240cbd58f05a 100644
> --- a/sound/synth/emux/emux_hwdep.c
> +++ b/sound/synth/emux/emux_hwdep.c
> @@ -73,6 +73,26 @@ snd_emux_hwdep_misc_mode(struct snd_emux *emu, void __user *arg)
> }
>
>
> +static int snd_emux_hwdep_open(struct snd_hwdep *hw, struct file *file)
> +{
> + struct snd_emux *emu = hw->private_data;
> +
> + if (!snd_emux_inc_count(emu))
> + return -EFAULT;
> + atomic_inc(&emu->hwdep_users);
> + return 0;
> +}
[Severity: High]
Can a concurrent open succeed after wait_event() in snd_emux_free() returns,
leading to a use-after-free of emu and emu->hwdep?
snd_emux_free() waits for emu->hwdep_users to reach zero before calling
snd_emux_delete_hwdep(emu), while emu->hwdep is still registered and
snd_emux_hwdep_open() does not reject new opens once teardown begins. When
snd_emux_free() runs via sysfs driver unbind (or with a built-in synth
driver), try_module_get() inside snd_emux_inc_count() still succeeds:
CPU0 CPU1
snd_emux_free(emu)
wait_event(emu->free_wait,
!atomic_read(&emu->hwdep_users))
snd_hwdep_open()
hw->ops.open(hw, file)
snd_emux_hwdep_open()
snd_emux_inc_count(emu)
atomic_inc(&emu->hwdep_users)
snd_emux_proc_free(emu)
...
snd_emux_delete_hwdep(emu)
snd_sf_free(emu->sflist)
kfree(emu)
snd_emux_hwdep_ioctl() / release()
dereferences emu <-- use-after-free
> +
> +static int snd_emux_hwdep_release(struct snd_hwdep *hw, struct file *file)
> +{
> + struct snd_emux *emu = hw->private_data;
> +
> + atomic_dec(&emu->hwdep_users);
> + wake_up(&emu->free_wait);
> + snd_emux_dec_count(emu);
> + return 0;
> +}
[Severity: High]
Does waking emu->free_wait here allow snd_emux_free() to free emu and hw
before snd_emux_dec_count() and snd_hwdep_release() finish using them?
CPU0 CPU1
snd_hwdep_release()
mutex_lock(&hw->open_mutex)
snd_emux_hwdep_release()
atomic_dec(&emu->hwdep_users)
wake_up(&emu->free_wait)
snd_emux_free()
/* wait_event() wakes */
...
snd_emux_delete_hwdep(emu)
snd_device_free(card, hw)
snd_emux_dec_count(emu)
mutex_unlock(&hw->open_mutex)
snd_hwdep_dev_disconnect()
snd_hwdep_dev_free()
kfree(hw)
kfree(emu)
wake_up(&hw->open_wait) <-- use-after-free
snd_card_file_remove(hw->card, file) <-- use-after-free
Because snd_emux_delete_hwdep() calls snd_device_free() on a live card rather
than deferring snd_hwdep_dev_free() until card->card_dev release, freeing
starts as soon as emu->free_wait wakes and hw->open_mutex is dropped. If
hwdep was already disconnected (for example during snd_card_disconnect()),
snd_device_free() does not take hw->open_mutex at all and kfree(emu) can also
race directly with snd_emux_dec_count(emu).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202101231.1-1-xuhx@hust.edu.cn?part=1
prev parent reply other threads:[~2026-12-02 3:26 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 2:12 [PATCH] ALSA: emux: drain open hwdep users before freeing the emux structures Hanxiang Xu
2026-12-02 3:26 ` 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-244777@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.