qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ppc/pnv: Fix potential overflow in I2C model
@ 2023-11-09  8:05 Cédric Le Goater
  2023-11-09  8:12 ` Philippe Mathieu-Daudé
  2023-11-09 15:02 ` Peter Maydell
  0 siblings, 2 replies; 6+ messages in thread
From: Cédric Le Goater @ 2023-11-09  8:05 UTC (permalink / raw)
  To: qemu-devel, qemu-ppc
  Cc: Frédéric Barrat, Nicholas Piggin, Cédric Le Goater,
	Glenn Miles

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 <milesg@linux.vnet.ibm.com>
Signed-off-by: Cédric Le Goater <clg@kaod.org>
---
 hw/ppc/pnv_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
index f75e59e70977..ab73c59f7704 100644
--- a/hw/ppc/pnv_i2c.c
+++ b/hw/ppc/pnv_i2c.c
@@ -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;
 
-- 
2.41.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppc/pnv: Fix potential overflow in I2C model
  2023-11-09  8:05 [PATCH] ppc/pnv: Fix potential overflow in I2C model Cédric Le Goater
@ 2023-11-09  8:12 ` Philippe Mathieu-Daudé
  2023-11-09 15:02 ` Peter Maydell
  1 sibling, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-11-09  8:12 UTC (permalink / raw)
  To: Cédric Le Goater, qemu-devel, qemu-ppc
  Cc: Frédéric Barrat, Nicholas Piggin, Glenn Miles

On 9/11/23 09:05, Cédric Le Goater wrote:
> 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 <milesg@linux.vnet.ibm.com>
> Signed-off-by: Cédric Le Goater <clg@kaod.org>
> ---
>   hw/ppc/pnv_i2c.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
> index f75e59e70977..ab73c59f7704 100644
> --- a/hw/ppc/pnv_i2c.c
> +++ b/hw/ppc/pnv_i2c.c
> @@ -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]));

>           }
>           break;
>   



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppc/pnv: Fix potential overflow in I2C model
  2023-11-09  8:05 [PATCH] ppc/pnv: Fix potential overflow in I2C model Cédric Le Goater
  2023-11-09  8:12 ` Philippe Mathieu-Daudé
@ 2023-11-09 15:02 ` Peter Maydell
  2023-11-09 15:54   ` Cédric Le Goater
  1 sibling, 1 reply; 6+ messages in thread
From: Peter Maydell @ 2023-11-09 15:02 UTC (permalink / raw)
  To: Cédric Le Goater
  Cc: qemu-devel, qemu-ppc, Frédéric Barrat, Nicholas Piggin,
	Glenn Miles

On Thu, 9 Nov 2023 at 08:06, Cédric Le Goater <clg@kaod.org> wrote:
>
> 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 <milesg@linux.vnet.ibm.com>
> Signed-off-by: Cédric Le Goater <clg@kaod.org>
> ---
>  hw/ppc/pnv_i2c.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
> index f75e59e70977..ab73c59f7704 100644
> --- a/hw/ppc/pnv_i2c.c
> +++ b/hw/ppc/pnv_i2c.c
> @@ -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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppc/pnv: Fix potential overflow in I2C model
  2023-11-09 15:02 ` Peter Maydell
@ 2023-11-09 15:54   ` Cédric Le Goater
  2023-11-09 16:07     ` Peter Maydell
  2023-11-09 18:51     ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 6+ messages in thread
From: Cédric Le Goater @ 2023-11-09 15:54 UTC (permalink / raw)
  To: Peter Maydell
  Cc: qemu-devel, qemu-ppc, Frédéric Barrat, Nicholas Piggin,
	Glenn Miles, Philippe Mathieu-Daudé

On 11/9/23 16:02, Peter Maydell wrote:
> On Thu, 9 Nov 2023 at 08:06, Cédric Le Goater <clg@kaod.org> wrote:
>>
>> 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 <milesg@linux.vnet.ibm.com>
>> Signed-off-by: Cédric Le Goater <clg@kaod.org>
>> ---
>>   hw/ppc/pnv_i2c.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
>> index f75e59e70977..ab73c59f7704 100644
>> --- a/hw/ppc/pnv_i2c.c
>> +++ b/hw/ppc/pnv_i2c.c
>> @@ -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.

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

> 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.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppc/pnv: Fix potential overflow in I2C model
  2023-11-09 15:54   ` Cédric Le Goater
@ 2023-11-09 16:07     ` Peter Maydell
  2023-11-09 18:51     ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Maydell @ 2023-11-09 16:07 UTC (permalink / raw)
  To: Cédric Le Goater
  Cc: qemu-devel, qemu-ppc, Frédéric Barrat, Nicholas Piggin,
	Glenn Miles, Philippe Mathieu-Daudé

On Thu, 9 Nov 2023 at 15:54, Cédric Le Goater <clg@kaod.org> wrote:
>
> On 11/9/23 16:02, Peter Maydell wrote:
> > On Thu, 9 Nov 2023 at 08:06, Cédric Le Goater <clg@kaod.org> wrote:
> >>
> >> 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 <milesg@linux.vnet.ibm.com>
> >> Signed-off-by: Cédric Le Goater <clg@kaod.org>
> >> ---
> >>   hw/ppc/pnv_i2c.c | 2 +-
> >>   1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
> >> index f75e59e70977..ab73c59f7704 100644
> >> --- a/hw/ppc/pnv_i2c.c
> >> +++ b/hw/ppc/pnv_i2c.c
> >> @@ -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.
>
> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppc/pnv: Fix potential overflow in I2C model
  2023-11-09 15:54   ` Cédric Le Goater
  2023-11-09 16:07     ` Peter Maydell
@ 2023-11-09 18:51     ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2023-11-09 18:51 UTC (permalink / raw)
  To: Cédric Le Goater, Peter Maydell
  Cc: qemu-devel, qemu-ppc, Frédéric Barrat, Nicholas Piggin,
	Glenn Miles

On 9/11/23 16:54, Cédric Le Goater wrote:
> On 11/9/23 16:02, Peter Maydell wrote:
>> On Thu, 9 Nov 2023 at 08:06, Cédric Le Goater <clg@kaod.org> wrote:
>>>
>>> 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 <milesg@linux.vnet.ibm.com>
>>> Signed-off-by: Cédric Le Goater <clg@kaod.org>
>>> ---
>>>   hw/ppc/pnv_i2c.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/hw/ppc/pnv_i2c.c b/hw/ppc/pnv_i2c.c
>>> index f75e59e70977..ab73c59f7704 100644
>>> --- a/hw/ppc/pnv_i2c.c
>>> +++ b/hw/ppc/pnv_i2c.c
>>> @@ -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.
> 
> yes. I will add an assert(i2c->num_busses < 64). The current max
> is 16 for POWER10.
> 
>> 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 ¯\_(ツ)_/¯



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-11-09 18:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-09  8:05 [PATCH] ppc/pnv: Fix potential overflow in I2C model Cédric Le Goater
2023-11-09  8:12 ` Philippe Mathieu-Daudé
2023-11-09 15:02 ` Peter Maydell
2023-11-09 15:54   ` Cédric Le Goater
2023-11-09 16:07     ` Peter Maydell
2023-11-09 18:51     ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).