mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arnav Kapoor <kapoorarnav43@gmail.com>
To: Takashi Iwai <tiwai@suse.com>
Cc: Jaroslav Kysela <perex@perex.cz>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	Arnav Kapoor <kapoorarnav43@gmail.com>
Subject: [PATCH] ALSA: hda: reject invalid values in the IEC958 switch put callbacks
Date: Mon, 23 Nov 2026 22:32:01 +0530	[thread overview]
Message-ID: <20261123170203.243633-1-kapoorarnav43@gmail.com> (raw)

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.

Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
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(-)

diff --git a/sound/hda/common/codec.c b/sound/hda/common/codec.c
index 51189e305ece..0470f4e085f7 100644
--- a/sound/hda/common/codec.c
+++ b/sound/hda/common/codec.c
@@ -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
-- 
2.53.0


                 reply	other threads:[~2026-11-23 17:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123170203.243633-1-kapoorarnav43@gmail.com \
    --to=kapoorarnav43@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®