[PATCH] Re: Re: sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF)

From: 徐晗翔

Date: Wed Nov 25 2026 - 07:45:19 EST


close_substream() cancelled the input event work before clearing
substream->opened, but snd_rawmidi_receive() serializes only against
the opened store: a driver that delivers input in that window
re-queued event_work after the cancellation, and the work then ran
on the runtime freed by snd_rawmidi_runtime_free() (KASAN UAF in
snd_rawmidi_input_event_work()).

Cancel the work a second time after opened is cleared and before
the runtime is freed. Also restore the per-substream disconnected
flag: snd_rawmidi_dev_disconnect() marks input substreams
disconnected under substream->lock and drains the event work, so a
driver that keeps calling snd_rawmidi_receive() after the
disconnect cannot re-queue the work either (snd_rawmidi_receive()
and snd_rawmidi_input_trigger() check the flag).
---
sound/core/rawmidi.c | 23 +++++++++++++++++++++--
include/sound/rawmidi.h | 1 +
2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c
--- a/sound/core/rawmidi.c
+++ b/sound/core/rawmidi.c
@@ -215,7 +215,7 @@

static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
{
- if (!substream->opened)
+ if (!substream->opened || substream->disconnected)
return;
substream->ops->trigger(substream, up);
if (!up)
@@ -557,6 +557,14 @@
substream->opened = 0;
substream->append = 0;
}
+ /* Drain the input event work that a racing snd_rawmidi_receive()
+ * may have queued after the earlier cancellation in
+ * snd_rawmidi_input_trigger(): receive() serializes only against
+ * the opened=0 store above, not against the earlier cancel, so the
+ * work can be pending again here. It must not outlive the runtime
+ * free below. */
+ if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
+ cancel_work_sync(&substream->runtime->event_work);
substream->ops->close(substream);
if (substream->runtime->private_free)
substream->runtime->private_free(substream);
@@ -1151,7 +1159,7 @@
struct snd_rawmidi_runtime *runtime;

guard(spinlock_irqsave)(&substream->lock);
- if (!substream->opened)
+ if (!substream->opened || substream->disconnected)
return -EBADFD;
runtime = substream->runtime;
if (!runtime || !runtime->buffer) {
@@ -2065,6 +2073,17 @@
list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
if (s->runtime)
wake_up(&s->runtime->sleep);
+ /* mark input substreams disconnected and drain the
+ * event work: a driver that keeps calling
+ * snd_rawmidi_receive() after the disconnect must
+ * not re-queue the work on a runtime that the close
+ * path is about to free */
+ if (s->stream == SNDRV_RAWMIDI_STREAM_INPUT) {
+ scoped_guard(spinlock_irqsave, &s->lock)
+ s->disconnected = true;
+ if (s->runtime)
+ cancel_work_sync(&s->runtime->event_work);
+ }
}
}

diff --git a/include/sound/rawmidi.h b/include/sound/rawmidi.h
--- a/include/sound/rawmidi.h
+++ b/include/sound/rawmidi.h
@@ -84,6 +84,7 @@
int stream; /* direction */
int number; /* substream number */
bool opened; /* open flag */
+ bool disconnected; /* device disconnected flag */
bool append; /* append flag (merge more streams) */
bool active_sensing; /* send active sensing when close */
unsigned int framing; /* whether to frame input data */
--


> -----原始邮件-----
> 发件人: "Krzysztof Kozlowski" <krzk@xxxxxxxxxx>
> 发送时间:2026-11-24 22:52:58 (星期二)
> 收件人: 徐晗翔 <xuhx@xxxxxxxxxxx>, perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: Re: sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF)
>
> On 22/11/2026 08:50, 徐晗翔 wrote:
> >
> > Please keep me in Cc; I am not subscribed to the lists.
>
>
> Why? You never responded to any other query we asked for the last month!
>
>
> Best regards,
> Krzysztof