gpio: xilinx: Fix potential integer overflow on shift of a u32 int
Subsystems (4)
PATCH 1/1 gpio: xilinx: Fix potential integer overflow on shift of a u32 int
From: Colin Ian King <[email protected]>
The left shift of the u32 integer v is evaluated using 32 bit arithmetic and then assigned to a u64 integer. There are cases where v will currently overflow on the shift. Avoid this by casting it to unsigned long (same type as map[]) before shifting it.
Addresses-Coverity: ("Unintentional integer overflow")
--- drivers/gpio/gpio-xilinx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-xilinx.c +1 -1
@@ -99,7 +99,7 @@ static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)const unsigned long offset = (bit %BITS_PER_LONG ) &BIT (5 ); map[index] &= ~(0xFFFFFFFFul << offset); - map[index] |= v << offset; + map[index] |= (unsigned long )v << offset; }static inline int xgpio_regoffset (struct xgpio_instance *chip,int ch)
Signature · 1 line
2.30.2
From: Colin King
Quoted 22 lines
Sent: 13 May 2021 09:52
From: Colin Ian King <[email protected]>
The left shift of the u32 integer v is evaluated using 32 bit arithmetic and then assigned to a u64 integer. There are cases where v will currently overflow on the shift. Avoid this by casting it to unsigned long (same type as map[]) before shifting it.
Addresses-Coverity: ("Unintentional integer overflow")
Fixes 02b3f84d9080 ("gpio: xilinx: Switch to use bitmap APIs")Signed-off-by Colin Ian King <[email protected]>--- drivers/gpio/gpio-xilinx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpio/gpio-xilinx.c +0
-0
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 103
Quoted 6 lines
drivers/gpio/gpio-xilinx.c +1
-1@@ -99,7 +99,7 @@ static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)const unsigned long offset = (bit %BITS_PER_LONG ) &BIT (5 ); map[index] &= ~(0xFFFFFFFFul << offset); - map[index] |= v << offset; + map[index] |= (unsigned long )v << offset; }
That code looks dubious on 32bit architectures.
I don't have 02b3f84d9080 in any of my source trees. But that patch may itself be very dubious.
Since the hardware requires explicit bits be set, relying on the bitmap functions seems pointless and possibly wrong. Clearly they cause additional problems because they use long[] and here the code needs u32[].
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 5 lines · David Laight
Quoted 5 lines
From: Colin Ian King <[email protected]>
drivers/gpio/gpio-xilinx.c +0
-0@@ -99,7 +99,7 @@ static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)const unsigned long offset = (bit %BITS_PER_LONG ) &BIT (5 );
Quoted 8 lines
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 103
Quoted 4 lines
drivers/gpio/gpio-xilinx.c +1
-1map[index] &= ~(0xFFFFFFFFul << offset); - map[index] |= v << offset; + map[index] |= (unsigned long )v << offset; }That code looks dubious on 32bit architectures.
I don't have
02b3f84d9080in any of my source trees.
Can you please be more specific on which code is dubious on 32-bit arches and why?
Quoted 6 lines
But that patch may itself be very dubious.
Since the hardware requires explicit bits be set, relying on the bitmap functions seems pointless and possibly wrong. Clearly they cause additional problems because they use long[] and here the code needs u32[].
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 20 lines · Colin King
From: Colin Ian King <[email protected]>
The left shift of the u32 integer v is evaluated using 32 bit arithmetic and then assigned to a u64 integer. There are cases where v will currently overflow on the shift. Avoid this by casting it to unsigned long (same type as map[]) before shifting it.
Addresses-Coverity: ("Unintentional integer overflow")
Fixes 02b3f84d9080 ("gpio: xilinx: Switch to use bitmap APIs")Signed-off-by Colin Ian King <[email protected]>--- drivers/gpio/gpio-xilinx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpio/gpio-xilinx.c +0
-0
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 102
Quoted 5 lines
drivers/gpio/gpio-xilinx.c +1
-1@@ -99,7 +99,7 @@ static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)const unsigned long offset = (bit %BITS_PER_LONG ) &BIT (5 ); map[index] &= ~(0xFFFFFFFFul << offset); - map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;
Doing a shift by BIT(5) is super weird. It looks like a double shift bug and should probably trigger a static checker warning. It's like when people do BIT(BIT(5)).
It would be more readable to write it as:
int shift = (bit % BITS_PER_LONG) ? 32 : 0;
regards,
dan carpenter
Quoted 1 line · Dan Carpenter
On Thu, May 13, 2021 at 09:52:27AM +0100, Colin King wrote:
...
Quoted 7 lines
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 102
Quoted 5 lines
const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5); map[index] &= ~(0xFFFFFFFFul << offset);drivers/gpio/gpio-xilinx.c +1
-1- map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;Doing a shift by BIT(5) is super weird.
Not the first place in the kernel with such a trick.
Quoted 7 lines
It looks like a double shift
bug and should probably trigger a static checker warning. It's like when people do BIT(BIT(5)).It would be more readable to write it as:
int shift = (bit % BITS_PER_LONG) ? 32 : 0;
Usually this code is in a kinda fast path. Have you checked if the compiler generates the same or better code when you are using ternary?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 19 lines · Andy Shevchenko
Quoted 1 line · Dan Carpenter
On Thu, May 13, 2021 at 09:52:27AM +0100, Colin King wrote:
...
Quoted 7 lines
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 102
Quoted 5 lines
const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5); map[index] &= ~(0xFFFFFFFFul << offset);drivers/gpio/gpio-xilinx.c +1
-1- map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;Doing a shift by BIT(5) is super weird.
Not the first place in the kernel with such a trick.
Quoted 4 lines
It looks like a double shift
bug and should probably trigger a static checker warning. It's like when people do BIT(BIT(5)).
Quoted 6 lines
Quoted 3 lines
It would be more readable to write it as:
int shift = (bit % BITS_PER_LONG) ? 32 : 0;Usually this code is in a kinda fast path. Have you checked if the compiler generates the same or better code when you are using ternary?
I wrote a little benchmark to see which was faster and they're the same as far as I can see.
regards,
dan carpenter
static inline __attribute__((__gnu_inline__)) unsigned long xgpio_set_value_orig(unsigned long *map, int bit, u32 v)
{
int shift = (bit % 64) & ((((1UL))) << (5));
return v << shift;
}static inline __attribute__((__gnu_inline__)) unsigned long xgpio_set_value_new(unsigned long *map, int bit, u32 v)
{
int shift = (bit % 64) ? 32 : 0;
return v << shift;
}int main(void)
{
int i;
for (i = 0; i < INT_MAX; i++)
xgpio_set_value_orig(NULL, i, 0);// for (i = 0; i < INT_MAX; i++)
// xgpio_set_value_new(NULL, i, 0);
return 0; }
Quoted 22 lines · Dan Carpenter
Quoted 21 lines · Andy Shevchenko
Quoted 1 line · Dan Carpenter
On Thu, May 13, 2021 at 09:52:27AM +0100, Colin King wrote:
...
Quoted 7 lines
drivers/gpio/gpio-xilinx.c static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v) · line 102
Quoted 5 lines
const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5); map[index] &= ~(0xFFFFFFFFul << offset);drivers/gpio/gpio-xilinx.c +1
-1- map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;Doing a shift by BIT(5) is super weird.
Not the first place in the kernel with such a trick.
Quoted 6 lines
It looks like a double shift
bug and should probably trigger a static checker warning. It's like when people do BIT(BIT(5)).It would be more readable to write it as:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
int shift = (bit % BITS_PER_LONG) ? 32 : 0;Usually this code is in a kinda fast path. Have you checked if the compiler generates the same or better code when you are using ternary?
I wrote a little benchmark to see which was faster and they're the same as far as I can see.
Thanks for checking.
Besides the fact that offset should be 0 for 32-bit always and if compiler can proof that...
The test below doesn't take into account the exact trick is used for offset (i.e. implicit dependency between BITS_PER_LONG, size of unsigned long, and
using 5th bit out of value). I don't know if compiler can properly optimize
the ternary in this case (but it looks like it should generate the same code).
That said, I would rather to see the diff between assembly of the exact function before and after your proposal.
Quoted 25 lines
static inline __attribute__((__gnu_inline__)) unsigned long
xgpio_set_value_orig(unsigned long *map, int bit, u32 v){ int shift = (bit % 64) & ((((1UL))) << (5)); return v << shift; }static inline __attribute__((__gnu_inline__)) unsigned long
xgpio_set_value_new(unsigned long *map, int bit, u32 v){ int shift = (bit % 64) ? 32 : 0; return v << shift; }int main(void)
{ int i; for (i = 0; i < INT_MAX; i++) xgpio_set_value_orig(NULL, i, 0);// for (i = 0; i <
INT_MAX; i++)
//xgpio_set_value_new(NULL, i, 0);return 0; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 11 lines · Colin King
From: Colin Ian King <[email protected]>
The left shift of the u32 integer v is evaluated using 32 bit arithmetic and then assigned to a u64 integer. There are cases where v will currently overflow on the shift. Avoid this by casting it to unsigned long (same type as map[]) before shifting it.
Addresses-Coverity: ("Unintentional integer overflow")
Fixes 02b3f84d9080 ("gpio: xilinx: Switch to use bitmap APIs")
No, it is a false positive,
Quoted 1 line
const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);
See above, offset is 0 when BITS_PER_LONG == 32 and 32 when it's equal to 64.
Quoted 2 lines
drivers/gpio/gpio-xilinx.c +1
-1- map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Colin King
From: Colin Ian King <[email protected]>
The left shift of the u32 integer v is evaluated using 32 bit arithmetic and then assigned to a u64 integer. There are cases where v will currently overflow on the shift. Avoid this by casting it to unsigned long (same type as map[]) before shifting it.
Quoted 8 lines
Quoted 2 lines
Addresses-Coverity: ("Unintentional integer overflow")
Fixes 02b3f84d9080 ("gpio: xilinx: Switch to use bitmap APIs")No, it is a false positive,
Quoted 1 line
const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);See above, offset is 0 when
BITS_PER_LONG== 32 and 32 when it's equal to 64.
Should be read as "...and 0 or 32 when..."
Quoted 2 lines
Quoted 2 lines
drivers/gpio/gpio-xilinx.c +1
-1- map[index] |= v << offset; + map[index] |= (unsigned long )v << offset;
Signature · 2 lines
With Best Regards, Andy Shevchenko