All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: hwdep: do not free a still-open hwdep on single-device teardown
@ 2026-12-02  2:07 Hanxiang Xu
  2026-12-02  3:22 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:07 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

An independent teardown path calls snd_device_free(card, hwdep),
which runs snd_hwdep_free(): it kfrees the struct snd_hwdep without
waiting for hwdep->used to drop.  Open descriptors keep
file->private_data pointing at the freed object with the real hwdep
f_ops, and their in-flight or later operations (ioctl/read/poll and
the final close) are use-after-frees.  The card-wide path swaps
file->f_op for stub operations, but a single-device teardown never
runs it.

Refuse the free while hwdep->used is non-zero: __snd_device_free()
still unlinks the snd_device wrapper, so the card never revisits the
device, and the object stays valid for the open descriptors.

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

diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c
index fb90348..0d22c30 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;
 }
 
 /**
@@ -412,8 +415,7 @@ EXPORT_SYMBOL(snd_hwdep_new);
 
 static int snd_hwdep_dev_free(struct snd_device *device)
 {
-	snd_hwdep_free(device->device_data);
-	return 0;
+	return snd_hwdep_free(device->device_data);
 }
 
 static int snd_hwdep_dev_register(struct snd_device *device)
-- 
2.43.0


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

* Re: [PATCH] ALSA: hwdep: do not free a still-open hwdep on single-device teardown
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:22 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: 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

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

end of thread, other threads:[~2026-12-02  3:22 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: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 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.