Skip to content

Commit ec67aaa

Browse files
Davidlohr Buesotorvalds
authored andcommitted
sysvipc/sem: mitigate semnum index against spectre v1
Both smatch and coverity are reporting potential issues with spectre variant 1 with the 'semnum' index within the sma->sems array, ie: ipc/sem.c:388 sem_lock() warn: potential spectre issue 'sma->sems' ipc/sem.c:641 perform_atomic_semop_slow() warn: potential spectre issue 'sma->sems' ipc/sem.c:721 perform_atomic_semop() warn: potential spectre issue 'sma->sems' Avoid any possible speculation by using array_index_nospec() thus ensuring the semnum value is bounded to [0, sma->sem_nsems). With the exception of sem_lock() all of these are slowpaths. Link: http://lkml.kernel.org/r/20180423171131.njs4rfm2yzyeg6do@linux-n805 Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: "Gustavo A. R. Silva" <gustavo@embeddedor.com> Cc: Manfred Spraul <manfred@colorfullife.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f1b4bd0 commit ec67aaa

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎ipc/sem.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#include <linux/nsproxy.h>
8686
#include <linux/ipc_namespace.h>
8787
#include <linux/sched/wake_q.h>
88+
#include <linux/nospec.h>
8889

8990
#include <linux/uaccess.h>
9091
#include "util.h"
@@ -368,6 +369,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
368369
int nsops)
369370
{
370371
struct sem *sem;
372+
int idx;
371373

372374
if (nsops != 1) {
373375
/* Complex operation - acquire a full lock */
@@ -385,7 +387,8 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
385387
*
386388
* Both facts are tracked by use_global_mode.
387389
*/
388-
sem = &sma->sems[sops->sem_num];
390+
idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
391+
sem = &sma->sems[idx];
389392

390393
/*
391394
* Initial check for use_global_lock. Just an optimization,
@@ -638,7 +641,8 @@ static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
638641
un = q->undo;
639642

640643
for (sop = sops; sop < sops + nsops; sop++) {
641-
curr = &sma->sems[sop->sem_num];
644+
int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
645+
curr = &sma->sems[idx];
642646
sem_op = sop->sem_op;
643647
result = curr->semval;
644648

@@ -718,7 +722,9 @@ static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
718722
* until the operations can go through.
719723
*/
720724
for (sop = sops; sop < sops + nsops; sop++) {
721-
curr = &sma->sems[sop->sem_num];
725+
int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
726+
727+
curr = &sma->sems[idx];
722728
sem_op = sop->sem_op;
723729
result = curr->semval;
724730

@@ -1356,6 +1362,7 @@ static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
13561362
return -EIDRM;
13571363
}
13581364

1365+
semnum = array_index_nospec(semnum, sma->sem_nsems);
13591366
curr = &sma->sems[semnum];
13601367

13611368
ipc_assert_locked_object(&sma->sem_perm);
@@ -1509,6 +1516,8 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
15091516
err = -EIDRM;
15101517
goto out_unlock;
15111518
}
1519+
1520+
semnum = array_index_nospec(semnum, nsems);
15121521
curr = &sma->sems[semnum];
15131522

15141523
switch (cmd) {
@@ -2081,7 +2090,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
20812090
*/
20822091
if (nsops == 1) {
20832092
struct sem *curr;
2084-
curr = &sma->sems[sops->sem_num];
2093+
int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
2094+
curr = &sma->sems[idx];
20852095

20862096
if (alter) {
20872097
if (sma->complex_count) {

0 commit comments

Comments
 (0)