snd_jack_kctl_new() publishes the jack-detection kcontrol to the card first (snd_ctl_add() links it into card->controls and into the ctl_numids/ctl_hash xarrays) and only then allocates the jack private part; when that kzalloc fails, the error path calls snd_ctl_free_one() on the kcontrol that is still linked into all three indexes -- without holding card->controls_rwsem and without unlinking it. The result is a persistent dangling entry: every later control-face access (ELEM_LIST / ELEM_INFO / ELEM_READ ioctls) dereferences freed memory, and the final card teardown walks the same dangling entry and frees it a second time. Reached on a live registered card through the HDA reconfig path: `echo 1 > /sys/class/sound/hwC0D0/reconfig` runs snd_hda_codec_reset(), which clears card->shutdown via snd_hda_unlock_devices() before device_reprobe(), so /dev/snd/controlC* is fully operational while the reprobe creates jacks through snd_jack_kctl_new(). The only random component is the allocation failure itself; since our test kernel ships no CONFIG_FAILSLAB, we injected it surgically with a one-shot kprobe on the kzalloc return instruction inside snd_jack_kctl_new (+0x56, guarded against build drift by live opcode bytes), which drives the byte-identical in-tree code through the audited error branch. 957 KASAN use-after-free reports followed immediately, with the free stack pointing at the audited rollback itself and the use stacks in the control-face readers and in the removal path. A negative control running the same reconfig + reader + unbind sequence without injection is clean. Writing the sysfs attribute requires root or equivalent; the consequence is kernel memory corruption and a double free. 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 (kernel.org), KASAN enabled, QEMU/KVM x86_64 guest with intel-hda + hda-duplex; kernel configured with CONFIG_SND_HDA_RECONFIG=y and CONFIG_SND_HDA_GENERIC=y (built-in). sound/core/jack.c, sound/core/control.c and sound/core/ctljack.c are byte-identical to the mainline 7.3 release; line numbers below are from 7.3. * Mechanism - Publish first: snd_jack_kctl_new() calls snd_ctl_add() (sound/core/jack.c:436) -> __snd_ctl_add_replace() links the kcontrol into card->controls under controls_rwlock (control.c:497-502) and fills ctl_numids/ctl_hash via add_hash_entries() (control.c:403-423, 504), all under write controls_rwsem; on return the kcontrol is globally visible. - Rollback frees the published object: the kzalloc_obj(*jack_kctl) at jack.c:440 fails -> "goto error" -> snd_ctl_free_one(kctl) (jack.c:453) -> kfree(kcontrol) at control.c:331. Nothing removes the list node or the xarray entries; snd_ctl_free_one()'s own contract comment says "Don't call this after the control was added to the card" (control.c:324). The correct sequence (remove_hash_entries() -> list_del -> free) lives in __snd_ctl_remove() (control.c:577-600) and is never reached for this object. - The dangling state is persistent, not a narrow window: ELEM_LIST walks card->controls and copies the freed kcontrol's id; ELEM_INFO / ELEM_READ resolve it through snd_ctl_find_numid()/find_id() (an unlocked xa_load under CONFIG_SND_CTL_FAST_LOOKUP); and the card teardown, snd_ctl_dev_free() (control.c:2457-2475), walks card->controls under the write lock, runs __snd_ctl_remove() on the dangling node (UAF reads in remove_hash_entries() and snd_ctl_notify_one()) and then frees it a second time. - Secondary data race in the same function: kctl->private_data and kctl->private_free are written after snd_ctl_add() returned and with no lock held (jack.c:448-449); a concurrent free between the two writes calls snd_ctl_free_one() with private_free still NULL and the post-release write at jack.c:449 lands on freed memory. * Trigger model Root (or equivalent -- the codec sysfs attributes are root-writable) writes the reconfig attribute while the card is registered, and the jack private-part kzalloc (jack.c:440, GFP_KERNEL) fails: real memory pressure, or CONFIG_FAILSLAB in a debug kernel (the kernel we tested has no fault injection, hence the targeted injection). Any concurrently opened /dev/snd/controlC* (its ioctl paths hold only the read lock) hits the dangling entry; the card teardown double- frees it. Consequence: use-after-free reads through snd_kcontrol->id/vd[]/ops, a kfree() double free, and a corrupted control namespace (numid/hash entries point at freed memory). * Reproducer (attached: fwmr01.c, fwmr_test.c) and results on 7.3 fwmr01.c is a module that resolves the (static) snd_jack_kctl_new via kallsyms, registers a one-shot kprobe at +0x56 (the instruction after the kzalloc call, guarded by live opcode bytes "48 89 c5" to catch build drift) and clears %rax in its post_handler exactly once while armed -- an allocation-failure injection at the exact audited point, changing no event order. fwmr_test.c hammers SNDRV_CTL_IOCTL_ELEM_LIST + ELEM_INFO on /dev/snd/controlC0. # build (against the tested kernel tree) make -C M=$PWD modules # guest sequence: insmod fwmr01.ko echo 1 > /sys/module/fwmr01/parameters/arm echo 1 > /sys/class/sound/hwC0D0/reconfig # injection fires in the reprobe ./fwmr_test mr01read 20 & # reader hammer echo "$PCIADDR" > /sys/bus/pci/drivers/snd_hda_intel/unbind Results (7.3, 4 vCPU, KASAN): - injection hits inside the reprobe on the first jack creation; 957 KASAN use-after-free reports on a kmalloc-192 object (snd_kcontrol), starting ~1 ms after the injection; - three-stack closure of the first report: allocated by __kmalloc_noprof <- snd_jack_kctl_new+0x15 <- snd_hda_jack_add_kctls (real generic-probe jack creation); freed by kfree <- snd_jack_kctl_new+0xb5 -- that offset *is* the audited error-branch call to snd_ctl_free_one(); used by __snd_ctl_remove+0x5a, snd_ctl_elem_list+0x1fa (Read of size 64 = two elem_id copies from card->controls), snd_ctl_elem_info+0xb8, snd_ctl_release; - negative control (same reconfig + reader + unbind, no injection): 0 reports, clean unbind. Evidence excerpts attached (first-hit three stacks, injection point, hit-site distribution). * Suggested patch Allocate and fully initialize the private part before publishing the kcontrol. With private_free set before snd_ctl_add(), the add()'s own failure path (snd_ctl_add_replace() -> snd_ctl_free_one()) frees both objects correctly, and the audited error branch disappears. The complete patch against 7.3 follows (also kept as patch/0001-fw-mr01.patch); happy to rework per review. ---- patch begins ---- --- --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -433,25 +433,31 @@ if (!kctl) return NULL; + jack_kctl = kzalloc_obj(*jack_kctl); + if (!jack_kctl) { + snd_ctl_free_one(kctl); + return NULL; + } + + jack_kctl->kctl = kctl; + jack_kctl->mask_bits = mask; + INIT_LIST_HEAD(&jack_kctl->list); + + kctl->private_data = jack_kctl; + kctl->private_free = snd_jack_kctl_private_free; + err = snd_ctl_add(card, kctl); if (err < 0) return NULL; - jack_kctl = kzalloc_obj(*jack_kctl); - - if (!jack_kctl) - goto error; - - jack_kctl->kctl = kctl; - jack_kctl->mask_bits = mask; - - kctl->private_data = jack_kctl; - kctl->private_free = snd_jack_kctl_private_free; - return jack_kctl; -error: - snd_ctl_free_one(kctl); - return NULL; } /** ---- patch ends ---- Please keep me in Cc; I am not subscribed to the lists. Regards, Hanxiang Xu xuhx@hust.edu.cn