ppc/pnv: Fix potential overflow in I2C model
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.
--- hw/ppc/pnv_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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 ;
Signature · 1 line
2.41.0
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 auint64_t.Fixes Coverity CID 1523918Cc Glenn Miles <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/ppc/pnv_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/pnv_i2c.c +0
-0
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 ;
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 auint64_t.Fixes Coverity CID 1523918Cc Glenn Miles <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/ppc/pnv_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/pnv_i2c.c +0
-0
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
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 auint64_t.Fixes Coverity CID 1523918Cc Glenn Miles <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/ppc/pnv_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/pnv_i2c.c +0
-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;
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.
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 auint64_t.Fixes Coverity CID 1523918Cc Glenn Miles <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/ppc/pnv_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/pnv_i2c.c +0
-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;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
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 auint64_t.Fixes Coverity CID 1523918Cc Glenn Miles <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>---
hw/ppc/pnv_i2c.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/pnv_i2c.c +0
-0@@ -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 ¯\_(ツ)_/¯