Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed
@ 2026-12-02  2:30 Hanxiang Xu
  2026-12-02  3:17 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:30 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

When a sub-driver removes its hwdep device from a live card with
snd_device_free(), snd_hwdep_dev_disconnect() does not wait for open
files and snd_hwdep_free() immediately kfree()s struct snd_hwdep.
Open files keep file->private_data pointing at the freed object, so
an in-flight ioctl dereferences hw->ops and the delayed close() runs
snd_hwdep_release() on the freed memory (hw->card->module,
hw->open_mutex, hw->used).

Anchor the lifetime of struct snd_hwdep on its embedded device: the
release callback now performs the kfree(), opening the device takes a
reference and every close/error path drops it again.  snd_hwdep_free()
only releases the registry reference.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 sound/core/hwdep.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c
index fb9034897c561b2df01c655728afad550f010c85..bddd74ee1014e7117c51c7592d6960cc3b5729b0 100644
@@ -84,8 +84,10 @@
 		return -ENXIO;
 	if (hw == NULL)
 		return -ENODEV;
+	get_device(hw->dev);
 
 	if (!try_module_get(hw->card->module)) {
+		put_device(hw->dev);
 		snd_card_unref(hw->card);
 		return -ENODEV;
 	}
@@ -137,8 +139,10 @@ static int snd_hwdep_open(struct inode *inode, struct file * file)
 		}
 	}
 	mutex_unlock(&hw->open_mutex);
-	if (err < 0)
+	if (err < 0) {
 		module_put(hw->card->module);
+		put_device(hw->dev);
+	}
 	snd_card_unref(hw->card);
 	return err;
 }
@@ -159,6 +163,7 @@ static int snd_hwdep_release(struct inode *inode, struct file * file)
 
 	snd_card_file_remove(hw->card, file);
 	module_put(mod);
+	put_device(hw->dev);
 	return err;
 }
 
@@ -337,6 +342,13 @@ static int snd_hwdep_mmap(struct file * file, struct vm_area_struc...
 	.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);
+}
+
 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);
 }
 
 /**
@@ -392,6 +403,7 @@ static void snd_hwdep_free(struct snd_hwdep *hwdep)
 		snd_hwdep_free(hwdep);
 		return err;
 	}
+	hwdep->dev->release = snd_hwdep_dev_release;
 
 	dev_set_name(hwdep->dev, "hwC%iD%i", card->number, device);
 #ifdef CONFIG_SND_OSSEMUL
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:17 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:17 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: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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox