stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Schspa Shi <schspa@gmail.com>,
	syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com,
	Andrey Konovalov <andreyknvl@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>,
	balbi@kernel.org, jj251510319013@gmail.com,
	Julia.Lawall@inria.fr, jannh@google.com,
	linux-usb@vger.kernel.org
Subject: [PATCH AUTOSEL 5.10 13/13] usb: gadget: fix race when gadget driver register via ioctl
Date: Wed, 18 May 2022 08:28:44 -0400	[thread overview]
Message-ID: <20220518122844.343220-13-sashal@kernel.org> (raw)
In-Reply-To: <20220518122844.343220-1-sashal@kernel.org>

From: Schspa Shi <schspa@gmail.com>

[ Upstream commit 5f0b5f4d50fa0faa8c76ef9d42a42e8d43f98b44 ]

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                     <context switch>
      drv->p = priv;
      <schedule out>
      kobject_init_and_add // refcount = 1;
   //couldn't find an available UDC or it's busy
   <context switch>
                                       priv alloced
                                       drv->priv = priv;
                                       kobject_init_and_add
                                         ---> refcount = 1 <------
                                       // register success
                                       <context switch>
===================== another ioctl/process ======================
                                      driver_register
                                       driver_find
                                        k = kset_find_obj()
                                         ---> refcount = 2 <------
                                        <context out>
   driver_unregister
   // drv->p become T2's priv
   ---> refcount = 1 <------
   <context switch>
                                        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/
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Signed-off-by: Schspa Shi <schspa@gmail.com>
Link: https://lore.kernel.org/r/20220508150247.38204-1-schspa@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 33efa6915b91..34cecd3660bf 100644
--- a/drivers/usb/gadget/legacy/raw_gadget.c
+++ b/drivers/usb/gadget/legacy/raw_gadget.c
@@ -144,6 +144,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
@@ -507,6 +508,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_probe_driver(&dev->driver);
-- 
2.35.1


      parent reply	other threads:[~2022-05-18 12:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18 12:28 [PATCH AUTOSEL 5.10 01/13] scsi: qla2xxx: Fix missed DMA unmap for aborted commands Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 02/13] mac80211: fix rx reordering with non explicit / psmp ack policy Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 03/13] nl80211: validate S1G channel width Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 04/13] selftests: add ping test with ping_group_range tuned Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 05/13] nl80211: fix locking in nl80211_set_tx_bitrate_mask() Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 06/13] ethernet: tulip: fix missing pci_disable_device() on error in tulip_init_one() Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 07/13] net: stmmac: fix missing pci_disable_device() on error in stmmac_pci_probe() Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 08/13] net: atlantic: fix "frag[0] not initialized" Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 09/13] net: atlantic: reduce scope of is_rsc_complete Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 10/13] net: atlantic: add check for MAX_SKB_FRAGS Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 11/13] net: atlantic: verify hw_head_ lies within TX buffer ring Sasha Levin
2022-05-18 12:28 ` [PATCH AUTOSEL 5.10 12/13] arm64: Enable repeat tlbi workaround on KRYO4XX gold CPUs Sasha Levin
2022-05-18 12:28 ` Sasha Levin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220518122844.343220-13-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=Julia.Lawall@inria.fr \
    --cc=andreyknvl@gmail.com \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=jj251510319013@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=schspa@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).