Message ID | 20241218021940.2967550-1-make_ruc2021@163.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [v2] usb: fix reference leak in usb_new_device() | expand |
On Wed, Dec 18, 2024 at 10:19:40AM +0800, Ma Ke wrote: > When device_add(&udev->dev) failed, calling put_device() to explicitly > release udev->dev. And the routine which calls usb_new_device() does > not call put_device() when an error occurs. That is wrong. usb_new_device() is called by hub_port_connect(). The code does: status = usb_new_device(udev); ... if (status) goto loop_disable; ... loop_disable: hub_port_disable(hub, port1, 1); loop: usb_ep0_reinit(udev); release_devnum(udev); hub_free_dev(udev); if (retry_locked) { mutex_unlock(hcd->address0_mutex); usb_unlock_port(port_dev); } usb_put_dev(udev); And usb_put_dev() is defined in usb.c as: void usb_put_dev(struct usb_device *dev) { if (dev) put_device(&dev->dev); } So you see, if usb_new_device() returns a nonzero value then put_device() _is_ called. > 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'. You are correct that if device_add() succeeds and a later call fails, then usb_new_device() does not properly call device_del(). Please rewrite your patch to fix only that problem. Alan Stern
Alan Stern<stern@rowland.harvard.edu> wrote: > Ma Ke <make_ruc2021@163.com> writes: > > When device_add(&udev->dev) failed, calling put_device() to explicitly > > release udev->dev. And the routine which calls usb_new_device() does > > not call put_device() when an error occurs. > > That is wrong. > > usb_new_device() is called by hub_port_connect(). The code does: > > status = usb_new_device(udev); > ... > > if (status) > goto loop_disable; > ... > > loop_disable: > hub_port_disable(hub, port1, 1); > loop: > usb_ep0_reinit(udev); > release_devnum(udev); > hub_free_dev(udev); > if (retry_locked) { > mutex_unlock(hcd->address0_mutex); > usb_unlock_port(port_dev); > } > usb_put_dev(udev); > > And usb_put_dev() is defined in usb.c as: > > void usb_put_dev(struct usb_device *dev) > { > if (dev) > put_device(&dev->dev); > } > > So you see, if usb_new_device() returns a nonzero value then > put_device() _is_ called. > > > 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'. > > You are correct that if device_add() succeeds and a later call fails, > then usb_new_device() does not properly call device_del(). Please > rewrite your patch to fix only that problem. > > Alan Stern Thank you for guiding me on the vulnerability I submitted. I will resubmit the patch based on your guidance and suggestions. -- Regards, Ma Ke
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c index 4b93c0bd1d4b..ddd572312296 100644 --- a/drivers/usb/core/hub.c +++ b/drivers/usb/core/hub.c @@ -2651,6 +2651,7 @@ int usb_new_device(struct usb_device *udev) err = device_add(&udev->dev); if (err) { dev_err(&udev->dev, "can't device_add, error %d\n", err); + put_device(&udev->dev); goto fail; } @@ -2663,13 +2664,13 @@ int usb_new_device(struct usb_device *udev) err = sysfs_create_link(&udev->dev.kobj, &port_dev->dev.kobj, "port"); if (err) - goto fail; + goto out_del_dev; err = sysfs_create_link(&port_dev->dev.kobj, &udev->dev.kobj, "device"); if (err) { sysfs_remove_link(&udev->dev.kobj, "port"); - goto fail; + goto out_del_dev; } if (!test_and_set_bit(port1, hub->child_usage_bits)) @@ -2683,6 +2684,9 @@ int usb_new_device(struct usb_device *udev) pm_runtime_put_sync_autosuspend(&udev->dev); return err; +out_del_dev: + device_del(&udev->dev); + put_device(&udev->dev); fail: usb_set_device_state(udev, USB_STATE_NOTATTACHED); pm_runtime_disable(&udev->dev);
When device_add(&udev->dev) failed, calling put_device() to explicitly release udev->dev. And the routine which calls usb_new_device() does not call put_device() when an error occurs. 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@vger.kernel.org Fixes: 9f8b17e643fe ("USB: make usbdevices export their device nodes instead of using a separate class") Signed-off-by: Ma Ke <make_ruc2021@163.com> --- Changes in v2: - modified the bug description to make it more clear; - added the missed part of the patch. --- drivers/usb/core/hub.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)