From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed
Date: Wed, 02 Dec 2026 03:23:28 +0000 [thread overview]
Message-ID: <sashiko-outbox-244772@kernel.org> (raw)
In-Reply-To: <20261202103100.2-1-xuhx@hust.edu.cn>
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
prev parent reply other threads:[~2026-12-02 3:23 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-244772@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=xuhx@hust.edu.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox