Skip to contentSkip to conversation

hw/usb/hcd-ehci: Handle get_dwords() failures in async writeback

PATCH Superseded Jamin Lin 3 participants 6 messages Aug 13 – Aug 17

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

PATCH 1/1 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.

---
 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.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Jamin Lin

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.

---
  hw/usb/hcd-ehci.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
Fixes 2b3de6ada5d ("ehci: writeback_async_complete_packet: verify qh and qtd")
author
#2 lore raw

+ Cédric Le Goater <[email protected]>

Quoted 17 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Thursday, August 13, 2026 5:43 PM
To: Jamin Lin <[email protected]>; [email protected]; Philippe Mathieu-Daudé <[email protected]>; Paolo Bonzini <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [PATCH v1] hw/usb/hcd-ehci: Handle get_dwords() failures in async writeback

Quoted 6 lines · Jamin Lin

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.

Quoted 8 lines
Quoted 4 lines
---
  hw/usb/hcd-ehci.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
Fixes 2b3de6ada5d ("ehci: writeback_async_complete_packet: verify qh and

qtd")

Thanks for adding the Fixes tag.
I’ve also added the Resolves tag.

Resolves: Coverity CID 1685236

Jamin

#3 lore raw
In reply to Jamin Lin
Quoted 23 lines · Jamin Lin

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.

---
 hw/usb/hcd-ehci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
hw/usb/hcd-ehci.c +0 -3
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),

hw/usb/hcd-ehci.c static void ehci_writeback_async_complete_packet(EHCIPacket *p) · line 543

Quoted 10 lines
hw/usb/hcd-ehci.c +5 -2
-               (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;

Looking at the coverity report, my question is whether echi->as can ever actually be NULL. This is the address space we use to do DMA, so it feels like every EHCI device must set that up somehow. ehci_sysbus_init() does. So does usb_ehci_pci_realize(). usb_ehci_pci_write_config() can change it, but never to NULL.

If echi->as is always non-NULL then we could change get_dwords() and put_dwords() to return "void".

Alternatively, maybe get_dwords() and put_dwords() should be checking the return value from dma_memory_write() and dma_memory_read() so that they fail if the DMA fails...

-- PMM

author
#4 lore raw

Hi Peter,

Quoted 30 lines

hw/usb/hcd-ehci.c static void

Quoted 5 lines
hw/usb/hcd-ehci.c index renamed from hw/usb/hcd-ehci.c +0 -0

451a918e9f..d43951975a 100644

hw/usb/hcd-ehci.c +0 -0
@@ -533,11 +533,11 @@ static void

ehci_writeback_async_complete_packet(EHCIPacket *p)

hw/usb/hcd-ehci.c static void · line 533

Quoted 1 line
hw/usb/hcd-ehci.c +0 -0
     /* Verify the qh + qtd, like we do when going through fetchqh &

fetchqtd */

Quoted 5 lines
hw/usb/hcd-ehci.c +0 -3
     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),

hw/usb/hcd-ehci.c static void ehci_writeback_async_complete_packet(EHCIPacket *p) · line 543

Quoted 10 lines
hw/usb/hcd-ehci.c +5 -2
-               (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;

Looking at the coverity report, my question is whether echi->as can ever actually be NULL. This is the address space we use to do DMA, so it feels like every EHCI device must set that up somehow. ehci_sysbus_init() does. So does usb_ehci_pci_realize().
usb_ehci_pci_write_config() can change it, but never to NULL.

Quoted 7 lines

If echi->as is always non-NULL then we could change get_dwords() and put_dwords() to return "void".

Alternatively, maybe get_dwords() and put_dwords() should be checking the return value from dma_memory_write() and
dma_memory_read() so that they fail if the DMA fails...

Thanks for the review and the suggestion.

I have sent a v2 that drops the dead NULL check, checks the MemTxResult of dma_memory_read()/dma_memory_write() instead, and makes put_dwords() return void since no caller can act on a failed writeback.

https://patchwork.kernel.org/project/qemu-devel/list/?series=1146899

Thanks,
Jamin

Quoted 1 line

-- PMM

#5 lore raw
In reply to Peter Maydell
Quoted 41 lines · Peter Maydell
Quoted 23 lines · Jamin Lin

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.

---
  hw/usb/hcd-ehci.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
hw/usb/hcd-ehci.c +0 -3
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),

hw/usb/hcd-ehci.c static void ehci_writeback_async_complete_packet(EHCIPacket *p) · line 543

Quoted 10 lines
hw/usb/hcd-ehci.c +5 -2
-               (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;

Looking at the coverity report, my question is whether echi->as can ever actually be NULL. This is the address space we use to do DMA, so it feels like every EHCI device must set that up somehow. ehci_sysbus_init() does. So does usb_ehci_pci_realize(). usb_ehci_pci_write_config() can change it, but never to NULL.

Quoted 6 lines

If echi->as is always non-NULL then we could change get_dwords() and put_dwords() to return "void".

Alternatively, maybe get_dwords() and put_dwords() should be checking the return value from dma_memory_write() and dma_memory_read() so that they fail if the DMA fails...

Oh good point, I missed that. We really should qualify dma_memory_write() & co with G_GNUC_WARN_UNUSED_RESULT, that'd help us preventing such mistakes.