Skip to contentSkip to conversation

hw/usb/hcd-dwc2: fix divide-by-zero in dwc2_handle_packet()

PATCH In review Mauro Matteo Cascella 3 participants 3 messages Oct 15, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/usb/hcd-dwc2: fix divide-by-zero in dwc2_handle_packet()

Check the value of mps to avoid potential divide-by-zero later in the function. Since HCCHAR_MPS is guest controllable, this prevents a malicious/buggy guest from crashing the QEMU process on the host.

---
Moved check earlier in the function and added "Reviewed-by" line.

 hw/usb/hcd-dwc2.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/usb/hcd-dwc2.c +6 -0
index 97688d21bf..9856c4540c 100644
@@ -245,6 +245,12 @@ static void dwc2_handle_packet(DWC2State *s, uint32_t devadr, USBDevice *dev,
     trace_usb_dwc2_handle_packet(chan, dev, &p->packet, epnum, types[eptype],
                                  dirs[epdir], mps, len, pcnt);
 
+    if (mps == 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: Bad HCCHAR_MPS set to zero\n", __func__);
+        return;
+    }
+
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
         pid = USB_TOKEN_SETUP;
     } else {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 10 lines · Mauro Matteo Cascella

Check the value of mps to avoid potential divide-by-zero later in the function. Since HCCHAR_MPS is guest controllable, this prevents a malicious/buggy guest from crashing the QEMU process on the host.

---
Moved check earlier in the function and added "Reviewed-by" line.

Added to usb queue.

thanks,
Gerd

Tested-by Paul Zimmerman on v2 1/1
#2 lore raw
In reply to Mauro Matteo Cascella
Quoted 9 lines · Mauro Matteo Cascella

Check the value of mps to avoid potential divide-by-zero later in the function. Since HCCHAR_MPS is guest controllable, this prevents a malicious/buggy guest from crashing the QEMU process on the host.

Maybe also the implicit

from:
https://lists.gnu.org/archive/html/qemu-devel/2020-10/msg04254.html

Quoted 27 lines

---
Moved check earlier in the function and added "Reviewed-by" line.

 hw/usb/hcd-dwc2.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/usb/hcd-dwc2.c +6 -1
index 97688d21bf..9856c4540c 100644
@@ -245,6 +245,12 @@ static void dwc2_handle_packet(DWC2State *s, uint32_t devadr, USBDevice *dev,
     trace_usb_dwc2_handle_packet(chan, dev, &p->packet, epnum, types[eptype],
                                  dirs[epdir], mps, len, pcnt);
 
+    if (mps == 0) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: Bad HCCHAR_MPS set to zero\n", __func__);
+        return;
+    }
+
     if (eptype == USB_ENDPOINT_XFER_CONTROL && pid == TSIZ_SC_MC_PID_SETUP) {
         pid = USB_TOKEN_SETUP;
     } else {
--

2.26.2