sound: core: jack kcontrol is published before jack_kctl allocation, and the failure path frees it without removing it from card->controls (null-ptr-deref)
From: 徐晗翔
Date: Sun Nov 22 2026 - 02:48:42 EST
snd_jack_kctl_new() publishes a new snd_kcontrol with snd_ctl_add() and
only then allocates the private struct snd_jack_kctl. If that allocation
fails, the error path calls snd_ctl_free_one(kctl) although the control
is still linked in card->controls and card->ctl_hash; snd_ctl_free_one()
does not take controls_rwsem, does not list_del() and does not
remove_hash_entries(). The result is a freed snd_kcontrol that remains
reachable from the card's control list and hash table, so a concurrent
control read (snd_ctl_elem_list(), or the lockless fast path of
snd_ctl_find_id()) can read freed memory and card teardown
(snd_ctl_dev_free() -> __snd_ctl_remove()) will list_del()/kfree() it
again. Upstream's error branch is snd_ctl_remove(card, kctl), not
snd_ctl_free_one(); this tree changed exactly that line.
The same failure-rollback bug has a deterministic companion path:
snd_jack_new() can fail after snd_ctl_add() has already published the
kcontrol (for example, when its subsequent kstrdup() of the jack id
fails), and it also does not undo the publication. On card teardown the
kcontrol's private_free function then runs snd_jack_kctl_private_free()
on a zero-initialized jack_kctl->list and executes list_del() on the
zero page. I reproduced that companion path on Linux 7.3 with KASAN and
a small test module: snd_jack_new() with a 64 MB id returns -ENOMEM after
publishing the kcontrol, and snd_card_free() reports a
null-ptr-deref in snd_jack_kctl_private_free() from
__snd_ctl_remove()/snd_ctl_dev_free().
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 7.3 source tree, KASAN, QEMU/KVM x86_64 (4 vCPU, 4 GB).
CONFIG_SND_JACK=y and CONFIG_SND_JACK_INPUT_DEV=y. Version line in
evidence/kernel-version.txt.
* Root cause
sound/core/jack.c:426-456 (snd_jack_kctl_new):
kctl = snd_kctl_jack_new(name, card);
if (!kctl)
return NULL;
err = snd_ctl_add(card, kctl); /* publishes into card->controls */
if (err < 0)
return NULL; /* snd_ctl_add already freed it */
jack_kctl = kzalloc_obj(*jack_kctl);
if (!jack_kctl)
goto error; /* kctl is already published */
...
return jack_kctl;
error:
snd_ctl_free_one(kctl); /* no snd_ctl_remove() */
return NULL;
snd_ctl_free_one() (sound/core/control.c:326-334) only calls
private_free and kfree(); its own comment says "Don't call this after
the control was added to the card." The correct primitive,
snd_ctl_remove() (control.c:621-627) / __snd_ctl_remove()
(control.c:581-601), takes controls_rwsem for writing, removes the
hash entries, list_del()s the node and only then frees the object.
Upstream jack.c has snd_ctl_remove(card, kctl) in this error branch.
The second half is in snd_jack_new() (jack.c:480-570): after
snd_jack_kctl_new() has published the kcontrol, later failures
(jack allocation, jack->id kstrdup, input device allocation,
snd_device_new) return without removing it; the kcontrol stays in
card->controls with snd_jack_kctl_private_free installed but with a
never-linked (zeroed) jack_kctl->list.
* Consequences
- Dangling list/hash node: card->controls still contains kctl and
card->ctl_hash still maps its id to it after kfree(kctl).
- Concurrent reader: snd_ctl_elem_list() walks card->controls under
controls_rwsem and reads kctl->id; snd_ctl_find_id()'s fast path
(CONFIG_SND_CTL_FAST_LOOKUP) does xa_load() and elem_id_matches()
without the lock. Either can read freed memory.
- Card teardown: snd_ctl_dev_free() (control.c:2457-2479) walks
card->controls and calls __snd_ctl_remove() on the freed node:
list_del() on freed memory and a second kfree().
- Companion path: snd_jack_new() failure leaves the kcontrol with a
zeroed jack_kctl->list; the card teardown runs
snd_jack_kctl_private_free() -> list_del(&jack_kctl->list), which
writes to address 0x8 and crashes deterministically.
* Trigger model
Primary MR-02 branch: memory allocation failure of
kzalloc_obj(*jack_kctl) after a successful snd_ctl_add(). This can be
induced by memory pressure or fault injection; once it happens, the
damage is persistent (no concurrency needed for the teardown crash).
A concurrent control read/remove widens the impact to UAF before
teardown.
Companion branch: any snd_jack_new() caller (ASoC jack, HDA, test
module) whose later allocation fails; the failure returns an error to
the driver but leaves the published kcontrol behind.
* Reproducer (attached) and results on 7.3
sndcore_jack.c creates a card and calls snd_jack_new() with a 64 MB id
string; snd_jack_kctl_new() publishes the kcontrol, then kstrdup()
fails with -ENOMEM. sndcore_test.c writes the module's "free"
parameter to trigger snd_card_free(). Observed
(evidence/mr02-kasan-null-deref.log):
snd_jack_new(huge id) returned -12 (expected -ENOMEM)
BUG: KASAN: null-ptr-deref in snd_jack_kctl_private_free+0x44/0x80
Write of size 8 at addr 0000000000000008
Call Trace:
snd_jack_kctl_private_free+0x44/0x80
__snd_ctl_remove+0x106/0x140
snd_ctl_dev_free+0x53/0xb0
__snd_device_free+0x86/0x150
snd_device_free_all+0xaa/0xd0
release_card_device+0x4a/0x120
KASAN bug count: 1
This is the companion failure path, not the exact
kzalloc_obj(*jack_kctl) branch; the latter is covered by the static
analysis above and by the upstream-vs-tree diff (upstream uses
snd_ctl_remove()). I state this distinction explicitly rather than
claiming the exact branch was forced.
* Suggested direction
- In snd_jack_kctl_new(), replace the error-path
snd_ctl_free_one(kctl) with snd_ctl_remove(card, kctl), restoring
the upstream behavior.
- In snd_jack_new(), add rollback for every failure after the
snd_jack_kctl_new() call (remove the kcontrol or allocate all
resources before publishing).
- Optionally add a debug check in snd_ctl_free_one() that the control
is not linked, to catch this class of API misuse.
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sound/asound.h>
static int write_free(const char *mod)
{
char path[256];
int fd;
snprintf(path, sizeof(path), "/sys/module/%s/parameters/free", mod);
fd = open(path, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open %s: %s\n", path, strerror(errno));
return -1;
}
if (write(fd, "1\n", 2) != 2)
fprintf(stderr, "write %s: %s\n", path, strerror(errno));
close(fd);
return 0;
}
static int do_hwdep(void)
{
struct snd_hwdep_dsp_image img;
int fd;
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwdep: %s\n", strerror(errno));
return 1;
}
memset(&img, 0, sizeof(img));
img.index = 0;
img.length = 0;
img.image = 0;
for (int i = 0; i < 4; i++) {
if (ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img) < 0)
fprintf(stderr, "ioctl dsp_load: %s\n", strerror(errno));
}
close(fd);
printf("HWDEP: ioctl done (max_active should be >= 2)\n");
sleep(1);
return 0;
}
static void *hwdep_thread(void *arg)
{
int fd = *(int *)arg;
struct snd_hwdep_dsp_image img;
memset(&img, 0, sizeof(img));
img.index = 0;
ioctl(fd, SNDRV_HWDEP_IOCTL_DSP_LOAD, &img);
return NULL;
}
static int do_hwdep_race(void)
{
pthread_t th[4];
int fd;
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwdep: %s\n", strerror(errno));
return 1;
}
for (int i = 0; i < 4; i++)
pthread_create(&th[i], NULL, hwdep_thread, &fd);
for (int i = 0; i < 4; i++)
pthread_join(th[i], NULL);
close(fd);
printf("HWDEP: 4 concurrent DSP_LOAD issued\n");
sleep(1);
return 0;
}
static int do_compress(void)
{
int fd;
fd = open("/dev/snd/comprC0D0", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "open compress: %s\n", strerror(errno));
return 1;
}
printf("COMPRESS: fd open, freeing card\n");
write_free("sndcore_compress");
printf("COMPRESS: card free returned, issuing ioctl on stale fd\n");
if (ioctl(fd, 0x4300, 0) < 0)
fprintf(stderr, "ioctl stale fd: %s\n", strerror(errno));
printf("COMPRESS: ioctl returned without KASAN report\n");
close(fd);
sleep(1);
return 0;
}
static int do_rawmidi(void)
{
int fd;
fd = open("/dev/snd/midiC0D0", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open rawmidi: %s\n", strerror(errno));
return 1;
}
printf("RAWMIDI: fd open, letting flood thread run\n");
sleep(2);
printf("RAWMIDI: closing (trigger/cancel/free window)\n");
close(fd);
sleep(3);
printf("RAWMIDI: close returned, freeing card\n");
write_free("sndcore_rawmidi");
sleep(1);
return 0;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s jackfree|compress|rawmidi|hwdep|hwdep-race\n", argv[0]);
return 2;
}
if (!strcmp(argv[1], "jackfree"))
return write_free("sndcore_jack");
if (!strcmp(argv[1], "compress"))
return do_compress();
if (!strcmp(argv[1], "rawmidi"))
return do_rawmidi();
if (!strcmp(argv[1], "hwdep"))
return do_hwdep();
if (!strcmp(argv[1], "hwdep-race"))
return do_hwdep_race();
fprintf(stderr, "unknown mode %s\n", argv[1]);
return 2;
}
Attachment:
sndcore_parent.h
Description: Binary data
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <sound/core.h>
#include <sound/jack.h>
#include "sndcore_parent.h"
static struct snd_card *card;
static int free_set(const char *val, const struct kernel_param *kp)
{
if (card) {
pr_info("free: calling snd_card_free(%p)\n", card);
snd_card_free(card);
card = NULL;
}
return 0;
}
static const struct kernel_param_ops free_ops = { .set = free_set };
module_param_cb(free, &free_ops, NULL, 0200);
static int __init sndcore_jack_init(void)
{
struct snd_jack *jack = NULL;
char *big;
int err;
err = sndcore_card_new(&card, "sndcore_jack");
if (err < 0)
return err;
big = kvmalloc(64 * 1024 * 1024, GFP_KERNEL);
if (!big) {
snd_card_free(card);
card = NULL;
sndcore_parent_put();
return -ENOMEM;
}
memset(big, 'A', 64 * 1024 * 1024 - 1);
big[64 * 1024 * 1024 - 1] = 0;
err = snd_jack_new(card, big, SND_JACK_HEADPHONE, &jack, true, false);
pr_info("snd_jack_new(huge id) returned %d (expected -ENOMEM)\n", err);
if (!err)
pr_warn("unexpected success: jack=%p\n", jack);
kvfree(big);
err = snd_card_register(card);
if (err < 0) {
snd_card_free(card);
card = NULL;
sndcore_parent_put();
return err;
}
pr_info("card registered, kcontrol is published but jack is NULL\n");
return 0;
}
static void __exit sndcore_jack_exit(void)
{
if (card) {
snd_card_free(card);
card = NULL;
}
sndcore_parent_put();
}
module_init(sndcore_jack_init);
module_exit(sndcore_jack_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("sound/core MR-02 jack failure-rollback verifier");