From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed
Date: Wed, 02 Dec 2026 03:17:20 +0000 [thread overview]
Message-ID: <sashiko-outbox-244750@kernel.org> (raw)
In-Reply-To: <20261202103000.1-1-xuhx@hust.edu.cn>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] ALSA: hwdep: container_of() on pointer member in
snd_hwdep_dev_release()
- [High] ALSA: hwdep: use-after-free of private_data on open files in
snd_hwdep_free()
- [Medium] ALSA: hwdep: struct snd_hwdep leak in snd_hwdep_new() on
device alloc error
- [Medium] ALSA: hwdep: race between minor lookup and get_device() in
snd_hwdep_open()
--
commit e7fc06a622ec98bf6bee98cb33f2ea175648caa4
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed
This commit ties the lifetime of struct snd_hwdep to its device reference
count by taking a device reference on open, dropping it on close, and
deferring kfree(hwdep) to a device release callback.
> diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c
> index fb9034897c561..bddd74ee1014e 100644
> --- a/sound/core/hwdep.c
> +++ b/sound/core/hwdep.c
> @@ -84,8 +84,10 @@ static int snd_hwdep_open(struct inode *inode, struct file * file)
> return -ENXIO;
> if (hw == NULL)
> return -ENODEV;
> + get_device(hw->dev);
[Severity: Medium]
Can snd_device_free() drop the last reference on hw->dev before
get_device(hw->dev) runs here?
snd_lookup_minor_data() and snd_lookup_oss_minor_data() only take a
reference on card->card_dev and release sound_mutex / sound_oss_mutex
before returning hw. When a sub-driver removes its hwdep device from a
live card, card->card_dev does not keep hw alive:
CPU0 CPU1
snd_hwdep_open()
hw = snd_lookup_minor_data(...)
lock(&sound_mutex)
get_device(&card->card_dev)
unlock(&sound_mutex)
snd_device_free(card, hw)
snd_hwdep_dev_disconnect()
snd_hwdep_dev_free()
snd_hwdep_free()
put_device(hw->dev)
get_device(hw->dev) <-- use-after-free
[ ... ]
> @@ -337,6 +342,13 @@ static const struct file_operations snd_hwdep_f_ops = {
> .mmap = snd_hwdep_mmap,
> };
>
> +static void snd_hwdep_dev_release(struct device *dev)
> +{
> + struct snd_hwdep *hwdep = container_of(dev, struct snd_hwdep, dev);
> +
> + kfree(hwdep);
> +}
[Severity: High]
Does container_of(dev, struct snd_hwdep, dev) compile or compute a valid
struct snd_hwdep pointer here?
Since commit 897c8882df58 ("ALSA: hwdep: Don't embed device"),
struct snd_hwdep::dev is a struct device * allocated separately by
snd_device_alloc(), not an embedded struct device:
include/sound/hwdep.h:
struct snd_hwdep {
...
void *private_data;
void (*private_free) (struct snd_hwdep *hwdep);
struct device *dev;
Passing a pointer member to container_of() trips its compile-time type
static_assert, and subtracting offsetof(struct snd_hwdep, dev) from the
standalone struct device address passes a bogus pointer to kfree() while
leaking both allocations.
> +
> static void snd_hwdep_free(struct snd_hwdep *hwdep)
> {
> if (!hwdep)
> @@ -344,7 +356,6 @@ static void snd_hwdep_free(struct snd_hwdep *hwdep)
> if (hwdep->private_free)
> hwdep->private_free(hwdep);
> put_device(hwdep->dev);
> - kfree(hwdep);
> }
[Severity: High]
Even if struct snd_hwdep stays allocated until the last file is closed,
doesn't running hwdep->private_free(hwdep) and returning from
snd_device_free() while files are still open leave hw->ops callbacks
dereferencing freed hw->private_data?
When a sub-driver removes a hwdep device from a live card (for example,
snd_emux_free() -> snd_emux_delete_hwdep() -> snd_device_free()),
snd_hwdep_dev_disconnect() does not replace open files' file->f_op or wait
for ongoing operations to finish, and snd_hwdep_free() immediately runs
hwdep->private_free() before the caller frees its private state. Any
subsequent ioctl, read, write, poll, mmap or release on an open file still
invokes hw->ops with the freed hw->private_data. In addition, a thread
woken from hw->open_wait in snd_hwdep_open() only checks
hw->card->shutdown (which remains 0 on a live card) and proceeds to call
hw->ops.open() after disconnect.
>
> /**
> @@ -392,6 +403,7 @@ int snd_hwdep_new(struct snd_card *card, char *id, int device,
> snd_hwdep_free(hwdep);
> return err;
> }
> + hwdep->dev->release = snd_hwdep_dev_release;
[Severity: Medium]
Does the snd_device_alloc() error path right above this leak hwdep?
When snd_device_alloc(&hwdep->dev, card) fails, hwdep->dev is NULL and
hwdep->dev->release has not been set, so snd_hwdep_free(hwdep) calls
put_device(NULL) and returns without freeing hwdep.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202103000.1-1-xuhx@hust.edu.cn?part=1
prev parent reply other threads:[~2026-12-02 3:17 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 2:30 [PATCH] ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed Hanxiang Xu
2026-12-02 3:17 ` 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-244750@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox