Skip to contentSkip to conversation

xilinx_spips: Correct usage of an uninitialized local variable

PATCH Superseded Francisco Iglesias 2 participants 3 messages Jan 17, 2018 – Jan 22, 2018 Arm/Zynq architecture

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xilinx_spips: Correct usage of an uninitialized local variable

Coverity found that the variable tx_rx in the function xilinx_spips_flush_txfifo was being used uninitialized (CID 1383841). This patch corrects this by always initializing tx_rx to zeros.

---
v2. Add a sanity check on the num_busses property when realizing the
devices.

---
 hw/ssi/xilinx_spips.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/ssi/xilinx_spips.c +12 -1
index 85c5d0c..12f1de9 100644
@@ -210,6 +210,9 @@
 #define SNOOP_NONE 0xEE
 #define SNOOP_STRIPING 0
 
+#define MIN_NUM_BUSSES 1
+#define MAX_NUM_BUSSES 2
+
 static inline int num_effective_busses(XilinxSPIPS *s)
 {
     return (s->regs[R_LQSPI_CFG] & LQSPI_CFG_SEP_BUS &&
@@ -573,7 +576,7 @@ static void xilinx_spips_flush_txfifo(XilinxSPIPS *s)
     for (;;) {
         int i;
         uint8_t tx = 0;
-        uint8_t tx_rx[num_effective_busses(s)];
+        uint8_t tx_rx[MAX_NUM_BUSSES] = { 0 };
         uint8_t dummy_cycles = 0;
         uint8_t addr_length;
 
@@ -1221,6 +1224,14 @@ static void xilinx_spips_realize(DeviceState *dev, Error **errp)
 
     DB_PRINT_L(0, "realized spips\n");
 
+    if (s->num_busses < MIN_NUM_BUSSES || s->num_busses > MAX_NUM_BUSSES) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Invalid number of spi busses requested, "
+                      "will default to use one bus\n",
+                      __func__);
+        s->num_busses = 1;
+    }
+
     s->spi = g_new(SSIBus *, s->num_busses);
     for (i = 0; i < s->num_busses; ++i) {
         char bus_name[16];
Signature · 1 line
2.9.3
#1 lore raw
Quoted 37 lines · Francisco Iglesias

Coverity found that the variable tx_rx in the function xilinx_spips_flush_txfifo was being used uninitialized (CID 1383841). This patch corrects this by always initializing tx_rx to zeros.

---
v2. Add a sanity check on the num_busses property when realizing the
devices.

---
 hw/ssi/xilinx_spips.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/ssi/xilinx_spips.c +4 -1
index 85c5d0c..12f1de9 100644
@@ -210,6 +210,9 @@
 #define SNOOP_NONE 0xEE
 #define SNOOP_STRIPING 0
 
+#define MIN_NUM_BUSSES 1
+#define MAX_NUM_BUSSES 2
+
 static inline int num_effective_busses(XilinxSPIPS *s)
 {
     return (s->regs[R_LQSPI_CFG] & LQSPI_CFG_SEP_BUS &&
@@ -573,7 +576,7 @@ static void xilinx_spips_flush_txfifo(XilinxSPIPS *s)
     for (;;) {
         int i;
         uint8_t tx = 0;
-        uint8_t tx_rx[num_effective_busses(s)];
+        uint8_t tx_rx[MAX_NUM_BUSSES] = { 0 };
         uint8_t dummy_cycles = 0;
         uint8_t addr_length;
 

hw/ssi/xilinx_spips.c static void xilinx_spips_realize(DeviceState *dev, Error **errp) · line 1232

Quoted 9 lines
hw/ssi/xilinx_spips.c +6 -0
@@ -1221,6 +1224,14 @@ static void xilinx_spips_realize(DeviceState *dev, Error **errp)
 
     DB_PRINT_L(0, "realized spips\n");
 
+    if (s->num_busses < MIN_NUM_BUSSES || s->num_busses > MAX_NUM_BUSSES) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Invalid number of spi busses requested, "
+                      "will default to use one bus\n",
+                      __func__);
+        s->num_busses = 1;

Realize functions should report errors in their parameters via the Error** parameter (which will cause the realize operation to fail, which the caller can deal with appropriately). Here's an example from the realize function for the GICv3:

    if (s->num_irq > GICV3_MAXIRQ) {
        error_setg(errp,
                   "requested %u interrupt lines exceeds GIC maximum %d",
                   s->num_irq, GICV3_MAXIRQ);
        return;
    }

(Note no trailing \n required for error_setg strings, unlike qemu_log_mask.)

thanks
-- PMM

#2 lore raw
Quoted 65 lines · Peter Maydell
Quoted 2 lines · Francisco Iglesias

Coverity found that the variable tx_rx in the function xilinx_spips_flush_txfifo was being used uninitialized (CID 1383841).

This

hw/ssi/xilinx_spips.c static void xilinx_spips_flush_txfifo(XilinxSPIPS

Quoted 26 lines

patch corrects this by always initializing tx_rx to zeros.

---
v2. Add a sanity check on the num_busses property when realizing the
devices.

---
 hw/ssi/xilinx_spips.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/ssi/xilinx_spips.c +3 -0
index 85c5d0c..12f1de9 100644
@@ -210,6 +210,9 @@
 #define SNOOP_NONE 0xEE
 #define SNOOP_STRIPING 0
 
+#define MIN_NUM_BUSSES 1
+#define MAX_NUM_BUSSES 2
+
 static inline int num_effective_busses(XilinxSPIPS *s)
 {
     return (s->regs[R_LQSPI_CFG] & LQSPI_CFG_SEP_BUS &&
@@ -573,7 +576,7 @@ static void xilinx_spips_flush_txfifo(XilinxSPIPS

*s)

hw/ssi/xilinx_spips.c static void xilinx_spips_realize(DeviceState

Quoted 9 lines
hw/ssi/xilinx_spips.c +1 -1
     for (;;) {
         int i;
         uint8_t tx = 0;
-        uint8_t tx_rx[num_effective_busses(s)];
+        uint8_t tx_rx[MAX_NUM_BUSSES] = { 0 };
         uint8_t dummy_cycles = 0;
         uint8_t addr_length;
 
@@ -1221,6 +1224,14 @@ static void xilinx_spips_realize(DeviceState

*dev, Error **errp)

hw/ssi/xilinx_spips.c static void xilinx_spips_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/ssi/xilinx_spips.c +1 -0
     DB_PRINT_L(0, "realized spips\n");
 
+    if (s->num_busses < MIN_NUM_BUSSES || s->num_busses >

MAX_NUM_BUSSES) {

hw/ssi/xilinx_spips.c static void xilinx_spips_realize(DeviceState *dev, Error **errp) · line 1232

Quoted 5 lines
hw/ssi/xilinx_spips.c +5 -0
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Invalid number of spi busses requested, "
+                      "will default to use one bus\n",
+                      __func__);
+        s->num_busses = 1;

Realize functions should report errors in their parameters via the Error** parameter (which will cause the realize operation to fail, which the caller can deal with appropriately). Here's an example from the realize function for the GICv3:

    if (s->num_irq > GICV3_MAXIRQ) {
        error_setg(errp,
                   "requested %u interrupt lines exceeds GIC maximum %d",
                   s->num_irq, GICV3_MAXIRQ);
        return;
    }
Quoted 6 lines

(Note no trailing \n required for error_setg strings, unlike qemu_log_mask.)

thanks
-- PMM

Hi Peter,

Thank you very much for reviewing the patch again! I'll correct this in the next version (3) of the patch.

Best regards,
Francisco Iglesias