ALSA: x86: hdmi: fix chmap array missing terminator entry
Subsystems (2)
PATCH 1/1 ALSA: x86: hdmi: fix chmap array missing terminator entry
had_build_channel_allocation_map() allocates a single struct snd_pcm_chmap_elem via kzalloc() and stores it as intelhaddata->chmap->chmap. struct snd_pcm_chmap.chmap is expected to be a zero-terminated array (see snd_pcm_std_chmaps[]), but had_register_chmap_ctls() never overrides the TLV callback installed by snd_pcm_add_chmap_ctls(), so the generic pcm_chmap_ctl_tlv() from ALSA core is left in place.
pcm_chmap_ctl_tlv() is reachable by any local user via SNDRV_CTL_IOCTL_TLV_READ on the "Playback Channel Map" control and walks the array with "for (map = info->chmap; map->channels; map++)". Since the driver's single element is never terminated, this walks past the allocation. The same pointer is also freed on hot-unplug and rebuilt on hot-plug under intelhaddata->mutex, while the TLV callback runs under card->controls_rwsem without that mutex, so the walk can also race a concurrent free of the array.
Fix this by allocating a second, zeroed element so the array is properly terminated, and by dropping the inherited TLV callback entirely at registration time, since the driver never implements one of its own.
--- sound/x86/intel_hdmi_audio.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
sound/x86/intel_hdmi_audio.c +8 -1
@@ -475,7 +475,8 @@ static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)kfree (intelhaddata->chmap->chmap); intelhaddata->chmap->chmap =NULL ; - chmap =kzalloc_obj (*chmap); +/* info->chmap must be zero-terminated; allocate a spare terminator entry */ + chmap =kzalloc_objs (*chmap,2 );if (!chmap)return ;
@@ -575,6 +576,12 @@ static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata, intelhaddata->chmap->kctl->info = had_chmap_ctl_info; intelhaddata->chmap->kctl->get = had_chmap_ctl_get; intelhaddata->chmap->chmap =NULL ; + +/* drop the inherited TLV read: it walks chmap without our mutex */ + intelhaddata->chmap->kctl->tlv.c =NULL ; + intelhaddata->chmap->kctl->vd[0 ].access &= + ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK ); +return 0 ; }
Signature · 1 line
2.56.0
Takashi Iwai
AppliedQuoted 18 lines · Peter Ujfalusi
had_build_channel_allocation_map()allocates a single structsnd_pcm_chmap_elemviakzalloc()and stores it asintelhaddata->chmap->chmap. struct snd_pcm_chmap.chmap is expected to be a zero-terminated array (see snd_pcm_std_chmaps[]), buthad_register_chmap_ctls()never overrides the TLV callback installed bysnd_pcm_add_chmap_ctls(), so the genericpcm_chmap_ctl_tlv()from ALSA core is left in place.
pcm_chmap_ctl_tlv()is reachable by any local user viaSNDRV_CTL_IOCTL_TLV_READon the "Playback Channel Map" control and walks the array with "for (map =info->chmap;map->channels; map++)". Since the driver's single element is never terminated, this walks past the allocation. The same pointer is also freed on hot-unplug and rebuilt on hot-plug underintelhaddata->mutex, while the TLV callback runs undercard->controls_rwsemwithout that mutex, so the walk can also race a concurrent free of the array.
Quoted 8 lines
Fix this by allocating a second, zeroed element so the array is properly terminated, and by dropping the inherited TLV callback entirely at registration time, since the driver never implements one of its own.
Fixes 5dab11d89777 ("ALSA: x86: hdmi: Add audio support for BYT and CHT")Signed-off-by Peter Ujfalusi <[email protected]>
Applied to for-next branch. Thanks.
Takashi