Skip to content

Commit c6e7b38

Browse files
Ma Kemehmetb0
authored andcommitted
usb: fix reference leak in usb_new_device()
BugLink: https://bugs.launchpad.net/bugs/2106770 commit 0df11fa upstream. When device_add(&udev->dev) succeeds and a later call fails, usb_new_device() does not properly call device_del(). As comment of device_add() says, 'if device_add() succeeds, you should call device_del() when you want to get rid of it. If device_add() has not succeeded, use only put_device() to drop the reference count'. Found by code review. Cc: stable <stable@kernel.org> Fixes: 9f8b17e ("USB: make usbdevices export their device nodes instead of using a separate class") Signed-off-by: Ma Ke <make_ruc2021@163.com> Reviewed-by: Alan Stern <stern@rowland.harvard.edu> Link: https://lore.kernel.org/r/20241218071346.2973980-1-make_ruc2021@163.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Manuel Diewald <manuel.diewald@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent 98f5ecb commit c6e7b38

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/usb/core/hub.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,13 +2635,13 @@ int usb_new_device(struct usb_device *udev)
26352635
err = sysfs_create_link(&udev->dev.kobj,
26362636
&port_dev->dev.kobj, "port");
26372637
if (err)
2638-
goto fail;
2638+
goto out_del_dev;
26392639

26402640
err = sysfs_create_link(&port_dev->dev.kobj,
26412641
&udev->dev.kobj, "device");
26422642
if (err) {
26432643
sysfs_remove_link(&udev->dev.kobj, "port");
2644-
goto fail;
2644+
goto out_del_dev;
26452645
}
26462646

26472647
if (!test_and_set_bit(port1, hub->child_usage_bits))
@@ -2655,6 +2655,8 @@ int usb_new_device(struct usb_device *udev)
26552655
pm_runtime_put_sync_autosuspend(&udev->dev);
26562656
return err;
26572657

2658+
out_del_dev:
2659+
device_del(&udev->dev);
26582660
fail:
26592661
usb_set_device_state(udev, USB_STATE_NOTATTACHED);
26602662
pm_runtime_disable(&udev->dev);

0 commit comments

Comments
 (0)