Skip to contentSkip to conversation

USB: core: Fix unlocked hcd->status_urb access in usb_hcd_poll_rh_status()

PATCH In review Ginger Li 3 participants 3 messages Sep 23 USB

lore
Share & tools
lore

Ginger Li

author
#0 lore raw

PATCH 1/1 USB: core: Fix unlocked hcd->status_urb access in usb_hcd_poll_rh_status()

usb_hcd_poll_rh_status() tests hcd->status_urb without holding hcd_root_hub_lock, while rh_queue_status() and the completion path inside usb_hcd_poll_rh_status() itself update that field under the lock.

Take hcd_root_hub_lock around the early bail-out check so that the read is serialized with the writers.

Fixes d5926ae7a827 ("[PATCH] usbcore support for root-hub IRQ instead of polling")
---
 drivers/usb/core/hcd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/usb/core/hcd.c +6 -1
@@ -727,8 +727,13 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
 
 	if (unlikely(!hcd->rh_pollable))
 		return;
-	if (!hcd->uses_new_polling && !hcd->status_urb)
+
+	spin_lock_irqsave(&hcd_root_hub_lock, flags);
+	if (!hcd->uses_new_polling && !hcd->status_urb) {
+		spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
 		return;
+	}
+	spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
 
 	length = hcd->driver->hub_status_data(hcd, buffer);
 	if (length > 0) {
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 6 lines · Ginger Li

usb_hcd_poll_rh_status() tests hcd->status_urb without holding hcd_root_hub_lock, while rh_queue_status() and the completion path inside usb_hcd_poll_rh_status() itself update that field under the lock.

Take hcd_root_hub_lock around the early bail-out check so that the read is serialized with the writers.

Is that a real issue? How can this be hit?

How was this found? How was it tested?
Did you forget an Assisted-by tag?

Quoted 11 lines
Fixes d5926ae7a827 ("[PATCH] usbcore support for root-hub IRQ instead of polling")
---
 drivers/usb/core/hcd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

drivers/usb/core/hcd.c void usb_hcd_poll_rh_status(struct usb_hcd *hcd) · line 731

Quoted 6 lines
drivers/usb/core/hcd.c +2 -1
@@ -727,8 +727,13 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
 
 	if (unlikely(!hcd->rh_pollable))
 		return;
-	if (!hcd->uses_new_polling && !hcd->status_urb)
+
+	spin_lock_irqsave(&hcd_root_hub_lock, flags);

guard()?

thanks,

greg k-h

#2 lore raw
In reply to Ginger Li
Quoted 6 lines · Ginger Li

usb_hcd_poll_rh_status() tests hcd->status_urb without holding hcd_root_hub_lock, while rh_queue_status() and the completion path inside usb_hcd_poll_rh_status() itself update that field under the lock.

Take hcd_root_hub_lock around the early bail-out check so that the read is serialized with the writers.

Why does the read need to be serialized with the writers? What problems does lack of serialization cause?

Alan Stern

Quoted 28 lines
Fixes d5926ae7a827 ("[PATCH] usbcore support for root-hub IRQ instead of polling")
---
 drivers/usb/core/hcd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/usb/core/hcd.c +6 -1
@@ -727,8 +727,13 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
 
 	if (unlikely(!hcd->rh_pollable))
 		return;
-	if (!hcd->uses_new_polling && !hcd->status_urb)
+
+	spin_lock_irqsave(&hcd_root_hub_lock, flags);
+	if (!hcd->uses_new_polling && !hcd->status_urb) {
+		spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
 		return;
+	}
+	spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
 
 	length = hcd->driver->hub_status_data(hcd, buffer);
 	if (length > 0) {
Signature · 1 line
2.43.0