Received: by 2002:ac2:5a04:0:0:0:0:0 with SMTP id q4csp152923lfn; Fri, 25 Feb 2022 02:46:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJxr/+wSxDaEZhCqMT2eqCNypSyCRr7h6DKqKsCQI5ZYdik7aQqczh1g9NISh9pMNKsrr8t4 X-Received: by 2002:a17:907:8a04:b0:6cd:2902:8db3 with SMTP id sc4-20020a1709078a0400b006cd29028db3mr5560661ejc.530.1645785997211; Fri, 25 Feb 2022 02:46:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645785997; cv=none; d=google.com; s=arc-20160816; b=SDvlaDwrlFWeAy5Dqv//o8FBUFUE7kl5fcg2vgve1nmN/ON+0PHiLfWij9P9EhVng9 y2LzFK8CUjqLjMK+6xxC1jPQPtCC1Wfgl9+Ug2ESL7ujaY2pKDTehmgXmkuqvlVrtxqV tFPnLvtNjYt7FYgX6iKOXoxXCAdY5vkNHdav+GhRHhiIz8N5Wut4Q8a5lTgNqQL8mJXo XdlUQrZnyXECUZerk0jvDLk4cZJQ/ZJnjbnLMozL7J3jo/gEAPKgcKkAkDPmFTnyo9+t 0CSV9+OFpNG6iRpHftVxFPbjz4jh1WeVW6G7bK8kbc56cxngUQYmp5//lEkf5cgjViK/ N4QA== 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=taAYKyhekVR4wxm0yINJqu3axzhJpexnUI2IM1RgHX4=; b=BvQF33GyMgg3o9Jkjte3/tzFAENNFnAPGCTkBwW507lmiMB7hZT8k8SN9WPE1v7eA5 wO6Tc6T62FpxnMcm5oRx0OdPtbumFNfo9iGS4cBtP26KlAAG0eAS67kFnd6ctT9ltxKr TS2xh/WxQjCYQIIDE8Vf6iIsFOZQW/hPR/NBv2y/Ggq1DZU4t8jBVdptRSJpiVUfFeJD rAuDD32iM23P46UetEoEj0JnbV/SGmNUpEZ+cIgE1u+aENJr9XPGUXdGFvcmc1GFnoBv mr3yMcw/5Lf7iRcjT2/MNwiquQ0HbRb9P8LPGS/XzMQdsfxEToSe9/NT/amoAudALcsm VNaw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s23-20020a508d17000000b004131a227fc3si1413758eds.288.2022.02.25.02.46.10; Fri, 25 Feb 2022 02:46:37 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234647AbiBXVYE (ORCPT + 99 others); Thu, 24 Feb 2022 16:24:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234635AbiBXVX7 (ORCPT ); Thu, 24 Feb 2022 16:23:59 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id D726E1598F6 for ; Thu, 24 Feb 2022 13:23:27 -0800 (PST) Received: (qmail 1060076 invoked by uid 1000); 24 Feb 2022 16:23:26 -0500 Date: Thu, 24 Feb 2022 16:23:26 -0500 From: "stern@rowland.harvard.edu" To: "Zhang, Qiang1" , Tejun Heo Cc: "gregkh@linuxfoundation.org" , syzbot , "linux-kernel@vger.kernel.org" , "syzkaller-bugs@googlegroups.com" , "balbi@kernel.org" Subject: Re: [syzbot] KASAN: use-after-free Read in dev_uevent Message-ID: References: <0000000000005a991a05a86970bb@google.com> <00000000000033314805d8765175@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 On Thu, Feb 24, 2022 at 03:14:54AM +0000, Zhang, Qiang1 wrote: > > On Wed, Feb 23, 2022 at 05:00:12PM +0100, gregkh@linuxfoundation.org wrote: > > On Wed, Feb 23, 2022 at 09:38:20AM -0500, stern@rowland.harvard.edu wrote: > > > Which bus locks are you referring to? I'm not aware of any locks > > > that synchronize dev_uevent() with anything (in particular, with > > > driver unbinding). > > > > The locks in the driver core that handle the binding and unbinding of > > drivers to devices. > > > > > And as far as I know, usb_gadget_remove_driver() doesn't play any > > > odd tricks with pointers. > > > > Ah, I never noticed that this is doing a "fake" bus and does the > > bind/unbind itself outside of the driver core. It should just be a > > normal bus type and have the core do the work for it, but oh well. > > > > And there is a lock that should serialize all of this already, so it's > > odd that this is able to be triggered at all. > > >>I guess at a minimum the UDC core should hold the device lock when it registers, unregisters, binds, or unbinds UDC and gadget devices. > >>Would that be enough to fix the problem? I really don't understand how sysfs file access gets synchronized with device removal. > > >>> > >>>Agree with you, in usb_gadget_remove_driver() function, the udc->dev.driver and udc->gadget->dev.driver be set to null without any protection, so when the udevd accessed the dev->driver, this address may be invalid at this time. > >>>maybe the operation of dev->driver can be protected by device_lock(). > >>> > > Is it enough that we just need to protect "dev.driver" ? I don't know, although I doubt it. The right way to fix it is to make sure that the existing protections, which apply to drivers that are registered in the driver core, can also work properly with gadgets. But I don't know what those protections are or how they work. > diff --git a/drivers/base/core.c b/drivers/base/core.c > index 3d6430eb0c6a..9bd2624973d7 100644 > --- a/drivers/base/core.c > +++ b/drivers/base/core.c > @@ -2316,8 +2316,10 @@ static int dev_uevent(struct kobject *kobj, struct kobj_uevent_env *env) > if (dev->type && dev->type->name) > add_uevent_var(env, "DEVTYPE=%s", dev->type->name); > > + device_lock(dev); > if (dev->driver) > add_uevent_var(env, "DRIVER=%s", dev->driver->name); > + device_unlock(dev); You probably should not do this. Unless there's a serious bug, the driver core already takes all the locks it needs. Doing this might cause a deadlock (because the caller may already hold the device lock). > > /* Add common DT information about the device */ > of_device_uevent(dev, env); > diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c > index 568534a0d17c..7877142397d3 100644 > --- a/drivers/usb/gadget/udc/core.c > +++ b/drivers/usb/gadget/udc/core.c > @@ -1436,8 +1436,14 @@ static void usb_gadget_remove_driver(struct usb_udc *udc) > usb_gadget_udc_stop(udc); > > udc->driver = NULL; > + > + device_lock(&udc->dev); > udc->dev.driver = NULL; > + device_unlock(&udc->dev); > + > + device_lock(&udc->gadget->dev); > udc->gadget->dev.driver = NULL; > + device_unlock(&udc->gadget->dev); > } These are reasonable things to do, but I don't know if they will fix the problem. We really should ask advice from somebody who understands how this stuff is supposed to work. I'm not sure who to ask, though. Tejun Heo, perhaps (CC'ed). Tejun: The USB Gadget core binds and unbinds drivers without using the normal driver core facilities (see the code in usb_gadget_remove_driver() above). As a result, unbinding races with uevent generation, which can lead to a NULL pointer dereference as found by syzbot testing. In particular, dev->driver can become NULL between the times when dev_uevent() tests it and uses it (see above). Can you tell us how this should be fixed? Alan Stern