From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796131756; b=s4JtrwlsPXXGDF5t19lPcnviALav9ubZYVLjH+fSr+oZRrHmdLbh9oAh2vaKk8yLxL2U mgblK5IRIIzpPkiKVW/UoOcm0O6DAUhuAB/6BfyqLfyM4YXlWw6puyw37/IJUJT3jwfXF xyC40yQ5LYGnBrxLllefrtRmpD4SWgiaObYjOTjby1TbHmAeUTg/EgwYsYASUJv7x8cI4 1rVZxWvQKLwqZ8K5xG13rrRxKfnBeKx18zTEIuETSSM3jQ42Bns97pfCti5qn6qmNBWdw y0GqOMJTSI9UJHK40yx6HcgjIS9Wqv4zSiTVgNKL4mu9V0pkbhSBLnq4HbYrrLwecww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796131756; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=TCdFIdp6Gir4ROw56RqqoQoXoSt/5MiGShnmsNrT2Uo=; b=En39atxcWrbhyzBX6vhmRmqZCLM3mrUq5/dW8viJZSJyF1ibh1uG518Z4VoHcFvtIIQQ ZRRyQ6dg890h/Z+FMGaOa2kcE7UNJCP53baEnYzGlWTYahFzBSXfF45TQLN9gdyRMwX7I IrzWwr+MFV70gL9htQbsVU/CnQ8KTHjyd9S6LUu/59s9xN/yz867cYwz9lk1YwzXYnXOa 8q3MrESDtXi4LedG59t+lHRNkCepY66BUIgIHItOodTN8yo6Wr0NJH8DiGGtskeI6iZ/L +5wwZ7FpI3TkYANiPwM0uIPvW61CpB/6SEwyKMioTUf/CQtBXuPHuL7Y13nhHSYr9+g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ihr0XCpm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C4DF606A329 for ; Tue, 1 Dec 2026 13:29:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ihr0XCpm Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDD9C1F000C1; Tue, 1 Dec 2026 13:29:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796131756; bh=TCdFIdp6Gir4ROw56RqqoQoXoSt/5MiGShnmsNrT2Uo=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=ihr0XCpmgfgY4vvp37mNJ0X4T0QIiIfeaGeoqrtedl+tc+5l1J5bP/ar8Z6WtdDLd 5HEWkF2fBE6LVgPPf1h4P97EL1nuzRnycNUJffJFEeTaiRgeVM+/knLIJs5ON4G1+X /V3LnJZu76vh2b7qh4izK+IgdJC+cu7G405RhiaDEZus7rBZH9aODHYJOx+Udrttfc qfj3kcL5WX8DpG/TiQIgb8W5fE6pWj88ABxjnVirB8qwk8vK2v0TJlFhS04eZ1d676 xnBaMWt/NxAazC+90xZQ5wYOnrq6xkEU4gYHqLzTTNW/XpS3JqIDm+z+w9G3e4JgDk KiBKdepZH6idQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: ALSA: hda/intel: serialize IRQ (de)registration against card teardown (UAF) Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?5b6Q5pmX57+U?= Cc: linux-sound@vger.kernel.org In-Reply-To: <659e49b7.500e.1a231bfe03b.Coremail.xuhx@hust.edu.cn> References: <4c7f9772.192e1.1a17d5c2ed5.Coremail.xuhx@hust.edu.cn> <2026102727-data-sequel-ce41@gregkh> <659e49b7.500e.1a231bfe03b.Coremail.xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 13:29:15 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] ALSA: hda/intel: self-deadlock on chip->irq_mutex in disable_msi_reset_irq() - [High] ALSA: hda/intel: missing Signed-off-by trailer in commit message - [High] ALSA: hda/intel: unscoped guard(mutex) in azx_free() inverts lock order - [Medium] ALSA: hda/intel: inaccurate locking and teardown claims in commit message -- commit 01684f20070bf15ff72cde8229db69092a0e8f9e Author: =E5=BE=90=E6=99=97=E7=BF=94 > ALSA: hda/intel: serialize IRQ (de)registration against card teardown (UA= F) >=20 > 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. [Severity: Medium] Are the concurrency and teardown descriptions in this commit message accurate? Every verb execution and RIRB response wait on a controller holds the bus-wide bus->core.cmd_mutex (in call_exec_verb(), snd_hdac_bus_exec_verb(), and probe_codec()) across the entire command, timeout wait, and azx_rirb_get_response() -> disable_msi_reset_irq() fallback ladder, so two codecs on the same bus cannot enter disable_msi_reset_irq() concurrently. In addition, struct azx is not embedded in the card private area freed by kfree(card): azx_probe() passes extra_size =3D 0 to snd_card_new(), and azx_create() allocates struct hda_intel with devm_kzalloc_obj(&pci->dev, *hda), which lives until devres release after pci_driver->remove returns. During snd_card_free(), snd_device_free_all() also frees all non-lowlevel devices (including SNDRV_DEV_CODEC) in its first pass before calling azx_dev_free() -> azx_free() for SNDRV_DEV_LOWLEVEL in the second pass. Freeing SNDRV_DEV_CODEC runs hda_codec_driver_remove(), which cancels codec->jackpoll_work and calls snd_hda_hdmi_generic_remove() (clearing the DRM audio component notifier and canceling per_pin->work) before azx_free() is entered. Finally, the commit message includes raw bug-report template sections (* Test environment / affected version, * Mechanism, * Trigger model, * Reproducer and results, * Fix (full patch below), * Patch) that duplicate the changelog description. >=20 > The interleavings: >=20 > - 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); >=20 > - pci_free_irq_vectors() concurrent with pci_alloc_irq_vectors(): > half-configured MSI/INTX domain, chip->pci->irq inconsistent > between the racing threads; >=20 > - 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. >=20 > 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. >=20 > 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. >=20 > The detailed description: >=20 > * Test environment / affected version (still present as of 7.4-rc5) >=20 > 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. >=20 > * Mechanism >=20 > - 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. >=20 > - 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. >=20 > - azx_free() (intel.c:1348-1409): chip->running =3D 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 =3D -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. >=20 > - 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. >=20 > * Trigger model >=20 > 1. a real verb timeout (PCIe surprise removal / bus fault / > firmware hang; hbus->no_response_fallback =3D=3D 0 is the default), > with chip->msi enabled (the norm on Intel controllers); >=20 > 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). >=20 > 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. >=20 > * Reproducer and results on 7.3 (still present as of 7.4-rc5) >=20 > 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. >=20 > * Fix (full patch below) >=20 > 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(); >=20 > 2. set hda->freed =3D 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; >=20 > 3. bookkeeping: azx_free() now sets bus->irq =3D -1 after > free_irq() (previously the stale line number survived and made > the double free possible). >=20 > 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. >=20 > * Patch >=20 > 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. >=20 > 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. [Severity: High] The commit message is missing a Signed-off-by: trailer. > diff --git a/sound/hda/controllers/intel.c b/sound/hda/controllers/intel.c > index 1f754f04eea8d..804e1ecb036f8 100644 > --- a/sound/hda/controllers/intel.c > +++ b/sound/hda/controllers/intel.c > @@ -792,8 +792,13 @@ static void azx_clear_irq_pending(struct azx *chip) > static int azx_acquire_irq(struct azx *chip, int do_disconnect) > { > struct hdac_bus *bus =3D azx_bus(chip); > + struct hda_intel *hda =3D container_of(chip, struct hda_intel, chip); > int ret; > =20 > + 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 =3D pci_alloc_irq_vectors(chip->pci, 1, 1, PCI_IRQ_INTX); > if (ret < 0) > @@ -1392,8 +1397,12 @@ static void azx_free(struct azx *chip) > azx_stop_chip(chip); > } > =20 > + /* refuse in-flight verb-timeout fallbacks before the IRQ goes away */ > + hda->freed =3D 1; > + guard(mutex)(&chip->irq_mutex); > if (bus->irq >=3D 0) > - free_irq(bus->irq, (void*)chip); > + free_irq(bus->irq, (void *)chip); > + bus->irq =3D -1; > =20 > azx_free_stream_pages(chip); > azx_free_streams(chip); > @@ -1403,8 +1412,6 @@ static void azx_free(struct azx *chip) > =20 > if (chip->driver_caps & AZX_DCAPS_I915_COMPONENT) > snd_hdac_i915_exit(bus); > - > - hda->freed =3D 1; > } [Severity: High] Does using an unscoped guard(mutex)(&chip->irq_mutex) in azx_free() invert the lock ordering against DRM modeset and component locks? Because guard(mutex) holds chip->irq_mutex until the end of azx_free(), the lock remains held across display_power(chip, false) (which takes bus->lock) and snd_hdac_i915_exit(bus), which calls component_master_del() and intel_audio_component_unbind() to acquire component_mutex and drm_modeset_lock. In the other direction, DRM modeset and component callbacks hold drm_modeset_lock or component_mutex while issuing HDA verbs that can time out and enter disable_msi_reset_irq(): CPU0 CPU1 ---- ---- lock(&chip->irq_mutex) lock(&drm_modeset_lock) lock(&drm_modeset_lock) lock(&chip->irq_mutex) *** DEADLOCK *** CPU0: azx_free() -> snd_hdac_i915_exit() -> snd_hdac_acomp_exit() -> component_master_del() -> ... -> intel_audio_component_unbind() CPU1: intel_audio_codec_enable() -> intel_pin_eld_notify() -> ... -> azx_rirb_get_response() -> disable_msi_reset_irq() hda->freed is also a bitfield in struct hda_intel that is written here before chip->irq_mutex is acquired. Wrapping hda->freed =3D 1, free_irq(), and bus->irq =3D -1 in a scoped_guard(mutex, &chip->irq_mutex) block would release chip->irq_mutex before tearing down the rest of the controller. [ ... ] > @@ -2093,8 +2101,13 @@ static int azx_first_init(struct azx *chip) > static int disable_msi_reset_irq(struct azx *chip) > { > struct hdac_bus *bus =3D azx_bus(chip); > + struct hda_intel *hda =3D container_of(chip, struct hda_intel, chip); > int err; > =20 > + guard(mutex)(&chip->irq_mutex); > + if (hda->freed) > + return -EIO; > + > free_irq(bus->irq, chip); > bus->irq =3D -1; > chip->card->sync_irq =3D -1; [Severity: High] Does disable_msi_reset_irq() self-deadlock on chip->irq_mutex when it calls azx_acquire_irq(chip, 1)? disable_msi_reset_irq() acquires chip->irq_mutex via guard(mutex) for the whole function and then calls azx_acquire_irq(), which immediately tries to lock chip->irq_mutex again on the same thread. Any verb timeout that enters disable_msi_reset_irq() will hang permanently while holding bus->core.cmd_mutex, wedging all subsequent HDA verbs and card teardown. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/659e49b7.500e.1a231= bfe03b.Coremail.xuhx@hust.edu.cn?part=3D1