* [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown
@ 2026-12-02 2:08 Hanxiang Xu
2026-12-02 3:23 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02 2:08 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel
An independent teardown path calls snd_device_free(card, rmidi),
which runs snd_rawmidi_free(): kfree(substream) for every substream,
rmidi->private_free() (which frees the virmidi device data), and
kfree(rmidi) -- none of which waits for the open file descriptors on
/dev/snd/midiC*D*, and the virmidi output work queued on the global
workqueue keeps dereferencing the freed substream and device data.
Refuse the free while any substream is opened: __snd_device_free()
unlinks the snd_device wrapper but leaves the object tree alive for
the open descriptors and the in-flight work. Additionally never kfree
a substream whose ->opened flag is still set.
Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
sound/core/rawmidi.c | 5 +++++
1 file changed, 5 insertions(+), 0 deletions(-)
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
index 630d9fc..851509e 100644
--- a/sound/core/rawmidi.c
+++ b/sound/core/rawmidi.c
@@ -1969,6 +1969,8 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
while (!list_empty(&stream->substreams)) {
substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
list_del(&substream->list);
+ if (substream->opened)
+ continue;
kfree(substream);
}
}
@@ -1978,6 +1980,9 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
{
if (!rmidi)
return 0;
+ if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened ||
+ rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened)
+ return -EBUSY;
snd_info_free_entry(rmidi->proc_entry);
rmidi->proc_entry = NULL;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown
2026-12-02 2:08 [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown 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 2 potential
issues to consider:
- [High] ALSA: rawmidi: permanent resource leak on -EBUSY in
snd_rawmidi_free()
- [High] ALSA: rawmidi: UAF race in snd_rawmidi_free() against open and
release
--
commit a8c690d1a9c9b070160648c8b1f5677136e99bce
Author: Hanxiang Xu <xuhx@hust.edu.cn>
ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown
This commit skips freeing open substreams in snd_rawmidi_free_substreams()
and returns -EBUSY from snd_rawmidi_free() when any substream is open.
> diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
> index 630d9fc8e9da0..851509e9d43ff 100644
> --- a/sound/core/rawmidi.c
> +++ b/sound/core/rawmidi.c
> @@ -1969,6 +1969,8 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
> while (!list_empty(&stream->substreams)) {
> substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
> list_del(&substream->list);
> + if (substream->opened)
> + continue;
> kfree(substream);
> }
> }
> @@ -1978,6 +1980,9 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi)
> {
> if (!rmidi)
> return 0;
> + if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened ||
> + rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened)
> + return -EBUSY;
>
> snd_info_free_entry(rmidi->proc_entry);
> rmidi->proc_entry = NULL;
[Severity: High]
Does returning -EBUSY here permanently leak rmidi, its substreams, and its
subdevice resources?
__snd_device_free() unlinks the snd_device from card->devices and frees
it regardless of the error returned by dev_free():
sound/core/device.c:__snd_device_free() {
/* unlink */
list_del(&dev->list);
__snd_device_disconnect(dev);
if (dev->ops->dev_free) {
if (dev->ops->dev_free(dev))
dev_err(dev->card->dev, "device free failure\n");
}
kfree(dev);
}
When the open file descriptors on /dev/snd/midiC*D* are later closed,
snd_rawmidi_release() and close_substream() only decrement
substream_opened and free the runtime and snd_rawmidi_file; nothing ever
calls snd_rawmidi_free() again. In addition to leaking rmidi, the
substreams, rmidi->private_data, and the rmidi->dev reference, bailing out
early leaves rmidi->proc_entry registered in procfs and skips
rmidi->ops->dev_unregister(). Likewise, snd_rawmidi_free_substreams()
calls list_del(&substream->list) before checking substream->opened, which
orphans and leaks any open substream.
[Severity: High]
Can checking substream_opened and substream->opened without holding
rmidi->open_mutex race with concurrent open and release calls and cause a
use-after-free?
open_substream() and close_substream() update substream->opened and
substream_opened under rmidi->open_mutex, but snd_rawmidi_free() runs
unlocked. If the last open file descriptor is closing concurrently,
close_substream() decrements substream_opened to 0 before
rawmidi_release_priv() and snd_rawmidi_release() finish using rmidi:
CPU0 CPU1
snd_rawmidi_release()
mutex_lock(&rmidi->open_mutex)
rawmidi_release_priv()
close_substream()
...
stream->substream_opened--
snd_device_free()
...
snd_rawmidi_free()
/* sees substream_opened == 0 */
kfree(rmidi)
wake_up(&rmidi->open_wait) <-- use-after-free
mutex_unlock(&rmidi->open_mutex) <-- use-after-free
module = rmidi->card->module <-- use-after-free
A similar race exists against open_substream(): during single-device
teardown rmidi->card->shutdown is still 0, so a concurrent open can enter
open_substream() and run snd_rawmidi_runtime_create() and
substream->ops->open() before incrementing substream_opened, while
snd_rawmidi_free() sees substream_opened == 0 and frees the substreams,
rmidi->private_data, and rmidi underneath it. If open_substream() sets
substream->opened = 1 just after snd_rawmidi_free() passes the
substream_opened check, snd_rawmidi_free_substreams() skips freeing that
substream while snd_rawmidi_free() still frees rmidi->private_data and
rmidi, leaving the open substream and file with dangling rmidi pointers.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100804.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: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:08 [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown 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