]> www.infradead.org Git - users/willy/linux.git/commitdiff
ALSA: usb-audio: Add input value sanity checks for standard types
authorTakashi Iwai <tiwai@suse.de>
Tue, 6 Aug 2024 12:46:50 +0000 (14:46 +0200)
committerTakashi Iwai <tiwai@suse.de>
Tue, 6 Aug 2024 16:27:08 +0000 (18:27 +0200)
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>
sound/usb/mixer.c
sound/usb/mixer.h

index f7ce8e8c3c3eaa0cdee680302afc768436096997..2d27d729c3bea80855eb231220b9dc38829e8828 100644 (file)
@@ -1377,6 +1377,19 @@ no_res_check:
 
 #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);
index d43895c1ae5c6c275b38cb21d539b652b1a8823d..167fbfcf01ace9b67b8502adbd3f694975f6d202 100644 (file)
@@ -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];