Skip to content

Commit

Permalink
ALSA: usb-audio: avoid integer overflow in create_fixed_stream_quirk()
Browse files Browse the repository at this point in the history
A malicious USB device could feed in a large nr_rates value.  This would
cause the subsequent call to kmemdup() to allocate a smaller buffer than
expected, leading to out-of-bounds access.

This patch validates the nr_rates value and reuses the limit introduced
in commit 4fa0e81 ("ALSA: usb-audio: fix possible hang and overflow
in parse_uac2_sample_rate_range()").

Signed-off-by: Xi Wang <[email protected]>
Signed-off-by: Takashi Iwai <[email protected]>
  • Loading branch information
xiw authored and tiwai committed Feb 15, 2012
1 parent 27c3afe commit 8866f40
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions sound/usb/card.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __USBAUDIO_CARD_H
#define __USBAUDIO_CARD_H

#define MAX_NR_RATES 1024
#define MAX_PACKS 20
#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
#define MAX_URBS 8
Expand Down
4 changes: 1 addition & 3 deletions sound/usb/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audiof
return 0;
}

#define MAX_UAC2_NR_RATES 1024

/*
* Helper function to walk the array of sample rate triplets reported by
* the device. The problem is that we need to parse whole array first to
Expand Down Expand Up @@ -255,7 +253,7 @@ static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets,
fp->rates |= snd_pcm_rate_to_rate_bit(rate);

nr_rates++;
if (nr_rates >= MAX_UAC2_NR_RATES) {
if (nr_rates >= MAX_NR_RATES) {
snd_printk(KERN_ERR "invalid uac2 rates\n");
break;
}
Expand Down
6 changes: 5 additions & 1 deletion sound/usb/quirks.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,14 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
unsigned *rate_table = NULL;

fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
if (! fp) {
if (!fp) {
snd_printk(KERN_ERR "cannot memdup\n");
return -ENOMEM;
}
if (fp->nr_rates > MAX_NR_RATES) {
kfree(fp);
return -EINVAL;
}
if (fp->nr_rates > 0) {
rate_table = kmemdup(fp->rate_table,
sizeof(int) * fp->nr_rates, GFP_KERNEL);
Expand Down

0 comments on commit 8866f40

Please sign in to comment.