thunderbolt: Ensure left shift of 512 does not overflow a 32 bit int
authorColin Ian King <colin.king@canonical.com>
Tue, 30 Jun 2020 14:55:58 +0000 (15:55 +0100)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Wed, 1 Jul 2020 10:46:47 +0000 (13:46 +0300)
The 32 bit int value 512 is being left shifted and then used in a context
that expects the expression to be a larger unsigned long. There may be
a potential integer overflow, so make 512 a UL before shift to avoid
any such issues.

Addresses-Coverity: ("Uninintentional integer overflow")
Fixes: 3b1d8d577ca8 ("thunderbolt: Implement USB3 bandwidth negotiation routines")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/usb4.c

index 966f334b40108b89570dcabd71bb911774b01f24..2b8355e6b65f4d6825cbafbcbd910f9196e8f590 100644 (file)
@@ -1368,7 +1368,7 @@ static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
 {
        unsigned long uframes;
 
-       uframes = bw * 512 << scale;
+       uframes = bw * 512UL << scale;
        return DIV_ROUND_CLOSEST(uframes * 8000, 1000 * 1000);
 }
 
@@ -1378,7 +1378,7 @@ static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
 
        /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
        uframes = ((unsigned long)mbps * 1000 *  1000) / 8000;
-       return DIV_ROUND_UP(uframes, 512 << scale);
+       return DIV_ROUND_UP(uframes, 512UL << scale);
 }
 
 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,