Skip to contentSkip to conversation

hw/usb/hcd-dwc2: Enforce epnum to 0 for the control endpoint to avoid the assertion failure in usb_ep_get()

PATCH In review Qiang Liu 3 participants 5 messages Jun 27, 2021 – Jul 21, 2021

lore
Share & tools
lore

Qiang Liu

author
#0 lore raw

PATCH 1/1 hw/usb/hcd-dwc2: Enforce epnum to 0 for the control endpoint to avoid the assertion failure in usb_ep_get()

When eptype is USB_ENDPOINT_XFER_CONTROL and pid is TSIZ_SC_MC_PID_SETUP, usb_ep_get() should return the control endpoint. In hw/usb/core.c, the assumed epnum of the control endpoint is 0. When epnum is not 0, usb_ep_get() will crash due to the check assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT).

The description
http://www.capital-micro.com/PDF/CME-M7_Family_User_Guide_EN.pdf
(18.5.3.4 (14), 18.5.3.4 (10)) a) mentions that the pid is maintained by the host, b) but doesn't mention that whether the epnum should be 0 for the control endpoint. However, usb_ep_get() assumes it is 0. To avoid potential assertion failure in usb_ep_get(), we could enforce epnum to 0 and warn users.

Fixes 153ef1662c3 ("dwc-hsotg (dwc2) USB host controller emulation")
---
 hw/usb/hcd-dwc2.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/hcd-dwc2.c +5 -0
index e1d96ac..65d9d46 100644
@@ -636,6 +636,11 @@ static void dwc2_enable_chan(DWC2State *s,  uint32_t index)
     }
 
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
+        if (epnum != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "epnum should be 0 for the control endpoint\n");
+            epnum = 0;
+        }
         pid = USB_TOKEN_SETUP;
     } else {
         pid = epdir ? USB_TOKEN_IN : USB_TOKEN_OUT;
Signature · 1 line
2.7.4
author
#1 lore raw

Hi folks,

I found this bug by my dwc2 fuzzer.
It seems that
* https://bugs.launchpad.net/qemu/+bug/1907042
* https://bugs.launchpad.net/qemu/+bug/1525123
or
* https://gitlab.com/qemu-project/qemu/-/issues/119
* https://gitlab.com/qemu-project/qemu/-/issues/303
have reported similar issues.

Would it be better to consider and fix them together?

Best,
Qiang

Quoted 40 lines · Qiang Liu

When eptype is USB_ENDPOINT_XFER_CONTROL and pid is TSIZ_SC_MC_PID_SETUP, usb_ep_get() should return the control endpoint. In hw/usb/core.c, the assumed epnum of the control endpoint is 0. When epnum is not 0, usb_ep_get() will crash due to the check assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT).

The description
http://www.capital-micro.com/PDF/CME-M7_Family_User_Guide_EN.pdf
(18.5.3.4 (14), 18.5.3.4 (10)) a) mentions that the pid is maintained by the host, b) but doesn't mention that whether the epnum should be 0 for the control endpoint. However, usb_ep_get() assumes it is 0. To avoid potential assertion failure in usb_ep_get(), we could enforce epnum to 0 and warn users.

Fixes 153ef1662c3 ("dwc-hsotg (dwc2) USB host controller emulation")
---
 hw/usb/hcd-dwc2.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/hcd-dwc2.c +5 -1
index e1d96ac..65d9d46 100644
@@ -636,6 +636,11 @@ static void dwc2_enable_chan(DWC2State *s,  uint32_t index)
     }
 
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
+        if (epnum != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "epnum should be 0 for the control endpoint\n");
+            epnum = 0;
+        }
         pid = USB_TOKEN_SETUP;
     } else {
         pid = epdir ? USB_TOKEN_IN : USB_TOKEN_OUT;
--

2.7.4

#2 lore raw
Quoted 46 lines · Qiang Liu

Hi folks,

I found this bug by my dwc2 fuzzer.
It seems that
* https://bugs.launchpad.net/qemu/+bug/1907042
* https://bugs.launchpad.net/qemu/+bug/1525123
or
* https://gitlab.com/qemu-project/qemu/-/issues/119
* https://gitlab.com/qemu-project/qemu/-/issues/303
have reported similar issues.

Would it be better to consider and fix them together?

Best,
Qiang

Quoted 28 lines · Qiang Liu

When eptype is USB_ENDPOINT_XFER_CONTROL and pid is TSIZ_SC_MC_PID_SETUP, usb_ep_get() should return the control endpoint. In hw/usb/core.c, the assumed epnum of the control endpoint is 0. When epnum is not 0, usb_ep_get() will crash due to the check assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT).

The description
http://www.capital-micro.com/PDF/CME-M7_Family_User_Guide_EN.pdf
(18.5.3.4 (14), 18.5.3.4 (10)) a) mentions that the pid is maintained by the host, b) but doesn't mention that whether the epnum should be 0 for the control endpoint. However, usb_ep_get() assumes it is 0. To avoid potential assertion failure in usb_ep_get(), we could enforce epnum to 0 and warn users.

Fixes 153ef1662c3 ("dwc-hsotg (dwc2) USB host controller emulation")
---
 hw/usb/hcd-dwc2.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/hcd-dwc2.c +0 -0
index e1d96ac..65d9d46 100644
@@ -636,6 +636,11 @@ static void dwc2_enable_chan(DWC2State *s,  uint32_t index)
     }
 
Quoted 12 lines
Quoted 10 lines
hw/usb/hcd-dwc2.c +5 -1
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
+        if (epnum != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "epnum should be 0 for the control endpoint\n");
+            epnum = 0;
+        }
         pid = USB_TOKEN_SETUP;
     } else {
         pid = epdir ? USB_TOKEN_IN : USB_TOKEN_OUT;
--
Quoted 2 lines

2.7.4

Hi Qiang,

Sorry for the late reply, I've had a busy week.
Yes, I think it would be best to fix this in the core since it affects more than one host. I'm not sure that forcing the Control endpoint to 0 is the best solution though, perhaps it would be better to print an error message and fail the operation? AFAIK there are no real-world devices that have Control endpoints other than 0, although I believe it is allowed by the USB spec.

Let's wait and see what Gerd thinks.

- Paul

author
#3 lore raw

Hi Paul,

Quoted 63 lines · Paul Zimmerman
Quoted 46 lines · Qiang Liu

Hi folks,

I found this bug by my dwc2 fuzzer.
It seems that
* https://bugs.launchpad.net/qemu/+bug/1907042
* https://bugs.launchpad.net/qemu/+bug/1525123
or
* https://gitlab.com/qemu-project/qemu/-/issues/119
* https://gitlab.com/qemu-project/qemu/-/issues/303
have reported similar issues.

Would it be better to consider and fix them together?

Best,
Qiang

Quoted 28 lines · Qiang Liu

When eptype is USB_ENDPOINT_XFER_CONTROL and pid is TSIZ_SC_MC_PID_SETUP, usb_ep_get() should return the control endpoint. In hw/usb/core.c, the assumed epnum of the control endpoint is 0. When epnum is not 0, usb_ep_get() will crash due to the check assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT).

The description
http://www.capital-micro.com/PDF/CME-M7_Family_User_Guide_EN.pdf
(18.5.3.4 (14), 18.5.3.4 (10)) a) mentions that the pid is maintained by the host, b) but doesn't mention that whether the epnum should be 0 for the control endpoint. However, usb_ep_get() assumes it is 0. To avoid potential assertion failure in usb_ep_get(), we could enforce epnum to 0 and warn users.

Fixes 153ef1662c3 ("dwc-hsotg (dwc2) USB host controller emulation")
---
 hw/usb/hcd-dwc2.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/hcd-dwc2.c +0 -0
index e1d96ac..65d9d46 100644
@@ -636,6 +636,11 @@ static void dwc2_enable_chan(DWC2State *s,  uint32_t index)
     }
 
Quoted 12 lines
Quoted 10 lines
hw/usb/hcd-dwc2.c +5 -1
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
+        if (epnum != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "epnum should be 0 for the control endpoint\n");
+            epnum = 0;
+        }
         pid = USB_TOKEN_SETUP;
     } else {
         pid = epdir ? USB_TOKEN_IN : USB_TOKEN_OUT;
--
Quoted 2 lines

2.7.4

Hi Qiang,

Quoted 3 lines

Sorry for the late reply, I've had a busy week.
Yes, I think it would be best to fix this in the core since it affects more than one host.

My current fix is in the following.

hw/usb/core.c +1 -1
index 975f762..a29b378 100644
@@ -735,10 +735,11 @@ void usb_ep_dump(USBDevice *dev)
 
 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
 {
     struct USBEndpoint *eps;
 
     assert(dev != NULL);
-    if (ep == 0) {
+    if (ep == 0 || pid == USB_TOKEN_SETUP) {
         return &dev->ep_ctl;
     }
     assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
Quoted 5 lines

I'm not sure that forcing the Control endpoint to 0 is
the best solution though, perhaps it would be better to print an error message and fail the operation? AFAIK there are no real-world devices that have Control endpoints other than 0, although I believe it is allowed by the USB spec.

I think, as a malicious guest, if the spec allows, it can set the control endpoint to non-zero.

Quoted 1 line

Let's wait and see what Gerd thinks.

Yes!

Best,
Qiang

#4 lore raw
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

* https://gitlab.com/qemu-project/qemu/-/issues/119
* https://gitlab.com/qemu-project/qemu/-/issues/303

hw/usb/core.c +0 -0
index 975f762..a29b378 100644

hw/usb/core.c void usb_ep_dump(USBDevice *dev) · line 743

Quoted 10 lines
hw/usb/core.c +1 -1
@@ -735,10 +735,11 @@ void usb_ep_dump(USBDevice *dev)
 
 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
 {
     struct USBEndpoint *eps;
 
     assert(dev != NULL);
-    if (ep == 0) {
+    if (ep == 0 || pid == USB_TOKEN_SETUP) {
         return &dev->ep_ctl;
     }

Looks sane to me, although I'm not sure it matches what real hardware does.

Can you submit that as proper patch (including Resolves: for the gitlab issues fixed)?

Quoted 3 lines
Quoted 3 lines

message and fail the operation? AFAIK there are no real-world devices that have Control endpoints other than 0, although I believe it is allowed by the USB spec.

IIRC control endpoint is zero by spec.

thanks & take care,
Gerd