Re: 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: Tue Dec 01 2026 - 07:02:18 EST
Update: partially fixed in 7.4-rc5 -- get_available_index() and
snd_ctl_add_followers() now hold controls_rwsem, but the two
kerneldoc blocks still claim the functions take the lock internally
and snd_usb_mixer_add_list()'s dedup loop is still unlocked, so the
stale-documentation and remaining-caller part of the report still
stands.
Thanks.
> -----原始邮件-----
> 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> 发送时间:2026-12-01 12:39:55 (星期二)
> 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> 主题: 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)
>
> 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