UPSTREAM: ALSA: usb-audio: Fix out of bounds reads when finding clock sources

commit a3dd4d63eeb452cfb064a13862fb376ab108f6a6 upstream.

The current USB-audio driver code doesn't check bLength of each
descriptor at traversing for clock descriptors.  That is, when a
device provides a bogus descriptor with a shorter bLength, the driver
might hit out-of-bounds reads.

For addressing it, this patch adds sanity checks to the validator
functions for the clock descriptor traversal.  When the descriptor
length is shorter than expected, it's skipped in the loop.

For the clock source and clock multiplier descriptors, we can just
check bLength against the sizeof() of each descriptor type.
OTOH, the clock selector descriptor of UAC2 and UAC3 has an array
of bNrInPins elements and two more fields at its tail, hence those
have to be checked in addition to the sizeof() check.

Bug: 382239029
Reported-by: Benoît Sevens <bsevens@google.com>
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/20241121140613.3651-1-bsevens@google.com
Link: https://patch.msgid.link/20241125144629.20757-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Benoît Sevens <bsevens@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
(cherry picked from commit 45a92cbc88e4013bfed7fd2ccab3ade45f8e896b)
Signed-off-by: Lee Jones <joneslee@google.com>
Change-Id: I13e916ffd46fce6fd08f7b9f96cea82bb4bc475d
This commit is contained in:
Takashi Iwai
2024-11-25 15:46:16 +01:00
committed by Michael Bestas
parent 9124f74b06
commit 8736793a17

View File

@@ -35,6 +35,10 @@
#include "clock.h" #include "clock.h"
#include "quirks.h" #include "quirks.h"
/* check whether the descriptor bLength has the minimal length */
#define DESC_LENGTH_CHECK(p) \
(p->bLength >= sizeof(*p))
static void *find_uac_clock_desc(struct usb_host_interface *iface, int id, static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
bool (*validator)(void *, int), u8 type) bool (*validator)(void *, int), u8 type)
{ {
@@ -52,36 +56,60 @@ static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
static bool validate_clock_source_v2(void *p, int id) static bool validate_clock_source_v2(void *p, int id)
{ {
struct uac_clock_source_descriptor *cs = p; struct uac_clock_source_descriptor *cs = p;
if (!DESC_LENGTH_CHECK(cs))
return false;
return cs->bClockID == id; return cs->bClockID == id;
} }
static bool validate_clock_source_v3(void *p, int id) static bool validate_clock_source_v3(void *p, int id)
{ {
struct uac3_clock_source_descriptor *cs = p; struct uac3_clock_source_descriptor *cs = p;
if (!DESC_LENGTH_CHECK(cs))
return false;
return cs->bClockID == id; return cs->bClockID == id;
} }
static bool validate_clock_selector_v2(void *p, int id) static bool validate_clock_selector_v2(void *p, int id)
{ {
struct uac_clock_selector_descriptor *cs = p; struct uac_clock_selector_descriptor *cs = p;
return cs->bClockID == id; if (!DESC_LENGTH_CHECK(cs))
return false;
if (cs->bClockID != id)
return false;
/* additional length check for baCSourceID array (in bNrInPins size)
* and two more fields (which sizes depend on the protocol)
*/
return cs->bLength >= sizeof(*cs) + cs->bNrInPins +
1 /* bmControls */ + 1 /* iClockSelector */;
} }
static bool validate_clock_selector_v3(void *p, int id) static bool validate_clock_selector_v3(void *p, int id)
{ {
struct uac3_clock_selector_descriptor *cs = p; struct uac3_clock_selector_descriptor *cs = p;
return cs->bClockID == id; if (!DESC_LENGTH_CHECK(cs))
return false;
if (cs->bClockID != id)
return false;
/* additional length check for baCSourceID array (in bNrInPins size)
* and two more fields (which sizes depend on the protocol)
*/
return cs->bLength >= sizeof(*cs) + cs->bNrInPins +
4 /* bmControls */ + 2 /* wCSelectorDescrStr */;
} }
static bool validate_clock_multiplier_v2(void *p, int id) static bool validate_clock_multiplier_v2(void *p, int id)
{ {
struct uac_clock_multiplier_descriptor *cs = p; struct uac_clock_multiplier_descriptor *cs = p;
if (!DESC_LENGTH_CHECK(cs))
return false;
return cs->bClockID == id; return cs->bClockID == id;
} }
static bool validate_clock_multiplier_v3(void *p, int id) static bool validate_clock_multiplier_v3(void *p, int id)
{ {
struct uac3_clock_multiplier_descriptor *cs = p; struct uac3_clock_multiplier_descriptor *cs = p;
if (!DESC_LENGTH_CHECK(cs))
return false;
return cs->bClockID == id; return cs->bClockID == id;
} }