Skip to contentSkip to conversation

usb: usbip: add missing device lock on tweak configuration cmd

PATCH In review Niels Dossche 2 participants 4 messages Apr 12, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: usbip: add missing device lock on tweak configuration cmd

The function documentation of usb_set_configuration says that its callers should hold the device lock. This lock is held for all callsites except tweak_set_configuration_cmd. The code path can be executed for example when attaching a remote USB device. The solution is to surround the call by the device lock.

This bug was found using my experimental own-developed static analysis tool, which reported the missing lock on v5.17.2. I manually verified this bug report by doing code review as well. I runtime checked that the required lock is not held. I compiled and runtime tested this on x86_64 with a USB mouse. After applying this patch, my analyser no longer reports this potential bug.

Fixes 2c8c98158946 ("staging: usbip: let client choose device configuration")

---

I'm developing this tool as part of my master's dissertation in order to obtain my master's degree. If you'd like more information about the details of the tool, please let me know.

 drivers/usb/usbip/stub_rx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/usbip/stub_rx.c +2 -0
index 325c22008e53..5dd41e8215e0 100644
@@ -138,7 +138,9 @@ static int tweak_set_configuration_cmd(struct urb *urb)
 	req = (struct usb_ctrlrequest *) urb->setup_packet;
 	config = le16_to_cpu(req->wValue);
 
+	usb_lock_device(sdev->udev);
 	err = usb_set_configuration(sdev->udev, config);
+	usb_unlock_device(sdev->udev);
 	if (err && err != -ENODEV)
 		dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
 			config, err);
Signature · 1 line
2.35.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Niels Dossche

The function documentation of usb_set_configuration says that its callers should hold the device lock. This lock is held for all callsites except tweak_set_configuration_cmd. The code path can be executed for example when attaching a remote USB device. The solution is to surround the call by the device lock.

Quoted 7 lines

This bug was found using my experimental own-developed static analysis tool, which reported the missing lock on v5.17.2. I manually verified this bug report by doing code review as well. I runtime checked that the required lock is not held. I compiled and runtime tested this on x86_64 with a USB mouse. After applying this patch, my analyser no longer reports this potential bug.

How did you run-time check that the lock isn't held? Also x86_64 with a USB mouse - did you test it over loopback?

Quoted 17 lines
Fixes 2c8c98158946 ("staging: usbip: let client choose device configuration")

---

I'm developing this tool as part of my master's dissertation in order to obtain my master's degree. If you'd like more information about the details of the tool, please let me know.

  drivers/usb/usbip/stub_rx.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/usb/usbip/stub_rx.c +0 -0
index 325c22008e53..5dd41e8215e0 100644

drivers/usb/usbip/stub_rx.c static int tweak_set_configuration_cmd(struct urb *urb) · line 146

Quoted 9 lines
drivers/usb/usbip/stub_rx.c +2 -0
@@ -138,7 +138,9 @@ static int tweak_set_configuration_cmd(struct urb *urb)
  	req = (struct usb_ctrlrequest *) urb->setup_packet;
  	config = le16_to_cpu(req->wValue);
 
+	usb_lock_device(sdev->udev);
  	err = usb_set_configuration(sdev->udev, config);
+	usb_unlock_device(sdev->udev);
  	if (err && err != -ENODEV)
  		dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
  			config, err);

Looks good to me with the above questions answered.

Greg, please pick this patch up.

thanks,
-- Shuah

#2 lore raw
Quoted 15 lines · Shuah Khan
Quoted 6 lines · Niels Dossche

The function documentation of usb_set_configuration says that its callers should hold the device lock. This lock is held for all callsites except tweak_set_configuration_cmd. The code path can be executed for example when attaching a remote USB device. The solution is to surround the call by the device lock.

Quoted 7 lines

This bug was found using my experimental own-developed static analysis tool, which reported the missing lock on v5.17.2. I manually verified this bug report by doing code review as well. I runtime checked that the required lock is not held. I compiled and runtime tested this on x86_64 with a USB mouse. After applying this patch, my analyser no longer reports this potential bug.

Quoted 2 lines

How did you run-time check that the lock isn't held? Also x86_64 with a USB mouse - did you test it over loopback?

I checked the lock isn't held with lockdep_assert_not_held and also by checking the return value of usb_trylock_device to be equal to 1 in my testing. I checked my USB mouse over loopback yes, binding the mouse for remote use and attaching on loopback.

Thank you
Niels

Quoted 35 lines
Quoted 18 lines
Fixes 2c8c98158946 ("staging: usbip: let client choose device configuration")

---

I'm developing this tool as part of my master's dissertation in order to obtain my master's degree. If you'd like more information about the details of the tool, please let me know.

  drivers/usb/usbip/stub_rx.c | 2 ++

  1 file changed, 2 insertions(+)
drivers/usb/usbip/stub_rx.c +0 -0
index 325c22008e53..5dd41e8215e0 100644

drivers/usb/usbip/stub_rx.c static int tweak_set_configuration_cmd(struct urb *urb) · line 145

Quoted 8 lines
drivers/usb/usbip/stub_rx.c +1 -0
@@ -138,7 +138,9 @@ static int tweak_set_configuration_cmd(struct urb *urb)
       req = (struct usb_ctrlrequest *) urb->setup_packet;
       config = le16_to_cpu(req->wValue);
   +    usb_lock_device(sdev->udev);
       err = usb_set_configuration(sdev->udev, config);
+    usb_unlock_device(sdev->udev);
       if (err && err != -ENODEV)
           dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
               config, err);

Looks good to me with the above questions answered.

Greg, please pick this patch up.

thanks,
-- Shuah

maintainer
#3 lore raw
Quoted 16 lines · Niels Dossche
Quoted 15 lines · Shuah Khan
Quoted 13 lines · Niels Dossche

The function documentation of usb_set_configuration says that its callers should hold the device lock. This lock is held for all callsites except tweak_set_configuration_cmd. The code path can be executed for example when attaching a remote USB device. The solution is to surround the call by the device lock.

This bug was found using my experimental own-developed static analysis tool, which reported the missing lock on v5.17.2. I manually verified this bug report by doing code review as well. I runtime checked that the required lock is not held. I compiled and runtime tested this on x86_64 with a USB mouse. After applying this patch, my analyser no longer reports this potential bug.

Quoted 8 lines
Quoted 2 lines

How did you run-time check that the lock isn't held? Also x86_64 with a USB mouse - did you test it over loopback?

I checked the lock isn't held with lockdep_assert_not_held and also by checking the return value of usb_trylock_device to be equal to 1 in my testing. I checked my USB mouse over loopback yes, binding the mouse for remote use and attaching on loopback.

Great. Thank you for a detailed answer.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Fixes 2c8c98158946 ("staging: usbip: let client choose device configuration")

---

Quoted 6 lines
Quoted 6 lines

Looks good to me with the above questions answered.

Greg, please pick this patch up.

thanks,
-- Shuah