Skip to content

Commit e34139c

Browse files
Ma Kemehmetb0
authored andcommitted
usb: fix reference leak in usb_new_device()
BugLink: https://bugs.launchpad.net/bugs/2097298 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: Noah Wager <noah.wager@canonical.com> Signed-off-by: Koichiro Den <koichiro.den@canonical.com>
1 parent 1da8373 commit e34139c

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
@@ -2605,13 +2605,13 @@ int usb_new_device(struct usb_device *udev)
26052605
err = sysfs_create_link(&udev->dev.kobj,
26062606
&port_dev->dev.kobj, "port");
26072607
if (err)
2608-
goto fail;
2608+
goto out_del_dev;
26092609

26102610
err = sysfs_create_link(&port_dev->dev.kobj,
26112611
&udev->dev.kobj, "device");
26122612
if (err) {
26132613
sysfs_remove_link(&udev->dev.kobj, "port");
2614-
goto fail;
2614+
goto out_del_dev;
26152615
}
26162616

26172617
if (!test_and_set_bit(port1, hub->child_usage_bits))
@@ -2623,6 +2623,8 @@ int usb_new_device(struct usb_device *udev)
26232623
pm_runtime_put_sync_autosuspend(&udev->dev);
26242624
return err;
26252625

2626+
out_del_dev:
2627+
device_del(&udev->dev);
26262628
fail:
26272629
usb_set_device_state(udev, USB_STATE_NOTATTACHED);
26282630
pm_runtime_disable(&udev->dev);

0 commit comments

Comments
 (0)