]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
sysvipc/sem: mitigate semnum index against spectre v1
authorDavidlohr Bueso <dave@stgolabs.net>
Thu, 14 Jun 2018 22:27:51 +0000 (15:27 -0700)
committerStefan Bader <stefan.bader@canonical.com>
Wed, 24 Apr 2019 08:09:06 +0000 (10:09 +0200)
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>
CVE-2017-5753

(cherry picked from commit ec67aaa46dce26d671b46c94ac674ad0b67d044c)
Signed-off-by: Juerg Haefliger <juergh@canonical.com>
Acked-by: Stefan Bader <stefan.bader@canonical.com>
Acked-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
ipc/sem.c

index 87bd38f38dc328529ef2f6854a7a3d1e945bd9a0..1b82bd336dccecefe761b26668da3f73ab176b1b 100644 (file)
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -84,6 +84,7 @@
 #include <linux/nsproxy.h>
 #include <linux/ipc_namespace.h>
 #include <linux/sched/wake_q.h>
+#include <linux/nospec.h>
 
 #include <linux/uaccess.h>
 #include "util.h"
@@ -333,6 +334,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
                              int nsops)
 {
        struct sem *sem;
+       int idx;
 
        if (nsops != 1) {
                /* Complex operation - acquire a full lock */
@@ -350,7 +352,8 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
         *
         * Both facts are tracked by use_global_mode.
         */
-       sem = &sma->sems[sops->sem_num];
+       idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
+       sem = &sma->sems[idx];
 
        /*
         * Initial check for use_global_lock. Just an optimization,
@@ -608,7 +611,8 @@ static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
        un = q->undo;
 
        for (sop = sops; sop < sops + nsops; sop++) {
-               curr = &sma->sems[sop->sem_num];
+               int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
+               curr = &sma->sems[idx];
                sem_op = sop->sem_op;
                result = curr->semval;
 
@@ -688,7 +692,9 @@ static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
         * until the operations can go through.
         */
        for (sop = sops; sop < sops + nsops; sop++) {
-               curr = &sma->sems[sop->sem_num];
+               int idx = array_index_nospec(sop->sem_num, sma->sem_nsems);
+
+               curr = &sma->sems[idx];
                sem_op = sop->sem_op;
                result = curr->semval;
 
@@ -1304,6 +1310,7 @@ static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
                return -EIDRM;
        }
 
+       semnum = array_index_nospec(semnum, sma->sem_nsems);
        curr = &sma->sems[semnum];
 
        ipc_assert_locked_object(&sma->sem_perm);
@@ -1457,6 +1464,8 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                err = -EIDRM;
                goto out_unlock;
        }
+
+       semnum = array_index_nospec(semnum, nsems);
        curr = &sma->sems[semnum];
 
        switch (cmd) {
@@ -2015,7 +2024,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
         */
        if (nsops == 1) {
                struct sem *curr;
-               curr = &sma->sems[sops->sem_num];
+               int idx = array_index_nospec(sops->sem_num, sma->sem_nsems);
+               curr = &sma->sems[idx];
 
                if (alter) {
                        if (sma->complex_count) {