Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp50408ybl; Thu, 15 Aug 2019 12:26:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqw2Chvih0tOEAbZBebXhxNIyB9m7CnkGL+cgNxhcNj/swNSWbRQ28IVh+AnyHrCKZUnhQZD X-Received: by 2002:a17:902:524:: with SMTP id 33mr5805825plf.27.1565897185843; Thu, 15 Aug 2019 12:26:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565897185; cv=none; d=google.com; s=arc-20160816; b=w1wL1F/3SfbO6FaZLd2WMIoldJcUIwDXAFq8gJO/2MZsSeJlbbqXc/stXjkgdPvdDY iAmrLAN3Q9snjc9dvB6qMM8HpKTKprIC9Wf7jxSzNVEYUrtZplnDD6MB3amfvpGWANNA DQ4A0+av32HS1IN2/+UwsTGvLHcJsKq1XIvL7u+Ni3LRya9nLu5SrsqJABSy3FuCBFEa X3eZ2W600yRnu4CnAYaVao/SlyW3Sum1vts51LbaFgHAFMzBQ0qxxDDwHx+Hm3/QEwzQ eHxks+WGhpRXB6GDQEgIMepb5tKyPHAFO8OxFIbimYtcYcHaRdM/D642Oq+/2zKZ15Ed ImHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date; bh=9BpyjJA8KHqjndUroBjXzqebSa2tm85eZw8xb4bA8Kw=; b=0LLHNHwt9y0eLb2m1AQpMG8a/fGDwvUeaRHsa8ZVkliiXbVw2PGfYdr1nmP4hknZEq OEGTqjQnukP8rL6PfdRlzqt6TYfMri8zTKiM4pyxH7M1Ms2kkHzUF0jtM43CmZ8OUbmx wPqWuNAthPwYmnJBvEan8mYHVqB/k/u6j9IFKN5slvbH3l0GLWqjCJp0k/6dkCmyzw9H 2InfygU0Vc1ylpCohyswIJFd/GUhZQ+AGYKDBzD6mO18lG1XPlqIwvTCmtRSz7tvss92 Ay58aPptAHSrUE3dkwexhETHBD0G3DJAJwgxnwYYaM8wGZn0emtKI3HQKccEC6Xlkrqy JRRA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o1si1485863pjt.51.2019.08.15.12.26.10; Thu, 15 Aug 2019 12:26:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732498AbfHORic (ORCPT + 99 others); Thu, 15 Aug 2019 13:38:32 -0400 Received: from mx2.suse.de ([195.135.220.15]:49714 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728685AbfHORib (ORCPT ); Thu, 15 Aug 2019 13:38:31 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 8BE42AC47; Thu, 15 Aug 2019 17:38:29 +0000 (UTC) Date: Thu, 15 Aug 2019 19:38:27 +0200 Message-ID: From: Takashi Iwai To: Hui Peng Cc: security@kernel.org, alsa-devel@alsa-project.org, YueHaibing , Thomas Gleixner , Allison Randal , Mathias Payer , Jaroslav Kysela , Takashi Iwai , Wenwen Wang , linux-kernel@vger.kernel.org Subject: Re: [PATCH] Fix a stack buffer overflow bug check_input_term In-Reply-To: References: <20190815043554.16623-1-benquike@gmail.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/25.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=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 15 Aug 2019 19:19:10 +0200, Hui Peng wrote: > > Hi, Takashi: > > One point I want to be clear: if an endless recursive loop is detected, should > we return 0, or a negative error code?  An error might be more appropriate, but it's no big deal, as you'll likely hit other errors sooner or later at parsing further :) thanks, Takashi > On Thu, Aug 15, 2019 at 2:58 AM Takashi Iwai wrote: > > On Thu, 15 Aug 2019 08:13:57 +0200, > Takashi Iwai wrote: > > > > On Thu, 15 Aug 2019 06:35:49 +0200, > > Hui Peng wrote: > > > > > > `check_input_term` recursively calls itself with input > > > from device side (e.g., uac_input_terminal_descriptor.bCSourceID) > > > as argument (id). In `check_input_term`, if `check_input_term` > > > is called with the same `id` argument as the caller, it triggers > > > endless recursive call, resulting kernel space stack overflow. > > > > > > This patch fixes the bug by adding a bitmap to `struct mixer_build` > > > to keep track of the checked ids by `check_input_term` and stop > > > the execution if some id has been checked (similar to how > > > parse_audio_unit handles unitid argument). > > > > > > Reported-by: Hui Peng > > > Reported-by: Mathias Payer > > > Signed-off-by: Hui Peng > > > > The fix looks almost good, but we need to be careful about the > > bitmap check.  In theory, it's possible that multiple nodes point to > > the same input terminal, and your patch would break that scenario. > > For fixing that, we need to zero-clear the termbitmap at each first > > invocation of check_input_term(), something like below. > > > > Could you check whether this works? > > Thinking of this further, there is another possible infinite loop. > Namely, when the feature unit in the input terminal chain points to > itself as the source, it'll loop endlessly without the stack > overflow. > > So the check of the termbitmap should be inside the loop. > The revised patch is below. > > thanks, > > Takashi > > -- 8< -- > From: Hui Peng > Subject: [PATCH] ALSA: usb-audio: Fix a stack buffer overflow bug >  check_input_term > > `check_input_term` recursively calls itself with input > from device side (e.g., uac_input_terminal_descriptor.bCSourceID) > as argument (id). In `check_input_term`, if `check_input_term` > is called with the same `id` argument as the caller, it triggers > endless recursive call, resulting kernel space stack overflow. > > This patch fixes the bug by adding a bitmap to `struct mixer_build` > to keep track of the checked ids by `check_input_term` and stop > the execution if some id has been checked (similar to how > parse_audio_unit handles unitid argument). > > [ The termbitmap needs to be cleared at each first check of the input >   terminal, so the function got split now.  Also, for catching another >   endless loop in the input terminal chain -- where the feature unit >   points to itself as its source -- the termbitmap check is moved >   inside the parser loop. -- tiwai ] > > Reported-by: Hui Peng > Reported-by: Mathias Payer > Signed-off-by: Hui Peng > Cc: > Signed-off-by: Takashi Iwai > --- >  sound/usb/mixer.c | 36 ++++++++++++++++++++++++++---------- >  1 file changed, 26 insertions(+), 10 deletions(-) > > diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c > index ea487378be17..aa8b046aa91f 100644 > --- a/sound/usb/mixer.c > +++ b/sound/usb/mixer.c > @@ -68,6 +68,7 @@ struct mixer_build { >         unsigned char *buffer; >         unsigned int buflen; >         DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS); > +       DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS); >         struct usb_audio_term oterm; >         const struct usbmix_name_map *map; >         const struct usbmix_selector_map *selector_map; > @@ -775,16 +776,23 @@ static int uac_mixer_unit_get_channels(struct > mixer_build *state, >   * parse the source unit recursively until it reaches to a terminal >   * or a branched unit. >   */ > -static int check_input_term(struct mixer_build *state, int id, > -                           struct usb_audio_term *term) > +static int __check_input_term(struct mixer_build *state, int id, > +                             struct usb_audio_term *term) >  { >         int protocol = state->mixer->protocol; >         int err; >         void *p1; > +       unsigned char *hdr; > > -       memset(term, 0, sizeof(*term)); > -       while ((p1 = find_audio_control_unit(state, id)) != NULL) { > -               unsigned char *hdr = p1; > +       for (;;) { > +               /* a loop in the terminal chain? */ > +               if (test_and_set_bit(id, state->termbitmap)) > +                       break; > + > +               p1 = find_audio_control_unit(state, id); > +               if (!p1) > +                       break; > +               hdr = p1; >                 term->id = id; > >                 if (protocol == UAC_VERSION_1 || protocol == > UAC_VERSION_2) { > @@ -802,7 +810,7 @@ static int check_input_term(struct mixer_build *state, > int id, > >                                         /* call recursively to verify that > the >                                          * referenced clock entity is > valid */ > -                                       err = check_input_term(state, d-> > bCSourceID, term); > +                                       err = __check_input_term(state, > d->bCSourceID, term); >                                         if (err < 0) >                                                 return err; > > @@ -836,7 +844,7 @@ static int check_input_term(struct mixer_build *state, > int id, >                         case UAC2_CLOCK_SELECTOR: { >                                 struct uac_selector_unit_descriptor *d = > p1; >                                 /* call recursively to retrieve the > channel info */ > -                               err = check_input_term(state, d-> > baSourceID[0], term); > +                               err = __check_input_term(state, d-> > baSourceID[0], term); >                                 if (err < 0) >                                         return err; >                                 term->type = UAC3_SELECTOR_UNIT << 16; /* > virtual type */ > @@ -899,7 +907,7 @@ static int check_input_term(struct mixer_build *state, > int id, > >                                 /* call recursively to verify that the >                                  * referenced clock entity is valid */ > -                               err = check_input_term(state, d-> > bCSourceID, term); > +                               err = __check_input_term(state, d-> > bCSourceID, term); >                                 if (err < 0) >                                         return err; > > @@ -950,7 +958,7 @@ static int check_input_term(struct mixer_build *state, > int id, >                         case UAC3_CLOCK_SELECTOR: { >                                 struct uac_selector_unit_descriptor *d = > p1; >                                 /* call recursively to retrieve the > channel info */ > -                               err = check_input_term(state, d-> > baSourceID[0], term); > +                               err = __check_input_term(state, d-> > baSourceID[0], term); >                                 if (err < 0) >                                         return err; >                                 term->type = UAC3_SELECTOR_UNIT << 16; /* > virtual type */ > @@ -966,7 +974,7 @@ static int check_input_term(struct mixer_build *state, > int id, >                                         return -EINVAL; > >                                 /* call recursively to retrieve the > channel info */ > -                               err = check_input_term(state, d-> > baSourceID[0], term); > +                               err = __check_input_term(state, d-> > baSourceID[0], term); >                                 if (err < 0) >                                         return err; > > @@ -984,6 +992,14 @@ static int check_input_term(struct mixer_build > *state, int id, >         return -ENODEV; >  } > > +static int check_input_term(struct mixer_build *state, int id, > +                           struct usb_audio_term *term) > +{ > +       memset(term, 0, sizeof(*term)); > +       memset(state->termbitmap, 0, sizeof(state->termbitmap)); > +       return __check_input_term(state, id, term); > +} > + >  /* >   * Feature Unit >   */ > -- > 2.16.4 > >