linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
@ 2016-10-28 18:11 Colin King
  2016-10-28 19:21 ` Manfred Spraul
  2016-10-28 22:15 ` Davidlohr Bueso
  0 siblings, 2 replies; 5+ messages in thread
From: Colin King @ 2016-10-28 18:11 UTC (permalink / raw)
  To: Andrew Morton, Davidlohr Bueso, Manfred Spraul, Peter Zijlstra,
	Ingo Molnar, Nikolay Borisov
  Cc: linux-kernel

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

The left shift amount is sop->sem_num % 64, which is up to 63, so
ensure we are shifting a ULL rather than a 32 bit value.

CoverityScan CID#1372862 "Bad bit shift operation"

Fixes: 7c24530cb4e3c0ae ("ipc/sem: optimize perform_atomic_semop()")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 ipc/sem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ipc/sem.c b/ipc/sem.c
index ebd18a7..ca4aa23 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -1839,7 +1839,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
 
 	max = 0;
 	for (sop = sops; sop < sops + nsops; sop++) {
-		unsigned long mask = 1 << ((sop->sem_num) % BITS_PER_LONG);
+		unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
 
 		if (sop->sem_num >= max)
 			max = sop->sem_num;
-- 
2.9.3

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

* Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
  2016-10-28 18:11 [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer Colin King
@ 2016-10-28 19:21 ` Manfred Spraul
  2016-10-28 19:29   ` Colin Ian King
  2016-10-28 22:15 ` Davidlohr Bueso
  1 sibling, 1 reply; 5+ messages in thread
From: Manfred Spraul @ 2016-10-28 19:21 UTC (permalink / raw)
  To: Colin King, Andrew Morton, Davidlohr Bueso, Peter Zijlstra,
	Ingo Molnar, Nikolay Borisov
  Cc: linux-kernel

Hi Colin,

On 10/28/2016 08:11 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> The left shift amount is sop->sem_num % 64, which is up to 63, so
> ensure we are shifting a ULL rather than a 32 bit value.
Good catch, thanks.
> CoverityScan CID#1372862 "Bad bit shift operation"
>
> Fixes: 7c24530cb4e3c0ae ("ipc/sem: optimize perform_atomic_semop()")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   ipc/sem.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/ipc/sem.c b/ipc/sem.c
> index ebd18a7..ca4aa23 100644
> --- a/ipc/sem.c
> +++ b/ipc/sem.c
> @@ -1839,7 +1839,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
>   
>   	max = 0;
>   	for (sop = sops; sop < sops + nsops; sop++) {
> -		unsigned long mask = 1 << ((sop->sem_num) % BITS_PER_LONG);
> +		unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
>   
Why 1ULL? Is 1UL not sufficient?

--
     Manfred

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

* Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
  2016-10-28 19:21 ` Manfred Spraul
@ 2016-10-28 19:29   ` Colin Ian King
  2016-10-30 15:33     ` Manfred Spraul
  0 siblings, 1 reply; 5+ messages in thread
From: Colin Ian King @ 2016-10-28 19:29 UTC (permalink / raw)
  To: Manfred Spraul, Andrew Morton, Davidlohr Bueso, Peter Zijlstra,
	Ingo Molnar, Nikolay Borisov
  Cc: linux-kernel

On 28/10/16 20:21, Manfred Spraul wrote:
> Hi Colin,
> 
> On 10/28/2016 08:11 PM, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The left shift amount is sop->sem_num % 64, which is up to 63, so
>> ensure we are shifting a ULL rather than a 32 bit value.
> Good catch, thanks.
>> CoverityScan CID#1372862 "Bad bit shift operation"
>>
>> Fixes: 7c24530cb4e3c0ae ("ipc/sem: optimize perform_atomic_semop()")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>   ipc/sem.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/ipc/sem.c b/ipc/sem.c
>> index ebd18a7..ca4aa23 100644
>> --- a/ipc/sem.c
>> +++ b/ipc/sem.c
>> @@ -1839,7 +1839,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct
>> sembuf __user *, tsops,
>>         max = 0;
>>       for (sop = sops; sop < sops + nsops; sop++) {
>> -        unsigned long mask = 1 << ((sop->sem_num) % BITS_PER_LONG);
>> +        unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
>>   
> Why 1ULL? Is 1UL not sufficient?

For example, 1UL i386 is 32 bits, where as 1ULL is 64.
> 
> -- 
>     Manfred

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

* Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
  2016-10-28 18:11 [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer Colin King
  2016-10-28 19:21 ` Manfred Spraul
@ 2016-10-28 22:15 ` Davidlohr Bueso
  1 sibling, 0 replies; 5+ messages in thread
From: Davidlohr Bueso @ 2016-10-28 22:15 UTC (permalink / raw)
  To: Colin King
  Cc: Andrew Morton, Manfred Spraul, Peter Zijlstra, Ingo Molnar,
	Nikolay Borisov, linux-kernel

On Fri, 28 Oct 2016, Colin King wrote:

Thanks.

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

* Re: [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer
  2016-10-28 19:29   ` Colin Ian King
@ 2016-10-30 15:33     ` Manfred Spraul
  0 siblings, 0 replies; 5+ messages in thread
From: Manfred Spraul @ 2016-10-30 15:33 UTC (permalink / raw)
  To: Colin Ian King, Andrew Morton, Davidlohr Bueso, Peter Zijlstra,
	Ingo Molnar, Nikolay Borisov
  Cc: linux-kernel

On 10/28/2016 09:29 PM, Colin Ian King wrote:
> On 28/10/16 20:21, Manfred Spraul wrote:
>> Hi Colin,
>>
>> On 10/28/2016 08:11 PM, Colin King wrote:
>> [...]
>>> --- a/ipc/sem.c
>>> +++ b/ipc/sem.c
>>> @@ -1839,7 +1839,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct
>>> sembuf __user *, tsops,
>>>          max = 0;
>>>        for (sop = sops; sop < sops + nsops; sop++) {
>>> -        unsigned long mask = 1 << ((sop->sem_num) % BITS_PER_LONG);
>>> +        unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
>>>    
>> Why 1ULL? Is 1UL not sufficient?
> For example, 1UL i386 is 32 bits, where as 1ULL is 64.
Exactly: on i386, 'unsigned long" is 32 bits. BITS_PER_LONG is 32.
Thus with 1UL, the code should be correct.
With 1ULL & -Wconversion, gcc would even report a warning:
> gcc -m32 -Wall -Wconversion -O1 test.c
> test.c: In function ‘main’:
> test.c:13:6: warning: conversion to ‘long unsigned int’ from ‘long 
> long unsigned int’ may alter its value [-Wconversion]
>    j= 1ULL << k;
>       ^~~~

test.c:
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char **argv)
> {
>     unsigned long j;
>     int i;
>
>     for (i=1;i<argc;i++) {
>         long k;
>
>         k=atoi(argv[i]);
>         j= 1ULL << k;
>         printf("%d: %lu %ld.\n", i, j, k);
>     }
>     return 0;
> }
>

--
     Manfred
(still thinks "1UL" is what is required)

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

end of thread, other threads:[~2016-10-30 15:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-28 18:11 [PATCH] ipc/sem: ensure we left shift a ULL rather than a 32 bit integer Colin King
2016-10-28 19:21 ` Manfred Spraul
2016-10-28 19:29   ` Colin Ian King
2016-10-30 15:33     ` Manfred Spraul
2016-10-28 22:15 ` Davidlohr Bueso

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