Fix an OOB bug in parse_audio_mixer_unit
PATCH 1/1 Fix an OOB bug in parse_audio_mixer_unit
The uac_mixer_unit_descriptor shown as below is read from the device side. In parse_audio_mixer_unit, baSourceID field is accessed from index 0 to bNrInPins - 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 + bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.
```
struct uac_mixer_unit_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__u8 bDescriptorSubtype;
__u8 bUnitID;
__u8 bNrInPins;
__u8 baSourceID[];
}
```This patch fixes the bug by add a sanity check on the length of the descriptor.
--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)
sound/usb/mixer.c +9 -0
@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc); + +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err <0 ) {
Signature · 1 line
2.22.1
The attached file is the KASAN report.
Quoted 54 lines · Hui Peng
The
uac_mixer_unit_descriptorshown as below is read from the device side. Inparse_audio_mixer_unit,baSourceIDfield is accessed from index 0 tobNrInPins- 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +9
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc); + +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err < 0) {--
2.22.1
Signature · 1 line
May the *Lord* Richly Bless you and yours !
Quoted 40 lines · Hui Peng
The
uac_mixer_unit_descriptorshown as below is read from the device side. Inparse_audio_mixer_unit,baSourceIDfield is accessed from index 0 tobNrInPins- 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +2
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc);
sound/usb/mixer.c static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, · line 2105
Quoted 10 lines
sound/usb/mixer.c +7
-0+ +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err <0 ) {
Hm, what is the desc->bLength value in the error case?
Basically the buffer boundary is already checked against bLength in snd_usb_find_desc() which is called from obtaining the raw_desc in the caller of this function (parse_audio_unit()).
So, if any, we need to check bLength for the possible overflow like below.
thanks,
Takashi
sound/usb/mixer.c +2 -0
@@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state,return -EINVAL ;if (!desc->bNrInPins)return -EINVAL ; +if (desc->bLength <sizeof (*desc) + desc->bNrInPins) +return -EINVAL ;switch (state->mixer->protocol) {case UAC_VERSION_1 :
Quoted 70 lines · Takashi Iwai
Quoted 40 lines · Hui Peng
The
uac_mixer_unit_descriptorshown as below is read from the device side. Inparse_audio_mixer_unit,baSourceIDfield is accessed from index 0 tobNrInPins- 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +2
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc);sound/usb/mixer.c static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, · line 2105
Quoted 10 lines
sound/usb/mixer.c +7
-0+ +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err <0 ) {Hm, what is the
desc->bLengthvalue in the error case?Basically the buffer boundary is already checked against bLength in
snd_usb_find_desc()which is called from obtaining theraw_descin the caller of this function (parse_audio_unit()).So, if any, we need to check bLength for the possible overflow like below.
thanks,
Takashi
sound/usb/mixer.c static int uac_mixer_unit_get_channels(struct mixer_build *state, · line 748
Quoted 5 lines
sound/usb/mixer.c +2
-0@@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state,return -EINVAL ;if (!desc->bNrInPins)return -EINVAL ; +if (desc->bLength <sizeof (*desc) + desc->bNrInPins) +return -EINVAL ;
VERSION 1 and 2 already have a different check:
if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1) return 0; /* no bmControls -> skip */
So something is possibly off by one. It's just version 3 which doesn't have a check.
regards,
dan carpenter
Quoted 80 lines · Dan Carpenter
Quoted 70 lines · Takashi Iwai
Quoted 40 lines · Hui Peng
The
uac_mixer_unit_descriptorshown as below is read from the device side. Inparse_audio_mixer_unit,baSourceIDfield is accessed from index 0 tobNrInPins- 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +2
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid,struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc);sound/usb/mixer.c static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, · line 2105
Quoted 10 lines
sound/usb/mixer.c +7
-0+ +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err <0 ) {Hm, what is the
desc->bLengthvalue in the error case?Basically the buffer boundary is already checked against bLength in
snd_usb_find_desc()which is called from obtaining theraw_descin the caller of this function (parse_audio_unit()).So, if any, we need to check bLength for the possible overflow like below.
thanks,
Takashi
sound/usb/mixer.c static int uac_mixer_unit_get_channels(struct mixer_build *state, · line 748
Quoted 5 lines
sound/usb/mixer.c +2
-0@@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state,return -EINVAL ;if (!desc->bNrInPins)return -EINVAL ; +if (desc->bLength <sizeof (*desc) + desc->bNrInPins) +return -EINVAL ;VERSION 1 and 2 already have a different check:
Quoted 6 lines
if (desc->bLength < sizeof(*desc) + desc->bNrInPins + 1) return 0; /* no bmControls -> skip */So something is possibly off by one. It's just version 3 which doesn't have a check.
No, both are sensible checks. The first check is about the minimal size that doesn't contain bmControls bitmap which is optional on some devices, while the latter checks about the presence of bmControls field. Note that the latter returns zero, which means no error, while the former returns an error.
thanks,
Takashi
Hi, Takashi:
Here the problem is that desc->bLength is controlled by the device side, so desc->bLength may not represent the real length of the descriptor. That is why I use pointer arithmetic operations to derive the real size of the buffer
in my patch.
Quoted 81 lines · Takashi Iwai
sound/usb/mixer.c static int parse_audio_mixer_unit(struct
Quoted 35 lines · Hui Peng
The
uac_mixer_unit_descriptorshown as below is read from the device side. Inparse_audio_mixer_unit,baSourceIDfield is accessed from index 0 tobNrInPins- 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +0
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct
mixer_build*state, int unitid,Quoted 5 lines
sound/usb/mixer.c +2
-0struct usb_audio_term iterm;int input_pins, num_ins, num_outs;int pin, ich, err; +int desc_len = (int ) ((unsigned long ) state->buffer + + state->buflen - (unsigned long ) raw_desc);sound/usb/mixer.c static int parse_audio_mixer_unit(struct · line 2105
Quoted 10 lines
sound/usb/mixer.c +7
-0+ +if (desc_len <sizeof (*desc) + desc->bNrInPins) { +usb_audio_err (state->chip, +"descriptor %d too short\n" , + unitid); +return -EINVAL ; + } err =uac_mixer_unit_get_channels (state, desc);if (err <0 ) {Hm, what is the
desc->bLengthvalue in the error case?Basically the buffer boundary is already checked against bLength in
snd_usb_find_desc()which is called from obtaining theraw_descin the caller of this function (parse_audio_unit()).So, if any, we need to check bLength for the possible overflow like below.
thanks,
Takashi
sound/usb/mixer.c +2
-0@@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state, return -EINVAL ;if (!desc->bNrInPins)return -EINVAL ; +if (desc->bLength <sizeof (*desc) + desc->bNrInPins) +return -EINVAL ;switch (state->mixer->protocol) {case
UAC_VERSION_1:
Signature · 1 line
May the *Lord* Richly Bless you and yours !
Quoted 7 lines · 彭辉
Hi, Takashi:
Here the problem is thatdesc->bLengthis controlled by the device side, sodesc->bLengthmay not represent the real length of the descriptor. That is why I use pointer arithmetic operations to derive the real size of the buffer
in my patch.
But bLength is checked before calling this, i.e. it's already assured that bLength fits within the buffer limit. So, the result calls don't have to care about the buffer limit itself, and they can just concentrate on overflow over bLength.
thanks,
Takashi
Quoted 87 lines · Takashi Iwai
On Wed, 14 Aug 2019 04:36:24 +0200, Hui Peng wrote: > > The `uac_mixer_unit_descriptor` shown as below is read from the > device side. In `parse_audio_mixer_unit`, `baSourceID` field is > accessed from index 0 to `bNrInPins` - 1, the current implementation > assumes that descriptor is always valid (the length of descriptor > is no shorter than 5 + `bNrInPins`). If a descriptor read from > the device side is invalid, it may trigger out-of-bound memory > access. > > ``` > struct uac_mixer_unit_descriptor { > __u8 bLength; > __u8 bDescriptorType; > __u8 bDescriptorSubtype; > __u8 bUnitID; > __u8 bNrInPins; > __u8 baSourceID[]; > } > ``` > > This patch fixes the bug by add a sanity check on the length of > the descriptor. > > Signed-off-by: Hui Peng <[email protected]> > Reported-by: Hui Peng <[email protected]> > Reported-by: Mathias Payer <[email protected]> > --- > sound/usb/mixer.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c > index 7498b5191b68..38202ce67237 100644 > --- a/sound/usb/mixer.c > +++ b/sound/usb/mixer.c > @@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, > struct usb_audio_term iterm; > int input_pins, num_ins, num_outs; > int pin, ich, err; > + int desc_len = (int) ((unsigned long) state->buffer + > + state->buflen - (unsigned long) raw_desc); > + > + if (desc_len < sizeof(*desc) + desc->bNrInPins) { > + usb_audio_err(state->chip, > + "descriptor %d too short\n", > + unitid); > + return -EINVAL; > + } > > err = uac_mixer_unit_get_channels(state, desc); > if (err < 0) { Hm, what is the desc->bLength value in the error case? Basically the buffer boundary is already checked against bLength in snd_usb_find_desc() which is called from obtaining the raw_desc in the caller of this function (parse_audio_unit()). So, if any, we need to check bLength for the possible overflow like below. thanks, Takashi --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state, return -EINVAL; if (!desc->bNrInPins) return -EINVAL; + if (desc->bLength < sizeof(*desc) + desc->bNrInPins) + return -EINVAL; switch (state->mixer->protocol) { case UAC_VERSION_1:--
May the Lord Richly Bless you and yours !
Hi, Takashi:
Thanks for the guide.
The new patch is confirmed and attached.
Quoted 112 lines · Takashi Iwai
Quoted 3 lines · 彭辉
Hi, Takashi:
Here the problem is thatdesc->bLengthis controlled by the deviceside,
Quoted 2 lines
so
desc->bLengthmay not represent the real length of the descriptor. That is why I use pointer arithmetic operations to derive the real sizeof the
Quoted 2 lines
buffer
in my patch.But bLength is checked before calling this, i.e. it's already assured that bLength fits within the buffer limit. So, the result calls don't have to care about the buffer limit itself, and they can just concentrate on overflow over bLength.
thanks,
Takashi
Quoted 9 lines · Takashi Iwai
On Wed, 14 Aug 2019 04:36:24 +0200,
Hui Peng wrote:
>
> Theuac_mixer_unit_descriptorshown as below is read from the > device side. Inparse_audio_mixer_unit,baSourceIDfield is > accessed from index 0 tobNrInPins- 1, the currentimplementation
Quoted 44 lines
sound/usb/mixer.c static int parse_audio_mixer_unit(struct
Quoted 31 lines
assumes that descriptor is always valid (the length of descriptor is no shorter than 5 +
bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.``` struct uac_mixer_unit_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bDescriptorSubtype; __u8 bUnitID; __u8 bNrInPins; __u8 baSourceID[]; } ```This patch fixes the bug by add a sanity check on the length of the descriptor.
Signed-off-by Hui Peng <[email protected]>Reported-by Hui Peng <[email protected]>Reported-by Mathias Payer <[email protected]>--- sound/usb/mixer.c | 9 +++++++++ 1 file changed, 9 insertions(+)sound/usb/mixer.c +0
-0@@ -2091,6 +2091,15 @@ static int parse_audio_mixer_unit(structmixer_build *state, int unitid, > struct usb_audio_term iterm; > int input_pins, num_ins, num_outs; > int pin, ich, err; > + int desc_len = (int) ((unsigned long) state->buffer + > + state->buflen - (unsigned long) raw_desc); > + > + if (desc_len < sizeof(*desc) + desc->bNrInPins) { > + usb_audio_err(state->chip, > + "descriptor %d too short\n", > + unitid); > + return -EINVAL; > + }Quoted 8 lines
> > err = uac_mixer_unit_get_channels(state, desc); > if (err < 0) { Hm, what is the desc->bLength value in the error case? Basically the buffer boundary is already checked against bLength in snd_usb_find_desc() which is called from obtaining the raw_desc inthe
Quoted 26 lines
caller of this function (parse_audio_unit()). So, if any, we need to check bLength for the possible overflow like below. thanks, Takashi --- a/sound/usb/mixer.c +++ b/sound/usb/mixer.c @@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state, return -EINVAL; if (!desc->bNrInPins) return -EINVAL; + if (desc->bLength < sizeof(*desc) + desc->bNrInPins) + return -EINVAL; switch (state->mixer->protocol) { case UAC_VERSION_1:--
May the Lord Richly Bless you and yours !
Signature · 1 line
May the *Lord* Richly Bless you and yours !
From da17e2db04d29d39f3298738badd036cf3f256d2 Mon Sep 17 00:00:00 2001
From: Hui Peng <[email protected]>
Date: Tue, 13 Aug 2019 22:34:04 -0400
Subject: [PATCH] Fix an OOB bug in parse_audio_mixer_unit
The uac_mixer_unit_descriptor shown as below is read from the device side. In parse_audio_mixer_unit, baSourceID field is accessed from index 0 to bNrInPins - 1, the current implementation assumes that descriptor is always valid (the length of descriptor is no shorter than 5 + bNrInPins). If a descriptor read from the device side is invalid, it may trigger out-of-bound memory access.
```
struct uac_mixer_unit_descriptor {
__u8 bLength;
__u8 bDescriptorType;
__u8 bDescriptorSubtype;
__u8 bUnitID;
__u8 bNrInPins;
__u8 baSourceID[];
}
```This patch fixes the bug by add a sanity check on the length of the descriptor.
--- sound/usb/mixer.c | 2 ++ 1 file changed, 2 insertions(+)
sound/usb/mixer.c +2 -0
@@ -744,6 +744,8 @@ static int uac_mixer_unit_get_channels(struct mixer_build *state,return -EINVAL ;if (!desc->bNrInPins)return -EINVAL ; +if (desc->bLength <sizeof (*desc) + desc->bNrInPins) +return -EINVAL ;switch (state->mixer->protocol) {case UAC_VERSION_1 :
Signature · 1 line
2.22.1
Takashi Iwai
AppliedQuoted 5 lines · 彭辉
Hi, Takashi:
Thanks for the guide.
The new patch is confirmed and attached.
Thanks, applied now.
Takashi