Skip to contentSkip to conversation

usb: fix unbounded stack warning for xhci_dma_write_u32s

PATCH Superseded Peter Xu 2 participants 5 messages Mar 10, 2016 – Mar 11, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 usb: fix unbounded stack warning for xhci_dma_write_u32s

---
 hw/usb/hcd-xhci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci.c +4 -2
index 44b6f8c..d15918f 100644
@@ -698,11 +698,13 @@ static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
                                        uint32_t *buf, size_t len)
 {
     int i;
-    uint32_t tmp[len / sizeof(uint32_t)];
+    uint32_t tmp[12];
+    uint32_t n = len / sizeof(uint32_t);
 
     assert((len % sizeof(uint32_t)) == 0);
+    assert(n <= ARRAY_SIZE(tmp));
 
-    for (i = 0; i < (len / sizeof(uint32_t)); i++) {
+    for (i = 0; i < n; i++) {
         tmp[i] = cpu_to_le32(buf[i]);
     }
     pci_dma_write(PCI_DEVICE(xhci), addr, tmp, len);
Signature · 1 line
2.4.3
#1 lore raw
Quoted 10 lines · Peter Xu
---
 hw/usb/hcd-xhci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci.c +0 -0
index 44b6f8c..d15918f 100644

hw/usb/hcd-xhci.c static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr, · line 701

Quoted 5 lines
hw/usb/hcd-xhci.c +1 -1
@@ -698,11 +698,13 @@ static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
                                        uint32_t *buf, size_t len)
 {
     int i;
-    uint32_t tmp[len / sizeof(uint32_t)];
+    uint32_t tmp[12];

Where does the 12 come from?

cheers,
Gerd

author
#2 lore raw
Quoted 6 lines · Gerd Hoffmann
Quoted 5 lines · Peter Xu
---
 hw/usb/hcd-xhci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 12 lines

hw/usb/hcd-xhci.c static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr, · line 701

Quoted 10 lines
hw/usb/hcd-xhci.c +1 -1
index 44b6f8c..d15918f 100644
@@ -698,11 +698,13 @@ static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
                                        uint32_t *buf, size_t len)
 {
     int i;
-    uint32_t tmp[len / sizeof(uint32_t)];
+    uint32_t tmp[12];

Where does the 12 come from?

As mentioned in previous thread, because all the callers of xhci_dma_write_u32s() are using const size in "len". The maximum currently is 5 * sizeof(uint32_t) = 20 bytes. Here I choose number bigger than 5 should work for now. To make it a little bit bigger, I just chose 12 with no specific reason... Since 8/12/16/... seems all works for me.

Thanks.
Peter

#3 lore raw
Quoted 20 lines · Peter Xu
Quoted 6 lines · Gerd Hoffmann
Quoted 5 lines · Peter Xu
---
 hw/usb/hcd-xhci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 12 lines

hw/usb/hcd-xhci.c static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr, · line 701

Quoted 10 lines
hw/usb/hcd-xhci.c +1 -1
index 44b6f8c..d15918f 100644
@@ -698,11 +698,13 @@ static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
                                        uint32_t *buf, size_t len)
 {
     int i;
-    uint32_t tmp[len / sizeof(uint32_t)];
+    uint32_t tmp[12];

Where does the 12 come from?

Quoted 3 lines

As mentioned in previous thread, because all the callers of xhci_dma_write_u32s() are using const size in "len". The maximum currently is 5 * sizeof(uint32_t) = 20 bytes

Can you note that in the commit message please?

Quoted 2 lines

. Here I choose number
bigger than 5 should work for now.

Why bigger? 5 should do just fine then, and the assert added should make sure we'll notice if this needs an update due to code changes elsewhere.

thanks,
Gerd

author
#4 lore raw
Quoted 6 lines · Gerd Hoffmann
Quoted 3 lines · Peter Xu

As mentioned in previous thread, because all the callers of xhci_dma_write_u32s() are using const size in "len". The maximum currently is 5 * sizeof(uint32_t) = 20 bytes

Can you note that in the commit message please?

Sure!

Quoted 7 lines
Quoted 3 lines

. Here I choose number
bigger than 5 should work for now.

Why bigger? 5 should do just fine then, and the assert added should make sure we'll notice if this needs an update due to code changes elsewhere.

Will repost with 5.

Thanks.
Peter