Skip to contentSkip to conversation

hw/usb/hcd-ohci: check for MPS=0 to avoid infinite loop

PATCH Superseded Jenny Guanni Qu 2 participants 3 messages Mar 18 – Mar 21

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/usb/hcd-ohci: check for MPS=0 to avoid infinite loop

When a guest sets MaxPacketSize to 0 in an OHCI Endpoint Descriptor, ohci_service_td() transfers 0 bytes per iteration. The Transfer Descriptor never completes because CBP never advances toward BE, causing ohci_service_ed_list() to loop indefinitely and hang QEMU.

Add a check for MPS==0 after extracting the field from ED flags. If MPS is zero, call ohci_die() to reset the controller and return an error, preventing the infinite loop.

---
 hw/usb/hcd-ohci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/hcd-ohci.c +4 -0
index 1aeed9286f..b93a99593e 100644
@@ -956,6 +956,10 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
         if (len && dir != OHCI_TD_DIR_IN) {
             /* The endpoint may not allow us to transfer it all now */
             pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
+            if (pktlen == 0) {
+                ohci_die(ohci);
+                return 1;
+            }
             if (pktlen > len) {
                 pktlen = len;
             }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Jenny Guanni Qu

When a guest sets MaxPacketSize to 0 in an OHCI Endpoint Descriptor, ohci_service_td() transfers 0 bytes per iteration. The Transfer Descriptor never completes because CBP never advances toward BE, causing ohci_service_ed_list() to loop indefinitely and hang QEMU.

Add a check for MPS==0 after extracting the field from ED flags. If MPS is zero, call ohci_die() to reset the controller and return an error, preventing the infinite loop.

---
 hw/usb/hcd-ohci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/hcd-ohci.c +0 -0
index 1aeed9286f..b93a99593e 100644

hw/usb/hcd-ohci.c static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) · line 962

Quoted 7 lines
hw/usb/hcd-ohci.c +4 -0
@@ -956,6 +956,10 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
         if (len && dir != OHCI_TD_DIR_IN) {
             /* The endpoint may not allow us to transfer it all now */
             pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
+            if (pktlen == 0) {
+                ohci_die(ohci);
+                return 1;
+            }

The OHCI spec doesn't (as far as I can tell) say what the hardware should do for this situation, so I guess "flag it as an error" is a reasonable choice. I think it would be useful to note in a comment that this is our best-guess, though:

/*
* The OHCI spec doesn't say what to do if the guest hands us
* an endpoint descriptor which specifies a MaximumPacketSize
* of zero, which would mean we can never actually make forward
* progress transferring data to it. We choose to treat it as
* an error.
*/

With something like that,

(Or if I've missed something in the spec, let me know...)

thanks
-- PMM

author
#2 lore raw

Thanks Peter. v2 sent with your suggested comment added.

Quoted 53 lines · Peter Maydell
Quoted 22 lines · Jenny Guanni Qu

When a guest sets MaxPacketSize to 0 in an OHCI Endpoint Descriptor, ohci_service_td() transfers 0 bytes per iteration. The Transfer Descriptor never completes because CBP never advances toward BE, causing ohci_service_ed_list() to loop indefinitely and hang QEMU.

Add a check for MPS==0 after extracting the field from ED flags. If MPS is zero, call ohci_die() to reset the controller and return an error, preventing the infinite loop.

---
 hw/usb/hcd-ohci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/hcd-ohci.c +0 -0
index 1aeed9286f..b93a99593e 100644

hw/usb/hcd-ohci.c static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed) · line 962

Quoted 7 lines
hw/usb/hcd-ohci.c +4 -0
@@ -956,6 +956,10 @@ static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
         if (len && dir != OHCI_TD_DIR_IN) {
             /* The endpoint may not allow us to transfer it all now */
             pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
+            if (pktlen == 0) {
+                ohci_die(ohci);
+                return 1;
+            }

The OHCI spec doesn't (as far as I can tell) say what the hardware should do for this situation, so I guess "flag it as an error" is a reasonable choice. I think it would be useful to note in a comment that this is our best-guess, though:

/*
* The OHCI spec doesn't say what to do if the guest hands us
* an endpoint descriptor which specifies a MaximumPacketSize
* of zero, which would mean we can never actually make forward
* progress transferring data to it. We choose to treat it as
* an error.
*/

With something like that,

(Or if I've missed something in the spec, let me know...)

thanks
-- PMM