linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] can: usb: fix potential integer overflow on shift of a int
@ 2020-11-05 11:24 Colin King
  2020-11-05 13:07 ` Marc Kleine-Budde
  0 siblings, 1 reply; 2+ messages in thread
From: Colin King @ 2020-11-05 11:24 UTC (permalink / raw)
  To: Wolfgang Grandegger, David S . Miller, Jakub Kicinski,
	Stephane Grosjean, linux-can, netdev
  Cc: kernel-janitors, linux-kernel

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

The left shift of int 32 bit integer constant 1 is evaluated using
32 bit arithmetic and then assigned to a signed 64 bit variable. In
the case where time_ref->adapter->ts_used_bits is 32 or more this
can lead to an oveflow. Avoid this by shifting using the BIT_ULL macro
instead.

Addresses-Coverity: ("Unintentional integer overflow")
Fixes: bb4785551f64 ("can: usb: PEAK-System Technik USB adapters driver core")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index c2764799f9ef..204ccb27d6d9 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -156,7 +156,7 @@ void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
 		if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
 			/* case when event time (tsw) wraps */
 			if (ts < time_ref->ts_dev_1)
-				delta_ts = 1 << time_ref->adapter->ts_used_bits;
+				delta_ts = BIT_ULL(time_ref->adapter->ts_used_bits);
 
 		/* Otherwise, sync time counter (ts_dev_2) has wrapped:
 		 * handle case when event time (tsn) hasn't.
@@ -168,7 +168,7 @@ void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
 		 *              tsn            ts
 		 */
 		} else if (time_ref->ts_dev_1 < ts) {
-			delta_ts = -(1 << time_ref->adapter->ts_used_bits);
+			delta_ts = -BIT_ULL(time_ref->adapter->ts_used_bits);
 		}
 
 		/* add delay between last sync and event timestamps */
-- 
2.27.0


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

* Re: [PATCH][next] can: usb: fix potential integer overflow on shift of a int
  2020-11-05 11:24 [PATCH][next] can: usb: fix potential integer overflow on shift of a int Colin King
@ 2020-11-05 13:07 ` Marc Kleine-Budde
  0 siblings, 0 replies; 2+ messages in thread
From: Marc Kleine-Budde @ 2020-11-05 13:07 UTC (permalink / raw)
  To: Colin King, Wolfgang Grandegger, David S . Miller,
	Jakub Kicinski, Stephane Grosjean, linux-can, netdev
  Cc: kernel-janitors, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 932 bytes --]

On 11/5/20 12:24 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The left shift of int 32 bit integer constant 1 is evaluated using
> 32 bit arithmetic and then assigned to a signed 64 bit variable. In
> the case where time_ref->adapter->ts_used_bits is 32 or more this
> can lead to an oveflow. Avoid this by shifting using the BIT_ULL macro
> instead.
> 
> Addresses-Coverity: ("Unintentional integer overflow")
> Fixes: bb4785551f64 ("can: usb: PEAK-System Technik USB adapters driver core")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Applied to linux-can/testing (not to next).

Thanks,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2020-11-05 13:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-05 11:24 [PATCH][next] can: usb: fix potential integer overflow on shift of a int Colin King
2020-11-05 13:07 ` Marc Kleine-Budde

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