From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: hwdep: do not free a still-open hwdep on single-device teardown
Date: Wed, 02 Dec 2026 03:22:19 +0000 [thread overview]
Message-ID: <sashiko-outbox-244760@kernel.org> (raw)
In-Reply-To: <20261202100703.1-1-xuhx@hust.edu.cn>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: hwdep: permanent memory and device leak in
snd_hwdep_free() on -EBUSY
- [High] ALSA: hwdep: racy hwdep->used check in snd_hwdep_free() vs open
and release
- [High] ALSA: hwdep: UAF of hwdep->private_data on open fds after
snd_device_free()
--
commit 18518a58d07618a159e9b439a7a6de2178ecf5de
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: hwdep: do not free a still-open hwdep on single-device teardown
This commit changes snd_hwdep_free() to return -EBUSY when hwdep->used is
non-zero so that single-device teardown does not free a still-open struct
snd_hwdep.
> diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c
> index fb9034897c561..0d22c305e242f 100644
> --- a/sound/core/hwdep.c
> +++ b/sound/core/hwdep.c
> @@ -337,14 +337,17 @@ static const struct file_operations snd_hwdep_f_ops = {
> .mmap = snd_hwdep_mmap,
> };
>
> -static void snd_hwdep_free(struct snd_hwdep *hwdep)
> +static int snd_hwdep_free(struct snd_hwdep *hwdep)
> {
> if (!hwdep)
> - return;
> + return 0;
> + if (hwdep->used)
> + return -EBUSY;
> if (hwdep->private_free)
> hwdep->private_free(hwdep);
> put_device(hwdep->dev);
> kfree(hwdep);
> + return 0;
> }
[Severity: High]
Does returning -EBUSY here permanently leak struct snd_hwdep, hwdep->dev, and
any resources released by hwdep->private_free()?
In __snd_device_free(), dev is unconditionally unlinked from card->devices and
freed even when dev->ops->dev_free() fails. When the remaining file
descriptors later close, snd_hwdep_release() only decrements hw->used and has
no path to free the orphaned hwdep object, and later card teardown never
revisits it either.
[Severity: High]
Can reading hwdep->used without synchronization race with snd_hwdep_release()
or snd_hwdep_open() and cause a use-after-free?
In snd_hwdep_release(), hw->used is decremented to 0 before open_mutex is
unlocked, open_wait is woken, and snd_card_file_remove() reads hw->card:
CPU0 CPU1
snd_hwdep_release()
mutex_lock(&hw->open_mutex)
hw->used--; /* drops to 0 */
snd_device_free()
...
snd_hwdep_free()
if (hwdep->used) /* 0 */
kfree(hwdep)
mutex_unlock(&hw->open_mutex)
wake_up(&hw->open_wait)
snd_card_file_remove(hw->card, ...) <-- use-after-free
Similarly, an in-flight snd_hwdep_open() that has already looked up the minor
data still has hw->used == 0 until after hw->ops.open() and
snd_card_file_add() complete, so snd_hwdep_free() can free hwdep while open is
still using it.
[Severity: High]
Does leaving struct snd_hwdep alive while snd_device_free() returns to its
caller still leave hwdep->private_data vulnerable to use-after-free on open
descriptors?
During single-device teardown, snd_emux_free() calls snd_emux_delete_hwdep()
-> snd_device_free() and then immediately frees emu:
sound/synth/emux/emux.c:snd_emux_free() {
...
snd_emux_delete_hwdep(emu);
snd_sf_free(emu->sflist);
kfree(emu->voices);
kfree_const(emu->name);
kfree(emu);
return 0;
}
Because file->f_op and hwdep->ops remain active when snd_hwdep_free() returns
-EBUSY, a subsequent ioctl on the open file descriptor invokes
snd_emux_hwdep_ioctl() and dereferences the freed struct snd_emux through
hw->private_data.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100703.1-1-xuhx@hust.edu.cn?part=1
prev parent reply other threads:[~2026-12-02 3:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 2:07 [PATCH] ALSA: hwdep: do not free a still-open hwdep on single-device teardown Hanxiang Xu
2026-12-02 3:22 ` 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-244760@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.