sound: core: rawmidi input close can re-queue event_work after cancel_work_sync() (UAF)
From: 徐晗翔
Date: Sun Nov 22 2026 - 02:51:14 EST
Closing a RawMIDI input stream can leave a work item queued after the
runtime object it uses has been freed. close_substream() calls
snd_rawmidi_input_trigger(substream, 0) first; that function calls the
driver's trigger(0), then cancel_work_sync(&runtime->event_work) -- but
it does not hold substream->lock and it runs while substream->opened is
still 1. Only afterwards does close_substream() take substream->lock,
set opened = 0, call ops->close() and snd_rawmidi_runtime_free(), which
kfree()s struct snd_rawmidi_runtime.
In the window between cancel_work_sync() returning and opened becoming 0
on another CPU, a driver can still call snd_rawmidi_receive(). receive()
takes substream->lock, sees opened == 1, queues data and executes
schedule_work(&runtime->event_work). The work is then pending again, the
runtime is freed, and when the worker runs it dereferences
runtime->event and runtime->substream -- a use-after-free. Upstream
closes this window with a second cancel_work_sync() after opened is
cleared (the comment says "Drain input work queued after the earlier
cancellation"), and also marks the substream disconnected in
snd_rawmidi_dev_disconnect(). Both protections are missing here.
I reproduced the UAF on Linux 7.3 with KASAN. A test module registers a
rawmidi input stream and a kthread keeps calling snd_rawmidi_receive();
the user program opens /dev/snd/midiC0D0, closes it and frees the card.
The close path re-queues event_work after the first cancellation and the
work reports slab-use-after-free in snd_rawmidi_input_event_work()
(16 KASAN reports in the run). To make the timing deterministic I added
verification-only mdelay() calls in the patched snd-rawmidi.ko: 100 ms
after cancel_work_sync() and 3000 ms at the start of the work. They
widen the window but do not change the ordering; the patch is attached
so the claim can be audited and reverted.
The detailed description:
* Test environment / affected version
Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC,
vanilla 7.3 source with KASAN, QEMU/KVM x86_64 (4 vCPU, 4 GB).
CONFIG_SND_RAWMIDI=m was enabled for the test; the only functional
source change is the verification-only mdelay instrumentation in
sound/core/rawmidi.c (patch attached). Version line in
evidence/kernel-version.txt.
* Root cause
sound/core/rawmidi.c:216-224:
static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
{
if (!substream->opened)
return;
substream->ops->trigger(substream, up);
if (!up)
cancel_work_sync(&substream->runtime->event_work);
}
No substream->lock is held across the opened check, trigger and cancel.
sound/core/rawmidi.c:533-565 (close_substream):
if (cleanup) {
if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
snd_rawmidi_input_trigger(substream, 0);
...
}
scoped_guard(spinlock_irq, &substream->lock) {
substream->opened = 0;
substream->append = 0;
}
substream->ops->close(substream);
...
snd_rawmidi_runtime_free(substream); /* kfree(runtime) */
sound/core/rawmidi.c:1146-1218 (snd_rawmidi_receive):
guard(spinlock_irqsave)(&substream->lock);
if (!substream->opened)
return -EBADFD;
...
if (result > 0) {
if (runtime->event)
schedule_work(&runtime->event_work);
...
}
Because receive() serializes with the opened=0 store but not with the
earlier cancel_work_sync(), it can queue the work after cancel and
before opened is cleared. snd_rawmidi_runtime_free() does not cancel
the work and does not re-check it.
Upstream has an additional cancel_work_sync() after opened is cleared
in close_substream(), plus a disconnected flag and cancel in
snd_rawmidi_dev_disconnect(); this tree has neither.
* Trigger model
- An input RawMIDI stream is open and has runtime->event set (the
sequencer's snd_midi_input_event(), or a driver using the RawMIDI
input API);
- the stream is closed while the driver can still deliver input (for
example a serial UART whose receive path checks its own
"input open" mode bit until input_close(), or any driver that calls
snd_rawmidi_receive() from its RX path);
- receive() queues event_work after cancel_work_sync() and before
opened=0; runtime is freed; the worker runs.
The attached test module does not require special hardware: its kthread
plays the role of a driver delivering input.
* Reproducer (attached) and results on 7.3
sndcore_rawmidi.c creates a card + rawmidi input stream, installs a
runtime->event callback and starts a kthread that loops:
reset runtime->avail under substream->lock, then call
snd_rawmidi_receive() with one byte. sndcore_test.c opens
/dev/snd/midiC0D0, sleeps, closes the fd, waits and frees the card.
Observed :
event callback entered n=3 opened=0
BUG: KASAN: slab-use-after-free in snd_rawmidi_input_event_work+0x2d/0x60 [snd_rawmidi]
Workqueue: events snd_rawmidi_input_event_work [snd_rawmidi]
Call Trace:
snd_rawmidi_input_event_work+0x2d/0x60 [snd_rawmidi]
test_event+0x3f/0x60 [sndcore_rawmidi]
process_one_work+...
Allocated by task ...:
open_substream+...
Freed by task ...:
close_substream.part.0+...
Last potentially related work creation:
snd_rawmidi_receive+0x31b/0x5a0 [snd_rawmidi]
flood_fn+...
KASAN bug count: 16
The "opened=0" in the callback is the value observed after the UAF;
the work ran after close, on the freed runtime.
* Suggested direction
- In close_substream(), after opened is cleared, do a second
cancel_work_sync(&runtime->event_work) for the input stream, as
upstream does.
- Restore the per-substream disconnected flag and the receive/trigger
checks, and cancel the work in snd_rawmidi_dev_disconnect().
- Consider taking substream->lock consistently around the opened
check and cancellation, or document the lock ordering.
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sound/asound.h>
static int write_free(const char *mod)
{
char path[256];
int fd;
snprintf(path, sizeof(path), "/sys/module/%s/parameters/free", mod);
fd = open(path, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open %s: %s\n", path, strerror(errno));
return -1;
}
if (write(fd, "1\n", 2) != 2)
fprintf(stderr, "write %s: %s\n", path, strerror(errno));
close(fd);
return 0;
}
static int do_hwdep(void)
{
struct snd_hwdep_dsp_image img;
int fd;
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwdep: %s\n", strerror(errno));
return 1;
}
memset(&img, 0, sizeof(img));
img.index = 0;
img.length = 0;
img.image = 0;
for (int i = 0; i < 4; i++) {
if (ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img) < 0)
fprintf(stderr, "ioctl dsp_load: %s\n", strerror(errno));
}
close(fd);
printf("HWDEP: ioctl done (max_active should be >= 2)\n");
sleep(1);
return 0;
}
static void *hwdep_thread(void *arg)
{
int fd = *(int *)arg;
struct snd_hwdep_dsp_image img;
memset(&img, 0, sizeof(img));
img.index = 0;
ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img);
return NULL;
}
static int do_hwdep_race(void)
{
pthread_t th[4];
int fd;
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwdep: %s\n", strerror(errno));
return 1;
}
for (int i = 0; i < 4; i++)
pthread_create(&th[i], NULL, hwdep_thread, &fd);
for (int i = 0; i < 4; i++)
pthread_join(th[i], NULL);
close(fd);
printf("HWDEP: 4 concurrent DSP_LOAD issued\n");
sleep(1);
return 0;
}
static int do_compress(void)
{
int fd;
fd = open("/dev/snd/comprC0D0", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open compress: %s\n", strerror(errno));
return 1;
}
printf("COMPRESS: fd open, freeing card\n");
write_free("sndcore_compress");
printf("COMPRESS: card free returned, issuing ioctl on stale fd\n");
if (ioctl(fd, 0x4300, 0) < 0)
fprintf(stderr, "ioctl stale fd: %s\n", strerror(errno));
printf("COMPRESS: ioctl returned without KASAN report\n");
close(fd);
sleep(1);
return 0;
}
static int do_rawmidi(void)
{
int fd;
fd = open("/dev/snd/midiC0D0", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open rawmidi: %s\n", strerror(errno));
return 1;
}
printf("RAWMIDI: fd open, letting flood thread run\n");
sleep(2);
printf("RAWMIDI: closing (trigger/cancel/free window)\n");
close(fd);
sleep(3);
printf("RAWMIDI: close returned, freeing card\n");
write_free("sndcore_rawmidi");
sleep(1);
return 0;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s jackfree|compress|rawmidi|hwdep|hwdep-race\n", argv[0]);
return 2;
}
if (!strcmp(argv[1], "jackfree"))
return write_free("sndcore_jack");
if (!strcmp(argv[1], "compress"))
return do_compress();
if (!strcmp(argv[1], "rawmidi"))
return do_rawmidi();
if (!strcmp(argv[1], "hwdep"))
return do_hwdep();
if (!strcmp(argv[1], "hwdep-race"))
return do_hwdep_race();
fprintf(stderr, "unknown mode %s\n", argv[1]);
return 2;
}
Attachment:
sndcore_parent.h
Description: Binary data
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/atomic.h>
#include <sound/core.h>
#include <sound/rawmidi.h>
#include "sndcore_parent.h"
static struct snd_card *card;
static struct snd_rawmidi *rmidi;
static struct task_struct *flood;
static struct snd_rawmidi_substream *in_sub;
static atomic_t events = ATOMIC_INIT(0);
static unsigned char byte = 0x5a;
static int rm_open(struct snd_rawmidi_substream *s) { return 0; }
static int rm_close(struct snd_rawmidi_substream *s) { return 0; }
static void rm_trigger(struct snd_rawmidi_substream *s, int up) { }
static void rm_drain(struct snd_rawmidi_substream *s) { }
static const struct snd_rawmidi_ops rm_ops = {
.open = rm_open,
.close = rm_close,
.trigger = rm_trigger,
.drain = rm_drain,
};
static void test_event(struct snd_rawmidi_substream *s)
{
int n = atomic_inc_return(&events);
if (n < 8)
pr_info("event callback entered n=%d opened=%d\n", n, s->opened);
}
static int flood_fn(void *unused)
{
int i;
in_sub = list_first_entry(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
struct snd_rawmidi_substream, list);
for (i = 0; i < 4000 && !kthread_should_stop(); i++) {
if (READ_ONCE(in_sub->opened))
break;
msleep(5);
}
if (!READ_ONCE(in_sub->opened))
return 0;
if (in_sub->runtime)
in_sub->runtime->event = test_event;
pr_info("flood start: opened=%d runtime=%p\n", in_sub->opened, in_sub->runtime);
while (!kthread_should_stop() && READ_ONCE(in_sub->opened)) {
unsigned long flags;
if (in_sub->runtime) {
spin_lock_irqsave(&in_sub->lock, flags);
if (in_sub->runtime)
in_sub->runtime->avail = 0;
spin_unlock_irqrestore(&in_sub->lock, flags);
}
snd_rawmidi_receive(in_sub, &byte, 1);
cond_resched();
}
pr_info("flood stop: opened=%d events=%d\n", in_sub->opened, atomic_read(&events));
return 0;
}
static int free_set(const char *val, const struct kernel_param *kp)
{
if (card) {
pr_info("free: calling snd_card_free(%p)\n", card);
snd_card_free(card);
card = NULL;
}
return 0;
}
static const struct kernel_param_ops free_ops = { .set = free_set };
module_param_cb(free, &free_ops, NULL, 0200);
static int __init sndcore_rawmidi_init(void)
{
int err;
err = sndcore_card_new(&card, "sndcore_rawmidi");
if (err < 0)
return err;
pr_info("card=%p module=%p this=%p\n", card, card->module, THIS_MODULE);
if (card->module) {
pr_info("try_module_get(card->module)=%d\n", try_module_get(card->module));
module_put(card->module);
}
err = snd_rawmidi_new(card, "sndcore", 0, 0, 1, &rmidi);
if (err < 0) {
snd_card_free(card);
card = NULL;
sndcore_parent_put();
return err;
}
rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT;
snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &rm_ops);
err = snd_card_register(card);
if (err < 0) {
snd_card_free(card);
card = NULL;
sndcore_parent_put();
return err;
}
flood = kthread_run(flood_fn, NULL, "sndcore_flood");
if (IS_ERR(flood)) {
err = PTR_ERR(flood);
flood = NULL;
snd_card_free(card);
card = NULL;
sndcore_parent_put();
return err;
}
pr_info("rawmidi card registered: dev=%s devt=%u:%u\n",
dev_name(rmidi->dev), MAJOR(rmidi->dev->devt), MINOR(rmidi->dev->devt));
return 0;
}
static void __exit sndcore_rawmidi_exit(void)
{
if (flood) {
kthread_stop(flood);
flood = NULL;
}
if (card) {
snd_card_free(card);
card = NULL;
}
sndcore_parent_put();
}
module_init(sndcore_rawmidi_init);
module_exit(sndcore_rawmidi_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("sound/core MR-04 rawmidi event_work verifier");