Skip to contentSkip to conversation

usb: misc: fix improper handling of refcount in uss720_probe()

PATCH In review Hangyu Hua 2 participants 2 messages Apr 7, 2022 USB

lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH v2 1/1 usb: misc: fix improper handling of refcount in uss720_probe()

usb_put_dev shouldn't be called when uss720_probe succeeds because of priv->usbdev. At the same time, priv->usbdev shouldn't be set to NULL before destroy_priv in uss720_disconnect because usb_put_dev is in destroy_priv.

Fix this by moving priv->usbdev = NULL after usb_put_dev.

Fixes dcb4b8ad6a44 ("misc/uss720: fix memory leak in uss720_probe")

---

v2: fix a stupid UAF in the previous version.

 drivers/usb/misc/uss720.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/misc/uss720.c +1 -2
index 748139d26263..0be8efcda15d 100644
@@ -71,6 +71,7 @@ static void destroy_priv(struct kref *kref)
 
 	dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
 	usb_put_dev(priv->usbdev);
+	priv->usbdev = NULL;
 	kfree(priv);
 }
 
@@ -736,7 +737,6 @@ static int uss720_probe(struct usb_interface *intf,
 	parport_announce_port(pp);
 
 	usb_set_intfdata(intf, pp);
-	usb_put_dev(usbdev);
 	return 0;
 
 probe_abort:
@@ -754,7 +754,6 @@ static void uss720_disconnect(struct usb_interface *intf)
 	usb_set_intfdata(intf, NULL);
 	if (pp) {
 		priv = pp->private_data;
-		priv->usbdev = NULL;
 		priv->pp = NULL;
 		dev_dbg(&intf->dev, "parport_remove_port\n");
 		parport_remove_port(pp);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Hangyu Hua

usb_put_dev shouldn't be called when uss720_probe succeeds because of priv->usbdev. At the same time, priv->usbdev shouldn't be set to NULL before destroy_priv in uss720_disconnect because usb_put_dev is in destroy_priv.

Fix this by moving priv->usbdev = NULL after usb_put_dev.

Hi Hangyu,

good catch as priv->usbdev is still used after the probe function.

I agree with this patch.

Quoted 41 lines
Fixes dcb4b8ad6a44 ("misc/uss720: fix memory leak in uss720_probe")

---

v2: fix a stupid UAF in the previous version.

 drivers/usb/misc/uss720.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/misc/uss720.c +1 -3
index 748139d26263..0be8efcda15d 100644
@@ -71,6 +71,7 @@ static void destroy_priv(struct kref *kref)
 
        dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
        usb_put_dev(priv->usbdev);
+       priv->usbdev = NULL;
        kfree(priv);
 }
 
@@ -736,7 +737,6 @@ static int uss720_probe(struct usb_interface *intf,
        parport_announce_port(pp);
 
        usb_set_intfdata(intf, pp);
-       usb_put_dev(usbdev);
        return 0;
 
 probe_abort:
@@ -754,7 +754,6 @@ static void uss720_disconnect(struct usb_interface *intf)
        usb_set_intfdata(intf, NULL);
        if (pp) {
                priv = pp->private_data;
-               priv->usbdev = NULL;
                priv->pp = NULL;
                dev_dbg(&intf->dev, "parport_remove_port\n");
                parport_remove_port(pp);
--

2.25.1