Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2251762pxp; Mon, 21 Mar 2022 15:01:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkevcjM1dNBi0UZQFKmVVT+Zd4j3oAFacdlZJC4Gj1q0tGi9CNaQ820w081KXqyBxazKTI X-Received: by 2002:a17:90b:4f8d:b0:1c6:408b:6b0d with SMTP id qe13-20020a17090b4f8d00b001c6408b6b0dmr1316889pjb.90.1647900116128; Mon, 21 Mar 2022 15:01:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647900116; cv=none; d=google.com; s=arc-20160816; b=gL8amwdCPVvsDjFAdd2HWmtvZQplH9C6xgqAODk984XZu0IBq+PzZMH3yZxP9LOGR0 VN9/0ghRucqAmDOkIqs7gnDRXZI2CtEsq34Wx3ru/rSdTnhM3XcUnLd7OdoRymDJQh2p EGJnFGs6A22gGe0T/3hS0/St1LsLCzOFAsDJVjAmaassJWvyI154W5n2kwyscLAdxupl 6GVSajVnjsVtgAVljWTEZOJiRogoC9qM8aRsXhSlqHoHeW2NqzpqEzTbIPE5D1FLWZqg IBwaHrSzN1QhavFVW6Rg5HwPKip59idT4qiXZ09dO8QKSFZMjoSRzExVGmb0e9Cij4Oe ndoA== 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=f0eWYK8lF78nACYbeBgWm+Zk/DgCZq2dtZpWwpQ0t24=; b=TIlWhL2C/XNrOdrK9vqzXOVpzfiaTHF0OEHPgGGfQb7aOipy3UxThwPFX9LQTYUvAb 7wGhlEvb+9WsHGcyicuGb8Ky2VL9wBniXwx+VazPDrTJYanpQlHkS2ztAAWbZuzfkMPN Q7a0y/Y0oDiXed/mcZf83o1WihtoV2BqFzcU9+dY1GSSH5vCgSACGOKbKyJfOZLNdBJR x+lFKRorAKL2zM9qEpO19JwzgXrO35aJvHOZLV2iJ/5IV+WTgbhIkAgmi2y/Z4S9DuEM +GYWzyco9d2G7qmCJWp7ycbwZkdQHX+tXbAXLamGRSDeoWrfX31dJEGjQTM2ZsKwY0w1 a0DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="L77uKD0/"; 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 w10-20020a170902c78a00b00153b2d16647si7470650pla.591.2022.03.21.15.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 15:01:56 -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="L77uKD0/"; 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 8BD2D34A135; Mon, 21 Mar 2022 14:25:55 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348701AbiCUOFK (ORCPT + 99 others); Mon, 21 Mar 2022 10:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349126AbiCUN7S (ORCPT ); Mon, 21 Mar 2022 09:59:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C67D710CC; Mon, 21 Mar 2022 06:57:52 -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 63D586125C; Mon, 21 Mar 2022 13:57:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 767D3C340E8; Mon, 21 Mar 2022 13:57:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1647871071; bh=iTxtJC1l3ApI+080GeDg+/sXNoxalLmyjWgvZ1KIW7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L77uKD0/nJGMbQS0WMfu5y5ecBJlUs4VJxmVLoEfvFZCA+NrO2MCFIA36uvTRzvuu W59N5LIS+A1NAVbkdxU8nlYxtHn/jqVcpv/4ieLDCHNxtXthdN2C615twfjI7G9gbN BqhJEce6ywawTPE6VyKJ/D6b4aQsjoSIyHaTmTsE= 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 5.4 13/17] usb: gadget: Fix use-after-free bug by not setting udc->dev.driver Date: Mon, 21 Mar 2022 14:52:49 +0100 Message-Id: <20220321133217.540026477@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220321133217.148831184@linuxfoundation.org> References: <20220321133217.148831184@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 @@ -1303,7 +1303,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; } @@ -1352,7 +1351,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); @@ -1374,7 +1372,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; }