Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4671317iob; Sun, 8 May 2022 21:23:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkzpymvsKbumvjcRRwnP3GEnvx9vap4VC+KHKZQ07k1hUolthNm5EJMt8ODbW2uQF4tjL3 X-Received: by 2002:a63:6ac9:0:b0:3bc:321e:6d56 with SMTP id f192-20020a636ac9000000b003bc321e6d56mr11745117pgc.490.1652070234358; Sun, 08 May 2022 21:23:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652070234; cv=none; d=google.com; s=arc-20160816; b=soh5NQ+Im/eYPJtyuNYfsIzPxpW/CZKe9384t1cFDkC1YODPFvFoq3VIdd9FVSKA6H ya30fVpMwSJHr4tK4Tzy+WmB4P9j+uaX/PlqGnMGOg1skvNe8iuf/vI74978ePfHwNtP KJChODKNdLXCNIpSw6uCXVQIEHHgo4LrGUni7YMqZmruExdaa9ZJ+o7jIWM5LOQvkzH3 +8eKWYfyoY8UdDeUM9GmGDGPsodQPvBTvRXFnLt4G1GYipl8oYvPYwydDreQDpwteZ4a fhST6C8kbBOK8BbxcNpa/ixim5E0VrA/7Kfk2MJZMSPv+YMI4k55iKxtvnnJp38r/eGR /Dxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=41Mv+KsQAYVN8Us0Ti+umuHoMmg3u4PP5976C3vXHgk=; b=XvlVKGVSgEyxOxLQrbSNPz18r/arHbZ0P+0+FwkdRNColOM74iUereqH2V/r5JEsLe mlm4wCFPf8J/b8iZfmPgMUZzqI3b/iGwAyopbbfaYHRfSz2Aoi+KKAuoki6CMEYyZME9 Lk1P0pltWycXcOsPf8dyR0p3liYee1hbXpWVLqopDClrY5N3o/nZnXYdAaDHx8W820pk TcKT4InMujDF1aH03TVplpX+sbeyTWDvMWk2run+x+Mid/WFaOlpRxDHvx901PbzR2U/ PqXK7FewNSwalCmRwTxTO3NySOiGS8FV4m3Kdvj2EcR0gxS4/D/VIsghbroxtEbvZT4G khcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="EaEM/ppY"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id me1-20020a17090b17c100b001d285255f40si21778449pjb.32.2022.05.08.21.23.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 21:23:54 -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=@gmail.com header.s=20210112 header.b="EaEM/ppY"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C864611E5E7; Sun, 8 May 2022 21:23:14 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385839AbiEGPyf (ORCPT + 99 others); Sat, 7 May 2022 11:54:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229630AbiEGPyd (ORCPT ); Sat, 7 May 2022 11:54:33 -0400 Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 182EA6401; Sat, 7 May 2022 08:50:46 -0700 (PDT) Received: by mail-oi1-x22b.google.com with SMTP id m11so10748994oib.11; Sat, 07 May 2022 08:50:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=41Mv+KsQAYVN8Us0Ti+umuHoMmg3u4PP5976C3vXHgk=; b=EaEM/ppYw3SGKRW1BXCaJNfnDqfRYjjzWgOuqJh22Bt3tZah8jcyQ1ubPU6pZG6VPy 4cv2e3bzJr4G7OGX7A1qJiP1C3eVTsKJx6jXlmlaJZw7LPNak4EDuNS+Rro9/KAgHHdE IjZ48Lwaen5GFNu6O4alTJ8MwACBVKXe9cH38KDljQt1uqqQmDQB0fgDzq3SY0jfONJb rUzJ5nQltC7GMOzjYHLEbTsgpugPPny/CkZFmeQ0HU1xtnHwTv8990Zyfov6u/1a5bzg +SnZPAE28rVX7q5I8CGknTDT3nrw0iyDc13gmZEl0pmQZIH796PYBhs+rgJg255dRp7F 6rWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=41Mv+KsQAYVN8Us0Ti+umuHoMmg3u4PP5976C3vXHgk=; b=VfAUoRYy0YeZ7TK/eztL8QXTLMO2gde0u9uOcIDeYvTxXtOWRfogQRD6bMsHnr0Yhx iZ9w6UWAUcfaxptBSzdFHzRVEnOteOqZAYwLTsacsiPirGIK2ekqVIe/jMNpKBGUHGzV VRQtQ+btwp10hQuwLRiuAOzJ/0rFy4psCKBpIYco8/++GgCyxocfyrRjf/Z72M5y/XM8 RbEFkPjYfhFNO7i4m8IZxFXN7FrPrp5R3ddWX2CU1MS5BfO/q7Al+79t+RlfmdzL3Hgz BBaE0d5ohnZszu2fEq7y8fMUvjbxKB93G0b47l7LcuNrBZla2kAX0+A3WP4zzXbpKZe0 Ti0A== X-Gm-Message-State: AOAM531QHtfPFWwoVSCYcMspL/NBZl2PuY8Kezs6Iep7xI+sooe6GgTx ycU7efb8JGDlwLCfjCq2QAEcUESklO0RhQFtfFU= X-Received: by 2002:a05:6808:13d2:b0:322:7571:79e with SMTP id d18-20020a05680813d200b003227571079emr4008626oiw.52.1651938645103; Sat, 07 May 2022 08:50:45 -0700 (PDT) MIME-Version: 1.0 References: <20220507120851.29948-1-schspa@gmail.com> In-Reply-To: From: Schspa Shi Date: Sat, 7 May 2022 23:50:32 +0800 Message-ID: Subject: Re: [PATCH] usb: gadget: fix race when gadget driver register via ioctl To: Alan Stern Cc: Greg KH , andreyknvl@gmail.com, balbi@kernel.org, jj251510319013@gmail.com, jannh@google.com, Julia.Lawall@inria.fr, linux-usb@vger.kernel.org, Linux Kernel Mailing List , syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 Alan Stern writes: > On Sat, May 07, 2022 at 04:27:14PM +0200, Greg KH wrote: >> On Sat, May 07, 2022 at 08:08:51PM +0800, Schspa Shi wrote: >> > The usb_gadget_register_driver doesn't have inside locks to protect the >> > driver, and If there is two threads are registered at the same time via >> > the ioctl syscall, the system will crash as syzbot reported. >> > >> > Call trace as: >> > driver_register+0x220/0x3a0 drivers/base/driver.c:171 >> > usb_gadget_register_driver_owner+0xfb/0x1e0 >> > drivers/usb/gadget/udc/core.c:1546 >> > raw_ioctl_run drivers/usb/gadget/legacy/raw_gadget.c:513 [inline] >> > raw_ioctl+0x1883/0x2730 drivers/usb/gadget/legacy/raw_gadget.c:1220 >> > >> > This routine allows two processes to register the same driver instance >> > via ioctl syscall. which lead to a race condition. >> > >> > We can fix it by adding a driver_lock to avoid double register. >> > >> > Reported-by: syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com >> > Link: https://lore.kernel.org/all/000000000000e66c2805de55b15a@google.com/ >> > >> > Signed-off-by: Schspa Shi >> > --- >> > drivers/usb/gadget/legacy/raw_gadget.c | 8 ++++++++ >> > 1 file changed, 8 insertions(+) >> > >> > diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c >> > index b3be8db1ff63..d7ff9c2b5397 100644 >> > --- a/drivers/usb/gadget/legacy/raw_gadget.c >> > +++ b/drivers/usb/gadget/legacy/raw_gadget.c >> > @@ -155,7 +155,9 @@ struct raw_dev { >> > spinlock_t lock; >> > >> > const char *udc_name; >> > + /* Protected by driver_lock for reentrant registration */ >> > struct usb_gadget_driver driver; >> > + struct mutex driver_lock; >> >> Why are you adding another lock here? What's wrong with the existing >> lock in this structure that requires an additional one? >> >> > >> > /* Reference to misc device: */ >> > struct device *dev; >> > @@ -188,6 +190,8 @@ static struct raw_dev *dev_new(void) >> > spin_lock_init(&dev->lock); >> > init_completion(&dev->ep0_done); >> > raw_event_queue_init(&dev->queue); >> > + mutex_init(&dev->driver_lock); >> > + >> > return dev; >> > } >> > >> > @@ -398,7 +402,9 @@ static int raw_release(struct inode *inode, struct file *fd) >> > spin_unlock_irqrestore(&dev->lock, flags); >> > >> > if (unregister) { >> > + mutex_lock(&dev->driver_lock); >> > ret = usb_gadget_unregister_driver(&dev->driver); >> > + mutex_unlock(&dev->driver_lock); >> > if (ret != 0) >> > dev_err(dev->dev, >> > "usb_gadget_unregister_driver() failed with %d\n", >> > @@ -510,7 +516,9 @@ static int raw_ioctl_run(struct raw_dev *dev, unsigned long value) >> > } >> > spin_unlock_irqrestore(&dev->lock, flags); >> > >> > + mutex_lock(&dev->driver_lock); >> > ret = usb_gadget_register_driver(&dev->driver); >> > + mutex_unlock(&dev->driver_lock); >> >> How can unregister race with register? >> >> What ioctl is causing this race? What userspace program is doing this? >> Only one userspace program should be accessing this at once, right? > > These questions are on the right track. > > The problem here is not insufficient locking. The problem is that > dev->state does not have a special state to indicate that the driver is > being registered. > > Before calling usb_gadget_register_driver(), while still holding > dev->lock, the code should change dev->state to STATE_DEV_REGISTERING. > Then no race can occur, because the second thread to acquire the > spinlock will see that dev->state is not equal to STATE_DEV_INITIALIZED. > Yes, it's a good suggestion, I will upload a new patch set to use this method. > Alan Stern --- BRs Schspa Shi