Skip to contentSkip to conversation

usb: misc: yurex: fix ordering of usb_deregister_dev() and usb_set_intfdata()

PATCH In review Junzhe Li 2 participants 5 messages May 28 – May 29 USB

lore
Share & tools
lore

Junzhe Li

author
#0 lore raw

PATCH 1/1 usb: misc: yurex: fix ordering of usb_deregister_dev() and usb_set_intfdata()

In yurex_disconnect(), usb_set_intfdata(interface, NULL) was called before usb_deregister_dev(interface, &yurex_class).
This opens a race window with usb_open() in the USB core:

T0 (yurex_disconnect) T1 (usb_open)
-------------------------- -------------------------
usb_set_intfdata(iface, NULL) [t0]
fops = usb_minors[minor] [t1] /* fops still valid here */
usb_deregister_dev()

    usb_minors[minor] = NULL   [t2]
                                       file->f_op->open(inode, file)
                                         yurex_open()
                                           dev = usb_get_intfdata() [t3]
                                           /* dev is NULL! */

Because t0 precedes t1 precedes t2 precedes t3, T1 can obtain the file_operations pointer for the device (t1, while the minor is still registered), then continue into yurex_open() where it calls usb_get_intfdata() and gets NULL back, leading to a NULL dereference.

Fix the race by calling usb_deregister_dev() first, which removes the device from usb_minors[] before the interface data pointer is cleared. Any concurrent usb_open() that arrives after usb_deregister_dev() returns will fail to look up the fops and will never reach yurex_open().

---
 drivers/usb/misc/yurex.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/misc/yurex.c +2 -1
index 7a482cdee1e9..136272ac24ba 100644
@@ -310,11 +310,12 @@ static void yurex_disconnect(struct usb_interface *interface)
 	int minor = interface->minor;
 
 	dev = usb_get_intfdata(interface);
-	usb_set_intfdata(interface, NULL);
 
 	/* give back our minor */
 	usb_deregister_dev(interface, &yurex_class);
 
+	usb_set_intfdata(interface, NULL);
+
 	/* prevent more I/O from starting */
 	usb_poison_urb(dev->urb);
 	usb_poison_urb(dev->cntl_urb);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · Junzhe Li

In yurex_disconnect(), usb_set_intfdata(interface, NULL) was called before usb_deregister_dev(interface, &yurex_class).
This opens a race window with usb_open() in the USB core:

Quoted 11 lines
   T0 (yurex_disconnect)               T1 (usb_open)
   --------------------------           -------------------------
   usb_set_intfdata(iface, NULL) [t0]
                                        fops = usb_minors[minor]  [t1]
                                        /* fops still valid here */
   usb_deregister_dev()
     usb_minors[minor] = NULL   [t2]
                                        file->f_op->open(inode, file)
                                          yurex_open()
                                            dev = usb_get_intfdata() [t3]
                                            /* dev is NULL! */

Yes, but yurex_open() checks for dev == NULL
Could you please elaborate?

Regards
Oliver

author
#2 lore raw
Quoted 20 lines · Oliver Neukum
Quoted 4 lines · Junzhe Li

In yurex_disconnect(), usb_set_intfdata(interface, NULL) was called before usb_deregister_dev(interface, &yurex_class).
This opens a race window with usb_open() in the USB core:

Quoted 11 lines
   T0 (yurex_disconnect)               T1 (usb_open)
   --------------------------           -------------------------
   usb_set_intfdata(iface, NULL) [t0]
                                        fops = usb_minors[minor]  [t1]
                                        /* fops still valid here */
   usb_deregister_dev()
     usb_minors[minor] = NULL   [t2]
                                        file->f_op->open(inode, file)
                                          yurex_open()
                                            dev = usb_get_intfdata() [t3]
                                            /* dev is NULL! */
Quoted 6 lines

Yes, but yurex_open() checks for dev == NULL
Could you please elaborate?

Regards
Oliver

Hi Oliver,

Thanks for pointing that out. IMHO, the check for 'dev == NULL' does not nullify this bug. The potential race window with the null value check is elaborated below:

T0 (yurex_disconnect) T1 (usb_open)
-------------------------- -------------------------

                                       fops = usb_minors[minor]  [t0]
                                       /* fops still valid here */

                                       file->f_op->open(inode, file)
                                         yurex_open()
                                           dev = usb_get_intfdata() [t1]
                                           if (!dev)
                                            return -ENODEV;
  usb_set_intfdata(iface, NULL) [t2]
                                           kref_get(&dev->kref); [t3]
                                           /* dev is NULL! */

usb_deregister_dev()
usb_minors[minor] = NULL [t4]
/* The global usb_minors is nullified here */

I think the intuition is that the global exposure (i.e., the 'usb_minors') of usb fops should be disabled first, so that the subsequent nullification of internal fields can be considered local to prevent concurrent accesses.

Please correct me if I understand incorrectly.

Regards,
Junzhe

#3 lore raw
Quoted 4 lines · Ginger

I think the intuition is that the global exposure (i.e., the 'usb_minors') of usb fops should be disabled first, so that the subsequent nullification of internal fields can be considered local to prevent concurrent accesses.

Hi,

if I understand the logic correctly, the order in yurex_disconnect() makes sure if yurex_open() and yurex_disconnect() race, yurex_open() will never see an unregistered device with intfdata != NULL. That is, precisely because without a lock the race is unavoidable the newly opening task will be guaranteed to know that it has lost the race.

Regards
Oliver

author
#4 lore raw
Quoted 17 lines · Oliver Neukum
Quoted 4 lines · Ginger

I think the intuition is that the global exposure (i.e., the 'usb_minors') of usb fops should be disabled first, so that the subsequent nullification of internal fields can be considered local to prevent concurrent accesses.

Hi,

if I understand the logic correctly, the order in yurex_disconnect() makes sure if yurex_open() and yurex_disconnect() race, yurex_open() will never see an unregistered device with intfdata != NULL. That is, precisely because without a lock the race is unavoidable the newly opening task will be guaranteed to know that it has lost the race.

Quoted 3 lines

Regards
Oliver

Hi,

Yes, I believe we are on the same page about this patch.

Regards,
Junzhe