Skip to contentSkip to conversation

xhci: dbgtty: Fix unregister on tty_register_driver() failure

PATCH In review Lucas De Marchi 2 participants 4 messages Jul 20 – Jul 28

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

PATCH 1/3 xhci: dbgtty: Fix unregister on tty_register_driver() failure

If tty_register_driver() fails, it drops the reference, but fails to set the global dbc_tty_driver to NULL, causing the unregister to be called again when module exits.

On module unload dbc_tty_exit() only gates its cleanup on the driver pointer being non-NULL, so it operates on the already-freed driver:

    module_init(xhci_hcd_init)
      xhci_hcd_init()
        xhci_dbc_init()                       [return value ignored]
          dbc_tty_init()
            tty_register_driver() fails
              tty_driver_kref_put()           -> driver freed
              (dbc_tty_driver left dangling)
    ...
    module_exit(xhci_hcd_fini)
      xhci_hcd_fini()
        xhci_dbc_exit()
          dbc_tty_exit()
            if (dbc_tty_driver)               -> true (dangling)
              tty_unregister_driver()         -> use-after-free
Fixes 4521f1613940 ("xhci: dbctty: split dbc tty driver registration and unregistration functions.")
Cc Mathias Nyman <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/usb/host/xhci-dbgtty.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/host/xhci-dbgtty.c +1 -0
index 2e7384c6b6ec..01decd46f313 100644
@@ -651,6 +651,7 @@ int dbc_tty_init(void)
 		pr_err("Can't register dbc tty driver\n");
 		tty_driver_kref_put(dbc_tty_driver);
 		idr_destroy(&dbc_tty_minors);
+		dbc_tty_driver = NULL;
 	}
 
 	return ret;
Signature · 1 line
2.54.0
#1 lore raw

PATCH 2/3 xhci: dbgtty: Fix unregister on tty_alloc_driver() failure

Make sure to set dbc_tty_driver to NULL to match the check in dbc_tty_exit(). For that, make detached error handling path common to the other branch in the same function.

Fixes 4521f1613940 ("xhci: dbctty: split dbc tty driver registration and unregistration functions.")
Cc Mathias Nyman <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/usb/host/xhci-dbgtty.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-dbgtty.c +11 -5
index 01decd46f313..7e44e62d6ea9 100644
@@ -628,8 +628,8 @@ int dbc_tty_init(void)
 	dbc_tty_driver = tty_alloc_driver(64, TTY_DRIVER_REAL_RAW |
 					  TTY_DRIVER_DYNAMIC_DEV);
 	if (IS_ERR(dbc_tty_driver)) {
-		idr_destroy(&dbc_tty_minors);
-		return PTR_ERR(dbc_tty_driver);
+		ret = PTR_ERR(dbc_tty_driver);
+		goto fail;
 	}
 
 	dbc_tty_driver->driver_name = "dbc_serial";
@@ -649,11 +649,17 @@ int dbc_tty_init(void)
 	ret = tty_register_driver(dbc_tty_driver);
 	if (ret) {
 		pr_err("Can't register dbc tty driver\n");
-		tty_driver_kref_put(dbc_tty_driver);
-		idr_destroy(&dbc_tty_minors);
-		dbc_tty_driver = NULL;
+		goto fail_put;
 	}
 
+	return ret;
+
+fail_put:
+	tty_driver_kref_put(dbc_tty_driver);
+fail:
+	idr_destroy(&dbc_tty_minors);
+	dbc_tty_driver = NULL;
+
 	return ret;
 }
 
Signature · 1 line
2.54.0
#2 lore raw

PATCH 3/3 xhci: dbgtty: Drop extra call to idr_destroy()

In reply to Lucas De Marchi

idr_destroy() is already called on error paths in dbc_tty_init(). Do not call it again on exit. For symmetry with the init side, also use IS_ERR_OR_NULL() to gate the exit steps.

---
 drivers/usb/host/xhci-dbgtty.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/usb/host/xhci-dbgtty.c +5 -5
index 7e44e62d6ea9..3d51e8d82659 100644
@@ -665,11 +665,11 @@ int dbc_tty_init(void)
 
 void dbc_tty_exit(void)
 {
-	if (dbc_tty_driver) {
-		tty_unregister_driver(dbc_tty_driver);
-		tty_driver_kref_put(dbc_tty_driver);
-		dbc_tty_driver = NULL;
-	}
+	if (IS_ERR_OR_NULL(dbc_tty_driver))
+		return;
 
+	tty_unregister_driver(dbc_tty_driver);
+	tty_driver_kref_put(dbc_tty_driver);
 	idr_destroy(&dbc_tty_minors);
+	dbc_tty_driver = NULL;
 }
Signature · 1 line
2.54.0
#3 lore raw
In reply to Lucas De Marchi
Quoted 22 lines · Lucas De Marchi

If tty_register_driver() fails, it drops the reference, but fails to set the global dbc_tty_driver to NULL, causing the unregister to be called again when module exits.

On module unload dbc_tty_exit() only gates its cleanup on the driver pointer being non-NULL, so it operates on the already-freed driver:

     module_init(xhci_hcd_init)
       xhci_hcd_init()
         xhci_dbc_init()                       [return value ignored]
           dbc_tty_init()
             tty_register_driver() fails
               tty_driver_kref_put()           -> driver freed
               (dbc_tty_driver left dangling)
     ...
     module_exit(xhci_hcd_fini)
       xhci_hcd_fini()
         xhci_dbc_exit()
           dbc_tty_exit()
             if (dbc_tty_driver)               -> true (dangling)
               tty_unregister_driver()         -> use-after-free
Quoted 6 lines
Fixes 4521f1613940 ("xhci: dbctty: split dbc tty driver registration and unregistration functions.")
Cc Mathias Nyman <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

Thanks

Added series to queue

-Mathias