* [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
@ 2018-02-15 18:59 Colin King
2018-02-15 19:25 ` NACK: " Colin Ian King
2018-02-16 14:22 ` Andy Shevchenko
0 siblings, 2 replies; 4+ messages in thread
From: Colin King @ 2018-02-15 18:59 UTC (permalink / raw)
To: Alessandro Zummo, Alexandre Belloni, linux-rtc
Cc: kernel-janitors, linux-kernel
From: Colin Ian King <colin.king@canonical.com>
The shifting of buf[5] by 24 bits to the left will be promoted to
a 32 bit signed int and then sign-extended to an unsigned long. If
the top bit of buf[5] is set then all then all the upper bits sec
end up as also being set because of the sign-extension. Fix this by
casting buf[5] to an unsigned long before the shift.
Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
drivers/rtc/rtc-tx4939.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c
index feededce3ded..b8a066cbcc42 100644
--- a/drivers/rtc/rtc-tx4939.c
+++ b/drivers/rtc/rtc-tx4939.c
@@ -170,7 +170,8 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
spin_unlock_irq(&pdata->lock);
- sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
+ sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
+ (buf[3] << 8) | buf[2];
rtc_time_to_tm(sec, &alrm->time);
return rtc_valid_tm(&alrm->time);
}
--
2.15.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* NACK: [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
2018-02-15 18:59 [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift Colin King
@ 2018-02-15 19:25 ` Colin Ian King
2018-02-16 14:22 ` Andy Shevchenko
1 sibling, 0 replies; 4+ messages in thread
From: Colin Ian King @ 2018-02-15 19:25 UTC (permalink / raw)
To: Alessandro Zummo, Alexandre Belloni, linux-rtc
Cc: kernel-janitors, linux-kernel
On 15/02/18 18:59, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> The shifting of buf[5] by 24 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an unsigned long. If
> the top bit of buf[5] is set then all then all the upper bits sec
> end up as also being set because of the sign-extension. Fix this by
> casting buf[5] to an unsigned long before the shift.
>
> Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
>
> Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> drivers/rtc/rtc-tx4939.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/rtc/rtc-tx4939.c b/drivers/rtc/rtc-tx4939.c
> index feededce3ded..b8a066cbcc42 100644
> --- a/drivers/rtc/rtc-tx4939.c
> +++ b/drivers/rtc/rtc-tx4939.c
> @@ -170,7 +170,8 @@ static int tx4939_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> alrm->enabled = (ctl & TX4939_RTCCTL_ALME) ? 1 : 0;
> alrm->pending = (ctl & TX4939_RTCCTL_ALMD) ? 1 : 0;
> spin_unlock_irq(&pdata->lock);
> - sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> + sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> + (buf[3] << 8) | buf[2];
> rtc_time_to_tm(sec, &alrm->time);
> return rtc_valid_tm(&alrm->time);
> }
>
Nack, there are two occurrences of this sign extension, I missed the
other one. I'll re-send a fix.
Colin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
2018-02-15 18:59 [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift Colin King
2018-02-15 19:25 ` NACK: " Colin Ian King
@ 2018-02-16 14:22 ` Andy Shevchenko
2018-02-16 15:14 ` David Laight
1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2018-02-16 14:22 UTC (permalink / raw)
To: Colin King
Cc: Alessandro Zummo, Alexandre Belloni, linux-rtc, kernel-janitors,
Linux Kernel Mailing List
On Thu, Feb 15, 2018 at 8:59 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> The shifting of buf[5] by 24 bits to the left will be promoted to
> a 32 bit signed int and then sign-extended to an unsigned long. If
> the top bit of buf[5] is set then all then all the upper bits sec
> end up as also being set because of the sign-extension. Fix this by
> casting buf[5] to an unsigned long before the shift.
>
> Detected by CoverityScan, CID#1465292 ("Unintended sign extension")
>
> Fixes: 0e1492330cd2 ("rtc: add rtc-tx4939 driver")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> spin_unlock_irq(&pdata->lock);
> - sec = (buf[5] << 24) | (buf[4] << 16) | (buf[3] << 8) | buf[2];
> + sec = ((unsigned long)buf[5] << 24) | (buf[4] << 16) |
> + (buf[3] << 8) | buf[2];
Wouldn't be better to use le32_to_cpu() or get_unaligned()?
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift
2018-02-16 14:22 ` Andy Shevchenko
@ 2018-02-16 15:14 ` David Laight
0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2018-02-16 15:14 UTC (permalink / raw)
To: 'Andy Shevchenko', Colin King
Cc: Alessandro Zummo, Alexandre Belloni, linux-rtc, kernel-janitors,
Linux Kernel Mailing List
From: Andy Shevchenko
> Sent: 16 February 2018 14:23
...
> Wouldn't be better to use le32_to_cpu() or get_unaligned()?
Or shoot the members of the ANSI C committee who insisted on
promotion of unsigned char/short to int and sign preserving
promotion of int to unsigned long :-)
David
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-02-16 15:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-15 18:59 [PATCH] rtc: tx4939: avoid unintended sign extension on a 24 bit shift Colin King
2018-02-15 19:25 ` NACK: " Colin Ian King
2018-02-16 14:22 ` Andy Shevchenko
2018-02-16 15:14 ` David Laight
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).