Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9709374pxu; Tue, 29 Dec 2020 02:56:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJzQ+b4fBkQjag/RWgOrpIxoUMxS+ePAn+y5fq3VPrbV/Ui29u0tf7NBYGpGjq85W6W/jnYJ X-Received: by 2002:a17:906:4e53:: with SMTP id g19mr44418905ejw.454.1609239386243; Tue, 29 Dec 2020 02:56:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609239386; cv=none; d=google.com; s=arc-20160816; b=qPSRiO5QWcH3VQXoM+HjkbyAz/jWmiPnqARzYng9T7PF1tmFV1NmkJ8zkZvG3ZnE4H mKT32wUpMhTXrHhnccl1mCZPwb6+nIIRy4DOWrIkRj/BT+H/Jk1t4Ba8vPwG1aHuyEiu Tiec/h5s1OqsOJLVC0RzTOMfxaZ8QBkV9awcByPFB0598ycOgSLwv75Ll8mUCQbqbTX5 Gjgqb2Xbt2cad4EakWXJK5IR3WIov+dtf/i9nSrn55EiNJgz9Qe64heJ0NXVbDtQITe7 GUc/BwtuBbAq2oF5ReUnCXRkKB9Igbz3wcWLFVjxyNf5NJvPWbAUgqlSISiEcEkgDRi4 IV4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=kvIct6jZ7UVTYdJ5smtoaFFE2wEKX0eoYCEW4A5mW+k=; b=hRosuQqJlH5Q4YljruWForW80PzkSQasWkRmcA/IBQVuhiWTWzr2rBiXCOUBIgxX7x ZixW2AHb8Sew1oq163/NiAHUVXu8BzXKS9langzot6LEa4t08lEK2vacrIPwpEgE76Mj JHGbDQHCNa6sNPy1F/U54iA3OFnms90xlXGi3iuJhxZUE8gO5crAffZQp/9pZALp3sLA Y3/Y9lQJzGJU6MS95AracPVzgubBftDSmEoDfAz5/UyIBPtjriEd3YyU9fY/BiYdRtvC iV2/J61AMwbNUjWpuXzlzcvHETnKCHpmoxI7VmD9vDx2wOc/AlVklX5Ie7VZkmXMzlDa t/7g== 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=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f5si20161545ejd.332.2020.12.29.02.56.04; Tue, 29 Dec 2020 02:56:26 -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=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726336AbgL2KzN (ORCPT + 99 others); Tue, 29 Dec 2020 05:55:13 -0500 Received: from mailgw02.mediatek.com ([210.61.82.184]:54256 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725866AbgL2KzM (ORCPT ); Tue, 29 Dec 2020 05:55:12 -0500 X-UUID: 939ac46eb0be4dd28ecef758b110a329-20201229 X-UUID: 939ac46eb0be4dd28ecef758b110a329-20201229 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw02.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 277752702; Tue, 29 Dec 2020 18:54:25 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs01n2.mediatek.inc (172.21.101.79) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 29 Dec 2020 18:54:22 +0800 Received: from mtkswgap22.mediatek.inc (172.21.77.33) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Tue, 29 Dec 2020 18:54:22 +0800 From: Macpaul Lin To: Felipe Balbi , Greg Kroah-Hartman , Matthias Brugger , , , , , Mediatek WSD Upstream , Macpaul Lin CC: Ainge Hsu , Eddie Hung , Chunfeng Yun , Macpaul Lin , Subject: [PATCH RESEND v2] usb: gadget: configfs: Fix use-after-free issue with udc_name Date: Tue, 29 Dec 2020 18:53:35 +0800 Message-ID: <1609239215-21819-1-git-send-email-macpaul.lin@mediatek.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1595040303-23046-1-git-send-email-macpaul.lin@mediatek.com> References: <1595040303-23046-1-git-send-email-macpaul.lin@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-TM-SNTS-SMTP: D7599083FB75AC51A42E27F5D4110B8FDDD3B0ADAEA3DC865F0F32B8950722872000:8 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eddie Hung There is a use-after-free issue, if access udc_name in function gadget_dev_desc_UDC_store after another context free udc_name in function unregister_gadget. Context 1: gadget_dev_desc_UDC_store()->unregister_gadget()-> free udc_name->set udc_name to NULL Context 2: gadget_dev_desc_UDC_show()-> access udc_name Call trace: dump_backtrace+0x0/0x340 show_stack+0x14/0x1c dump_stack+0xe4/0x134 print_address_description+0x78/0x478 __kasan_report+0x270/0x2ec kasan_report+0x10/0x18 __asan_report_load1_noabort+0x18/0x20 string+0xf4/0x138 vsnprintf+0x428/0x14d0 sprintf+0xe4/0x12c gadget_dev_desc_UDC_show+0x54/0x64 configfs_read_file+0x210/0x3a0 __vfs_read+0xf0/0x49c vfs_read+0x130/0x2b4 SyS_read+0x114/0x208 el0_svc_naked+0x34/0x38 Add mutex_lock to protect this kind of scenario. Signed-off-by: Eddie Hung Signed-off-by: Macpaul Lin Reviewed-by: Peter Chen Cc: stable@vger.kernel.org --- Changes for v2: - Fix typo %s/contex/context, Thanks Peter. drivers/usb/gadget/configfs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c index 56051bb..d9743f4 100644 --- a/drivers/usb/gadget/configfs.c +++ b/drivers/usb/gadget/configfs.c @@ -221,9 +221,16 @@ static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item, static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page) { - char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name; + struct gadget_info *gi = to_gadget_info(item); + char *udc_name; + int ret; + + mutex_lock(&gi->lock); + udc_name = gi->composite.gadget_driver.udc_name; + ret = sprintf(page, "%s\n", udc_name ?: ""); + mutex_unlock(&gi->lock); - return sprintf(page, "%s\n", udc_name ?: ""); + return ret; } static int unregister_gadget(struct gadget_info *gi) -- 1.7.9.5