From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 2/3] commands: usbserial: prevent double free
Date: Fri, 7 Jul 2023 14:13:53 +0200 [thread overview]
Message-ID: <20230707121354.3248365-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230707121354.3248365-1-a.fatoum@pengutronix.de>
usbserial -d may be called more than once, which now results in removal
of a driver that may already be removed leading to double frees.
usbgadget -d doesn't suffer from this as both usb_multi_register and
usb_multi_unregister keep around a global variable to check if the
gadget is currently registered. Let's do likewise for usb_serial_unregister.
Fixes: 14211ab8b0e1 ("usb: gadget: Update core to Linux-6.3-rc2")
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/usb/gadget/legacy/serial.c | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/legacy/serial.c b/drivers/usb/gadget/legacy/serial.c
index 46c41f6dea30..913d174a9177 100644
--- a/drivers/usb/gadget/legacy/serial.c
+++ b/drivers/usb/gadget/legacy/serial.c
@@ -247,8 +247,17 @@ static struct usb_composite_driver gserial_driver = {
.unbind = gs_unbind,
};
+static bool usb_serial_registered;
+
int usb_serial_register(struct usb_serial_pdata *pdata)
{
+ int ret;
+
+ if (usb_serial_registered) {
+ pr_err("USB serial gadget already registered\n");
+ return -EBUSY;
+ }
+
/* We *could* export two configs; that'd be much cleaner...
* but neither of these product IDs was defined that way.
*/
@@ -273,10 +282,18 @@ int usb_serial_register(struct usb_serial_pdata *pdata)
device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
}
- return usb_composite_probe(&gserial_driver);
+ ret = usb_composite_probe(&gserial_driver);
+ if (!ret)
+ usb_serial_registered = true;
+
+ return ret;
}
void usb_serial_unregister(void)
{
+ if (!usb_serial_registered)
+ return;
+
usb_composite_unregister(&gserial_driver);
+ usb_serial_registered = false;
}
--
2.39.2
next prev parent reply other threads:[~2023-07-07 12:15 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-07 12:13 [PATCH master 1/3] usb: gadget: multi: do not double free on error Ahmad Fatoum
2023-07-07 12:13 ` Ahmad Fatoum [this message]
2023-07-07 12:13 ` [PATCH master 3/3] usb: gadget: return error on missing UDC Ahmad Fatoum
2023-07-26 13:32 ` [PATCH master 1/3] usb: gadget: multi: do not double free on error Sascha Hauer
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=20230707121354.3248365-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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