Skip to contentSkip to conversation

hw/usb/hcd-ehci: Fix Coverity CID 1685236 and check DMA errors

PATCH Superseded Jamin Lin 2 participants 5 messages Aug 17

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

v2 cover hw/usb/hcd-ehci: Fix Coverity CID 1685236 and check DMA errors

v1:
1. Fix Fix Coverity CID 1685236

v2:
1. Drops the dead NULL test and checks the actual accesses instead, and makes put_dwords() return void since no caller can act on a failed writeback.

Jamin Lin (2):
hw/usb/hcd-ehci: Handle get_dwords() failures in async writeback hw/usb/hcd-ehci: Check for DMA errors in get_dwords()/put_dwords()

 hw/usb/hcd-ehci.c | 48 +++++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 27 deletions(-)
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v2 1/2 hw/usb/hcd-ehci: Handle get_dwords() failures in async writeback

Coverity reports that ehci_writeback_async_complete_packet() ignores the return value of get_dwords() when reading the QH and qTD.

Handle read failures in the same way as QH and qTD verification failures by freeing the packet and returning early.

Resolves: Coverity CID 1685236

Fixes 2b3de6ada5d ("ehci: writeback_async_complete_packet: verify qh and qtd")
---
 hw/usb/hcd-ehci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
hw/usb/hcd-ehci.c +5 -5
index 451a918e9f..d43951975a 100644
@@ -533,11 +533,11 @@ static void ehci_writeback_async_complete_packet(EHCIPacket *p)
     /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
     memset(&qh, 0, sizeof(qh));
     memset(&qtd, 0, sizeof(qtd));
-    get_dwords(q->ehci, NLPTR_GET(q->qhaddr),
-               (uint32_t *) &qh, ehci_qh_dwords(q->ehci));
-    get_dwords(q->ehci, NLPTR_GET(q->qtdaddr),
-               (uint32_t *) &qtd, ehci_qtd_dwords(q->ehci));
-    if (!ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) {
+    if (get_dwords(q->ehci, NLPTR_GET(q->qhaddr),
+                   (uint32_t *) &qh, ehci_qh_dwords(q->ehci)) < 0 ||
+        get_dwords(q->ehci, NLPTR_GET(q->qtdaddr),
+                   (uint32_t *) &qtd, ehci_qtd_dwords(q->ehci)) < 0 ||
+        !ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) {
         p->async = EHCI_ASYNC_INITIALIZED;
         ehci_free_packet(p);
         return;
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH v2 2/2 hw/usb/hcd-ehci: Check for DMA errors in get_dwords()/put_dwords()

In reply to Jamin Lin

get_dwords() and put_dwords() return -1 when ehci->as is NULL, but that can no longer happen. The sysbus variants set it in instance_init (ehci_sysbus_init()), the PCI variant sets it in realize (usb_ehci_pci_realize()), and usb_ehci_pci_write_config() only switches between the bus master address space and address_space_memory.

Meanwhile the results of dma_memory_read() and dma_memory_write() are ignored, so a failed guest memory access is silently treated as success.

Drop the dead NULL test and check the MemTxResult of each access instead, so that the existing -1 error path reports real DMA errors:
raise USBSTS_HSE, clear USBCMD_RUNSTOP and stop processing the descriptor, as the NULL path used to do. All 11 get_dwords() callers already check the return value.

put_dwords() now returns void: none of its four callers (ehci_flush_qh(), ehci_state_fetchitd() and twice in ehci_state_writeback()) looks at the status.

---
 hw/usb/hcd-ehci.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
hw/usb/hcd-ehci.c +16 -22
index d43951975a..daab5783bc 100644
@@ -421,16 +421,14 @@ static inline int get_dwords(EHCIState *ehci, uint64_t addr,
 {
     int i;
 
-    if (!ehci->as) {
-        ehci_raise_irq(ehci, USBSTS_HSE);
-        ehci->usbcmd &= ~USBCMD_RUNSTOP;
-        trace_usb_ehci_dma_error();
-        return -1;
-    }
-
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ehci->as, addr, buf, sizeof(*buf),
-                        MEMTXATTRS_UNSPECIFIED);
+        if (dma_memory_read(ehci->as, addr, buf, sizeof(*buf),
+                            MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
+            ehci_raise_irq(ehci, USBSTS_HSE);
+            ehci->usbcmd &= ~USBCMD_RUNSTOP;
+            trace_usb_ehci_dma_error();
+            return -1;
+        }
         *buf = le32_to_cpu(*buf);
     }
 
@@ -438,25 +436,21 @@ static inline int get_dwords(EHCIState *ehci, uint64_t addr,
 }
 
 /* Put an array of dwords in to main memory */
-static inline int put_dwords(EHCIState *ehci, uint64_t addr,
-                             uint32_t *buf, int num)
+static inline void put_dwords(EHCIState *ehci, uint64_t addr,
+                              uint32_t *buf, int num)
 {
     int i;
 
-    if (!ehci->as) {
-        ehci_raise_irq(ehci, USBSTS_HSE);
-        ehci->usbcmd &= ~USBCMD_RUNSTOP;
-        trace_usb_ehci_dma_error();
-        return -1;
-    }
-
     for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
         uint32_t tmp = cpu_to_le32(*buf);
-        dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp),
-                         MEMTXATTRS_UNSPECIFIED);
+        if (dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp),
+                             MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
+            ehci_raise_irq(ehci, USBSTS_HSE);
+            ehci->usbcmd &= ~USBCMD_RUNSTOP;
+            trace_usb_ehci_dma_error();
+            return;
+        }
     }
-
-    return num;
 }
 
 static int ehci_get_pid(EHCIqtd *qtd)
Signature · 1 line
2.53.0
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] hw/usb/hcd-ehci: Check for DMA errors in get_dwords()/put_dwords()

Quoted 15 lines · Jamin Lin

get_dwords() and put_dwords() return -1 when ehci->as is NULL, but that can no longer happen. The sysbus variants set it in instance_init (ehci_sysbus_init()), the PCI variant sets it in realize (usb_ehci_pci_realize()), and usb_ehci_pci_write_config() only switches between the bus master address space and address_space_memory.

Meanwhile the results of dma_memory_read() and dma_memory_write() are ignored, so a failed guest memory access is silently treated as success.

Drop the dead NULL test and check the MemTxResult of each access instead, so that the existing -1 error path reports real DMA errors:
raise USBSTS_HSE, clear USBCMD_RUNSTOP and stop processing the descriptor, as the NULL path used to do. All 11 get_dwords() callers already check the return value.

Quoted 4 lines

put_dwords() now returns void: none of its four callers (ehci_flush_qh(), ehci_state_fetchitd() and twice in ehci_state_writeback()) looks at the status.

Quoted 22 lines
---
  hw/usb/hcd-ehci.c | 38 ++++++++++++++++----------------------
  1 file changed, 16 insertions(+), 22 deletions(-)
hw/usb/hcd-ehci.c +0 -8
index d43951975a..daab5783bc 100644
@@ -421,16 +421,14 @@ static inline int get_dwords(EHCIState *ehci, uint64_t addr,
  {
      int i;
 
-    if (!ehci->as) {
-        ehci_raise_irq(ehci, USBSTS_HSE);
-        ehci->usbcmd &= ~USBCMD_RUNSTOP;
-        trace_usb_ehci_dma_error();
-        return -1;
-    }
-
      for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ehci->as, addr, buf, sizeof(*buf),

hw/usb/hcd-ehci.c static inline int get_dwords(EHCIState *ehci, uint64_t addr, · line 433

Quoted 10 lines
hw/usb/hcd-ehci.c +7 -1
-                        MEMTXATTRS_UNSPECIFIED);
+        if (dma_memory_read(ehci->as, addr, buf, sizeof(*buf),
+                            MEMTXATTRS_UNSPECIFIED) != MEMTX_OK) {
+            ehci_raise_irq(ehci, USBSTS_HSE);
+            ehci->usbcmd &= ~USBCMD_RUNSTOP;
+            trace_usb_ehci_dma_error();
+            return -1;
+        }
          *buf = le32_to_cpu(*buf);
      }

Maybe reorder before previous patch?

Note, for EHCI get_dwords() is only used as returning a boolean, which would match with your conversion of put_dwords to void. While the change involves some code churn, the result would be clearer IMHO. Anyway can be done later.

author
#4 lore raw

RE: [PATCH v2 2/2] hw/usb/hcd-ehci: Check for DMA errors in get_dwords()/put_dwords()

Hi Philippe

Quoted 64 lines

Subject: Re: [PATCH v2 2/2] hw/usb/hcd-ehci: Check for DMA errors in get_dwords()/put_dwords()

Quoted 5 lines · Jamin Lin

get_dwords() and put_dwords() return -1 when ehci->as is NULL, but that can no longer happen. The sysbus variants set it in instance_init (ehci_sysbus_init()), the PCI variant sets it in realize (usb_ehci_pci_realize()), and usb_ehci_pci_write_config() only switches between the bus master address space and

address_space_memory.

Quoted 2 lines

Meanwhile the results of dma_memory_read() and dma_memory_write()

are

Quoted 8 lines

ignored, so a failed guest memory access is silently treated as success.

Drop the dead NULL test and check the MemTxResult of each access instead, so that the existing -1 error path reports real DMA errors:
raise USBSTS_HSE, clear USBCMD_RUNSTOP and stop processing the descriptor, as the NULL path used to do. All 11 get_dwords() callers already check the return value.

Quoted 5 lines

put_dwords() now returns void: none of its four callers (ehci_flush_qh(),
ehci_state_fetchitd() and twice in ehci_state_writeback()) looks at the status.

hw/usb/hcd-ehci.c static inline int get_dwords(EHCIState *ehci,

Quoted 10 lines
---
  hw/usb/hcd-ehci.c | 38 ++++++++++++++++----------------------
  1 file changed, 16 insertions(+), 22 deletions(-)
hw/usb/hcd-ehci.c index renamed from hw/usb/hcd-ehci.c +0 -0

d43951975a..daab5783bc 100644

hw/usb/hcd-ehci.c +0 -0
@@ -421,16 +421,14 @@ static inline int get_dwords(EHCIState *ehci,

uint64_t addr,

Quoted 5 lines
hw/usb/hcd-ehci.c +0 -2
  {
      int i;
 
-    if (!ehci->as) {
-        ehci_raise_irq(ehci, USBSTS_HSE);

hw/usb/hcd-ehci.c static inline int get_dwords(EHCIState *ehci, uint64_t addr,

Quoted 10 lines
hw/usb/hcd-ehci.c +2 -7
-        ehci->usbcmd &= ~USBCMD_RUNSTOP;
-        trace_usb_ehci_dma_error();
-        return -1;
-    }
-
      for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
-        dma_memory_read(ehci->as, addr, buf, sizeof(*buf),
-                        MEMTXATTRS_UNSPECIFIED);
+        if (dma_memory_read(ehci->as, addr, buf, sizeof(*buf),
+                            MEMTXATTRS_UNSPECIFIED) !=

MEMTX_OK) {

hw/usb/hcd-ehci.c static inline int get_dwords(EHCIState *ehci, uint64_t addr, · line 433

Quoted 7 lines
hw/usb/hcd-ehci.c +5 -0
+            ehci_raise_irq(ehci, USBSTS_HSE);
+            ehci->usbcmd &= ~USBCMD_RUNSTOP;
+            trace_usb_ehci_dma_error();
+            return -1;
+        }
          *buf = le32_to_cpu(*buf);
      }
Quoted 2 lines

Maybe reorder before previous patch?

Will resend v3.

Quoted 4 lines

Note, for EHCI get_dwords() is only used as returning a boolean, which would match with your conversion of put_dwords to void. While the change involves some code churn, the result would be clearer IMHO. Anyway can be done later.

Will add new patch in v3 and change get_dwords return type to a boolean. Thanks,
Jamin

Quoted 1 line