ALSA: emu8000: the self-rearming emu8k_pcm timer survives trigger-stop and fires on freed memory after close (UAF)
From: 徐晗翔
Date: Mon Nov 30 2026 - 23:36:40 EST
The Emu8000 playback pcm driver drives its period handling with a
timer that re-arms itself from inside its own callback: emu8k_pcm_timer_func()
mod_timer()s itself for the next jiffy unconditionally while holding
rec->timer_lock (emu8000_pcm.c:200), without checking rec->timer_running
or rec->running anywhere. The stop path, stop_voice() (trigger STOP,
emu8000_pcm.c:357-369), only performs a non-synchronizing
timer_delete() under the same lock, and emu8k_pcm_close()
(emu8000_pcm.c:246-252) frees the struct with no timer cleanup at all.
If trigger-stop executes while the per-jiffy callback is already
dequeued and in flight (it runs once per jiffy while playing), the
callback re-arms the timer AFTER the stop believes it dead; the
subsequent close() then kfree()s a struct with an armed timer, and at
most one jiffy later the timer core executes the callback on the
freed memory -- reading rec->timer_lock and the position fields and
re-arming the freed timer_list, which chains the freed object back
into the timer wheel and corrupts the timer subsystem state.
We verified the mechanism on vanilla 7.3 with a 1:1 replica of the
three audited fragments (identical lock/flag semantics, the real timer
machinery) on a lookalike struct, because the real driver needs AWE64
ISA hardware that a QEMU x86 guest cannot provide; the replica is
explicitly labeled as such in the attached module. Driving the
audited interleaving ("stop completes while the callback is already
dequeued, callback body runs afterwards") hit immediately: three KASAN
slab-use-after-free reports in __run_timers (ksoftirqd context --
Write of size 8 on the freed struct, i.e. the freed timer_list being
dequeued/re-armed by the timer core itself), after which the guest's
timer subsystem was wedged and the VM stopped making progress -- the
audited "freed timer_list re-arm corrupts the timer core" consequence,
demonstrated end to end. Natural hammering (300 start/stop/close
rounds without any artificial delay) did not hit, as expected for a
per-jiffy in-flight window of a few microseconds; we report that
negative honestly. Triggering needs no privileges beyond normal
device-node permissions (audio group) on an AWE64-class card with
DRAM; no root, no module removal, no fault injection.
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 mainline 7.3 release tarball, KASAN enabled, QEMU/KVM
x86_64 guest. sound/isa/sb/emu8000_pcm.c is byte-identical to
mainline 7.3; the fragments below quote its line numbers. The
driver requires AWE64-class ISA hardware (snd_sbawe + the
snd_emu8000_synth sub-driver, mem_size > 0), which is why the
dynamic evidence comes from the labeled replica module rather than
the real pcm.
* Mechanism
- The callback re-arms unconditionally: emu8k_pcm_timer_func()
(emu8000_pcm.c:183-211) under rec->timer_lock runs
mod_timer(&rec->timer, jiffies + 1) at line 200 with no check of
rec->timer_running or rec->running; after unlocking it calls
snd_pcm_period_elapsed(rec->substream) (210).
- The stop path is a plain timer_delete(): stop_voice()
(357-369) takes rec->timer_lock, timer_delete(&rec->timer) and
clears timer_running. timer_delete() only dequeues a timer that
is still pending; a callback that is already executing (or waiting
for the lock) on another CPU is not waited for and will run to
completion afterwards -- re-arming the timer from inside its body.
- The close path frees without cleanup: emu8k_pcm_close()
(246-252) does kfree(rec) directly. Nothing in the pcm core
touches rec->timer either: snd_pcm_free_stream() only handles the
separate internal snd_pcm_timer.
- The escape interleaving: stop_voice() runs while the callback has
already been dequeued for execution on another CPU ->
timer_delete() is a no-op -> the callback then takes the lock,
mod_timer() re-arms (timer_running is now 0 but nothing checks
it), and the timer fires again <=1 jiffy later, forever, unless a
second stop lands exactly in the callback's non-re-arm window --
which the immediately following close() prevents by freeing rec.
- The stop path itself is reachable both from
SNDRV_PCM_IOCTL_DROP/close (snd_pcm_release_substream() ->
snd_pcm_drop() -> snd_pcm_stop() -> snd_pcm_do_stop() invokes
TRIGGER_STOP for RUNNING/DRAINING streams) and from device
disconnect (snd_pcm_dev_disconnect() -> snd_pcm_stop()).
* Trigger model
A user with access to /dev/snd/pcmC*D1p (the audio group on a
desktop install) drives start -> play -> SNDRV_PCM_IOCTL_DROP ->
close on an AWE64-class card whose emu8000 sub-driver has created
the pcm (mem_size > 0). The escape needs trigger-stop to interleave
with the in-flight callback; since the callback runs every jiffy
while playing, repeated start/drop/close cycles sweep the window.
Consequence: use-after-free reads/writes on struct snd_emu8k_pcm and
a mod_timer() on the freed timer_list -- i.e. freed memory chained
back into the kernel timer wheel -- which is memory corruption well
beyond the driver itself.
* Reproducer (attached: fwmr05.c) and results on 7.3
fwmr05.c is a labeled mechanism replica: struct f5rec mirrors
struct snd_emu8k_pcm's audited fields (embedded timer, timer_lock,
state flags), and the three fragments are the audited semantics
1:1 -- the callback body with the unconditional L200 re-arm under
the lock, stop_voice() with the non-sync timer_delete, and a close
that simply kfrees. The real timer subsystem is used so the
freed-timer re-arm manifests in the genuine timer core.
# controlled escape order (the audited interleaving, force-built):
insmod fwmr05.ko
echo 1 > /sys/module/fwmr05/parameters/fw05_open
echo 1 > /sys/module/fwmr05/parameters/fw05_start # runs 2s, real fires
echo 1 > /sys/module/fwmr05/parameters/fw05_stop
echo 1 > /sys/module/fwmr05/parameters/fw05_inflight # verbatim callback body post-stop
echo 1 > /sys/module/fwmr05/parameters/fw05_close # kfree(rec)
# <=1 jiffy later the real timer core runs the callback on freed memory
# natural hammering variant:
echo 1 > /sys/module/fwmr05/parameters/fw05_nat # 300 start/stop/close rounds
Results (7.3, 2 vCPU, KASAN): controlled order -- 3
slab-use-after-free reports in __run_timers+0x20f/0x3af/0x3bb
(ksoftirqd/1; Write of size 8 = the freed timer_list being
dequeued/re-armed by the timer core), then the guest timer subsystem
wedged and the VM stopped progressing. Natural variant: 0 hits in
300 rounds (recorded as the honest negative; the window is the
callback's in-flight span, microseconds, vs. millisecond-scale
rounds).
* Suggested patch
Two-line-shaped fix: honor the stop flag inside the callback and
shut the timer down synchronously before freeing the struct.
The complete patch against 7.3 follows (also kept as
patch/0001-fw-mr05.patch); happy to rework per review.
---- patch begins ----
---
--- a/sound/isa/sb/emu8000_pcm.c
+++ b/sound/isa/sb/emu8000_pcm.c
@@ -196,8 +196,13 @@
rec->period_pos += delta;
rec->last_ptr = ptr;
- /* reprogram timer */
- mod_timer(&rec->timer, jiffies + 1);
+ if (rec->timer_running)
+ mod_timer(&rec->timer, jiffies + 1);
/* update period */
if (rec->period_pos >= (int)rec->period_size) {
@@ -246,6 +251,12 @@
static int emu8k_pcm_close(struct snd_pcm_substream *subs)
{
struct snd_emu8k_pcm *rec = subs->runtime->private_data;
+ timer_shutdown_sync(&rec->timer);
kfree(rec);
subs->runtime->private_data = NULL;
return 0;
---- patch ends ----
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
/*
* fwmr05.c - mechanism-replica harness for audit case FW-MR-05
* (docs/manual-review-sound-firewire-2026-11-26.md, v4):
*
* "emu8k è?ªç»è·?å®?æ?¶å?¨ä¸?æ? å??æ¥ timer_delete/close ç«?äº? ->
* snd_emu8k_pcm UAF ä¸?å·²é??æ?¾ timer_list é??æ??è½½"
*
* The real driver (sound/isa/sb/emu8000_pcm.c) needs AWE64 ISA hardware,
* which QEMU x86 cannot provide. This harness is a MECHANISM REPLICA: the
* three audited code fragments are replicated 1:1 in semantics on a
* lookalike struct, and the REAL kernel timer machinery is used so the
* freed-timer re-arm manifests through the genuine timer subsystem:
*
* f5_timer_func() <- emu8k_pcm_timer_func() (emu8000_pcm.c:183-211)
* scoped_guard(spinlock, &rec->timer_lock) { position update;
* mod_timer(&rec->timer, jiffies + 1); }
* (L200: unconditional self-re-arm, no state check anywhere)
* f5_start_voice() <- start_voice() timer half (emu8000_pcm.c:346-351)
* f5_stop_voice() <- stop_voice() (emu8000_pcm.c:357-369)
* guard(spinlock_irqsave)(&rec->timer_lock) {
* if (rec->timer_running) { timer_delete(&rec->timer);
* rec->timer_running = 0; } }
* f5_close() <- emu8k_pcm_close() (emu8000_pcm.c:246-252)
* kfree(rec); (no timer cleanup of any kind)
*
* Plans (module params):
* fw05_ctl - deterministic "escape order" (the audited Case-3
* interleaving, force-constructed and labeled as such):
* open -> start (timer runs, self-rearms for ~2s of real
* firing) -> stop (timer_delete + running=0) -> inflight
* (run the verbatim callback body once, mimicking the
* already-dequeued callback that re-arms AFTER stop: mod_timer
* re-arms the timer on a rec that stop() believes dead)
* -> close (kfree(rec)) -> <=1 jiffy later the REAL timer
* fires on the freed rec: UAF read of rec->timer_lock/fields
* and re-arm of the freed timer_list.
* fw05_nat - natural hammering: 300 rounds of
* start -> usleep(1-3ms) -> stop -> close, racing the real
* per-jiffy callback (narrow window, honest attempt).
*
* Zero kernel source modification.
*/
#define pr_fmt(fmt) "FWMR05: " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/kthread.h>
#include <linux/delay.h>
/*
* Lookalike of struct snd_emu8k_pcm (emu8000_pcm.c:44-60): the audited
* fields are laid out identically in role (timer embedded in the struct,
* spinlock guards it, state flags checked by NOBODY in the callback).
*/
struct f5rec {
spinlock_t timer_lock;
struct timer_list timer;
unsigned int timer_running:1;
unsigned int running:1;
int last_ptr;
int period_pos;
int buf_size;
int period_size;
void *substream; /* real driver: snd_pcm_substream */
};
static struct f5rec *rec;
/*
* Verbatim semantics of emu8k_pcm_timer_func() (emu8000_pcm.c:183-211):
* position update under timer_lock, UNCONDITIONAL self-re-arm at L200
* (no timer_running/running check anywhere), period handling, and the
* out-of-lock snd_pcm_period_elapsed(rec->substream) at L210.
*/
static void f5_body(struct f5rec *r)
{
bool elapsed = false;
scoped_guard(spinlock, &r->timer_lock) {
/* emu8k_get_curpos() + delta update analog */
int ptr = r->last_ptr + 1;
int delta = ptr - r->last_ptr;
r->period_pos += delta;
r->last_ptr = ptr;
/* L200: the audited unconditional self-re-arm */
mod_timer(&r->timer, jiffies + 1);
if (r->period_pos >= r->period_size) {
r->period_pos %= r->period_size;
elapsed = true;
}
}
if (elapsed) {
/* L210 analog: snd_pcm_period_elapsed(rec->substream);
* no real pcm in the replica -- the freed-rec read above is
* the KASAN-relevant dereference. */
}
}
static void f5_timer_func(struct timer_list *t)
{
struct f5rec *r = timer_container_of(r, t, timer);
f5_body(r);
}
/* start_voice() timer half (emu8000_pcm.c:346-352), verbatim semantics */
static void f5_start_voice(void)
{
guard(spinlock_irqsave)(&rec->timer_lock);
if (!rec->timer_running) {
mod_timer(&rec->timer, jiffies + 1);
rec->timer_running = 1;
}
}
/* stop_voice() (emu8000_pcm.c:357-369), verbatim semantics:
* timer_delete() without any synchronization for an in-flight callback. */
static void f5_stop_voice(void)
{
guard(spinlock_irqsave)(&rec->timer_lock);
if (rec->timer_running) {
timer_delete(&rec->timer);
rec->timer_running = 0;
}
}
/* emu8k_pcm_close() (emu8000_pcm.c:246-252), verbatim semantics */
static void f5_close(void)
{
pr_info("close: kfree(rec) with no timer cleanup\n");
kfree(rec);
rec = NULL;
}
static int f5_open_set(const char *val, const struct kernel_param *kp)
{
if (rec)
return -EBUSY;
rec = kzalloc(sizeof(*rec), GFP_KERNEL);
if (!rec)
return -ENOMEM;
spin_lock_init(&rec->timer_lock);
timer_setup(&rec->timer, f5_timer_func, 0);
rec->buf_size = 4096;
rec->period_size = 1024;
pr_info("open: rec=%p timer_setup done (emu8k_pcm_open analog)\n", rec);
return 0;
}
static int f5_start_set(const char *val, const struct kernel_param *kp)
{
if (!rec)
return -ENODEV;
rec->running = 1;
f5_start_voice();
pr_info("start: timer armed, will self-re-arm every jiffy\n");
return 0;
}
static int f5_stop_set(const char *val, const struct kernel_param *kp)
{
if (!rec)
return -ENODEV;
rec->running = 0;
f5_stop_voice();
pr_info("stop: timer_delete done, timer_running=0 "
"(no sync for in-flight callback -- audited shape)\n");
return 0;
}
/*
* The audited Case-3 escape interleaving, force-constructed: an already-
* dequeued callback (started before stop() completed) runs its verbatim
* body AFTER stop() and re-arms the timer on a rec the stop path believes
* dead. This is a controlled construction -- it only widens the window,
* the three code fragments are the audited ones verbatim.
*/
static int f5_inflight_set(const char *val, const struct kernel_param *kp)
{
if (!rec)
return -ENODEV;
pr_info("inflight: running verbatim callback body post-stop "
"(case-3 escape construction)\n");
f5_body(rec);
pr_info("inflight: body returned -- timer re-armed on a stopped rec\n");
return 0;
}
static int f5_close_set(const char *val, const struct kernel_param *kp)
{
if (!rec)
return -ENODEV;
f5_close();
pr_info("close done: armed timer on freed rec fires in <=1 jiffy\n");
return 0;
}
/* ---- natural hammering variant ---- */
static unsigned int f5_rounds = 300;
static struct task_struct *f5_thr;
static int f5_nat_fn(void *arg)
{
int n;
for (n = 0; n < f5_rounds && !kthread_should_stop(); n++) {
rec = kzalloc(sizeof(*rec), GFP_KERNEL);
if (!rec)
break;
spin_lock_init(&rec->timer_lock);
timer_setup(&rec->timer, f5_timer_func, 0);
rec->buf_size = 4096;
rec->period_size = 1024;
rec->running = 1;
f5_start_voice();
usleep_range(1000, 3000); /* 1-3 jiffies of real firing */
f5_stop_voice();
f5_close(); /* kfree(rec) */
usleep_range(2000, 4000); /* let a re-armed timer land */
}
pr_info("nat: %d rounds done\n", n);
return 0;
}
static int f5_nat_set(const char *val, const struct kernel_param *kp)
{
f5_thr = kthread_run(f5_nat_fn, NULL, "fwmr05_nat");
return 0;
}
static const struct kernel_param_ops f5_open_ops = { .set = f5_open_set };
static const struct kernel_param_ops f5_start_ops = { .set = f5_start_set };
static const struct kernel_param_ops f5_stop_ops = { .set = f5_stop_set };
static const struct kernel_param_ops f5_inflight_ops = { .set = f5_inflight_set };
static const struct kernel_param_ops f5_close_ops = { .set = f5_close_set };
static const struct kernel_param_ops f5_nat_ops = { .set = f5_nat_set };
module_param_cb(fw05_open, &f5_open_ops, NULL, 0200);
module_param_cb(fw05_start, &f5_start_ops, NULL, 0200);
module_param_cb(fw05_stop, &f5_stop_ops, NULL, 0200);
module_param_cb(fw05_inflight, &f5_inflight_ops, NULL, 0200);
module_param_cb(fw05_close, &f5_close_ops, NULL, 0200);
module_param_cb(fw05_nat, &f5_nat_ops, NULL, 0200);
static int __init fwmr05_init(void)
{
return 0;
}
static void __exit fwmr05_exit(void)
{
if (f5_thr)
kthread_stop(f5_thr);
if (rec) {
timer_delete_sync(&rec->timer);
kfree(rec);
rec = NULL;
}
}
module_init(fwmr05_init);
module_exit(fwmr05_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("FW-MR-05 emu8k timer mechanism-replica harness");