Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4628041iob; Sun, 8 May 2022 19:47:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8LYI4Tpbg+meG7NzFABi8uiSWHpv/MvP9VDI7A9UBnKaLZ97Wl6JqGc3czsnJaqt7wzYK X-Received: by 2002:a17:90a:b78d:b0:1d9:4f4f:bc2a with SMTP id m13-20020a17090ab78d00b001d94f4fbc2amr23974575pjr.155.1652064456678; Sun, 08 May 2022 19:47:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652064456; cv=none; d=google.com; s=arc-20160816; b=VCw/4a5BHas4AfSTTIaBr+zDpZrWEXcLNNwdj+uH0QORUOCGkCsEx6R3TZgz2ANs28 yFAD0I7XZOAmsdTpOT2i92ZWIv8Gtsk5uJQmMfUrpUYF5p/kzIjgQ/dgyptqQbm91swv 30LIHuhOdL3/Es432Z41xj1lAzWD6lWPQj5OjP+e5s2XvbPsuXa8xjl6S8BJuJka8HQP Bj/8jyBrlaPzyL+pMro4T2w/Hh2wYx7CNKHeZfm3eNTB/DXW3t9hh5bhYZ1mvjlTV8Wl qjn7UmscDWEnO44n9Co0QYu3ha28H0ZKVopXXOyVMTlTwspyg7fOPvR6ICyJ+wIfj9rp UuWg== 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=3olNGdf+8Z7y3hqfmjs6cgypq9FnCz+xiX2OSuOuFBQ=; b=AJWL86Vzma+OqnoJNpnjzNHyzp71+Nj7MYDZZDnrcP2z+gcIFwdaWqQ/vl/AF3kECw f676HMh396sJ+oovFtE+UyLwhoBrjPRLwR9xfNvgikSglZfhUsGsVLmaI1QcoMmPKw8f QJ4L2BdecCgqhBJgNo/6DN7DoMjCSQ/ZlwI+Vazll5sInPijCFBIGQuAJTP8TkpRPbO6 ptjP4l1X9ywBXMYcX+ixZm2+PPexdebFsLdqhqc9V8AGY5QESPsE9+hxwz+oNFthLASO PYfog8rUaw4NWJvF98i+ltSwBDanMK8G9s/vTPsduEFvGkTL42uxbhm7mPn/8kYiq5kX ZaQA== 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 g7-20020aa796a7000000b004fe3a6ea856si11819606pfk.176.2022.05.08.19.47.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 19:47:36 -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 8F58B403D9; Sun, 8 May 2022 19:47:24 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386606AbiEGSAt (ORCPT + 99 others); Sat, 7 May 2022 14:00:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385126AbiEGSAp (ORCPT ); Sat, 7 May 2022 14:00:45 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id AB727E73 for ; Sat, 7 May 2022 10:56:58 -0700 (PDT) Received: (qmail 76931 invoked by uid 1000); 7 May 2022 13:56:57 -0400 Date: Sat, 7 May 2022 13:56: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: <20220507160243.35304-1-schspa@gmail.com> 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:02:43AM +0800, Schspa Shi wrote: > The usb_gadget_register_driver can be called multi time by to > threads via USB_RAW_IOCTL_RUN ioctl syscall, which will lead > to multiple registrations. > > Call trace: > 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 > ioctl USB_RAW_IOCTL_RUN > > 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 new STATE_DEV_REGISTERING device state to > avoid double register. 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? 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. Yes, this could be caused by two threads both registering the same driver. But the evidence doesn't prove that this is what happened, as far as I can see. Alan Stern > 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 | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c > index b3be8db1ff63..b75f8f7b7b46 100644 > --- a/drivers/usb/gadget/legacy/raw_gadget.c > +++ b/drivers/usb/gadget/legacy/raw_gadget.c > @@ -146,6 +146,7 @@ enum dev_state { > STATE_DEV_OPENED, > STATE_DEV_INITIALIZED, > STATE_DEV_RUNNING, > + STATE_DEV_REGISTERING, > STATE_DEV_CLOSED, > STATE_DEV_FAILED > }; > @@ -508,6 +509,7 @@ static int raw_ioctl_run(struct raw_dev *dev, unsigned long value) > ret = -EINVAL; > goto out_unlock; > } > + dev->state = STATE_DEV_REGISTERING; > spin_unlock_irqrestore(&dev->lock, flags); > > ret = usb_gadget_register_driver(&dev->driver); > -- > 2.24.3 (Apple Git-128) >