Skip to contentSkip to conversation

USB: cdc-acm: fix minor-number release

PATCH In review Johan Hovold 2 participants 2 messages Sep 7, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 USB: cdc-acm: fix minor-number release

If the driver runs out of minor numbers it would release minor 0 and allow another device to claim the minor while still in use.

Fortunately, registering the tty class device of the second device would fail (with a stack dump) due to the sysfs name collision so no memory is leaked.

Fixes cae2bc768d17 ("usb: cdc-acm: Decrement tty port's refcount if probe() fail")
Cc Jaejoong Kim <[email protected]>

---

Changes in v2
- add a dedicated define for the invalid minor number (Oliver)

 drivers/usb/class/cdc-acm.c | 7 +++++--
 drivers/usb/class/cdc-acm.h | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/usb/class/cdc-acm.c +5 -2
index 4895325b16a4..5b90d0979c60 100644
@@ -726,7 +726,8 @@ static void acm_port_destruct(struct tty_port *port)
 {
 	struct acm *acm = container_of(port, struct acm, port);
 
-	acm_release_minor(acm);
+	if (acm->minor != ACM_MINOR_INVALID)
+		acm_release_minor(acm);
 	usb_put_intf(acm->control);
 	kfree(acm->country_codes);
 	kfree(acm);
@@ -1323,8 +1324,10 @@ static int acm_probe(struct usb_interface *intf,
 	usb_get_intf(acm->control); /* undone in destruct() */
 
 	minor = acm_alloc_minor(acm);
-	if (minor < 0)
+	if (minor < 0) {
+		acm->minor = ACM_MINOR_INVALID;
 		goto err_put_port;
+	}
 
 	acm->minor = minor;
 	acm->dev = usb_dev;
drivers/usb/class/cdc-acm.h +2 -0
index 8aef5eb769a0..3aa7f0a3ad71 100644
@@ -22,6 +22,8 @@
 #define ACM_TTY_MAJOR		166
 #define ACM_TTY_MINORS		256
 
+#define ACM_MINOR_INVALID	ACM_TTY_MINORS
+
 /*
  * Requests.
  */
Signature · 1 line
2.32.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 11 lines · Johan Hovold

If the driver runs out of minor numbers it would release minor 0 and allow another device to claim the minor while still in use.

Fortunately, registering the tty class device of the second device would fail (with a stack dump) due to the sysfs name collision so no memory is leaked.

Fixes cae2bc768d17 ("usb: cdc-acm: Decrement tty port's refcount if probe() fail")
Cc Jaejoong Kim <[email protected]>