ALSA: hda/hdmi: hold pcm_lock for the unlocked hdmi_present_sense() callers (Data race)

From: 徐晗翔

Date: Tue Dec 01 2026 - 08:18:48 EST


hdmi_present_sense() parses display ELD data into a single shared
scratch buffer, struct hdmi_spec::temp_eld (sound/hda/codecs/hdmi/
hdmi_local.h:143, one instance per codec), and update_eld() then
copies that buffer into the per-pin sink_eld and drives PCM-slot
attachment from it. The parsing helpers (hdmi_present_sense_via_verbs()
and sync_eld_via_acomp()) only take the *per-pin* mutex around the
fill-and-consume window (hdmi.c:1269/1383) - the buffer itself is
per-spec, so two pins parsed concurrently on different CPUs overwrite
each other's data while each holds a *different* mutex. The outer
spec->pcm_lock exists for exactly this serialization and IS taken by
the DRM hotplug notification path (snd_hda_hdmi_check_presence_and_report(),
hdmi.c:592) and by the ELD repoll work (hdmi_repoll_eld(),
hdmi.c:1428) - but two callers do not take it:

- snd_hda_hdmi_generic_build_controls() (hdmi.c:1960, reached from
codec probe, i.e. from the HDA reconfig path) and
- snd_hda_hdmi_generic_resume() (hdmi.c:2094, wired as
patch_ops.resume and reached on every runtime/system resume).

Consequences when a hotplug notification or a repoll races the
unlocked probe/resume call (multi-pin HDMI/DP codecs are the norm on
Intel desktop parts): update_eld() on pin A reads pin B's ELD data
mid-overwrite (cross-pin ELD mismatch - the wrong monitor
capabilities/EDID are published through the ELD kcontrol and
snd_jack_report()), torn eld_size/eld_valid reads give the
sink_eld memcpy a mismatched length, and the unlocked
hdmi_attach_hda_pcm()/hdmi_pcm_setup_pin() inside update_eld() do an
unlocked hdmi_find_pcm_slot() bitmap scan plus bare writes of
per_pin->pcm_idx/per_pin->pcm/spec->pcm_bitmap (hdmi.c:1013-1035,
1066-1107), which can double-bind the same PCM slot to two pins
against the pcm_lock-holding PCM open/close paths (hdmi.c:1656/1739).

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 -
both vanilla mainline trees. Re-check result: the 7.4-rc5 diff to
sound/hda/codecs/hdmi/hdmi.c is 5 lines (a cvt_nids array bound fix
and a typo) and does not touch spec->temp_eld, the unlocked call
sites (now at hdmi.c:1961/2095) or the lock-domain mismatch - the
race is unchanged in 7.4-rc5. The proposed patch applies cleanly
to 7.4-rc5 (patch --dry-run passes). The audited files are
byte-identical between the report baseline and the tested tree.
This mail is source-analysis based (see the environment note at
the end).

* Mechanism

Lock domain vs data ownership mismatch:

- temp_eld is per-spec (hdmi_local.h:143) but its fill-and-consume
window is guarded only by per_pin->lock (hdmi.c:1269 for the
verb path, 1383 for the audio-component path) - different pins
hold different mutexes over the same buffer;

- the pcm_lock-holding callers: snd_hda_hdmi_check_presence_and_report()
(hdmi.c:584-595, L592; entered from the DRM audio component
pin_eld_notify callback and from unsolicited events) and
hdmi_repoll_eld() (hdmi.c:1412-1430, L1428; 300ms retry work,
scheduled from update_eld() at hdmi.c:1154-1157);

- the unlocked callers: snd_hda_hdmi_generic_build_controls()
(hdmi.c:1950-1961, L1960 - codec probe/reconfig) and
snd_hda_hdmi_generic_resume() (hdmi.c:2083-2095, L2094 - wired
as generichdmi_codec_ops.resume, hdmi.c:2328; runtime/system
resume);

- the notify path cannot be excluded on the resume/probe side:
intel_pin_eld_notify() only filters PM_EVENT_SUSPEND
(intelhdmi.c:169-170), so a modeset/hotplug notification
concurrent with a runtime resume or a reconfig reaches the
same code.

Races:

1. cross-pin overwrite: thread A (notify, pcm_lock +
per_pin1->lock) fills temp_eld for pin1 while thread B
(resume, per_pin2->lock only) overwrites it for pin2; A's
update_eld() then copies pin2's ELD into pin1's sink_eld
(hdmi.c:1197-1213) and publishes it via the ELD control event
(L1226-1230) and snd_jack_report() (L1232-1235);

2. torn fields: eld_size/eld_valid/eld_buffer are written
non-atomically; a racing update_eld() can memcpy with a
mismatched length (bounded by ELD_MAX_SIZE, so this is state
corruption, not memory unsafety - the reason this is reported
as a functional data race rather than a security issue);

3. PCM slot binding: hdmi_attach_hda_pcm() does an unlocked
hdmi_find_pcm_slot() scan (hdmi.c:1001-1011) and bare
set_bit/clear_bit on spec->pcm_bitmap plus bare writes of
per_pin->pcm_idx/pcm (L1032-1034); two concurrent update_eld()
calls can select the same slot for two pins, and the bare
writes race the pcm_lock-holding open/close paths that base
stream configuration on per_pin->pcm.

* Trigger model

HDMI/DP codec with at least two pins (standard on Intel desktop
HDA), plus a display hotplug/modeset (DRM audio component notify)
or an ELD repoll landing inside a probe (reconfig) or a
runtime/system resume. The repoll mechanism itself (300ms retries
while the sink ELD is not ready, hdmi.c:1154-1157) widens the
window. Consequence: wrong ELD published to user space, wrong
jack state, PCM misrouting (audio silent/routed to the wrong
monitor); the pcm slot double-bind can wedge subsequent stream
setup.

* Fix

Take spec->pcm_lock around the two unlocked hdmi_present_sense()
call sites (build_controls and generic_resume), matching the lock
domain the notify/repoll callers already use. Lock order is
unchanged: pcm_lock is taken before per_pin->lock exactly as in
snd_hda_hdmi_check_presence_and_report() and hdmi_repoll_eld().
A per-pin scratch buffer would be the belt-and-braces alternative;
the two-line lock fix is the minimal, order-safe change.

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 note). Happy to test any
counter-proposal.

* Patch

hdmi_present_sense() parses the display ELD into the single shared
spec->temp_eld scratch buffer; the DRM notify and ELD repoll paths
hold spec->pcm_lock around it, but snd_hda_hdmi_generic_build_controls()
(probe/reconfig) and snd_hda_hdmi_generic_resume() call it with only
the per-pin lock. Two pins parsed concurrently share one buffer
while holding different per-pin mutexes, so one pin's update_eld()
copies the other pin's ELD into its sink_eld (cross-pin mismatch,
torn size/valid reads) and the unlocked slot-attach path races the
pcm_lock-protected PCM open/close state.

Take pcm_lock around the two call sites; the lock order
(pcm_lock -> per_pin->lock) is the one the existing callers already
use.

--- a/sound/hda/codecs/hdmi/hdmi.c
+++ b/sound/hda/codecs/hdmi/hdmi.c
@@ -1957,7 +1957,8 @@
}

pin_eld->eld_valid = false;
- hdmi_present_sense(per_pin, 0);
+ scoped_guard(mutex, &spec->pcm_lock)
+ hdmi_present_sense(per_pin, 0);
}

/* add channel maps */
@@ -2091,7 +2092,8 @@

for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
- hdmi_present_sense(per_pin, 1);
+ scoped_guard(mutex, &spec->pcm_lock)
+ hdmi_present_sense(per_pin, 1);
}
return 0;
}

--
2.47.1


Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx