Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4683641iob; Sun, 8 May 2022 21:54:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHzoD/exRKs5UZaNfDQbkVZmx8sRinVFwPWkEt9Ta3YiC5RrDmOQTRL3FsWK0bcTNFvF5G X-Received: by 2002:a17:90b:2241:b0:1dc:d4d3:3047 with SMTP id hk1-20020a17090b224100b001dcd4d33047mr16681666pjb.128.1652072041741; Sun, 08 May 2022 21:54:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652072041; cv=none; d=google.com; s=arc-20160816; b=BmwPqHBgeeT+VJtb/kZTMqraq/BzfksPxW62q+HLxDHjNmYh+FHRvaCvRW7G4UJbZC /OAfnpsWCP7kh2v/lk3JcMnX0LmoVUpyo79dKO+gNHEq0qNQX4d0L4AmK05WBYLkhH6S 201+n+45gozFB8wvS+LWrTpBbe5XHEfXbsOppKpr/pdm3DyTNv+1AhZDpSyyWFPkFXkI Vwgrb7MhhXEFxKHOPhvTLfHJAlerm9FaYlne1Friezad3YXxCTcWUtBBK/EC3U5cwOIO 6a2mRgmEFX2BsSe91O3bbHKBRVUt+vvmzHtaX3v9VFU2lYTMaK1GmsQ5tpiI4fu+okZF VFUQ== 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=BxaFS+xCAaFuiairO0C5T9kHt0om3VnNkUqEUClRljw=; b=LcQOXZJF086Po04yAsHcpyHfPddZihnbv+PQbMWYL48YrxnXYJAJq08nOj1ZK+TXxz sK9I8VWCUg6tiM+LSwn/LYq+sj+zCgDNCRP7m//WTiLZ8Za/RAmYy0SiNypUPuqZOjty B97H6gReDHaBsclDoAqvDLbNsQeBDsugOk9fFkgwdB23C0GcXO6u62qYadLhnihZ3cRf +VNHieDG+ZBRYjz+4ON0aelqJsUVhTmVkW+A2mrGX00KuYMOoWxvtS8jtWGrrmnPUUVC fpqRIoS+AHeD7ikKlUNjHqmAGag2V5zRsgq0+CwESjoZyKOj6R/T8UTrfsZ9BsgeeyRd /VFA== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j6-20020a17090276c600b0015f179ccabasi1386919plt.257.2022.05.08.21.54.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 21:54:01 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C15F713482F; Sun, 8 May 2022 21:53:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233900AbiEHOiv (ORCPT + 99 others); Sun, 8 May 2022 10:38:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229575AbiEHOis (ORCPT ); Sun, 8 May 2022 10:38:48 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 0274A6389 for ; Sun, 8 May 2022 07:34:57 -0700 (PDT) Received: (qmail 98159 invoked by uid 1000); 8 May 2022 10:34:57 -0400 Date: Sun, 8 May 2022 10:34:57 -0400 From: Alan Stern To: Schspa Shi Cc: Julia.Lawall@inria.fr, andreyknvl@gmail.com, balbi@kernel.org, gregkh@linuxfoundation.org, jannh@google.com, jj251510319013@gmail.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com Subject: Re: [PATCH v2] usb: gadget: fix race when gadget driver register via ioctl Message-ID: References: <20220507160243.35304-1-schspa@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,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 Sun, May 08, 2022 at 12:08:35PM +0800, Schspa Shi wrote: > Alan Stern writes: > > > > Are you sure that this patch will fix the problem found by syzbot? That > > is, are you sure that the problem really was caused by two threads > > registering the same driver concurrently? > > > > Yes, from the console log from syzbot. > T8324 alloced driver_private was released by T8326. That is a smoking gun. > > The fact that the error was "use after free" suggests that the problem > > might be something else. It looks like one of the threads was trying to > > access the driver structure after the other thread had done something > > that caused it to be deallocated, which suggests an error in reference > > counting. > > > > The direct cause of this place is because of the refcount error, but the > root cause is still caused by multiple registrations > > Please refer to the following scenarios. > > T1 T2 > ------------------------------------------------------------------ > usb_gadget_register_driver_owner > driver_register driver_register > driver_find driver_find > bus_add_driver bus_add_driver > priv alloced > drv->p = priv; > > kobject_init_and_add // refcount = 1; > //couldn't find an available UDC or it's busy > > priv alloced > drv->priv = priv; > kobject_init_and_add > ---> refcount = 1 <------ > // register success > > ===================== another ioctl/process ====================== > driver_register > driver_find > k = kset_find_obj() > ---> refcount = 2 <------ > > driver_unregister > // drv->p become T2's priv > ---> refcount = 1 <------ > > kobject_put(k) > ---> refcount = 0 <------ > return priv->driver; > --------UAF here---------- It looks like you've got T2 calling driver_register and driver_find twice, but the overall idea is pretty clear. > There will be UAF in this scenario. > And all the logs reported by syzbot can be matched to this scenario. And in any case it is obvious that the patch is necessary. (Although I would have put the new state before the RUNNING state, to reflect the actual order in which the states occur.) Acked-by: Alan Stern Alan Stern