Re: ALSA: emu8000: the self-rearming emu8k_pcm timer survives trigger-stop and fires on freed memory after close (UAF)

From: 徐晗翔

Date: Tue Dec 01 2026 - 07:01:44 EST


Update: still present in 7.4-rc5 -- sound/isa/sb/emu8000_pcm.c is
unchanged, and the same interleaving (trigger-stop racing the
in-flight self-rearming callback, followed by close) immediately
fires the timer on the freed snd_emu8k_pcm (3 KASAN reports in
__run_timers, guest timer subsystem wedged afterwards).

Thanks.


> -----原始邮件-----
> 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> 发送时间:2026-12-01 12:36:24 (星期二)
> 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: ALSA: emu8000: the self-rearming emu8k_pcm timer survives trigger-stop and fires on freed memory after close (UAF)
>
> 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