Harden USB-audio parsers
cover Harden USB-audio parsers
Hi,
as recently spotted by syzkaller, the parser codes in USB audio driver don't check the length of the descriptor unit before actually accessing the extra field, which may lead to out-of-bound access error. This patchset tries to address these by adding proper sanity checks.
Takashi
===
Takashi Iwai (4):
ALSA: usb-audio: Add sanity checks to FE parser
ALSA: usb-audio: Fix potential out-of-bound access at parsing SU
ALSA: usb-audio: Fix potential zero-division at parsing FU
ALSA: usb-audio: Add sanity checks in v2 clock parsers
sound/usb/clock.c | 9 ++++++--- sound/usb/mixer.c | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-)
Signature · 1 line
2.15.0
PATCH 1/4 ALSA: usb-audio: Add sanity checks to FE parser
When the usb-audio descriptor contains the malformed feature unit description with a too short length, the driver may access out-of-bounds. Add a sanity check of the header size at the beginning of parse_audio_feature_unit().
--- sound/usb/mixer.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
sound/usb/mixer.c +12 -0
@@ -1469,6 +1469,12 @@ static int parse_audio_feature_unit(struct mixer_build *state, int unitid,__u8 *bmaControls;if (state->mixer->protocol ==UAC_VERSION_1 ) { +if (hdr->bLength <7 ) { +usb_audio_err (state->chip, +"unit %u: invalid UAC_FEATURE_UNIT descriptor\n" , + unitid); +return -EINVAL ; + } csize = hdr->bControlSize;if (!csize) { usb_audio_dbg(state->chip,
@@ -1486,6 +1492,12 @@ static int parse_audio_feature_unit(struct mixer_build *state, int unitid, } }else {struct uac2_feature_unit_descriptor *ftr = _ftr; +if (hdr->bLength <6 ) { +usb_audio_err (state->chip, +"unit %u: invalid UAC_FEATURE_UNIT descriptor\n" , + unitid); +return -EINVAL ; + } csize =4 ; channels = (hdr->bLength -6 ) /4 -1 ; bmaControls = ftr->bmaControls;
Signature · 1 line
2.15.0
PATCH 2/4 ALSA: usb-audio: Fix potential out-of-bound access at parsing SU
In reply to Takashi IwaiThe usb-audio driver may trigger an out-of-bound access at parsing a malformed selector unit, as it checks the header length only after evaluating bNrInPins field, which can be already above the given length. Fix it by adding the length check beforehand.
--- sound/usb/mixer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
sound/usb/mixer.c +2 -1
@@ -2098,7 +2098,8 @@ static int parse_audio_selector_unit(struct mixer_build *state, int unitid,const struct usbmix_name_map *map;char **namelist; -if (!desc->bNrInPins || desc->bLength <5 + desc->bNrInPins) { +if (desc->bLength <5 || !desc->bNrInPins || + desc->bLength <5 + desc->bNrInPins) {usb_audio_err (state->chip,"invalid SELECTOR UNIT descriptor %d\n" , unitid);return -EINVAL ;
Signature · 1 line
2.15.0
PATCH 3/4 ALSA: usb-audio: Fix potential zero-division at parsing FU
In reply to Takashi Iwaiparse_audio_feature_unit() contains a code dividing potentially with zero when a malformed FU descriptor is passed. Although there is already a sanity check, it checks only the value zero, hence it can still lead to a zero-division when a value 1 is passed there.
Fix it by correcting the sanity check (and the error message thereof).
--- sound/usb/mixer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/usb/mixer.c +2 -2
@@ -1476,9 +1476,9 @@ static int parse_audio_feature_unit(struct mixer_build *state, int unitid,return -EINVAL ; } csize = hdr->bControlSize; -if (!csize) { +if (csize <=1 ) {usb_audio_dbg (state->chip, -"unit %u: invalid bControlSize == 0\n" , +"unit %u: invalid bControlSize <= 1\n" , unitid);return -EINVAL ; }
Signature · 1 line
2.15.0
PATCH 4/4 ALSA: usb-audio: Add sanity checks in v2 clock parsers
In reply to Takashi IwaiThe helper functions to parse and look for the clock source, selector and multiplier unit may return the descriptor with a too short length than required, while there is no sanity check in the caller side. Add some sanity checks in the parsers, at least, to guarantee the given descriptor size, for avoiding the potential crashes.
--- sound/usb/clock.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
sound/usb/clock.c +6 -3
@@ -43,7 +43,7 @@ static struct uac_clock_source_descriptor *while ((cs =snd_usb_find_csint_desc (ctrl_iface->extra, ctrl_iface->extralen, cs,UAC2_CLOCK_SOURCE ))) { -if (cs->bClockID == clock_id) +if (cs->bLength >=sizeof (*cs) && cs->bClockID == clock_id)return cs; }
@@ -59,8 +59,11 @@ static struct uac_clock_selector_descriptor *while ((cs =snd_usb_find_csint_desc (ctrl_iface->extra, ctrl_iface->extralen, cs,UAC2_CLOCK_SELECTOR ))) { -if (cs->bClockID == clock_id) +if (cs->bLength >=sizeof (*cs) && cs->bClockID == clock_id) { +if (cs->bLength <5 + cs->bNrInPins) +return NULL ;return cs; + } }return NULL ;
@@ -75,7 +78,7 @@ static struct uac_clock_multiplier_descriptor *while ((cs =snd_usb_find_csint_desc (ctrl_iface->extra, ctrl_iface->extralen, cs,UAC2_CLOCK_MULTIPLIER ))) { -if (cs->bClockID == clock_id) +if (cs->bLength >=sizeof (*cs) && cs->bClockID == clock_id)return cs; }
Signature · 1 line
2.15.0