mirror of https://lore.kernel.org/lkml/
 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  8:58 ` Takashi Iwai
  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	[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  8:58 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2026-12-02  8:58 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: Takashi Iwai, Jaroslav Kysela, linux-sound, linux-kernel

On Wed, 02 Dec 2026 03:07:03 +0100,
Hanxiang Xu wrote:
> 
> 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>

This is no-no.  You need a synchronization before release, instead.


Takashi

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

end of thread, other threads:[~2026-12-02  8:58 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  8:58 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®