ALSA: control: find_id()/find_numid() kerneldoc claims they take controls_rwlock internally; the FAST_LOOKUP implementation is a lockless, unreferenced xa_load and several in-tree callers use it unlocked (UAF)
From: 徐晗翔
Date: Mon Nov 30 2026 - 23:40:13 EST
With CONFIG_SND_CTL_FAST_LOOKUP, snd_ctl_find_numid() and
snd_ctl_find_id() are, on their fast paths, plain xa_load() calls on
card->ctl_numids / card->ctl_hash that return an unreferenced
struct snd_kcontrol pointer which the callers immediately dereference
(elem_id_matches(), control.c:855). Both kerneldoc blocks still state
"Note that this function takes card->controls_rwlock lock internally"
(control.c:816 and 841) -- that is stale and factually wrong for the
FAST_LOOKUP implementation, which only takes controls_rwlock in its
fallback branch. The removal path frees the object without any grace
period (__snd_ctl_remove() -> remove_hash_entries() -> kfree(),
control.c:588/598/331), so an xa_load that raced a concurrent
xa_erase() dereferences freed memory. The core ioctl paths hold
card->controls_rwsem (read) around their lookups and are safe, but
several in-tree callers use the two functions with no lock at all:
get_available_index() in sound/core/ctljack.c (the dedup loop used by
every jack-kcontrol creation, run from driver probe on an already
registered card), snd_ctl_add_followers() in sound/core/vmaster.c,
find_mixer_ctl() in sound/hda/common/codec.c, and the dedup loop of
snd_usb_mixer_add_list() in sound/usb/mixer.c.
We want to be explicit about what we could and could not demonstrate.
The reader window we probed -- get_available_index() during an HDA
reconfig reprobe -- turns out to be mutually exclusive with a
user-space ELEM_ADD/ELEM_REMOVE writer: snd_hda_lock_devices()
(sound/hda/common/codec.c) refuses the reconfig reset while any
control file is open ("The codec is being used"), and an
open("/dev/snd/controlC0") inside the reset window fails with ENODEV.
We measured both on 7.3 (negative plan attached), so the user-space
writer pairing is structurally closed and no dynamic hit was possible
through it. The remaining reachable writer pairs are driver-side (a
second codec of the same card being reset/unbound, or card teardown),
which a single-codec QEMU guest cannot build. We therefore report
this as a locking-contract defect (wrong kerneldoc, latent
use-after-free, unreferenced pointers escaping into longer-lived
state) rather than as an actively exploitable bug, together with a
patch that restores the contract at the unlocked call sites.
The detailed description:
* Test environment / affected version
Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #3 SMP PREEMPT_DYNAMIC
-- vanilla mainline 7.3 release tarball, KASAN + CONFIG_SND_CTL_FAST_LOOKUP
enabled, QEMU/KVM x86_64 guest with intel-hda + hda-duplex
(CONFIG_SND_HDA_RECONFIG / SND_HDA_GENERIC). sound/core/control.c,
sound/core/ctljack.c, sound/core/vmaster.c, sound/usb/mixer.c are
byte-identical to mainline 7.3; line numbers below are from 7.3.
* Mechanism
- FAST_LOOKUP read path: snd_ctl_find_numid() is
xa_load(&card->ctl_numids, numid) (control.c:825);
snd_ctl_find_id() does xa_load(&card->ctl_hash, hash) followed by
an immediate elem_id_matches(kctl, id) dereference of
kctl->id.{iface,device,subdevice,name,index,count} (control.c:854,
370-379). controls_rwlock is only taken in the collision/fallback
branch (control.c:861).
- Free path has no grace period: __snd_ctl_remove() erases the hash
entries (588) and kfree()s the kcontrol (598 -> 331) directly; the
XArray's internal RCU protects the array walk, not the object
lifetime, so "xa_load returned the pointer" and "kfree() already
ran" can overlap with the reader between load and dereference.
- The window is real code distance, not theory: between the
xa_load() at 854 and elem_id_matches() at 855 the writer can
complete both xa_erase() and kfree().
- Unlocked callers (all verified to hold no controls_rwsem in their
calling context): ctljack.c:41 (get_available_index loop, jack
creation during probe on a registered card), vmaster.c:298
(snd_ctl_add_followers), hda codec.c:1645 (find_mixer_ctl),
usb/mixer.c:592 (snd_usb_mixer_add_list dedup loop).
- Reachability boundary (measured): reconfig-driven readers cannot
run concurrently with any control-file holder because
snd_hda_lock_devices() gates the reset on an empty
card->ctl_files. Driver-side writers (second codec reset/unbind
on the same card; snd_ctl_dev_free() at card teardown) do not have
that coupling and remain reachable.
* Trigger model
Latent defect: needs an unlocked lookup in flight while the same
(or hash-colliding) control is removed concurrently -- realistically
the dual-codec HDA reconfig/unbind pairing, or card teardown. The
consequence of a hit is a use-after-free read in elem_id_matches()
and an unreferenced dangling pointer handed to callers that store it
(e.g. snd_ctl_add_followers() would register a freed follower), plus
wrong dedup decisions from re-used memory. We did not observe a
dynamic hit and are not claiming exploitability; the report is about
the violated contract, the stale documentation and the unlocked
in-tree callers.
* Reproducer (attached: fwmr_test.c) and results on 7.3
The attached mr06storm plan is the writer half (ELEM_ADD/ELEM_REMOVE
of an iface=CARD user control whose name matches an existing jack
control, i.e. same ctl_hash key). The measured negative: the
reconfig loop's reset is rejected ("being used") once the storm
holds controlC0, and open(controlC0) inside the reset window returns
ENODEV -- the pairing cannot be built from user space (log excerpt
attached). The dual-codec pairing needs hardware/guest topologies
we could not assemble.
* Suggested patch
Restore the documented contract at the unlocked call sites and fix
the documentation: both kerneldoc blocks now state the truth (the
returned pointer is unreferenced, FAST_LOOKUP is an unlocked
xa_load, callers must hold controls_rwsem or protect the object),
and the three straightforwardly wrappable callers
(ctljack.c get_available_index(), vmaster.c snd_ctl_add_followers(),
usb/mixer.c snd_usb_mixer_add_list() -- the latter releases the read
lock before snd_ctl_add() takes the write lock) hold
controls_rwsem read for their lookup sections. find_mixer_ctl() in
the HDA code was deliberately left out pending an audit of its
callers, several of which are reachable in contexts we could not all
verify. A deeper fix (kfree_rcu for kcontrols + RCU read sections)
is worth discussing but touches the release path of every control
type, so it is out of scope for this first patch. The complete
patch against 7.3 follows (also kept as patch/0001-fw-mr06.patch).
---- patch begins ----
---
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -813,7 +813,11 @@
*
* Return: The pointer of the instance if found, or %NULL if not.
*
* Note that this function takes card->controls_rwlock lock internally.
*/
struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card,
unsigned int numid)
@@ -838,7 +842,11 @@
*
* Return: The pointer of the instance if found, or %NULL if not.
*
* Note that this function takes card->controls_rwlock lock internally.
*/
struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
const struct snd_ctl_elem_id *id)
--- a/sound/core/ctljack.c
+++ b/sound/core/ctljack.c
@@ -38,6 +38,13 @@
sid.iface = SNDRV_CTL_ELEM_IFACE_CARD;
strscpy(sid.name, name, sizeof(sid.name));
+ guard(rwsem_read)(&card->controls_rwsem);
while (snd_ctl_find_id(card, &sid)) {
sid.index++;
/* reset numid; otherwise snd_ctl_find_id() hits this again */
--- a/sound/core/vmaster.c
+++ b/sound/core/vmaster.c
@@ -295,7 +295,10 @@
int err;
for (; *list; list++) {
- follower = snd_ctl_find_id_mixer(card, *list);
+ /* find_id_mixer() returns an unreferenced pointer; protect
+ * the lookup against concurrent removals */
+ scoped_guard(rwsem_read, &card->controls_rwsem)
+ follower = snd_ctl_find_id_mixer(card, *list);
if (follower) {
err = snd_ctl_add_follower(master, follower);
if (err < 0)
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -589,8 +589,13 @@
struct usb_mixer_interface *mixer = list->mixer;
int err;
- while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
- kctl->id.index++;
+ /* snd_ctl_find_id() returns an unreferenced pointer; hold the read
+ * lock for the dedup loop only (snd_ctl_add() takes the write
+ * lock itself) */
+ scoped_guard(rwsem_read, &mixer->chip->card->controls_rwsem) {
+ while (snd_ctl_find_id(mixer->chip->card, &kctl->id))
+ kctl->id.index++;
+ }
err = snd_ctl_add(mixer->chip->card, kctl);
if (err < 0) {
usb_audio_dbg(mixer->chip, "cannot add control (err = %d)\n",
---- 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
/*
* fwmr_test.c - userspace driver for the sound/firewire FW-MR-* race
* verification harnesses (docs/manual-review-sound-firewire-2026-11-26.md).
*
* Plans:
* mr02hold [secs] - FW-MR-02 deterministic release-path variant: open
* /dev/snd/hwC0D0 (real snd_hwdep_open, used=1), read
* once, HOLD the fd across the kernel-side
* snd_emux_free() (host script triggers it), then
* ioctl + close on the freed hwdep -> real
* snd_hwdep_release() derefs freed hw (hw->card->module,
* mutex_lock(&hw->open_mutex), hw->used--).
* mr02hammer [iters] - FW-MR-02 in-flight variant (same shape as the
* synth report's mr03, re-attributed): open + ioctl
* hammer while the free runs on the other side.
* mr03midi [secs] - FW-MR-03: poll for /dev/snd/midiC0D0, open
* O_WRONLY|O_NONBLOCK (real snd_rawmidi_open ->
* snd_virmidi_output_open INIT_WORK), write MIDI bytes
* (real snd_rawmidi_output_trigger -> queue_work
* system_highpri_wq), HOLD across fw3_free, write
* again (in-flight work + write path on freed rmidi),
* close (snd_rawmidi_release on freed rmidi).
* mr04pcm [secs] - FW-MR-04: poll for /dev/snd/pcmC1D0p, open (real
* snd_pcm_open, ref_count=1), hammer
* SNDRV_PCM_IOCTL_PVERSION/PINFO, HOLD across fw4_free
* (snd_device_free(card, pcm)), ioctl + close on the
* freed substream/pcm (real snd_pcm_release path).
* mr01read [secs] - FW-MR-01 reader side: hammer
* SNDRV_CTL_IOCTL_ELEM_LIST + ELEM_INFO on
* /dev/snd/controlC0 while a dangling (freed but
* still-linked) jack kcontrol sits in card->controls.
* mr06storm [secs] - FW-MR-06 writer side: loop SNDRV_CTL_IOCTL_ELEM_ADD +
* ELEM_REMOVE of a user control with iface=CARD whose
* name is copied from an existing CARD control (same
* ctl_hash key as the jack controls the reconfig probe
* looks up locklessly in get_available_index()).
* waitdev <path> [timeout_ms] - poll until a device node appears.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sound/asound.h>
#include "include/sound/sfnt_info.h"
static void waitdev(const char *path, int timeout_ms)
{
int waited = 0;
struct stat st;
while (stat(path, &st) != 0) {
if (waited >= timeout_ms)
return;
usleep(20000);
waited += 20;
}
}
/* ---- FW-MR-02 ---- */
static int plan_mr02hold(int secs)
{
int fd;
unsigned int ver = 0;
waitdev("/dev/snd/hwC0D0", 8000);
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR02HOLD: fd open (hw->used=1), VERSION probe done\n");
fflush(stdout);
printf("MR02HOLD: holding fd for %ds (host frees emux now)\n", secs);
fflush(stdout);
sleep(secs);
printf("MR02HOLD: ioctl on freed hwdep (in snd_hwdep_ioctl path)\n");
fflush(stdout);
ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver);
printf("MR02HOLD: closing -> real snd_hwdep_release() on freed hw\n");
fflush(stdout);
close(fd);
printf("MR02HOLD: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/hwC0D0", O_RDWR));
printf("MR02HOLD: done\n");
return 0;
}
static int plan_mr02hammer(int iters)
{
int fd, i;
unsigned int ver = 0;
waitdev("/dev/snd/hwC0D0", 8000);
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR02HAMMER: %d ioctl iterations\n", iters);
fflush(stdout);
for (i = 0; i < iters; i++) {
ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver);
if ((i % 256) == 0) {
printf("MR02HAMMER: iter %d\n", i);
fflush(stdout);
}
usleep(100);
}
printf("MR02HAMMER: closing\n");
fflush(stdout);
close(fd);
sleep(2);
close(open("/dev/snd/hwC0D0", O_RDWR));
printf("MR02HAMMER: done\n");
return 0;
}
/* ---- FW-MR-03 ---- */
static int plan_mr03midi(int secs)
{
int fd, n = 0;
unsigned char msg[3] = { 0x90, 0x40, 0x7f };
waitdev("/dev/snd/midiC0D0", 15000);
fd = open("/dev/snd/midiC0D0", O_WRONLY | O_NONBLOCK);
if (fd < 0) {
fprintf(stderr, "open midiC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR03MIDI: opened output; writing bytes (arms output_work)\n");
fflush(stdout);
/* a few writes: each runs snd_rawmidi_kernel_write1 ->
* snd_rawmidi_output_trigger(substream, 1) -> queue_work */
for (n = 0; n < 32; n++)
{ ssize_t wr_ = write(fd, msg, sizeof(msg)); (void)wr_; }
printf("MR03MIDI: %d writes queued; holding fd for %ds "
"(host frees emux now)\n", n, secs);
fflush(stdout);
sleep(secs);
printf("MR03MIDI: writing again on freed rmidi\n");
fflush(stdout);
for (n = 0; n < 8; n++)
{ ssize_t wr_ = write(fd, msg, sizeof(msg)); (void)wr_; }
printf("MR03MIDI: closing -> real snd_rawmidi_release() on freed rmidi\n");
fflush(stdout);
close(fd);
printf("MR03MIDI: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/midiC0D0", O_WRONLY | O_NONBLOCK));
printf("MR03MIDI: done\n");
return 0;
}
/* ---- FW-MR-04 ---- */
static int plan_mr04pcm(int secs)
{
int fd, i;
unsigned int ver = 0;
struct snd_pcm_info info;
waitdev("/dev/snd/pcmC1D0p", 15000);
fd = open("/dev/snd/pcmC1D0p", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open pcmC0D0p: %s\n", strerror(errno));
return 1;
}
printf("MR04PCM: opened via real snd_pcm_open (ref_count=1)\n");
fflush(stdout);
for (i = 0; i < 64; i++) {
ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver);
memset(&info, 0, sizeof(info));
ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info);
usleep(1000);
}
printf("MR04PCM: 64 probe ioctls done; holding fd for %ds "
"(host frees pcm now)\n", secs);
fflush(stdout);
sleep(secs);
printf("MR04PCM: INFO ioctl on freed substream/pcm (in snd_pcm_info)\n");
fflush(stdout);
memset(&info, 0, sizeof(info));
ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info);
printf("MR04PCM: closing -> real snd_pcm_release() on freed objects\n");
fflush(stdout);
close(fd);
printf("MR04PCM: reopen attempt (minor unregistered -> ENODEV)\n");
fflush(stdout);
close(open("/dev/snd/pcmC1D0p", O_WRONLY));
printf("MR04PCM: done\n");
return 0;
}
/* ---- FW-MR-01 / FW-MR-06 control-face helpers ---- */
static int ctl_open(void)
{
int fd = open("/dev/snd/controlC0", O_RDWR);
if (fd < 0)
fprintf(stderr, "open controlC0: %s\n", strerror(errno));
return fd;
}
static int plan_mr01read(int secs)
{
int fd, i, n, err;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
unsigned long until = (unsigned long)time(NULL) + secs;
fd = ctl_open();
if (fd < 0)
return 1;
ids = calloc(256, sizeof(*ids));
printf("MR01READ: hammering ELEM_LIST+ELEM_INFO for %ds\n", secs);
fflush(stdout);
i = 0;
while ((unsigned long)time(NULL) < until) {
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
err = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list);
if (err < 0)
break;
for (n = 0; n < (int)list.count && n < 256; n++) {
memset(&info, 0, sizeof(info));
info.id = ids[n];
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info);
}
if ((++i % 64) == 0) {
printf("MR01READ: round %d (count=%u)\n", i, list.count);
fflush(stdout);
}
usleep(2000);
}
printf("MR01READ: done (%d rounds)\n", i);
free(ids);
close(fd);
return 0;
}
static int plan_mr06storm(int secs)
{
int fd, card, i, added = 0;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
struct snd_ctl_elem_id remove_id;
unsigned long until = (unsigned long)time(NULL) + secs;
fd = ctl_open();
if (fd < 0)
return 1;
/* take the name of an existing CARD-interface control so the user
* control shares the ctl_hash key of the jack controls that the
* reconfig probe's get_available_index() looks up locklessly */
ids = calloc(256, sizeof(*ids));
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list) < 0) {
fprintf(stderr, "ELEM_LIST: %s\n", strerror(errno));
free(ids);
close(fd);
return 1;
}
memset(&info, 0, sizeof(info));
for (i = 0; i < (int)list.count && i < 256; i++) {
info.id = ids[i];
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info) < 0)
continue;
if (info.id.iface == SNDRV_CTL_ELEM_IFACE_CARD)
break;
}
if (i >= (int)list.count) {
fprintf(stderr, "no CARD control found\n");
free(ids);
close(fd);
return 1;
}
printf("MR06STORM: adding/removing iface=CARD user control '%s' "
"for %ds\n", info.id.name, secs);
fflush(stdout);
i = 0;
while ((unsigned long)time(NULL) < until) {
memset(&info, 0, sizeof(info));
info.id.iface = SNDRV_CTL_ELEM_IFACE_CARD;
strncpy((char *)info.id.name, "Headphone Jack",
sizeof(info.id.name) - 1);
info.type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
info.count = 1;
info.owner = 0;
if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_ADD, &info) < 0)
goto drop;
added++;
remove_id = info.id;
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_REMOVE, &remove_id);
drop:
if ((++i % 256) == 0) {
printf("MR06STORM: round %d (added=%d)\n", i, added);
fflush(stdout);
}
usleep(500);
}
(void)card;
printf("MR06STORM: done (%d rounds, %d added)\n", i, added);
free(ids);
close(fd);
return 0;
}
static int plan_mr01dump(void)
{
int fd, n, err;
struct snd_ctl_elem_list list;
struct snd_ctl_elem_id *ids;
struct snd_ctl_elem_info info;
fd = ctl_open();
if (fd < 0)
return 1;
ids = calloc(256, sizeof(*ids));
memset(&list, 0, sizeof(list));
list.offset = 0;
list.space = 256;
list.pids = ids;
err = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &list);
if (err < 0) {
printf("MR01DUMP: ELEM_LIST failed (%s)\n", strerror(errno));
free(ids);
close(fd);
return 1;
}
printf("MR01DUMP: controls_count=%u (dangling entry stays listed)\n",
list.count);
fflush(stdout);
for (n = 0; n < (int)list.count && n < 256; n++) {
memset(&info, 0, sizeof(info));
info.id = ids[n];
ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &info);
printf("MR01DUMP: numid=%-5u iface=%d %s\n",
ids[n].numid, ids[n].iface, ids[n].name);
fflush(stdout);
}
free(ids);
close(fd);
return 0;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: fwmr_test <plan> [arg]\n");
return 2;
}
if (!strcmp(argv[1], "mr02hold"))
return plan_mr02hold(argc > 2 ? atoi(argv[2]) : 4);
if (!strcmp(argv[1], "mr02hammer"))
return plan_mr02hammer(argc > 2 ? atoi(argv[2]) : 20000);
if (!strcmp(argv[1], "mr03midi"))
return plan_mr03midi(argc > 2 ? atoi(argv[2]) : 6);
if (!strcmp(argv[1], "mr04pcm"))
return plan_mr04pcm(argc > 2 ? atoi(argv[2]) : 6);
if (!strcmp(argv[1], "mr01dump"))
return plan_mr01dump();
if (!strcmp(argv[1], "mr01read"))
return plan_mr01read(argc > 2 ? atoi(argv[2]) : 15);
if (!strcmp(argv[1], "mr06storm"))
return plan_mr06storm(argc > 2 ? atoi(argv[2]) : 60);
if (!strcmp(argv[1], "waitdev"))
return (waitdev(argv[2], argc > 3 ? atoi(argv[3]) : 10000), 0);
fprintf(stderr, "unknown plan %s\n", argv[1]);
return 2;
}