Commit 7cc12c1d authored by Hui Peng's avatar Hui Peng Committed by Kleber Sacilotto de Souza

ALSA: usb-audio: Fix a stack buffer overflow bug in check_input_term

BugLink: https://bugs.launchpad.net/bugs/1845036

commit 19bce474 upstream.

`check_input_term` recursively calls itself with input from
device side (e.g., uac_input_terminal_descriptor.bCSourceID)
as argument (id). In `check_input_term`, if `check_input_term`
is called with the same `id` argument as the caller, it triggers
endless recursive call, resulting kernel space stack overflow.

This patch fixes the bug by adding a bitmap to `struct mixer_build`
to keep track of the checked ids and stop the execution if some id
has been checked (similar to how parse_audio_unit handles unitid
argument).
Reported-by: default avatarHui Peng <benquike@gmail.com>
Reported-by: default avatarMathias Payer <mathias.payer@nebelwelt.net>
Signed-off-by: default avatarHui Peng <benquike@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarConnor Kuehl <connor.kuehl@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent 4816e08e
...@@ -81,6 +81,7 @@ struct mixer_build { ...@@ -81,6 +81,7 @@ struct mixer_build {
unsigned char *buffer; unsigned char *buffer;
unsigned int buflen; unsigned int buflen;
DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
struct usb_audio_term oterm; struct usb_audio_term oterm;
const struct usbmix_name_map *map; const struct usbmix_name_map *map;
const struct usbmix_selector_map *selector_map; const struct usbmix_selector_map *selector_map;
...@@ -709,15 +710,24 @@ static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm ...@@ -709,15 +710,24 @@ static int get_term_name(struct mixer_build *state, struct usb_audio_term *iterm
* parse the source unit recursively until it reaches to a terminal * parse the source unit recursively until it reaches to a terminal
* or a branched unit. * or a branched unit.
*/ */
static int check_input_term(struct mixer_build *state, int id, static int __check_input_term(struct mixer_build *state, int id,
struct usb_audio_term *term) struct usb_audio_term *term)
{ {
int err; int err;
void *p1; void *p1;
unsigned char *hdr;
memset(term, 0, sizeof(*term)); memset(term, 0, sizeof(*term));
while ((p1 = find_audio_control_unit(state, id)) != NULL) { for (;;) {
unsigned char *hdr = p1; /* a loop in the terminal chain? */
if (test_and_set_bit(id, state->termbitmap))
return -EINVAL;
p1 = find_audio_control_unit(state, id);
if (!p1)
break;
hdr = p1;
term->id = id; term->id = id;
switch (hdr[2]) { switch (hdr[2]) {
case UAC_INPUT_TERMINAL: case UAC_INPUT_TERMINAL:
...@@ -732,7 +742,7 @@ static int check_input_term(struct mixer_build *state, int id, ...@@ -732,7 +742,7 @@ static int check_input_term(struct mixer_build *state, int id,
/* call recursively to verify that the /* call recursively to verify that the
* referenced clock entity is valid */ * referenced clock entity is valid */
err = check_input_term(state, d->bCSourceID, term); err = __check_input_term(state, d->bCSourceID, term);
if (err < 0) if (err < 0)
return err; return err;
...@@ -764,7 +774,7 @@ static int check_input_term(struct mixer_build *state, int id, ...@@ -764,7 +774,7 @@ static int check_input_term(struct mixer_build *state, int id,
case UAC2_CLOCK_SELECTOR: { case UAC2_CLOCK_SELECTOR: {
struct uac_selector_unit_descriptor *d = p1; struct uac_selector_unit_descriptor *d = p1;
/* call recursively to retrieve the channel info */ /* call recursively to retrieve the channel info */
err = check_input_term(state, d->baSourceID[0], term); err = __check_input_term(state, d->baSourceID[0], term);
if (err < 0) if (err < 0)
return err; return err;
term->type = d->bDescriptorSubtype << 16; /* virtual type */ term->type = d->bDescriptorSubtype << 16; /* virtual type */
...@@ -811,6 +821,15 @@ static int check_input_term(struct mixer_build *state, int id, ...@@ -811,6 +821,15 @@ static int check_input_term(struct mixer_build *state, int id,
return -ENODEV; return -ENODEV;
} }
static int check_input_term(struct mixer_build *state, int id,
struct usb_audio_term *term)
{
memset(term, 0, sizeof(*term));
memset(state->termbitmap, 0, sizeof(state->termbitmap));
return __check_input_term(state, id, term);
}
/* /*
* Feature Unit * Feature Unit
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment