From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Takashi Iwai <tiwai@suse.de>, Sasha Levin <sashal@kernel.org>,
perex@perex.cz, tiwai@suse.com, wangdicheng@kylinos.cn,
k.kosik@outlook.com, kl@kl.wtf, xristos.thes@gmail.com,
linux-sound@vger.kernel.org
Subject: [PATCH AUTOSEL 6.11 088/244] ALSA: usb-audio: Add input value sanity checks for standard types
Date: Wed, 25 Sep 2024 07:25:09 -0400 [thread overview]
Message-ID: <20240925113641.1297102-88-sashal@kernel.org> (raw)
In-Reply-To: <20240925113641.1297102-1-sashal@kernel.org>
From: Takashi Iwai <tiwai@suse.de>
[ Upstream commit 901e85677ec0bb9a69fb9eab1feafe0c4eb7d07e ]
For an invalid input value that is out of the given range, currently
USB-audio driver corrects the value silently and accepts without
errors. This is no wrong behavior, per se, but the recent kselftest
rather wants to have an error in such a case, hence a different
behavior is expected now.
This patch adds a sanity check at each control put for the standard
mixer types and returns an error if an invalid value is given.
Note that this covers only the standard mixer types. The mixer quirks
that have own control callbacks would need different coverage.
Link: https://patch.msgid.link/20240806124651.28203-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
sound/usb/mixer.c | 35 +++++++++++++++++++++++++++--------
sound/usb/mixer.h | 1 +
2 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index f7ce8e8c3c3ea..2d27d729c3bea 100644
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -1377,6 +1377,19 @@ static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,
#define get_min_max(cval, def) get_min_max_with_quirks(cval, def, NULL)
+/* get the max value advertised via control API */
+static int get_max_exposed(struct usb_mixer_elem_info *cval)
+{
+ if (!cval->max_exposed) {
+ if (cval->res)
+ cval->max_exposed =
+ DIV_ROUND_UP(cval->max - cval->min, cval->res);
+ else
+ cval->max_exposed = cval->max - cval->min;
+ }
+ return cval->max_exposed;
+}
+
/* get a feature/mixer unit info */
static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
@@ -1389,11 +1402,8 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
else
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = cval->channels;
- if (cval->val_type == USB_MIXER_BOOLEAN ||
- cval->val_type == USB_MIXER_INV_BOOLEAN) {
- uinfo->value.integer.min = 0;
- uinfo->value.integer.max = 1;
- } else {
+ if (cval->val_type != USB_MIXER_BOOLEAN &&
+ cval->val_type != USB_MIXER_INV_BOOLEAN) {
if (!cval->initialized) {
get_min_max_with_quirks(cval, 0, kcontrol);
if (cval->initialized && cval->dBmin >= cval->dBmax) {
@@ -1405,10 +1415,10 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,
&kcontrol->id);
}
}
- uinfo->value.integer.min = 0;
- uinfo->value.integer.max =
- DIV_ROUND_UP(cval->max - cval->min, cval->res);
}
+
+ uinfo->value.integer.min = 0;
+ uinfo->value.integer.max = get_max_exposed(cval);
return 0;
}
@@ -1449,6 +1459,7 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct usb_mixer_elem_info *cval = kcontrol->private_data;
+ int max_val = get_max_exposed(cval);
int c, cnt, val, oval, err;
int changed = 0;
@@ -1461,6 +1472,8 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
if (err < 0)
return filter_error(cval, err);
val = ucontrol->value.integer.value[cnt];
+ if (val < 0 || val > max_val)
+ return -EINVAL;
val = get_abs_value(cval, val);
if (oval != val) {
snd_usb_set_cur_mix_value(cval, c + 1, cnt, val);
@@ -1474,6 +1487,8 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
if (err < 0)
return filter_error(cval, err);
val = ucontrol->value.integer.value[0];
+ if (val < 0 || val > max_val)
+ return -EINVAL;
val = get_abs_value(cval, val);
if (val != oval) {
snd_usb_set_cur_mix_value(cval, 0, 0, val);
@@ -2337,6 +2352,8 @@ static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,
if (err < 0)
return filter_error(cval, err);
val = ucontrol->value.integer.value[0];
+ if (val < 0 || val > get_max_exposed(cval))
+ return -EINVAL;
val = get_abs_value(cval, val);
if (val != oval) {
set_cur_ctl_value(cval, cval->control << 8, val);
@@ -2699,6 +2716,8 @@ static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,
if (err < 0)
return filter_error(cval, err);
val = ucontrol->value.enumerated.item[0];
+ if (val < 0 || val >= cval->max) /* here cval->max = # elements */
+ return -EINVAL;
val = get_abs_value(cval, val);
if (val != oval) {
set_cur_ctl_value(cval, cval->control << 8, val);
diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
index d43895c1ae5c6..167fbfcf01ace 100644
--- a/sound/usb/mixer.h
+++ b/sound/usb/mixer.h
@@ -88,6 +88,7 @@ struct usb_mixer_elem_info {
int channels;
int val_type;
int min, max, res;
+ int max_exposed; /* control API exposes the value in 0..max_exposed */
int dBmin, dBmax;
int cached;
int cache_val[MAX_CHANNELS];
--
2.43.0
next parent reply other threads:[~2024-09-25 11:40 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240925113641.1297102-1-sashal@kernel.org>
2024-09-25 11:25 ` Sasha Levin [this message]
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 091/244] ALSA: usb-audio: Support multiple control interfaces Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 092/244] ALSA: usb-audio: Define macros for quirk table entries Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 093/244] ALSA: usb-audio: Replace complex quirk lines with macros Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 094/244] ALSA: usb-audio: Add quirk for RME Digiface USB Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 095/244] ALSA: usb-audio: Add mixer " Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 096/244] ALSA: hda/realtek: Refactor and simplify Samsung Galaxy Book init Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 097/244] ALSA: usb-audio: Add logitech Audio profile quirk Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 098/244] ASoC: codecs: wsa883x: Handle reading version failure Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 099/244] ALSA: control: Take power_ref lock primarily Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 105/244] ALSA: asihpi: Fix potential OOB array access Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 106/244] ALSA: hdsp: Break infinite MIDI input flush loop Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 109/244] ASoC: soc-pcm: Indicate warning if dpcm_playback/capture were used for availability limition Sasha Levin
2024-09-25 12:13 ` Mark Brown
2024-10-06 0:29 ` Sasha Levin
2024-09-25 11:25 ` [PATCH AUTOSEL 6.11 111/244] ASoC: Intel: boards: always check the result of acpi_dev_get_first_match_dev() Sasha Levin
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=20240925113641.1297102-88-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=k.kosik@outlook.com \
--cc=kl@kl.wtf \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=stable@vger.kernel.org \
--cc=tiwai@suse.com \
--cc=tiwai@suse.de \
--cc=wangdicheng@kylinos.cn \
--cc=xristos.thes@gmail.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