From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753892Ab2AHPE2 (ORCPT ); Sun, 8 Jan 2012 10:04:28 -0500 Received: from cantor2.suse.de ([195.135.220.15]:51876 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753746Ab2AHPE1 (ORCPT ); Sun, 8 Jan 2012 10:04:27 -0500 Date: Sun, 08 Jan 2012 16:04:21 +0100 Message-ID: From: Takashi Iwai To: Xi Wang Cc: Jaroslav Kysela , Clemens Ladisch , Daniel Mack , Wolfgang Breyha , alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] ALSA: usb-audio: fix possible hang and overflow in parse_uac2_sample_rate_range() In-Reply-To: <4F09A20C.7090705@gmail.com> References: <1325698749-5353-1-git-send-email-xi.wang@gmail.com> <4F09A20C.7090705@gmail.com> User-Agent: Wanderlust/2.15.6 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.7 Emacs/23.3 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At Sun, 08 Jan 2012 09:02:52 -0500, Xi Wang wrote: > > A malicious USB device may feed in carefully crafted min/max/res values, > so that the inner loop in parse_uac2_sample_rate_range() could run for > a long time or even never terminate, e.g., given max = INT_MAX. > > Also nr_rates could be a large integer, which causes an integer overflow > in the subsequent call to kmalloc() in parse_audio_format_rates_v2(). > Thus, kmalloc() would allocate a smaller buffer than expected, leading > to a memory corruption. > > To exploit the two vulnerabilities, an attacker needs physical access > to the machine to plug in a malicious USB device. > > This patch makes two changes. > > 1) The type of "rate" is changed to unsigned int, so that the loop could > stop once "rate" is larger than INT_MAX. > > 2) Limit nr_rates to 1024. > > Suggested-by: Takashi Iwai > Signed-off-by: Xi Wang Thanks, applied now. Takashi > --- > sound/usb/format.c | 8 +++++++- > 1 files changed, 7 insertions(+), 1 deletions(-) > > diff --git a/sound/usb/format.c b/sound/usb/format.c > index 89421d1..e09aba1 100644 > --- a/sound/usb/format.c > +++ b/sound/usb/format.c > @@ -209,6 +209,8 @@ 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 > @@ -226,7 +228,7 @@ static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets, > int min = combine_quad(&data[2 + 12 * i]); > int max = combine_quad(&data[6 + 12 * i]); > int res = combine_quad(&data[10 + 12 * i]); > - int rate; > + unsigned int rate; > > if ((max < 0) || (min < 0) || (res < 0) || (max < min)) > continue; > @@ -253,6 +255,10 @@ 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) { > + snd_printk(KERN_ERR "invalid uac2 rates\n"); > + break; > + } > > /* avoid endless loop */ > if (res == 0) > -- > 1.7.5.4 > > From mboxrd@z Thu Jan 1 00:00:00 1970 From: Takashi Iwai Subject: Re: [PATCH v2] ALSA: usb-audio: fix possible hang and overflow in parse_uac2_sample_rate_range() Date: Sun, 08 Jan 2012 16:04:21 +0100 Message-ID: References: <1325698749-5353-1-git-send-email-xi.wang@gmail.com> <4F09A20C.7090705@gmail.com> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx2.suse.de (cantor2.suse.de [195.135.220.15]) by alsa0.perex.cz (Postfix) with ESMTP id B3C042439E for ; Sun, 8 Jan 2012 16:04:26 +0100 (CET) In-Reply-To: <4F09A20C.7090705@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: alsa-devel-bounces@alsa-project.org Errors-To: alsa-devel-bounces@alsa-project.org To: Xi Wang Cc: alsa-devel@alsa-project.org, Clemens Ladisch , linux-kernel@vger.kernel.org, Daniel Mack , Wolfgang Breyha List-Id: alsa-devel@alsa-project.org At Sun, 08 Jan 2012 09:02:52 -0500, Xi Wang wrote: > > A malicious USB device may feed in carefully crafted min/max/res values, > so that the inner loop in parse_uac2_sample_rate_range() could run for > a long time or even never terminate, e.g., given max = INT_MAX. > > Also nr_rates could be a large integer, which causes an integer overflow > in the subsequent call to kmalloc() in parse_audio_format_rates_v2(). > Thus, kmalloc() would allocate a smaller buffer than expected, leading > to a memory corruption. > > To exploit the two vulnerabilities, an attacker needs physical access > to the machine to plug in a malicious USB device. > > This patch makes two changes. > > 1) The type of "rate" is changed to unsigned int, so that the loop could > stop once "rate" is larger than INT_MAX. > > 2) Limit nr_rates to 1024. > > Suggested-by: Takashi Iwai > Signed-off-by: Xi Wang Thanks, applied now. Takashi > --- > sound/usb/format.c | 8 +++++++- > 1 files changed, 7 insertions(+), 1 deletions(-) > > diff --git a/sound/usb/format.c b/sound/usb/format.c > index 89421d1..e09aba1 100644 > --- a/sound/usb/format.c > +++ b/sound/usb/format.c > @@ -209,6 +209,8 @@ 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 > @@ -226,7 +228,7 @@ static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets, > int min = combine_quad(&data[2 + 12 * i]); > int max = combine_quad(&data[6 + 12 * i]); > int res = combine_quad(&data[10 + 12 * i]); > - int rate; > + unsigned int rate; > > if ((max < 0) || (min < 0) || (res < 0) || (max < min)) > continue; > @@ -253,6 +255,10 @@ 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) { > + snd_printk(KERN_ERR "invalid uac2 rates\n"); > + break; > + } > > /* avoid endless loop */ > if (res == 0) > -- > 1.7.5.4 > >