Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2897710rwb; Mon, 15 Aug 2022 13:34:18 -0700 (PDT) X-Google-Smtp-Source: AA6agR6b4Y70hvR1PDJCzV9IzvBUarmdzHQbLdRiBxRMi5xlwcQrCYJKFrZnXURcdt/ZIe2pkd2C X-Received: by 2002:a17:907:6089:b0:734:be0c:e0cd with SMTP id ht9-20020a170907608900b00734be0ce0cdmr11535890ejc.474.1660595657970; Mon, 15 Aug 2022 13:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660595657; cv=none; d=google.com; s=arc-20160816; b=pSJUzYSenDa0DFvOvr5ouugkfSWHa35qeeGll9rQIF97/1PQyc3Onw9OGaCoBbiljo 02erl64MvnLWuAnAwB/oUthxdg7sWTa0WCDxAsmHQ7X1Lq34r9/SIdA+FVU5kfAid+1S WbzrmX1vC+uJgjIGvya1fkhrS5f2P6xy5frKvbKWuU4EVNyWoP8YdTx64Ipbc89bqs0y TJR7EKx/pIcRLW0LmXM80XP+DLn2Ra5l4L4UK03r2C1rzQb8gQiUF5GtZ9phbFPfcByp 0AHsaeVJMYysNMlZLdBqJwgytMCE+SdmsoqHmyFI+uQWWpWsGnZWRluZ8el/kciPruuU 6/rA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IKtRvKsd2qa24k1fVMqPVd8G6G/4euoakZbSrmb+K94=; b=HEKp84gcAUtQEYX5d6OT3LxqxPSGKU1ik/4ahtb5/bf5b+wR/mUFhKTIzU3uIeCxsH IOG4kppZpDmWPCZtzMMuTTMaJVTsSpdw4E+1RlRcdFUmpatFO6KcH0A5FF83zwhQ2ePr B02tf9dgpH+w6MkxPVvvfIZPKyt4l+Z3UBqA5A76kxTJ9jmL7c1vC1Xw1FJ8hMP1APzM /OLIak6vaRz7/fuBW8tx7vB89SdGyi+OtDfEyEHjoPQplixbE/GfESDvXLYz4NOaz4fc evA+6AtaUxpIQhMMY2GoDfp/n4b1Xo8z6CeywNVqodX2QAdn92dsH3ZEniUU7XFvEiyT eo7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PlMewoO6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f13-20020a0564021e8d00b0043d85ca3c96si8745209edf.440.2022.08.15.13.33.50; Mon, 15 Aug 2022 13:34:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PlMewoO6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243565AbiHOUag (ORCPT + 99 others); Mon, 15 Aug 2022 16:30:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347331AbiHOUWS (ORCPT ); Mon, 15 Aug 2022 16:22:18 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 854922CCBF; Mon, 15 Aug 2022 12:02:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3C9D9B81062; Mon, 15 Aug 2022 19:02:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B30FC433C1; Mon, 15 Aug 2022 19:02:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660590156; bh=IO9ap3hmjMTu+JcV3hDkZ5Y3LMiXumCCUXgCjw5opXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PlMewoO6S0orSvri4aj63cNOfxLRNtco1KmUbrETGIDFuBivshjftTLjZRK1mXc7H JIt57GWcQuQeIun8ATv2D+MQqZNuPz/CTEjLvmw8WsSkIn5wfip2jOtoGgTCay4pF8 pz4Qn7ix19FbFAkDfXrZWHAd68yYnyUWiO0dW4vA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , syzbot+b0de012ceb1e2a97891b@syzkaller.appspotmail.com Subject: [PATCH 5.18 0127/1095] USB: gadget: Fix use-after-free Read in usb_udc_uevent() Date: Mon, 15 Aug 2022 19:52:06 +0200 Message-Id: <20220815180434.838639592@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alan Stern commit 2191c00855b03aa59c20e698be713d952d51fc18 upstream. The syzbot fuzzer found a race between uevent callbacks and gadget driver unregistration that can cause a use-after-free bug: --------------------------------------------------------------- BUG: KASAN: use-after-free in usb_udc_uevent+0x11f/0x130 drivers/usb/gadget/udc/core.c:1732 Read of size 8 at addr ffff888078ce2050 by task udevd/2968 CPU: 1 PID: 2968 Comm: udevd Not tainted 5.19.0-rc4-next-20220628-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 06/29/2022 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_address_description mm/kasan/report.c:317 [inline] print_report.cold+0x2ba/0x719 mm/kasan/report.c:433 kasan_report+0xbe/0x1f0 mm/kasan/report.c:495 usb_udc_uevent+0x11f/0x130 drivers/usb/gadget/udc/core.c:1732 dev_uevent+0x290/0x770 drivers/base/core.c:2424 --------------------------------------------------------------- The bug occurs because usb_udc_uevent() dereferences udc->driver but does so without acquiring the udc_lock mutex, which protects this field. If the gadget driver is unbound from the udc concurrently with uevent processing, the driver structure may be accessed after it has been deallocated. To prevent the race, we make sure that the routine holds the mutex around the racing accesses. Link: CC: stable@vger.kernel.org # fc274c1e9973 Reported-and-tested-by: syzbot+b0de012ceb1e2a97891b@syzkaller.appspotmail.com Signed-off-by: Alan Stern Link: https://lore.kernel.org/r/YtlrnhHyrHsSky9m@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/udc/core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -1745,13 +1745,14 @@ static int usb_udc_uevent(struct device return ret; } - if (udc->driver) { + mutex_lock(&udc_lock); + if (udc->driver) ret = add_uevent_var(env, "USB_UDC_DRIVER=%s", udc->driver->function); - if (ret) { - dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); - return ret; - } + mutex_unlock(&udc_lock); + if (ret) { + dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n"); + return ret; } return 0;