linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Race condition when accessing authorized_default for a root hub
@ 2019-08-05 10:29 Thiébaud Weksteen
  2019-08-05 11:03 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 2+ messages in thread
From: Thiébaud Weksteen @ 2019-08-05 10:29 UTC (permalink / raw)
  To: linux-usb, Greg Kroah-Hartman

Hi,

I believe there is a race condition for userland in hcd.c where the
kernel sends a uevent before the underlying sysfs is fully populated:

(drivers/usb/core/hcd.c)
          /* starting here, usbcore will pay attention to this root hub */
          retval = register_root_hub(hcd);
          if (retval != 0)
                  goto err_register_root_hub;
          [...]
          retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
          if (retval < 0) {
                  printk(KERN_ERR "Cannot register USB bus sysfs
attributes: %d\n",
                         retval);
                  goto error_create_attr_group;
          }

Here, register_root_hub will call kobject_uevent(&dev->kobj, KOBJ_ADD)
(via usb_new_device and device_add). This was found in userland (see
https://github.com/USBGuard/usbguard/issues/321).

The fix is not as simple as swapping both blocks (that is, calling
sysfs_create_group before register_root_hub) as device_add is
responsible for calling kobject_add and kobject_uevent.

This can potentially be solved by adding the sysfs_create_group call
to the bus_notifier list (blocking). Thoughts?

Thanks,
Thiebaud

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Race condition when accessing authorized_default for a root hub
  2019-08-05 10:29 Race condition when accessing authorized_default for a root hub Thiébaud Weksteen
@ 2019-08-05 11:03 ` Greg Kroah-Hartman
  0 siblings, 0 replies; 2+ messages in thread
From: Greg Kroah-Hartman @ 2019-08-05 11:03 UTC (permalink / raw)
  To: Thiébaud Weksteen; +Cc: linux-usb

On Mon, Aug 05, 2019 at 12:29:27PM +0200, Thiébaud Weksteen wrote:
> Hi,
> 
> I believe there is a race condition for userland in hcd.c where the
> kernel sends a uevent before the underlying sysfs is fully populated:
> 
> (drivers/usb/core/hcd.c)
>           /* starting here, usbcore will pay attention to this root hub */
>           retval = register_root_hub(hcd);
>           if (retval != 0)
>                   goto err_register_root_hub;
>           [...]
>           retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
>           if (retval < 0) {
>                   printk(KERN_ERR "Cannot register USB bus sysfs
> attributes: %d\n",
>                          retval);
>                   goto error_create_attr_group;
>           }
> 
> Here, register_root_hub will call kobject_uevent(&dev->kobj, KOBJ_ADD)
> (via usb_new_device and device_add). This was found in userland (see
> https://github.com/USBGuard/usbguard/issues/321).
> 
> The fix is not as simple as swapping both blocks (that is, calling
> sysfs_create_group before register_root_hub) as device_add is
> responsible for calling kobject_add and kobject_uevent.
> 
> This can potentially be solved by adding the sysfs_create_group call
> to the bus_notifier list (blocking). Thoughts?

Yes, this type of race condition is _ALL_ over the kernel tree.  I am
working on solving these with the addition of the dev_groups pointer to
struct driver that went into linux-next last week.  I have a series of
patches I'm working on for all USB drivers to take advantage of this and
will post them later this week when they are done.

Ideally no driver should ever call sysfs_create_group(s) on their own,
their "parent" code should be the one doing it (either in the driver
core, or in the bus specific code.)  But, this one is a bit tricker as
root hubs are a bit different.  I'll add this to my list to fix up,
unless you want to send a patch earlier?

And as I said above, this is a well-known, and very old issue.  If you
are relying on sysfs attributes to always be present when KOBJ_ADD is
called, you _will_ race with the kernel at the moment.  Hopefully these
can all be fixed up so that next year you will not have to worry about
this :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-05 11:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-05 10:29 Race condition when accessing authorized_default for a root hub Thiébaud Weksteen
2019-08-05 11:03 ` Greg Kroah-Hartman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).