Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4698056iob; Sun, 8 May 2022 22:25:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyaDoNYd5BUHHj80QZZUd1vCOwbdzkNMXos+KN3Yordy9FdnQVN9Ifgzb059k8eXILblrE X-Received: by 2002:aa7:9085:0:b0:510:90d1:f445 with SMTP id i5-20020aa79085000000b0051090d1f445mr9552808pfa.67.1652073926795; Sun, 08 May 2022 22:25:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652073926; cv=none; d=google.com; s=arc-20160816; b=cLXGONyr1rC4cBT+zk0RRJ2Jn637ed5GeTJWWtMmohSpdzFiPXKvYjvSYH9qPrQIKL HRssvIgLsRVGJoZ5SQBbqMd3IBE2S586Q5iI0rSxkbVMNFMuVRR4rsoIOn4HATyz+UzN 5lR+uG1D+2Za89qJvqGyPgqrvGGXBEQVg1gJeEascn57y9yx1ttbVJ2pQIKxy5WLB0vs nstUHgwbqug4i+uMMTrJMC0CE0guFyOcOuBJyhI9laS9xg5/Iu9yM9YswLuo/w9bSwG9 Zd3yP7fqcd+Ecb5WYkFuOae4GNZPgs9Z4Fgopg3mlv4Anyu7m1fy86GL9dgSZHe3Azww dGpA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Kfre6/ygIE35uySKLnPsy2RBut+ZLxAp1S9dwQFPHzQ=; b=cL66I2CRnXexOpUbbVfO0Qku3AaeSP+gTxmXOTW1yG/Wgbm0164BKnBUTTHDHuAm4H YaZLQI76s6oEuovpWyKRDt7FSKpfKNQVeM9nRzPMfLr+bKAVCMUSATG2nmlSfgdMEVf4 FH7TNde45wtps7aOq1FiJiv178r2tgilRxqUc84Tz5RyBqQ9u8JFg2aN7lRFE1goTv7N koUf/M/exEttUIoQcWHCNDb6rjQkhj5AUOORSIpHe/LA47vR/dvpENzfQRd+urbj9RGL s/22+Q4rLxWb7arHMkTi1/KNOxq+ksRfi0Hr/8UQva6jzH29Ob9WzWIqhVpgfy5HZFBM rwdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gE9+S5xV; 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 t1-20020a170902a5c100b00156c0b0a42asi9691655plq.113.2022.05.08.22.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 22:25:26 -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=gE9+S5xV; 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 16BD61498E8; Sun, 8 May 2022 22:22:34 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234482AbiEHPGy (ORCPT + 99 others); Sun, 8 May 2022 11:06:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234437AbiEHPGw (ORCPT ); Sun, 8 May 2022 11:06:52 -0400 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31227DEFD; Sun, 8 May 2022 08:03:01 -0700 (PDT) Received: by mail-io1-xd36.google.com with SMTP id i20so12894122ion.0; Sun, 08 May 2022 08:03:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Kfre6/ygIE35uySKLnPsy2RBut+ZLxAp1S9dwQFPHzQ=; b=gE9+S5xVAj8TG6zihR6U+ec6I+JIqqkXGGEYtVqn7MyB22aoWPNZQR2QwDUIjBTcrR B0EsrGqnj8DFLwflOqWf+vBMKhGeDsBwsVMbh8Mknj366//20o4rR4zRoonMA/q2qpiw EwfBsRW58WEb5BkWA/80KJK5ordO2yuUt4kPHXcWaBe1bTQwTlHec7uro0gXhrBYs8Hk 6XeduX3jc7oElVM0w+mOu7W0zMFLhh0Mg5FkANVV3rJE+jeCSzpv8yz4KEZti8kS8I43 W1dQe3hUaI/BTapKVqt45RjTfbDkEBG4gNiBazFGtQaXj5QP6hyTcgOn3NCYSOEssjZw lHvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Kfre6/ygIE35uySKLnPsy2RBut+ZLxAp1S9dwQFPHzQ=; b=0GX/v3nT2v+na9d/C4HdwynY0l/IFClGcXvC2J0CtxWbwW7Tb4j/kzEplKjCRHKrN7 b0BaMKQGJXBlaH/Bl6DPCqU+p7zR9p9dHwiBBkx4HthKzNvcOod2FTnMHwRfgqNy0ewo /hP9gnKtuyPrDZZ77ymM/B4yYuWjys9ozw/Fu5ndGfjqZk8yv21iQuPyZJTNMxnySYzc b+djC8DXEWvzjt3gsi+0TmFh38o5/U4x1itwsSQPVVEZIj1jHsYAKuQo3IIhrfe9k1iu 0+KXFNmFvY2FpDbD/ZowH+shejHvzAYywwkahFWdvSOkbVtUqyYSwW0rTzpae/BphkZq T21Q== X-Gm-Message-State: AOAM5320TpEgvcRRGTgUMJW/MxIwwrAb6KVEyC2xQHV7oT9SPdzCbRrT +DCJUlZrcX+5Cgr5M8QlYPs= X-Received: by 2002:a05:6602:2f06:b0:65a:cfe5:1778 with SMTP id q6-20020a0566022f0600b0065acfe51778mr2642842iow.192.1652022180478; Sun, 08 May 2022 08:03:00 -0700 (PDT) Received: from localhost (ec2-13-59-0-164.us-east-2.compute.amazonaws.com. [13.59.0.164]) by smtp.gmail.com with UTF8SMTPSA id y25-20020a02bb19000000b0032b3a7817e7sm2890894jan.171.2022.05.08.08.02.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 08 May 2022 08:03:00 -0700 (PDT) From: Schspa Shi To: stern@rowland.harvard.edu 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, schspa@gmail.com, syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com Subject: [PATCH v3] usb: gadget: fix race when gadget driver register via ioctl Date: Sun, 8 May 2022 23:02:47 +0800 Message-Id: <20220508150247.38204-1-schspa@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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. 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---------- There will be UAF in this scenario. We can fix it by adding a new STATE_DEV_REGISTERING device state 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 --- Changelog: v1 -> v2: - Add a STATE_DEV_REGISTERING as Alan Stern suggested. v2 -> v3: - Adjust STATE_DEV_REGISTERING position to reflect the actual order in which the states occur. - Add the fault scenarios to comments. --- 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..241740024c50 100644 --- a/drivers/usb/gadget/legacy/raw_gadget.c +++ b/drivers/usb/gadget/legacy/raw_gadget.c @@ -145,6 +145,7 @@ enum dev_state { STATE_DEV_INVALID = 0, STATE_DEV_OPENED, STATE_DEV_INITIALIZED, + STATE_DEV_REGISTERING, STATE_DEV_RUNNING, 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)