Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2294274pxp; Mon, 21 Mar 2022 16:07:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXid54VVLleWCrWA5IpPmN9jZ5DFe2lvu4atbFUMT+eL2VOezb9K9H1o8sPd/C9cf/AcXd X-Received: by 2002:a63:1c58:0:b0:380:bfd8:9e10 with SMTP id c24-20020a631c58000000b00380bfd89e10mr19919296pgm.422.1647904072193; Mon, 21 Mar 2022 16:07:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647904072; cv=none; d=google.com; s=arc-20160816; b=0vUqy/WQxEbm5df/siPfDZkIR3xkvioxzTtoPjnHWmsQd9qNQ+ln+MVPGZRctzvAxx KL+1V5f88j210DvBctpzqD70Gzy8rTMh1VlLI2+Jxa53SdW68fpM+f7dD8UagPHrr7RP Wk/yRjgxZzeBRy3zVefHN1WeY1sfpd7GmCCYThAWIyssID+6xlYaa55WK1tprL7cZCGC 5OyUSjwEoqpWSSw1Hpkj8nt0OYEIRnndhVWe/9BweP/cxa8a9lzd5FEUeZDv/l5sI2uB 2YJl4IZIwJ9c66E6HNXfmTUQ9+VcP4+ntCz3tox1GDwoUYQiaYhf5d3ZbEM+rxArU37L cgGA== 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=5YnKKJ1APhLGYUo6Dm9uVfoisd8+l8To9PmxNB/xhvg=; b=fXjJyxFF0VJSx1VxNuSgItNOSyc4loKfB8fPiulXa3pqbHFvPyqdl4BJw8ACAadQqT vhSn55ZMrw1V00p+381QZHx/YG4MjkQLiexBVU0YoiWh3QIFvPe1qYrMmP/Ep4GOzyNm XSvlXxWLWYVG7yO5TUXL/sudHwitpS2GHADf+WQD52jtn2P3EE1DSKi79m7vpvC26V5b Ab7gq2pPVAyDQ8Md4+3ID6PWaVRMWp8adM/i8cToMskMJ0aNiqZgHVyG+TGicAzaKSWM rdDgP5+F60hwP7+Nr/4pnhSTrVnewtXDDeoLFVUUoLZLVL9DW657sQesFmVjk8DuODwV 0bDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aKSUCc7k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id m10-20020a170902bb8a00b00153b2d16655si11148864pls.605.2022.03.21.16.07.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 16:07:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aKSUCc7k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1D69D41E4D7; Mon, 21 Mar 2022 15:05:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349181AbiCUODa (ORCPT + 99 others); Mon, 21 Mar 2022 10:03:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349048AbiCUN6u (ORCPT ); Mon, 21 Mar 2022 09:58:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64117174B8D; Mon, 21 Mar 2022 06:57:10 -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 dfw.source.kernel.org (Postfix) with ESMTPS id EED8D611D5; Mon, 21 Mar 2022 13:57:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0653BC340E8; Mon, 21 Mar 2022 13:57:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647871029; bh=hEnXSXsF12xuYbzJLH4q7s0j+f4DoWhrVmOI/kJV8so=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aKSUCc7kqHxKZgExH6CsyFVQ1k6GhcBmmlh6bha6A4JzfYy7FpOePvXl+dxgHFISO Ad54jY9qMdCArU1ljQQwL13H4kwKHS/39ey63/GI8C+s/Cc4dTtFToP4iu5zgql2C5 RG1SZa09ib3LcHkEmNpUgKMweuEsicceQo72hugY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , syzbot+348b571beb5eeb70a582@syzkaller.appspotmail.com Subject: [PATCH 4.19 55/57] usb: gadget: Fix use-after-free bug by not setting udc->dev.driver Date: Mon, 21 Mar 2022 14:52:36 +0100 Message-Id: <20220321133223.575783577@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220321133221.984120927@linuxfoundation.org> References: <20220321133221.984120927@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 16b1941eac2bd499f065a6739a40ce0011a3d740 upstream. The syzbot fuzzer found a use-after-free bug: BUG: KASAN: use-after-free in dev_uevent+0x712/0x780 drivers/base/core.c:2320 Read of size 8 at addr ffff88802b934098 by task udevd/3689 CPU: 2 PID: 3689 Comm: udevd Not tainted 5.17.0-rc4-syzkaller-00229-g4f12b742eb2b #0 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-2 04/01/2014 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_address_description.constprop.0.cold+0x8d/0x303 mm/kasan/report.c:255 __kasan_report mm/kasan/report.c:442 [inline] kasan_report.cold+0x83/0xdf mm/kasan/report.c:459 dev_uevent+0x712/0x780 drivers/base/core.c:2320 uevent_show+0x1b8/0x380 drivers/base/core.c:2391 dev_attr_show+0x4b/0x90 drivers/base/core.c:2094 Although the bug manifested in the driver core, the real cause was a race with the gadget core. dev_uevent() does: if (dev->driver) add_uevent_var(env, "DRIVER=%s", dev->driver->name); and between the test and the dereference of dev->driver, the gadget core sets dev->driver to NULL. The race wouldn't occur if the gadget core registered its devices on a real bus, using the standard synchronization techniques of the driver core. However, it's not necessary to make such a large change in order to fix this bug; all we need to do is make sure that udc->dev.driver is always NULL. In fact, there is no reason for udc->dev.driver ever to be set to anything, let alone to the value it currently gets: the address of the gadget's driver. After all, a gadget driver only knows how to manage a gadget, not how to manage a UDC. This patch simply removes the statements in the gadget core that touch udc->dev.driver. Fixes: 2ccea03a8f7e ("usb: gadget: introduce UDC Class") CC: Reported-and-tested-by: syzbot+348b571beb5eeb70a582@syzkaller.appspotmail.com Signed-off-by: Alan Stern Link: https://lore.kernel.org/r/YiQgukfFFbBnwJ/9@rowland.harvard.edu Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/udc/core.c | 3 --- 1 file changed, 3 deletions(-) --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -1297,7 +1297,6 @@ static void usb_gadget_remove_driver(str usb_gadget_udc_stop(udc); udc->driver = NULL; - udc->dev.driver = NULL; udc->gadget->dev.driver = NULL; } @@ -1346,7 +1345,6 @@ static int udc_bind_to_driver(struct usb driver->function); udc->driver = driver; - udc->dev.driver = &driver->driver; udc->gadget->dev.driver = &driver->driver; usb_gadget_udc_set_speed(udc, driver->max_speed); @@ -1368,7 +1366,6 @@ err1: dev_err(&udc->dev, "failed to start %s: %d\n", udc->driver->function, ret); udc->driver = NULL; - udc->dev.driver = NULL; udc->gadget->dev.driver = NULL; return ret; }