Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4841192iob; Mon, 9 May 2022 02:56:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfDeU9lL2n3B1o9y8dFH6H1ZfbOJeGmaAyRDKYS1OMnD5HO6PyohIyEcaAVzwq6R8dz2KW X-Received: by 2002:a17:902:d58b:b0:15f:16f9:abe4 with SMTP id k11-20020a170902d58b00b0015f16f9abe4mr2718093plh.83.1652090202931; Mon, 09 May 2022 02:56:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652090202; cv=none; d=google.com; s=arc-20160816; b=NQQ9TNUam0QHSKgolMIXNxVpa78W0QH1oDWEMw3RzOh0/ZRAa7b9kikkzeIcet7Ccn XTkrb634B7c4NMnv7WJhfcTE1fhoXw4GXZqRYs2uoCOrKjtI7iFAXWi+jHtk1GJ3QCnl 0MQnDmeTNxEIHkfvuinIaS5D+3HWjv+F92kTskhhehKsDLyXMhspWbnzU5hztZjx+awG OG7uZTtFqHhk4wMmTSGmrQUWi2qUK74Ki/7qjmodbcYUyHGWc2CC9YbAeYtwxbDcFfHG DCfxNKdbHgV1U1CEYwSbgF5+khNwq9siHbeEn07p5hEIPSKIrgegRqGIgjGlLTV9hFm6 FBcA== 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=jvGYRgc/B1cVUQHYWbyzOUJsJsb2JybLUg0MtrNHLxU=; b=qhY0NOcGYqzXEoxABpqmMP3oYpyjqD+cSDIOWNzO/RugplHy93xCpNkkf3kssM4Zwq szAevQD96iJhXWkpFTinqE6f4meuLgQZwlglSAGlhCvnHxlzAn2/VvDHUtplAFYNn/mK BTz5Gm2RqQ4n5KPQBB4iXJrBEMZbGo7HyNtvyDntj8A85yO1A/WC2HS8ZcfRZsKqciO/ JfcclLH2O3Qj5enlWt8WFJbeIAJHBwDcCUYFiFK/LbSZFt6denxAJujoX23lJ9FxMSBW 7SkqxGeseK35PfFQRsdYIQtNRcJ/4VZnGkL+ClyXcH6T+KNgOp0lAM7xUU+doy/CfAPC rPBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gzCU6N4K; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id n1-20020a170902e54100b0015d9e670bfesi13353407plf.554.2022.05.09.02.56.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 02:56:42 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gzCU6N4K; 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; 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 9DADA1ED598; Mon, 9 May 2022 02:40:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235859AbiEIBcM (ORCPT + 99 others); Sun, 8 May 2022 21:32:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233966AbiEHXzM (ORCPT ); Sun, 8 May 2022 19:55:12 -0400 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04C6A63F8; Sun, 8 May 2022 16:51:21 -0700 (PDT) Received: by mail-io1-xd30.google.com with SMTP id z18so13694333iob.5; Sun, 08 May 2022 16:51:20 -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=jvGYRgc/B1cVUQHYWbyzOUJsJsb2JybLUg0MtrNHLxU=; b=gzCU6N4K+RH0gJYDOFzdClMUoqs+sC5dLzprBeR0gAh2NBdsFoIUDmtm6UrsHl1C22 hhvvyVwkcr85Bem8HAES8601Svm4cXUnKRpqZoX8B82AmRxwtFFhe+5rEgd2+b5IsAVc Ehjr2JxnbzsUtzOTCQt7zyUweMkWn98QmUIOyiJXmdSGJjb7Y2Vt2HHaF5zHjCVr5HeS C99xRgPNO6MydfVvHXf3vQvagErptsjIu4JbDRnLo1RFgGcMU4ziuIJU/ac6wD3Pxt6R /muL6QOrnANd9gjDvltFI7JhWsuPEeGkS8/eevwKXB3U+H9nokx36TkYQHhfUaB7A7yh U4LA== 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=jvGYRgc/B1cVUQHYWbyzOUJsJsb2JybLUg0MtrNHLxU=; b=1DcziIkBiYwJrYGc7DosG5IlHAOiDx906xdl/iMsWX/e0fk2Mmq8cgFnFG2rpxmkYI p55cExKOmlGGBULvH+wYJqHDnRQVobX+MIBKCfvCGAUg9QHmla5CDYIOKvW0NVIU1TTD fOyOltTCPVHYt0EcjYIjLUWrIdY4gx8zZZSsp8HX+NYytT35HG2b1+DxPtsa75d0oENM HXqoAyf05EegINk0ybyIbuXOvWhs8r7e26WjhlO0qKpsY7W1WSNgX7eahM2DAqqfvjx0 uUEPg91S4wzO6UHdBdU7LK6vLj4TOVLjS/DZ5NtD9MF0QO5zyhNm78PKXQMEAJJRhJIN UcNw== X-Gm-Message-State: AOAM533yBk3gcMvsz4ysDqzsta9vtIBQbZqp3L14P50bfYuAAqWl2Uz9 3qpM8H2fJpVDz6l/F2Os2uWHGno6olyilfB9QY8= X-Received: by 2002:a05:6638:168f:b0:32d:8105:7646 with SMTP id f15-20020a056638168f00b0032d81057646mr630122jat.9.1652053880413; Sun, 08 May 2022 16:51:20 -0700 (PDT) MIME-Version: 1.0 References: <20220508150247.38204-1-schspa@gmail.com> In-Reply-To: <20220508150247.38204-1-schspa@gmail.com> From: Andrey Konovalov Date: Mon, 9 May 2022 01:51:09 +0200 Message-ID: Subject: Re: [PATCH v3] usb: gadget: fix race when gadget driver register via ioctl To: Schspa Shi Cc: stern@rowland.harvard.edu, Julia Lawall , Felipe Balbi , Greg Kroah-Hartman , Jann Horn , Wei Ming Chen , LKML , USB 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 On Sun, May 8, 2022 at 5:03 PM 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. > > 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) > Reviewed-by: Andrey Konovalov Thanks, Schspa! Thanks for the review, Alan!