Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp243783pxf; Thu, 11 Mar 2021 02:54:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJyVhwFcuFVDhwyEDweKdYpzK5eFgiTT/pPagrLfz2Q7fbGncckPVWzEmCObHd+E/D/0IMgK X-Received: by 2002:aa7:d316:: with SMTP id p22mr7614213edq.107.1615460058381; Thu, 11 Mar 2021 02:54:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615460058; cv=none; d=google.com; s=arc-20160816; b=tH4fbQZeESrBRvILOfswi0b2CbdIW8hBUd32Gxx9185wh0G08Ixj6s2lsLYvAmV30A +gsxZWdEf5CRyHTbFwrUAYrvmXs5uRiJepvAVlkBLvGjYiEui2NyShq5TNZ0nMNW8a/s 3Wsg+HuQggE2pZq/I6/AuSsFon1JHNlzf2uAPi8GsS7TlmJBXmavpikN58M30JaOe5a8 LAr23p+msqHowsqGNXDYWYzU/XGYyNlmwEnl8E+3BE0Oi7QGR+A2epotd910wIhc/AWr A7/8WWDyqKD0r1CAHtlYYGVYHVRzP3r4eZfArrFoRbbossqefpz9ZTk0weR4Cbh3oIOv kJ5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Ufs5lWemJ9jPCgAw3vWn7GhDzaZHQFr/enTSBRlhufI=; b=JHlUmQyfSdBUJifwjkiclxuCSKbvHGGmXdlwIjBATe9gIbwIrisoWmUOtsClxodDUZ TwQCcVK0NwclIO0CMCOBr3ZxUjsjP6rDay/vPAiQ0zo6NWIk9lX+w8FuWUC3RUAkxV1/ w3H4lLHGZr29B9Cj5+pld6JP4TjRCOklFAdceK3APxqQ4IlZAKNd703bE7jv/qmViTxa dCKpRjW9Ro05+NSjsKnuiyBDJiyUE7SuuuVstZcGA70yyNHScw7oG0KRhak3TV6j/eYF CCHgVPe3x1cm9c/nQz505PrvhdrEUIXhnavXv65tHvTPIJPvvHxi8KBc0WyGcKlKi0EE W6pQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q17si1513188ejj.672.2021.03.11.02.53.55; Thu, 11 Mar 2021 02:54:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232416AbhCKKws (ORCPT + 99 others); Thu, 11 Mar 2021 05:52:48 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:54171 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232518AbhCKKwY (ORCPT ); Thu, 11 Mar 2021 05:52:24 -0500 Received: from [179.93.213.27] (helo=mussarela) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1lKIvR-0008VO-Pm; Thu, 11 Mar 2021 10:52:18 +0000 Date: Thu, 11 Mar 2021 07:52:10 -0300 From: Thadeu Lima de Souza Cascardo To: Macpaul Lin Cc: Jim Lin , Felipe Balbi , Greg Kroah-Hartman , Matthias Brugger , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, Ainge Hsu , Eddie Hung , Kuohong Wang , Mediatek WSD Upstream , Macpaul Lin , stable@vger.kernel.org Subject: Re: [PATCH v4] usb: gadget: configfs: Fix KASAN use-after-free Message-ID: <20210311105210.GS10958@mussarela> References: <1484647168-30135-1-git-send-email-jilin@nvidia.com> <1615444961-13376-1-git-send-email-macpaul.lin@mediatek.com> <1615445632.13420.2.camel@mtkswgap22> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1615445632.13420.2.camel@mtkswgap22> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 11, 2021 at 02:53:52PM +0800, Macpaul Lin wrote: > On Thu, 2021-03-11 at 14:42 +0800, Macpaul Lin wrote: > > From: Jim Lin > > > > When gadget is disconnected, running sequence is like this. > > . composite_disconnect > > . Call trace: > > usb_string_copy+0xd0/0x128 > > gadget_config_name_configuration_store+0x4 > > gadget_config_name_attr_store+0x40/0x50 > > configfs_write_file+0x198/0x1f4 > > vfs_write+0x100/0x220 > > SyS_write+0x58/0xa8 > > . configfs_composite_unbind > > . configfs_composite_bind > > > > In configfs_composite_bind, it has > > "cn->strings.s = cn->configuration;" > > > > When usb_string_copy is invoked. it would > > allocate memory, copy input string, release previous pointed memory space, > > and use new allocated memory. > > > > When gadget is connected, host sends down request to get information. > > Call trace: > > usb_gadget_get_string+0xec/0x168 > > lookup_string+0x64/0x98 > > composite_setup+0xa34/0x1ee8 > > > > If gadget is disconnected and connected quickly, in the failed case, > > cn->configuration memory has been released by usb_string_copy kfree but > > configfs_composite_bind hasn't been run in time to assign new allocated > > "cn->configuration" pointer to "cn->strings.s". > > > > When "strlen(s->s) of usb_gadget_get_string is being executed, the dangling > > memory is accessed, "BUG: KASAN: use-after-free" error occurs. > > > > Signed-off-by: Jim Lin > > Signed-off-by: Macpaul Lin > > Cc: stable@vger.kernel.org > > --- > > Changes in v2: > > Changes in v3: > > - Change commit description > > Changes in v4: > > - Fix build error and adapt patch to kernel-5.12-rc1. > > Replace definition "MAX_USB_STRING_WITH_NULL_LEN" with > > "USB_MAX_STRING_WITH_NULL_LEN". > > - Note: The patch v2 and v3 has been verified by > > Thadeu Lima de Souza Cascardo > > http://spinics.net/lists/kernel/msg3840792.html > > Dear Cascardo, > > Would you please help to confirm if you've tested it on Linux PC, > Chrome OS, or an Android OS? I tested v3 on Ubuntu GNU/Linux. I will test v4. Cascardo. > > Thanks! > Macpaul Lin > > > and > > Macpaul Lin on Android kernels. > > http://lkml.org/lkml/2020/6/11/8 > > - The patch is suggested to be applied to LTS versions. > > > > drivers/usb/gadget/configfs.c | 14 ++++++++++---- > > 1 file changed, 10 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c > > index 0d56f33..15a607c 100644 > > --- a/drivers/usb/gadget/configfs.c > > +++ b/drivers/usb/gadget/configfs.c > > @@ -97,6 +97,8 @@ struct gadget_config_name { > > struct list_head list; > > }; > > > > +#define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1) > > + > > static int usb_string_copy(const char *s, char **s_copy) > > { > > int ret; > > @@ -106,12 +108,16 @@ static int usb_string_copy(const char *s, char **s_copy) > > if (ret > USB_MAX_STRING_LEN) > > return -EOVERFLOW; > > > > - str = kstrdup(s, GFP_KERNEL); > > - if (!str) > > - return -ENOMEM; > > + if (copy) { > > + str = copy; > > + } else { > > + str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL); > > + if (!str) > > + return -ENOMEM; > > + } > > + strcpy(str, s); > > if (str[ret - 1] == '\n') > > str[ret - 1] = '\0'; > > - kfree(copy); > > *s_copy = str; > > return 0; > > } >