ALSA: hda: reject invalid values in the IEC958 switch put callbacks
PATCH 1/1 ALSA: hda: reject invalid values in the IEC958 switch put callbacks
snd_hda_spdif_out_switch_put(), snd_hda_spdif_in_switch_put() and spdif_share_sw_put() treat any non-zero value as "on", so writes of values outside 0..1, such as 2, -1 or INT_MAX, to these boolean controls succeed instead of being rejected. snd_hda_mixer_amp_switch_put() in the same file already returns -EINVAL for them.
Check the value in the same way before taking spdif_mutex.
---
Tested on a Lenovo V14 G2 ITL (Tiger Lake, SOF, 3 Intel HDMI converters, 7.0.14-based kernel) with raw SNDRV_CTL_IOCTL_ELEM_WRITE calls on the three "IEC958 Playback Switch" controls. Stock: 2, -1 and INT_MAX are all accepted and read back as 1. With this patch applied to the 7.0.14 snd-hda-codec module, all nine invalid writes fail with -EINVAL and leave the value unchanged, 0 and 1 still work, and the card comes up as before (no new messages in dmesg).
The SPDIF in and shared switches don't exist on this machine, so those two hunks are build-tested only (W=1).
sound/hda/common/codec.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
sound/hda/common/codec.c +9 -2
@@ -2353,6 +2353,9 @@ static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,if (WARN_ON (codec->spdif_out.used <= idx))return -EINVAL ; +if (ucontrol->value.integer.value[0 ] <0 || + ucontrol->value.integer.value[0 ] >1 ) +return -EINVAL ;guard (mutex)(&codec->spdif_mutex); spdif =snd_array_elem (&codec->spdif_out, idx); nid = spdif->nid;
@@ -2557,9 +2560,11 @@ static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, {struct hda_codec *codec =snd_kcontrol_chip (kcontrol);struct hda_multi_out *mout = (void *)kcontrol->private_value; -bool val = !!ucontrol->value.integer.value[0 ]; +long val = ucontrol->value.integer.value[0 ];int change; +if (val <0 || val >1 ) +return -EINVAL ;guard (mutex)(&codec->spdif_mutex); change = mout->share_spdif != val; mout->share_spdif = val;
@@ -2632,9 +2637,11 @@ static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, {struct hda_codec *codec =snd_kcontrol_chip (kcontrol);hda_nid_t nid = kcontrol->private_value; -unsigned int val = !!ucontrol->value.integer.value[0 ]; +long val = ucontrol->value.integer.value[0 ];int change; +if (val <0 || val >1 ) +return -EINVAL ;guard (mutex)(&codec->spdif_mutex); change = codec->spdif_in_enable != val;if (change) {
base-commit: 11ef0d515100259c7979f5b4c61b6ea97c6cdc27
Signature · 1 line
2.53.0