From: Schspa Shi <schspa@gmail.com>
To: schspa@gmail.com
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, stern@rowland.harvard.edu,
syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com
Subject: [PATCH v2] usb: gadget: fix race when gadget driver register via ioctl
Date: Sun, 8 May 2022 00:02:43 +0800 [thread overview]
Message-ID: <20220507160243.35304-1-schspa@gmail.com> (raw)
In-Reply-To: <CAMA88TrcHZH7vw8W4Jh+NCQJvpe3wQM-4k46MnDQC9agna4XJg@mail.gmail.com>
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.
Reported-by: syzbot+dc7c3ca638e773db07f6@syzkaller.appspotmail.com
Link: https://lore.kernel.org/all/000000000000e66c2805de55b15a@google.com/
Signed-off-by: Schspa Shi <schspa@gmail.com>
---
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)
next prev parent reply other threads:[~2022-05-07 16:03 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-07 12:08 [PATCH] usb: gadget: fix race when gadget driver register via ioctl Schspa Shi
2022-05-07 14:27 ` Greg KH
2022-05-07 15:06 ` Alan Stern
2022-05-07 15:50 ` Schspa Shi
2022-05-07 16:02 ` Schspa Shi [this message]
2022-05-07 17:56 ` [PATCH v2] " Alan Stern
2022-05-08 4:08 ` Schspa Shi
2022-05-08 14:34 ` Alan Stern
2022-05-08 15:02 ` [PATCH v3] " Schspa Shi
2022-05-08 23:51 ` Andrey Konovalov
2022-05-07 15:43 ` [PATCH] " Schspa Shi
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=20220507160243.35304-1-schspa@gmail.com \
--to=schspa@gmail.com \
--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=stern@rowland.harvard.edu \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.