Skip to contentSkip to conversation

ppc/pnv: Fix potential overflow in I2C model

PATCH Superseded Cédric Le Goater 3 participants 6 messages Nov 9, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ppc/pnv: Fix potential overflow in I2C model

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>
---
 hw/ppc/pnv_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +1 -1
index f75e59e70977..ab73c59f7704 100644
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
     case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
         val = 0;
         for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;
+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i;
         }
         break;
 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 16 lines · Cédric Le Goater

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>
---
  hw/ppc/pnv_i2c.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +0 -0
index f75e59e70977..ab73c59f7704 100644

hw/ppc/pnv_i2c.c static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr, · line 440

Quoted 5 lines
hw/ppc/pnv_i2c.c +1 -1
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
      case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
          val = 0;
          for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;
+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i;

Alternatively:

               val = deposit64(val, i, 1, i2c_bus_busy(i2c->busses[i]));
Quoted 3 lines
hw/ppc/pnv_i2c.c +0 -0
          }
          break;
 
#2 lore raw
In reply to Cédric Le Goater
Quoted 17 lines · Cédric Le Goater

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>
---
 hw/ppc/pnv_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +0 -0
index f75e59e70977..ab73c59f7704 100644

hw/ppc/pnv_i2c.c static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr, · line 442

Quoted 7 lines
hw/ppc/pnv_i2c.c +1 -1
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
     case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
         val = 0;
         for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;
+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i;
         }
         break;

Should the device's realize function also impose a max limit on the num-busses property? There doesn't seem to be anything preventing a caller from setting it to a big number like 128, which would then be UB here.

Style nit: casts shouldn't have a space after them before the thing they're casting.

thanks
-- PMM

#3 lore raw
Quoted 22 lines · Peter Maydell
Quoted 21 lines · Cédric Le Goater

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>
---
  hw/ppc/pnv_i2c.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +0 -1
index f75e59e70977..ab73c59f7704 100644
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
      case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
          val = 0;
          for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;
Quoted 8 lines

hw/ppc/pnv_i2c.c static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr, · line 442

Quoted 3 lines
hw/ppc/pnv_i2c.c +1 -0
+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i;
          }
          break;

Should the device's realize function also impose a max limit on the num-busses property? There doesn't seem to be anything preventing a caller from setting it to a big number like 128, which would then be UB here.

yes. I will add an assert(i2c->num_busses < 64). The current max is 16 for POWER10.

Quoted 2 lines

Style nit: casts shouldn't have a space after them before the thing they're casting.

yep.

I prefer the cast method than the deposit call. Philippe, I hope you don't mind ?

Thanks,

C.

#4 lore raw
Quoted 33 lines · Cédric Le Goater
Quoted 22 lines · Peter Maydell
Quoted 21 lines · Cédric Le Goater

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>
---
  hw/ppc/pnv_i2c.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +0 -1
index f75e59e70977..ab73c59f7704 100644
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
      case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
          val = 0;
          for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;
Quoted 8 lines

hw/ppc/pnv_i2c.c static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr, · line 442

Quoted 3 lines
hw/ppc/pnv_i2c.c +1 -0
+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i;
          }
          break;

Should the device's realize function also impose a max limit on the num-busses property? There doesn't seem to be anything preventing a caller from setting it to a big number like 128, which would then be UB here.

Quoted 2 lines

yes. I will add an assert(i2c->num_busses < 64). The current max is 16 for POWER10.

We generally make that kind of "property out of range" check be an error_setg()-and-return, rather than an assert.

thanks
-- PMM

#5 lore raw
In reply to Cédric Le Goater
Quoted 36 lines · Cédric Le Goater
Quoted 23 lines · Peter Maydell
Quoted 22 lines · Cédric Le Goater

Coverity warns that "i2c_bus_busy(i2c->busses[i]) << i" might overflow because the expression is evaluated using 32-bit arithmetic and then used in a context expecting a uint64_t.

Fixes Coverity CID 1523918
Cc Glenn Miles <[email protected]>

---
  hw/ppc/pnv_i2c.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/pnv_i2c.c +0 -0
index f75e59e70977..ab73c59f7704 100644
@@ -437,7 +437,7 @@ static uint64_t pnv_i2c_xscom_read(void *opaque, 
 hwaddr addr,

      case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */ val = 0;
          for (i = 0; i < i2c->num_busses; i++) {
-            val |= i2c_bus_busy(i2c->busses[i]) << i;

Quoted 8 lines
Quoted 3 lines

+            val |= (uint64_t) i2c_bus_busy(i2c->busses[i]) << i; }
          break;

Should the device's realize function also impose a max limit on the num-busses property? There doesn't seem to be anything preventing a caller from setting it to a big number like 128, which would then be UB here.

yes. I will add an assert(i2c->num_busses < 64). The current max is 16 for POWER10.

Quoted 7 lines
Quoted 2 lines

Style nit: casts shouldn't have a space after them before the thing they're casting.

yep.

I prefer the cast method than the deposit call. Philippe, I hope you don't mind ?

Matter of taste, I don't mind ¯\_(ツ)_/¯