2022-11-21 08:38:06

by Maxim Korotkov

[permalink] [raw]
Subject: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()

The value of an arithmetic expression "n * id.data" is subject
to possible overflow due to a failure to cast operands to a larger data
type before performing arithmetic. Added cast of first operand to u64
for avoiding overflow.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")
Signed-off-by: Maxim Korotkov <[email protected]>
---
net/ethtool/ioctl.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 6a7308de192d..cf87e53c2e74 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -2007,7 +2007,7 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
} else {
/* Driver expects to be called at twice the frequency in rc */
int n = rc * 2, interval = HZ / n;
- u64 count = n * id.data, i = 0;
+ u64 count = (u64)n * id.data, i = 0;

do {
rtnl_lock();
--
2.17.1



2022-11-21 15:17:08

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()

From: Andrew Lunn <[email protected]>
Date: Mon, 21 Nov 2022 15:10:18 +0100

> On Mon, Nov 21, 2022 at 10:56:18AM +0300, Maxim Korotkov wrote:
> > The value of an arithmetic expression "n * id.data" is subject
> > to possible overflow due to a failure to cast operands to a larger data
> > type before performing arithmetic. Added cast of first operand to u64
> > for avoiding overflow.
> >
> > Found by Linux Verification Center (linuxtesting.org) with SVACE.
> >
> > Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")
> > Signed-off-by: Maxim Korotkov <[email protected]>
> > ---
> > net/ethtool/ioctl.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
> > index 6a7308de192d..cf87e53c2e74 100644
> > --- a/net/ethtool/ioctl.c
> > +++ b/net/ethtool/ioctl.c
> > @@ -2007,7 +2007,7 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
> > } else {
> > /* Driver expects to be called at twice the frequency in rc */
> > int n = rc * 2, interval = HZ / n;
> > - u64 count = n * id.data, i = 0;
> > + u64 count = (u64)n * id.data, i = 0;
>
>
> How about moving the code around a bit, change n to a u64 and drop the
> cast? Does this look correct?
>
> int interval = HZ / rc / 2;
> u64 n = rc * 2;
> u64 count = n * id.data;
>
> i = 0;
>
> I just don't like casts, they suggest the underlying types are wrong,
> so should fix that, not add a cast.

This particular one is absolutely fine. When you want to multiply
u32 by u32, you always need a cast, otherwise the result will be
truncated. mul_u32_u32() does it the very same way[0].

>
> Andrew
>

[0] https://elixir.bootlin.com/linux/v6.1-rc6/source/include/linux/math64.h#L153

Thanks,
Olek

2022-11-21 15:50:26

by Andrew Lunn

[permalink] [raw]
Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()

On Mon, Nov 21, 2022 at 10:56:18AM +0300, Maxim Korotkov wrote:
> The value of an arithmetic expression "n * id.data" is subject
> to possible overflow due to a failure to cast operands to a larger data
> type before performing arithmetic. Added cast of first operand to u64
> for avoiding overflow.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")
> Signed-off-by: Maxim Korotkov <[email protected]>
> ---
> net/ethtool/ioctl.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
> index 6a7308de192d..cf87e53c2e74 100644
> --- a/net/ethtool/ioctl.c
> +++ b/net/ethtool/ioctl.c
> @@ -2007,7 +2007,7 @@ static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
> } else {
> /* Driver expects to be called at twice the frequency in rc */
> int n = rc * 2, interval = HZ / n;
> - u64 count = n * id.data, i = 0;
> + u64 count = (u64)n * id.data, i = 0;


How about moving the code around a bit, change n to a u64 and drop the
cast? Does this look correct?

int interval = HZ / rc / 2;
u64 n = rc * 2;
u64 count = n * id.data;

i = 0;

I just don't like casts, they suggest the underlying types are wrong,
so should fix that, not add a cast.

Andrew

2022-11-21 20:43:43

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()

On Mon, 21 Nov 2022 10:56:18 +0300 Maxim Korotkov wrote:
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")

I'm leaning towards dropping the fixes tag, and applying to -next.
Drivers returning high enough rc to cause an overflow seems theoretical,
and is pretty harmless. Please LMK if I'm missing something.

2022-11-21 21:43:21

by Jacob Keller

[permalink] [raw]
Subject: RE: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()



> -----Original Message-----
> From: Alexander Lobakin <[email protected]>
> Sent: Monday, November 21, 2022 7:03 AM
> To: Andrew Lunn <[email protected]>
> Cc: Lobakin, Alexandr <[email protected]>; Maxim Korotkov
> <[email protected]>; David S. Miller <[email protected]>; Eric
> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo
> Abeni <[email protected]>; Guangbin Huang
> <[email protected]>; Tom Rix <[email protected]>; Marco Bonelli
> <[email protected]>; Edward Cree <[email protected]>;
> [email protected]; [email protected]; lvc-
> [email protected]
> Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()
>
> From: Andrew Lunn <[email protected]>
> Date: Mon, 21 Nov 2022 15:10:18 +0100
>
> > On Mon, Nov 21, 2022 at 10:56:18AM +0300, Maxim Korotkov wrote:
> > > The value of an arithmetic expression "n * id.data" is subject
> > > to possible overflow due to a failure to cast operands to a larger data
> > > type before performing arithmetic. Added cast of first operand to u64
> > > for avoiding overflow.
> > >
> > > Found by Linux Verification Center (linuxtesting.org) with SVACE.
> > >
> > > Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")
> > > Signed-off-by: Maxim Korotkov <[email protected]>
> > > ---
> > > net/ethtool/ioctl.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
> > > index 6a7308de192d..cf87e53c2e74 100644
> > > --- a/net/ethtool/ioctl.c
> > > +++ b/net/ethtool/ioctl.c
> > > @@ -2007,7 +2007,7 @@ static int ethtool_phys_id(struct net_device *dev,
> void __user *useraddr)
> > > } else {
> > > /* Driver expects to be called at twice the frequency in rc */
> > > int n = rc * 2, interval = HZ / n;
> > > - u64 count = n * id.data, i = 0;
> > > + u64 count = (u64)n * id.data, i = 0;
> >
> >
> > How about moving the code around a bit, change n to a u64 and drop the
> > cast? Does this look correct?
> >
> > int interval = HZ / rc / 2;
> > u64 n = rc * 2;
> > u64 count = n * id.data;
> >
> > i = 0;
> >
> > I just don't like casts, they suggest the underlying types are wrong,
> > so should fix that, not add a cast.
>
> This particular one is absolutely fine. When you want to multiply
> u32 by u32, you always need a cast, otherwise the result will be
> truncated. mul_u32_u32() does it the very same way[0].
>

Why not just use mul_u32_u32 then?

Thanks,
Jake

> >
> > Andrew
> >
>
> [0] https://elixir.bootlin.com/linux/v6.1-
> rc6/source/include/linux/math64.h#L153
>
> Thanks,
> Olek

2022-11-22 07:10:45

by Maxim Korotkov

[permalink] [raw]
Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()

Ok, I'll replace cast to macro in patch V2

On 22.11.2022 00:30, Keller, Jacob E wrote:
>
>
>> -----Original Message-----
>> From: Alexander Lobakin <[email protected]>
>> Sent: Monday, November 21, 2022 7:03 AM
>> To: Andrew Lunn <[email protected]>
>> Cc: Lobakin, Alexandr <[email protected]>; Maxim Korotkov
>> <[email protected]>; David S. Miller <[email protected]>; Eric
>> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo
>> Abeni <[email protected]>; Guangbin Huang
>> <[email protected]>; Tom Rix <[email protected]>; Marco Bonelli
>> <[email protected]>; Edward Cree <[email protected]>;
>> [email protected]; [email protected]; lvc-
>> [email protected]
>> Subject: Re: [PATCH] ethtool: avoiding integer overflow in ethtool_phys_id()
>>
>> From: Andrew Lunn <[email protected]>
>> Date: Mon, 21 Nov 2022 15:10:18 +0100
>>
>>> On Mon, Nov 21, 2022 at 10:56:18AM +0300, Maxim Korotkov wrote:
>>>> The value of an arithmetic expression "n * id.data" is subject
>>>> to possible overflow due to a failure to cast operands to a larger data
>>>> type before performing arithmetic. Added cast of first operand to u64
>>>> for avoiding overflow.
>>>>
>>>> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>>>>
>>>> Fixes: 2adc6edcaec0 ("ethtool: fix error handling in ethtool_phys_id")
>>>> Signed-off-by: Maxim Korotkov <[email protected]>
>>>> ---
>>>> net/ethtool/ioctl.c | 2 +-
>>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
>>>> index 6a7308de192d..cf87e53c2e74 100644
>>>> --- a/net/ethtool/ioctl.c
>>>> +++ b/net/ethtool/ioctl.c
>>>> @@ -2007,7 +2007,7 @@ static int ethtool_phys_id(struct net_device *dev,
>> void __user *useraddr)
>>>> } else {
>>>> /* Driver expects to be called at twice the frequency in rc */
>>>> int n = rc * 2, interval = HZ / n;
>>>> - u64 count = n * id.data, i = 0;
>>>> + u64 count = (u64)n * id.data, i = 0;
>>>
>>>
>>> How about moving the code around a bit, change n to a u64 and drop the
>>> cast? Does this look correct?
>>>
>>> int interval = HZ / rc / 2;
>>> u64 n = rc * 2;
>>> u64 count = n * id.data;
>>>
>>> i = 0;
>>>
>>> I just don't like casts, they suggest the underlying types are wrong,
>>> so should fix that, not add a cast.
>>
>> This particular one is absolutely fine. When you want to multiply
>> u32 by u32, you always need a cast, otherwise the result will be
>> truncated. mul_u32_u32() does it the very same way[0].
>>
>
> Why not just use mul_u32_u32 then?
>
> Thanks,
> Jake
>
>>>
>>> Andrew
>>>
>>
>> [0] https://elixir.bootlin.com/linux/v6.1-
>> rc6/source/include/linux/math64.h#L153
>>
>> Thanks,
>> Olek