ALSA: hda/intel: serialize IRQ (de)registration against card teardown (UAF)
From: 徐晗翔
Date: Tue Dec 01 2026 - 08:16:48 EST
When an HDA codec verb times out, azx_rirb_get_response() walks a
fallback ladder and, with MSI enabled, calls disable_msi_reset_irq()
(sound/hda/controllers/intel.c:2088-2103): free_irq(bus->irq, chip),
pci_free_irq_vectors(), then azx_acquire_irq() ->
pci_alloc_irq_vectors() + request_irq(chip->pci->irq, azx_interrupt,
IRQF_SHARED, chip->card->irq_descr, chip) (intel.c:792-817). This
compound sequence runs with no lock at all, from contexts that never
take the PCI device lock - the jackpoll delayed work
(sound/hda/common/codec.c:640-654) and the DRM audio component
notification path (sound/hda/codecs/hdmi/intelhdmi.c:157-174; the
callback only filters PM_EVENT_SUSPEND) - and per-codec regmaps only
serialize verbs within one codec, so on multi-codec systems two
contexts can enter disable_msi_reset_irq() on the same chip
simultaneously (each codec waits on its own regmap; the RIRB response
slots are per-address). Meanwhile the teardown path
azx_remove() -> snd_card_free() -> ... -> azx_dev_free() -> azx_free()
executes free_irq(bus->irq, chip) (intel.c:1396-1397) equally
unlocked, reads bus->irq without resetting it to -1 afterwards, and
proceeds to release the chip memory (struct azx is embedded in the
card private area, freed by kfree(card)) while
snd_hdac_i915_exit() - which finally unregisters the DRM audio
component that feeds the notify path - only runs at the very end of
azx_free() (intel.c:1405-1406), i.e. after free_irq.
The interleavings:
- double free_irq: azx_free() reads a stale bus->irq and frees the
same line the fallback path is also freeing (or the fallback
self-converges from two codecs at once);
- pci_free_irq_vectors() concurrent with pci_alloc_irq_vectors():
half-configured MSI/INTX domain, chip->pci->irq inconsistent
between the racing threads;
- request-after-free (worst case): the fallback's request_irq()
completes AFTER azx_free()'s free_irq and after kfree(card): the
handler azx_interrupt is then registered on a shared INTX line
with a dev_id pointing into freed memory - every interrupt from
any line sharer dereferences freed chip/bus/reg_lock state
(sound/hda/common/controller.c:1061-1110), and chip->card->irq_descr
(the request_irq name string, init.c:346-347) is read from freed
memory at registration time.
Each verb costs up to two 1000ms response timeouts before the
fallback is entered (snd_hdac_bus_get_response_rirb,
sound/hda/core/controller.c:322 "msecs_to_jiffies(1000)"; the response
path retries once in polling mode), so the in-flight window is about
two seconds wide - far wider than a concurrent unbind. Note the verb
paths themselves have no shutdown gate (codec_exec_() and the bus
command path never check bus->shutdown), so a teardown cannot stop an
in-flight verb from entering the fallback.
Source-analysis based: verified environment boundary documented below.
The locking gap itself is plain from the code - free_irq /
pci_alloc_irq_vectors / request_irq are individually irq-subsystem
safe, but the compound "free -> free-vectors -> alloc-vectors ->
request" has no caller-level atomicity, and nothing serializes it
against object release.
The detailed description:
* Test environment / affected version (still present as of 7.4-rc5)
Source-verified on Linux 7.3.0 and RE-CHECKED on Linux 7.4.0-rc5
(#1, 2026-12-01) - both vanilla mainline trees, unmodified sources,
KASAN enabled, QEMU/KVM x86_64 guest with -device intel-hda
+ hda-duplex, snd_hda_intel builtin. Re-check result: the fallback
ladder (azx_rirb_get_response), disable_msi_reset_irq(),
azx_acquire_irq() and the azx_free() IRQ section are all unchanged
in 7.4-rc5 (the rc5 diff to intel.c touches only probe-failure
handling, quirks and a devm allocation helper); 7.4-rc5 adds a new
snd_hdac_bus_reset_response_counter() call after response timeouts,
which unblocks later verbs to the same codec but does not touch the
IRQ (de)registration path. The proposed patch applies cleanly to
7.4-rc5 (patch --dry-run passes). The audited files
(sound/hda/controllers/intel.c, sound/hda/common/controller.c,
sound/hda/core/controller.c, sound/hda/common/codec.c) are
byte-identical between the tested tree and the report baseline;
line numbers below are from 7.3.
* Mechanism
- disable_msi_reset_irq() (intel.c:2088-2103) is only entered from
azx_rirb_get_response() when chip->msi is set; it then clears
chip->msi (L2097) and azx_acquire_irq() takes the INTX branch
(L797-802). If pci_alloc_irq_vectors(PCI_IRQ_INTX) itself fails,
the function returns -EIO without any disconnect; only "INTX
allocation succeeded but request_irq() failed" (-EBUSY: line
exclusively claimed or IRQF_TRIGGER mismatch with an existing
action; -ENOMEM) reaches snd_card_disconnect(chip->card) at
L811 - which is the in-flight-disconnect trigger of the
companion card-lifecycle report.
- The IRQ bookkeeping state (bus->irq, chip->msi,
card->sync_irq at intel.c L814-815/L2094-2095, init.c:540-541) is
plain unlocked integer state shared by the fallback context, the
PM resume path and the teardown.
- azx_free() (intel.c:1348-1409): chip->running = 0 (L1363) does
not stop the notify path (intel_pin_eld_notify() checks only the
codec power state, intelhdmi.c:169-170); free_irq runs at
L1396-1397 with no bus->irq = -1 bookkeeping and no
synchronization; snd_hdac_i915_exit() (acomp unregister) runs at
L1405-1406, after free_irq - so a hotplug notification arriving
during azx_free() can still send verbs, time out and enter the
fallback while the IRQ is already gone.
- azx_dev_free() (intel.c:1422-1426) is the SNDRV_DEV_LOWLEVEL
dev_free: in snd_card_free()'s release phase it runs after the
codec devices were already freed by the same
snd_device_free_all() pass, so the fallback can also race kfree()
of the codec structures it is sending verbs to.
* Trigger model
1. a real verb timeout (PCIe surprise removal / bus fault /
firmware hang; hbus->no_response_fallback == 0 is the default),
with chip->msi enabled (the norm on Intel controllers);
2. plus either (a) a concurrent PCI unbind/hot-remove, or (b) a
second codec timing out at the same time (jackpoll_ms module
parameter arms the poll work; hotplug/modeset arms the DRM
notify path; GFHDMI/LOONGSON/ZHAOXIN controllers default to
polling mode and time out more readily, intel.c:1908-1935).
Under (a)/(b) the outcomes are the interleavings listed above:
double free_irq (irqaction UAF / "Trying to free already-free
IRQ"), torn vector-domain state, or azx_interrupt() registered with
a freed dev_id on a shared INTX line (interrupt storm / UAF on
every line trigger). Requires root/admin-level operations or a
hardware fault to arm; consequence: kernel crash / DoS, with the
freed-handler registration as a corruption primitive.
* Reproducer and results on 7.3 (still present as of 7.4-rc5)
Dynamic verification attempted in the KASAN QEMU environment with
jackpoll_ms armed (500ms, then the 50ms parameter floor) and PCI
remove+rescan churn overlapping reconfig/PCM verb traffic: QEMU
performs orderly PCI removal - the ->remove callback completes
before any MMIO/CORB-RIRB resource disappears - so no verb ever
times out and disable_msi_reset_irq() is never entered (both runs
clean; one run wedged in the unrelated card-lifecycle hang of the
companion mail). This mail is therefore source-analysis based: the
missing serialization is directly visible in the code quoted above,
and the environment boundary (orderly removal vs surprise removal)
is documented rather than papered over. Logs retained.
* Fix (full patch below)
1. add chip->irq_mutex and hold it across the whole compound
sequence in disable_msi_reset_irq()/azx_acquire_irq() and around
azx_free()'s free_irq();
2. set hda->freed = 1 BEFORE the IRQ teardown in azx_free() (today
it is only set at the very end) and refuse new
azx_acquire_irq()/disable_msi_reset_irq() work when set -
closing the request-after-free face, since a fallback waiting on
the mutex bails out instead of registering a handler on freed
memory;
3. bookkeeping: azx_free() now sets bus->irq = -1 after
free_irq() (previously the stale line number survived and made
the double free possible).
The patch is regenerated against 7.4-rc5 (patch --dry-run passes
cleanly) and reviewed by inspection; it has not been runtime-
validated (see the environment boundary above). Happy to test any
counter-proposal.
* Patch
disable_msi_reset_irq() (verb-timeout fallback) tears down and re-arms
the controller IRQ with no locking, from jackpoll work / DRM audio
component contexts, and azx_free() frees the same IRQ without any
synchronization and without clearing bus->irq. Interleavings range
from double free_irq() and torn PCI vector-domain state to
request_irq() completing after the chip memory was freed, leaving a
shared-line handler with a dev_id into freed memory.
Serialize all IRQ (de)registration under chip->irq_mutex, set
hda->freed before the IRQ teardown and refuse fallback work once set,
and fix the bus->irq bookkeeping.
--- a/sound/hda/common/hda_controller.h
+++ b/sound/hda/common/hda_controller.h
@@ -119,6 +119,7 @@
/* locks */
struct mutex open_mutex; /* Prevents concurrent open/close operations */
+ struct mutex irq_mutex; /* serializes IRQ (de)registration vs teardown */
/* PCM */
struct list_head pcm_list; /* azx_pcm list */
--- a/sound/hda/controllers/intel.c
+++ b/sound/hda/controllers/intel.c
@@ -792,7 +792,12 @@
static int azx_acquire_irq(struct azx *chip, int do_disconnect)
{
struct hdac_bus *bus = azx_bus(chip);
+ struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
int ret;
+
+ guard(mutex)(&chip->irq_mutex);
+ if (hda->freed)
+ return -EIO;
if (!chip->msi || pci_alloc_irq_vectors(chip->pci, 1, 1, PCI_IRQ_MSI) < 0) {
ret = pci_alloc_irq_vectors(chip->pci, 1, 1, PCI_IRQ_INTX);
@@ -1393,8 +1398,12 @@
azx_stop_chip(chip);
}
+ /* refuse in-flight verb-timeout fallbacks before the IRQ goes away */
+ hda->freed = 1;
+ guard(mutex)(&chip->irq_mutex);
if (bus->irq >= 0)
- free_irq(bus->irq, (void*)chip);
+ free_irq(bus->irq, (void *)chip);
+ bus->irq = -1;
azx_free_stream_pages(chip);
azx_free_streams(chip);
@@ -1404,8 +1413,6 @@
if (chip->driver_caps & AZX_DCAPS_I915_COMPONENT)
snd_hdac_i915_exit(bus);
-
- hda->freed = 1;
}
static int azx_dev_disconnect(struct snd_device *device)
@@ -1802,6 +1809,7 @@
chip = &hda->chip;
mutex_init(&chip->open_mutex);
+ mutex_init(&chip->irq_mutex);
chip->card = card;
chip->pci = pci;
chip->ops = &pci_hda_ops;
@@ -2088,7 +2096,12 @@
static int disable_msi_reset_irq(struct azx *chip)
{
struct hdac_bus *bus = azx_bus(chip);
+ struct hda_intel *hda = container_of(chip, struct hda_intel, chip);
int err;
+
+ guard(mutex)(&chip->irq_mutex);
+ if (hda->freed)
+ return -EIO;
free_irq(bus->irq, chip);
bus->irq = -1;
--
2.47.1
Please keep me in Cc; I am not subscribed to the lists.
Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx