kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
@ 2020-11-05 11:50 Colin King
  2020-11-05 12:32 ` Wilken Gottwalt
  2020-11-08  3:36 ` Guenter Roeck
  0 siblings, 2 replies; 6+ messages in thread
From: Colin King @ 2020-11-05 11:50 UTC (permalink / raw)
  To: Wilken Gottwalt, Jean Delvare, Guenter Roeck, linux-hwmon
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The shifting of the u8 integer data[3] by 24 bits to the left will
be promoted to a 32 bit signed int and then sign-extended to a
long. In the event that the top bit of data[3] is set then all
then all the upper 32 bits of a 64 bit long end up as also being
set because of the sign-extension. Fix this by casting data[3] to
a long before the shift.

Addresses-Coverity: ("Unintended sign extension")
Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/hwmon/corsair-psu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
index e92d0376e7ac..5d19a888231a 100644
--- a/drivers/hwmon/corsair-psu.c
+++ b/drivers/hwmon/corsair-psu.c
@@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, l
 	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
 	 * supported (HX1200i)
 	 */
-	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
+	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
 	switch (cmd) {
 	case PSU_CMD_IN_VOLTS:
 	case PSU_CMD_IN_AMPS:
-- 
2.27.0

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

* Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
  2020-11-05 11:50 [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue Colin King
@ 2020-11-05 12:32 ` Wilken Gottwalt
  2020-11-05 12:44   ` Colin Ian King
  2020-11-05 14:15   ` Guenter Roeck
  2020-11-08  3:36 ` Guenter Roeck
  1 sibling, 2 replies; 6+ messages in thread
From: Wilken Gottwalt @ 2020-11-05 12:32 UTC (permalink / raw)
  To: Colin King
  Cc: Jean Delvare, Guenter Roeck, linux-hwmon, kernel-janitors, linux-kernel

On Thu,  5 Nov 2020 11:50:19 +0000
Colin King <colin.king@canonical.com> wrote:

> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of the u8 integer data[3] by 24 bits to the left will
> be promoted to a 32 bit signed int and then sign-extended to a
> long. In the event that the top bit of data[3] is set then all
> then all the upper 32 bits of a 64 bit long end up as also being
> set because of the sign-extension. Fix this by casting data[3] to
> a long before the shift.
> 
> Addresses-Coverity: ("Unintended sign extension")
> Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/hwmon/corsair-psu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index e92d0376e7ac..5d19a888231a 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8
> rail, l
>  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest
> psu
>  	 * supported (HX1200i)
>  	 */
> -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>  	switch (cmd) {
>  	case PSU_CMD_IN_VOLTS:
>  	case PSU_CMD_IN_AMPS:

Yeah, this could happen if the uptime value in the micro-controller gets bigger
than 68 years (in seconds), and it is the only value which actually uses more
than 2 bytes for the representation. So what about architectures which are 32 bit
wide and where a long has 32 bits? I guess this simple cast is not enough.

greetings,
Wilken

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

* Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
  2020-11-05 12:32 ` Wilken Gottwalt
@ 2020-11-05 12:44   ` Colin Ian King
  2020-11-05 14:15   ` Guenter Roeck
  1 sibling, 0 replies; 6+ messages in thread
From: Colin Ian King @ 2020-11-05 12:44 UTC (permalink / raw)
  To: Wilken Gottwalt
  Cc: Jean Delvare, Guenter Roeck, linux-hwmon, kernel-janitors, linux-kernel

On 05/11/2020 12:32, Wilken Gottwalt wrote:
> On Thu,  5 Nov 2020 11:50:19 +0000
> Colin King <colin.king@canonical.com> wrote:
> 
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The shifting of the u8 integer data[3] by 24 bits to the left will
>> be promoted to a 32 bit signed int and then sign-extended to a
>> long. In the event that the top bit of data[3] is set then all
>> then all the upper 32 bits of a 64 bit long end up as also being
>> set because of the sign-extension. Fix this by casting data[3] to
>> a long before the shift.
>>
>> Addresses-Coverity: ("Unintended sign extension")
>> Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>  drivers/hwmon/corsair-psu.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
>> index e92d0376e7ac..5d19a888231a 100644
>> --- a/drivers/hwmon/corsair-psu.c
>> +++ b/drivers/hwmon/corsair-psu.c
>> @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8
>> rail, l
>>  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest
>> psu
>>  	 * supported (HX1200i)
>>  	 */
>> -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>> +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>>  	switch (cmd) {
>>  	case PSU_CMD_IN_VOLTS:
>>  	case PSU_CMD_IN_AMPS:
> 
> Yeah, this could happen if the uptime value in the micro-controller gets bigger
> than 68 years (in seconds), and it is the only value which actually uses more
> than 2 bytes for the representation. So what about architectures which are 32 bit
> wide and where a long has 32 bits? I guess this simple cast is not enough.

For 32 bits (unsigned) the 4 u8 values in data represents ~136 years no
matter if we use a 32 or 64 bit long for tmp. The cast to long is
signed, so yes, that's ~68 years in seconds. So perhaps
corsairpsu_get_value() should be passing a unsigned long for the *val
arg and tmp should be unsigned long too?

> 
> greetings,
> Wilken
> 

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

* Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
  2020-11-05 12:32 ` Wilken Gottwalt
  2020-11-05 12:44   ` Colin Ian King
@ 2020-11-05 14:15   ` Guenter Roeck
  2020-11-05 14:59     ` Wilken Gottwalt
  1 sibling, 1 reply; 6+ messages in thread
From: Guenter Roeck @ 2020-11-05 14:15 UTC (permalink / raw)
  To: Wilken Gottwalt
  Cc: Colin King, Jean Delvare, linux-hwmon, kernel-janitors, linux-kernel

On Thu, Nov 05, 2020 at 01:32:33PM +0100, Wilken Gottwalt wrote:
> On Thu,  5 Nov 2020 11:50:19 +0000
> Colin King <colin.king@canonical.com> wrote:
> 
> > From: Colin Ian King <colin.king@canonical.com>
> > 
> > The shifting of the u8 integer data[3] by 24 bits to the left will
> > be promoted to a 32 bit signed int and then sign-extended to a
> > long. In the event that the top bit of data[3] is set then all
> > then all the upper 32 bits of a 64 bit long end up as also being
> > set because of the sign-extension. Fix this by casting data[3] to
> > a long before the shift.
> > 
> > Addresses-Coverity: ("Unintended sign extension")
> > Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > ---
> >  drivers/hwmon/corsair-psu.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> > index e92d0376e7ac..5d19a888231a 100644
> > --- a/drivers/hwmon/corsair-psu.c
> > +++ b/drivers/hwmon/corsair-psu.c
> > @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8
> > rail, l
> >  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest
> > psu
> >  	 * supported (HX1200i)
> >  	 */
> > -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> > +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> >  	switch (cmd) {
> >  	case PSU_CMD_IN_VOLTS:
> >  	case PSU_CMD_IN_AMPS:
> 
> Yeah, this could happen if the uptime value in the micro-controller gets bigger
> than 68 years (in seconds), and it is the only value which actually uses more
> than 2 bytes for the representation. So what about architectures which are 32 bit
> wide and where a long has 32 bits? I guess this simple cast is not enough.
> 

The hwmon subsystem uses 'long' to pass values back to the core.
While that may be a bit unfortunate as it doesn't support uptimes
of more than 68 years in seconds, we are not going to change the
hwmon core to accommodate it. If the incoming data is always
expected to be positive, and you don't want to risk the overflow,
feel free to either use "data[3] & 0x7f" instead, or max out the
result at INT_MAX (from <linux/limits.h>).

Thanks,
Guenter

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

* Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
  2020-11-05 14:15   ` Guenter Roeck
@ 2020-11-05 14:59     ` Wilken Gottwalt
  0 siblings, 0 replies; 6+ messages in thread
From: Wilken Gottwalt @ 2020-11-05 14:59 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Colin King, Jean Delvare, linux-hwmon, kernel-janitors, linux-kernel

On Thu, 5 Nov 2020 06:15:49 -0800
Guenter Roeck <linux@roeck-us.net> wrote:

> On Thu, Nov 05, 2020 at 01:32:33PM +0100, Wilken Gottwalt wrote:
> > On Thu,  5 Nov 2020 11:50:19 +0000
> > Colin King <colin.king@canonical.com> wrote:
> > 
> > > From: Colin Ian King <colin.king@canonical.com>
> > > 
> > > The shifting of the u8 integer data[3] by 24 bits to the left will
> > > be promoted to a 32 bit signed int and then sign-extended to a
> > > long. In the event that the top bit of data[3] is set then all
> > > then all the upper 32 bits of a 64 bit long end up as also being
> > > set because of the sign-extension. Fix this by casting data[3] to
> > > a long before the shift.
> > > 
> > > Addresses-Coverity: ("Unintended sign extension")
> > > Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> > > Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > > ---
> > >  drivers/hwmon/corsair-psu.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> > > index e92d0376e7ac..5d19a888231a 100644
> > > --- a/drivers/hwmon/corsair-psu.c
> > > +++ b/drivers/hwmon/corsair-psu.c
> > > @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8
> > > rail, l
> > >  	 * the LINEAR11 conversion are the watts values which are about 1200 for the
> > > strongest psu
> > >  	 * supported (HX1200i)
> > >  	 */
> > > -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> > > +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> > >  	switch (cmd) {
> > >  	case PSU_CMD_IN_VOLTS:
> > >  	case PSU_CMD_IN_AMPS:
> > 
> > Yeah, this could happen if the uptime value in the micro-controller gets bigger
> > than 68 years (in seconds), and it is the only value which actually uses more
> > than 2 bytes for the representation. So what about architectures which are 32 bit
> > wide and where a long has 32 bits? I guess this simple cast is not enough.
> > 
> 
> The hwmon subsystem uses 'long' to pass values back to the core.
> While that may be a bit unfortunate as it doesn't support uptimes
> of more than 68 years in seconds, we are not going to change the
> hwmon core to accommodate it. If the incoming data is always
> expected to be positive, and you don't want to risk the overflow,
> feel free to either use "data[3] & 0x7f" instead, or max out the
> result at INT_MAX (from <linux/limits.h>).
> 
> Thanks,
> Guenter

And it is basically impossible to hit the 68 years. For example here are the
values for my psu, which I got about 6 years ago (2014 is the first year it
was sold) and which runs 8-20 hours nearly every day: 656 day(s), 21:26:34,
that are not even 2 years. And we are talking about gaming PSUs here, which
no sane person would put into a server where it runs 24/7. And if so, it
wouldn't survive 10 uptime years, not to mention 68 years. I'm pretty sure
that the fan in my PSU is somewhat damaged already (hence the best guess fan
value calculation). So yes, technically it is not perfect but practically it
doesn't make much sense here trying to find the perfect solution. It will
only introduce unnecessary complexity for a scenario which is so unlikely to
happen at all. I actually spent a lot of time thinking about it, and this
is the most practical solution I found - and which made Guenter happy ;-)

Though, I must admit there is a way to solve this problem - by making chained
requests and get all values at once and parse/split the response. You can
easily join all requests in this 64 bytes hid message. But this may not work
reliable for all supported devices. I already encountered issues while joining
rail select and read a rail value. It would at least increase the complexity
to quite a new level.

greetings,
Wilken

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

* Re: [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue
  2020-11-05 11:50 [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue Colin King
  2020-11-05 12:32 ` Wilken Gottwalt
@ 2020-11-08  3:36 ` Guenter Roeck
  1 sibling, 0 replies; 6+ messages in thread
From: Guenter Roeck @ 2020-11-08  3:36 UTC (permalink / raw)
  To: Colin King
  Cc: Wilken Gottwalt, Jean Delvare, linux-hwmon, kernel-janitors,
	linux-kernel

On Thu, Nov 05, 2020 at 11:50:19AM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The shifting of the u8 integer data[3] by 24 bits to the left will
> be promoted to a 32 bit signed int and then sign-extended to a
> long. In the event that the top bit of data[3] is set then all
> then all the upper 32 bits of a 64 bit long end up as also being
> set because of the sign-extension. Fix this by casting data[3] to
> a long before the shift.
> 
> Addresses-Coverity: ("Unintended sign extension")
> Fixes: ce15cd2cee8b ("hwmon: add Corsair PSU HID controller driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

I wasn't sure if I am going to get another version of this patch.
Either case, I applied this now.

Guenter

> ---
>  drivers/hwmon/corsair-psu.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwmon/corsair-psu.c b/drivers/hwmon/corsair-psu.c
> index e92d0376e7ac..5d19a888231a 100644
> --- a/drivers/hwmon/corsair-psu.c
> +++ b/drivers/hwmon/corsair-psu.c
> @@ -241,7 +241,7 @@ static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, l
>  	 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
>  	 * supported (HX1200i)
>  	 */
> -	tmp = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
> +	tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
>  	switch (cmd) {
>  	case PSU_CMD_IN_VOLTS:
>  	case PSU_CMD_IN_AMPS:

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

end of thread, other threads:[~2020-11-08  3:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-05 11:50 [PATCH][next] hwmon: corsair-psu: fix unintentional sign extension issue Colin King
2020-11-05 12:32 ` Wilken Gottwalt
2020-11-05 12:44   ` Colin Ian King
2020-11-05 14:15   ` Guenter Roeck
2020-11-05 14:59     ` Wilken Gottwalt
2020-11-08  3:36 ` Guenter Roeck

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