Skip to contentSkip to conversation

ipc/sem: ensure we left shift a ULL rather than a 32 bit integer

PATCH In review Colin King 3 participants 5 messages Oct 28, 2016 – Oct 30, 2016

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 ipc/sem: ensure we left shift a ULL rather than a 32 bit integer

From: Colin Ian King <[email protected]>

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()")
---
 ipc/sem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ipc/sem.c +1 -1
index ebd18a7..ca4aa23 100644
@@ -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;
Signature · 1 line
2.9.3
#1 lore raw

Hi Colin,

Quoted 4 lines · Colin King

From: Colin Ian King <[email protected]>

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.

Quoted 13 lines

CoverityScan CID#1372862 "Bad bit shift operation"

Fixes 7c24530cb4e3c0ae ("ipc/sem: optimize perform_atomic_semop()")
---
  ipc/sem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ipc/sem.c +0 -0
index ebd18a7..ca4aa23 100644

ipc/sem.c SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops, · line 1843

Quoted 6 lines
ipc/sem.c +1 -1
@@ -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

#2 lore raw
Quoted 16 lines · Manfred Spraul

Hi Colin,

Quoted 4 lines · Colin King

From: Colin Ian King <[email protected]>

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.

Quoted 8 lines

CoverityScan CID#1372862 "Bad bit shift operation"

Fixes 7c24530cb4e3c0ae ("ipc/sem: optimize perform_atomic_semop()")
---
  ipc/sem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

ipc/sem.c SYSCALL_DEFINE4(semtimedop, int, semid, struct · line 1843

Quoted 11 lines
ipc/sem.c +1 -1
index ebd18a7..ca4aa23 100644
@@ -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.

Quoted 3 lines
Signature · 2 lines
-- 
    Manfred
#3 lore raw
Quoted 15 lines · Colin Ian King
Quoted 4 lines · Manfred Spraul

Hi Colin,

On 10/28/2016 08:11 PM, Colin King wrote:
[...]

Quoted 10 lines

ipc/sem.c SYSCALL_DEFINE4(semtimedop, int, semid, struct · line 1843

Quoted 9 lines
ipc/sem.c +1 -1
@@ -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?

Quoted 1 line

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:

Quoted 5 lines

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:

Quoted 4 lines

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)

Quoted 14 lines
{
    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)