ALSA: aoa/i2sbus: unlocked codec_list / dev->pcm check-and-free in i2sbus_detach_codec() can double-free the pcm under concurrent codec detaches (Double free)
From: 徐晗翔
Date: Mon Nov 30 2026 - 23:42:32 EST
i2sbus_detach_codec() (sound/aoa/soundbus/i2sbus/pcm.c:1021-1043) is
the remove callback of the AOA soundbus codec plumbing. It walks and
mutates dev->codec_list with no lock, and ends in an unlocked
check-then-free: "if (list_empty(&dev->codec_list) && dev->pcm)
snd_device_free(dev->pcm->card, dev->pcm)". Two properties make that
a race rather than a benign sequence:
1. The callers hold device locks that do not serialize against each
other. Codec drivers detach from i2c driver context (onyx/tas/
toonie i2c remove -> aoa_codec_unregister() -> codec->exit() ->
i2sbus_detach_codec(), each holding its own i2c client device
lock), while i2sbus/fabric removal runs from the macio/soundbus
device lock context (i2sbus_remove() -> soundbus_remove_one() ->
aoa_fabric_layout_remove() -> aoa_fabric_unlink_codec() ->
codec->exit() -> the same function). No lock is shared between
any two of these entry points, and the function itself takes none.
2. A bus can host more than one codec: MAX_CODECS_PER_BUS is 2
(sound/aoa/fabrics/layout.c:22, codecs[MAX_CODECS_PER_BUS]). Two
codec drivers being removed concurrently (concurrent rmmod of
snd-aoa-codec-onyx and snd-aoa-codec-tas, or concurrent i2c
unbinds) therefore enter i2sbus_detach_codec() on the same
soundbus_dev from two CPUs.
With both threads past their own list_del(&cii->list), both evaluate
list_empty(&dev->codec_list) as true, and both see dev->pcm != NULL
(it is only cleared later, inside i2sbus_private_free(), which runs
from within snd_pcm_free() at the tail of the first snd_device_free()).
Both then call snd_device_free(card, dev->pcm) on the same pcm. The
interleavings are: both enter look_for_dev() and iterate card->devices
while the other thread list_del()s and kfree()s the same struct
snd_device wrapper (use-after-free on the wrapper and card-device-list
corruption), or both find the entry and run __snd_device_free() twice
-- snd_pcm_free() executed twice, including snd_ctl_remove() of the
chmap control and kfree(pcm) twice. Independently of the pcm, the
same snd_device_free() racing snd_card_disconnect()'s
snd_device_disconnect_all() walk (sound/core/device.c:208-216 -- the
card device list has no locking at all) corrupts the list from the
other direction. Consequence: double free / use-after-free /
list corruption, i.e. memory corruption beyond the driver.
The detailed description:
* Test environment / affected version
Source analysis against the mainline 7.3 release tree (unmodified);
sound/aoa/soundbus/i2sbus/pcm.c, sound/aoa/soundbus/soundbus.h,
sound/aoa/fabrics/layout.c and sound/aoa/core/core.c are
byte-identical to mainline 7.3; line numbers below are from 7.3.
No dynamic run was possible: AOA (Apple Onboard Audio,
sound/aoa/) exists only on PowerMac hardware (macio/i2sbus/i2c),
which our x86 QEMU environment cannot instantiate. The related
ALSA-core mechanism (concurrent snd_device_free() vs. the unlocked
card->devices walkers) has been demonstrated separately on 7.3 in a
controlled collision run of another report (14 use-after-free
reports across snd_device_disconnect_all()/snd_hwdep_dev_disconnect);
that collision was harness-driven and is cited only as mechanism
evidence, not as a natural-timing result.
* Mechanism
- Entry points and their locks:
codec driver rmmod/unbind -> aoa_codec_unregister()
(core/core.c:72-82) -> codec->exit = onyx_exit_codec/tas_exit_codec
(onyx.c:966-973, tas.c:838-843) -> dev->detach_codec =
i2sbus_detach_codec (core.c:228-229) [i2c client lock]
i2sbus macio removal -> i2sbus_remove() (i2sbus/core.c:371-379)
-> soundbus_remove_one() -> device_del() ->
aoa_fabric_layout_remove() (layout.c) ->
aoa_fabric_unlink_codec() (core.c:133-148) -> codec->exit() ->
i2sbus_detach_codec [soundbus/macio device lock]
fabric module rmmod -> aoa_fabric_unregister() ->
aoa_fabric_unlink_codec() per codec, then aoa_alsa_cleanup() ->
snd_card_free() [no i2c lock]
- The unchecked sequence: i2sbus_detach_codec() does
list_for_each_entry over dev->codec_list, list_del(&cii->list),
then "if (list_empty(&dev->codec_list) && dev->pcm)
snd_device_free(dev->pcm->card, dev->pcm)". dev->pcm is only
cleared by i2sbus_private_free() (the pcm->private_free callback),
which runs late inside the first snd_device_free() -- leaving a
wide two-thread check-then-free window.
- The double device_free: snd_device_free() looks the entry up in
the card->devices list (look_for_dev(), sound/core/device.c:82-91,
unlocked), list_del()s it and kfree()s the wrapper; two concurrent
calls on the same device_data either both find it (two
__snd_device_free() -> two snd_pcm_free() -> two kfree(pcm), plus
a double list_del of the same node) or the second walks the list
while the first mutates/frees it.
* Trigger model
Root on a PowerMac with an AOA layout that attaches two codecs to
one i2sbus (the layout tables support MAX_CODECS_PER_BUS = 2):
concurrent `rmmod snd-aoa-codec-onyx` and `rmmod snd-aoa-codec-tas`
(or two concurrent i2c unbinds), or a codec rmmod racing
`rmmod snd-aoa-soundbus` / i2sbus device removal. Consequence:
pcm double free, card->devices list corruption and use-after-free
on struct snd_device; the same corruption then propagates into
snd_card_do_free()'s device walk.
* Reproducer
None attached -- the platform is unavailable to us and we did not
want to hand a synthetic hammer for a driver we cannot test; the
interleaving above is directly constructible with the module
lifecycle commands on any PowerMac with a two-codec layout.
* Suggested patch
Serialize codec_list / dev->pcm with a per-driver mutex taken by
both i2sbus_attach_codec() and i2sbus_detach_codec() (the attach
path's list_add and pcm creation are equally unserialized), and
claim dev->pcm before calling snd_device_free() so a concurrent
detach can never re-enter the free. A deeper fix -- anchoring the
pcm lifetime on its device with file-held references, like the
suggested hwdep/rawmidi/pcm patches in the sibling reports -- would
additionally close the open-fd window of this detach path; that core
change is discussed in those mails and out of scope here. The
complete patch against 7.3 follows (also kept as
patch/0001-fw-mr07.patch); happy to rework per review.
---- patch begins ----
---
--- a/sound/aoa/soundbus/i2sbus/pcm.c
+++ b/sound/aoa/soundbus/i2sbus/pcm.c
@@ -862,6 +862,13 @@
struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
struct codec_info_item *cii;
+ /*
+ * codec attach/detach run from i2c driver context while bus removal
+ * runs from soundbus/macio context; the two hold different device
+ * locks, so codec_list and dev->pcm need their own serialization.
+ */
+ guard(mutex)(&i2sbus_pcm_mutex);
+
if (!dev->pcmname || dev->pcmid == -1) {
printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
return -EINVAL;
@@ -1018,10 +1025,19 @@
return err;
}
+static DEFINE_MUTEX(i2sbus_pcm_mutex);
+
void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
{
struct codec_info_item *cii = NULL, *i;
+ guard(mutex)(&i2sbus_pcm_mutex);
list_for_each_entry(i, &dev->codec_list, list) {
if (i->codec_data == data) {
cii = i;
@@ -1035,7 +1051,13 @@
}
/* no more codecs, but still a pcm? */
if (list_empty(&dev->codec_list) && dev->pcm) {
+ struct snd_pcm *pcm = dev->pcm;
+ struct snd_card *card = pcm->card;
+
+ /* claim before freeing: a concurrent detach_codec() must
+ * not re-enter snd_device_free() on the same pcm */
+ dev->pcm = NULL;
/* the actual cleanup is done by the callback above! */
- snd_device_free(dev->pcm->card, dev->pcm);
- }
-}
+ snd_device_free(card, pcm);
+ }
+}
---- patch ends ----
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx