* [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
@ 2026-12-02 2:31 Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02 2:31 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel
When a sub-driver removes its rawmidi device from a live card with
snd_device_free(), snd_rawmidi_dev_disconnect() does not wait for open
files and snd_rawmidi_free() immediately frees the substreams, the
driver private data and struct snd_rawmidi itself. Open files keep
rawmidi_file->rmidi and ->output pointing at the freed objects, a
close() runs snd_rawmidi_release() on the freed rmidi (open_mutex,
rmidi->card), and a queued output work item can still reference the
freed substream.
Anchor the lifetime of the rawmidi memory on its embedded device: the
release callback now performs free_substreams()/private_free()/
kfree(rmidi) once the last reference is dropped, opening the device
(userspace and kernel variants) takes a reference and every
close/error path drops it again. snd_rawmidi_free() only releases
the registry reference, so the un-drained work item can no longer
touch freed memory.
Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
sound/core/rawmidi.c | 31 +++++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
index 630d9fc8e9da0a0e7d6a2eded6aed531b094b932..824c37a7773d92d387d39d66150d5bf1778c381a 100644
@@ -469,11 +469,14 @@
return -EINVAL;
if (!try_module_get(rmidi->card->module))
return -ENXIO;
+ get_device(rmidi->dev);
mutex_lock_nested(&rmidi->open_mutex, depth);
err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
- if (err < 0)
+ if (err < 0) {
module_put(rmidi->card->module);
+ put_device(rmidi->dev);
+ }
mutex_unlock(&rmidi->open_mutex);
return err;
}
@@ -508,8 +511,10 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
if (rmidi == NULL)
return -ENODEV;
+ get_device(rmidi->dev);
if (!try_module_get(rmidi->card->module)) {
+ put_device(rmidi->dev);
snd_card_unref(rmidi->card);
return -ENXIO;
}
@@ -577,6 +582,7 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
mutex_unlock(&rmidi->open_mutex);
module_put(rmidi->card->module);
snd_card_unref(rmidi->card);
+ put_device(rmidi->dev);
return err;
}
@@ -650,6 +656,7 @@ static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
rawmidi_release_priv(rfile);
mutex_unlock(&rmidi->open_mutex);
module_put(rmidi->card->module);
+ put_device(rmidi->dev);
return 0;
}
EXPORT_SYMBOL(snd_rawmidi_kernel_release_nested);
@@ -668,6 +675,7 @@ static int snd_rawmidi_release(struct inode *inode, struct file *f...
module = rmidi->card->module;
snd_card_file_remove(rmidi->card, file);
module_put(module);
+ put_device(rmidi->dev);
return 0;
}
@@ -1900,6 +1908,7 @@ static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
err = snd_device_alloc(&rmidi->dev, card);
if (err < 0)
return err;
+ rmidi->dev->release = snd_rawmidi_dev_release;
if (rawmidi_is_ump(rmidi))
dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device);
else
@@ -1973,6 +1982,21 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *st...
}
}
+/*
+ * The rawmidi memory is freed by the embedded device's release
+ * callback once the last reference (an open file) is dropped.
+ */
+static void snd_rawmidi_dev_release(struct device *dev)
+{
+ struct snd_rawmidi *rmidi = container_of(dev, struct snd_rawmidi, dev);
+
+ snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
+ snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
+ if (rmidi->private_free)
+ rmidi->private_free(rmidi);
+ kfree(rmidi);
+}
+
/* called from ump.c, too */
int snd_rawmidi_free(struct snd_rawmidi *rmidi)
{
@@ -1984,12 +2008,7 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
if (rmidi->ops && rmidi->ops->dev_unregister)
rmidi->ops->dev_unregister(rmidi);
- snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
- snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
- if (rmidi->private_free)
- rmidi->private_free(rmidi);
put_device(rmidi->dev);
- kfree(rmidi);
return 0;
}
EXPORT_SYMBOL_GPL(snd_rawmidi_free);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
2026-12-02 2:31 [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed Hanxiang Xu
@ 2026-12-02 3:23 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 3:23 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] ALSA: rawmidi: undeclared snd_rawmidi_dev_release in
snd_rawmidi_init()
- [High] ALSA: rawmidi: invalid container_of() in
snd_rawmidi_dev_release()
- [High] ALSA: rawmidi: use-after-free of rmidi after put_device() on
open errors
- [High] ALSA: rawmidi: rmidi->private_free() deferred past
snd_card_do_free()
- [High] ALSA: rawmidi: race in snd_rawmidi_open() before
get_device(rmidi->dev)
- [Medium] ALSA: rawmidi: leak of rmidi in snd_rawmidi_free() on dev
alloc error
--
commit 09c08573d097cdce254107ff8d32fa804fdca7e7
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
This commit moves the freeing of substreams, driver private data, and
struct snd_rawmidi into a device release callback and takes device
references while rawmidi files are open.
> diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
> index 630d9fc8e9da0..824c37a7773d9 100644
> --- a/sound/core/rawmidi.c
> +++ b/sound/core/rawmidi.c
> @@ -469,11 +469,14 @@ int snd_rawmidi_kernel_open_nested(struct snd_rawmidi *rmidi, int subdevice,
> return -EINVAL;
> if (!try_module_get(rmidi->card->module))
> return -ENXIO;
> + get_device(rmidi->dev);
>
> mutex_lock_nested(&rmidi->open_mutex, depth);
> err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
> - if (err < 0)
> + if (err < 0) {
> module_put(rmidi->card->module);
> + put_device(rmidi->dev);
> + }
> mutex_unlock(&rmidi->open_mutex);
> return err;
> }
[Severity: High]
Can put_device(rmidi->dev) free rmidi while rmidi->open_mutex is still
held? If snd_rawmidi_free() concurrently drops its reference while
rawmidi_open_priv() fails, put_device(rmidi->dev) drops the final
reference and frees rmidi before mutex_unlock(&rmidi->open_mutex) runs.
Similarly, in snd_rawmidi_open(), when try_module_get() fails,
put_device(rmidi->dev) is called before snd_card_unref(rmidi->card),
which can read rmidi->card from freed memory.
> @@ -508,8 +511,10 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
>
> if (rmidi == NULL)
> return -ENODEV;
> + get_device(rmidi->dev);
>
> if (!try_module_get(rmidi->card->module)) {
> + put_device(rmidi->dev);
> snd_card_unref(rmidi->card);
> return -ENXIO;
> }
[Severity: High]
Can rmidi be freed before snd_rawmidi_open() calls get_device(rmidi->dev)?
snd_lookup_minor_data() only pins card->card_dev under sound_mutex and
does not take a reference on rmidi->dev. If a sub-driver removes the
rawmidi device from a live card right after snd_lookup_minor_data()
returns, snd_rawmidi_free() drops the only reference on rmidi->dev before
snd_rawmidi_open() reaches get_device():
CPU0 CPU1
snd_rawmidi_open()
rmidi = snd_lookup_minor_data(...)
snd_device_free(card, rmidi)
...
snd_rawmidi_free(rmidi)
put_device(rmidi->dev)
snd_rawmidi_dev_release()
kfree(rmidi)
get_device(rmidi->dev) <-- use-after-free
> @@ -577,6 +582,7 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
> mutex_unlock(&rmidi->open_mutex);
> module_put(rmidi->card->module);
> snd_card_unref(rmidi->card);
> + put_device(rmidi->dev);
> return err;
> }
>
> @@ -650,6 +656,7 @@ int snd_rawmidi_kernel_release_nested(struct snd_rawmidi_file *rfile,
> rawmidi_release_priv(rfile);
> mutex_unlock(&rmidi->open_mutex);
> module_put(rmidi->card->module);
> + put_device(rmidi->dev);
> return 0;
> }
> EXPORT_SYMBOL(snd_rawmidi_kernel_release_nested);
> @@ -668,6 +675,7 @@ static int snd_rawmidi_release(struct inode *inode, struct file *file)
> module = rmidi->card->module;
> snd_card_file_remove(rmidi->card, file);
> module_put(module);
> + put_device(rmidi->dev);
> return 0;
> }
[Severity: High]
Does calling put_device(rmidi->dev) after snd_card_file_remove() and
module_put() cause rmidi->private_free(rmidi) to run after the card has
already been freed?
When a card is disconnected while a rawmidi file is open,
snd_rawmidi_dev_disconnect() unregisters rmidi->dev (dropping its parent
reference on card->card_dev), leaving the file reference from
snd_card_file_add() as the last reference keeping the card alive. On
close, snd_card_file_remove() drops that last card reference and runs
snd_card_do_free():
sound/core/init.c:snd_card_do_free() {
...
snd_device_free_all(card);
...
if (card->private_free)
card->private_free(card);
...
kfree(card);
return 0;
}
During snd_device_free_all(card), snd_rawmidi_free() calls
put_device(rmidi->dev), but because snd_rawmidi_release() still holds its
reference on rmidi->dev, snd_rawmidi_dev_release() does not run yet.
snd_card_do_free() then finishes freeing the card and driver private data,
snd_rawmidi_release() drops the module reference, and only then does
put_device(rmidi->dev) invoke rmidi->private_free(rmidi) on the freed
card. The same ordering issue occurs in snd_rawmidi_open()'s error path
and in snd_rawmidi_kernel_release_nested().
> @@ -1900,6 +1908,7 @@ int snd_rawmidi_init(struct snd_rawmidi *rmidi,
> err = snd_device_alloc(&rmidi->dev, card);
> if (err < 0)
> return err;
> + rmidi->dev->release = snd_rawmidi_dev_release;
> if (rawmidi_is_ump(rmidi))
> dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device);
> else
[Severity: High]
Does this fail to compile because snd_rawmidi_dev_release() is used here
before its static definition later in sound/core/rawmidi.c without a
forward declaration?
> @@ -1973,6 +1982,21 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
> }
> }
>
> +/*
> + * The rawmidi memory is freed by the embedded device's release
> + * callback once the last reference (an open file) is dropped.
> + */
> +static void snd_rawmidi_dev_release(struct device *dev)
> +{
> + struct snd_rawmidi *rmidi = container_of(dev, struct snd_rawmidi, dev);
> +
> + snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
> + snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
> + if (rmidi->private_free)
> + rmidi->private_free(rmidi);
> + kfree(rmidi);
> +}
[Severity: High]
Is container_of(dev, struct snd_rawmidi, dev) valid when rmidi->dev is a
struct device * pointer rather than an embedded struct device?
Since commit ea29a02fd802 ("ALSA: rawmidi: Don't embed device"),
struct snd_rawmidi declares struct device *dev, and snd_device_alloc()
allocates a separate struct device on the heap. Applying container_of()
to dev fails the compile-time static_assert type check in container_of(),
computes a wild pointer from the standalone struct device allocation, and
leaks dev because default_release_alloc's kfree(dev) is replaced.
> +
> /* called from ump.c, too */
> int snd_rawmidi_free(struct snd_rawmidi *rmidi)
> {
> @@ -1984,12 +2008,7 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
> if (rmidi->ops && rmidi->ops->dev_unregister)
> rmidi->ops->dev_unregister(rmidi);
>
> - snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
> - snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
> - if (rmidi->private_free)
> - rmidi->private_free(rmidi);
> put_device(rmidi->dev);
> - kfree(rmidi);
> return 0;
> }
> EXPORT_SYMBOL_GPL(snd_rawmidi_free);
[Severity: Medium]
Does snd_rawmidi_free() leak rmidi when snd_device_alloc() fails in
snd_rawmidi_init()? In that case rmidi->dev is still NULL when
snd_rawmidi_new() (or snd_ump_endpoint_new()) calls snd_rawmidi_free(),
so put_device(NULL) is a no-op and rmidi is never freed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202103100.2-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:23 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:31 [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox