summaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2017-07-12 14:35:02 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2017-07-12 16:26:01 -0700
commit101ede01dfd5072651965e974bc6e30c8d0748e2 (patch)
treede85b9884a50812354b9f9ccdc3193a30239fa4d /ipc
parent5ccc8fb54f1c083f1dc7e073150ec18d43457872 (diff)
downloadlinux-101ede01dfd5072651965e974bc6e30c8d0748e2.tar.gz
linux-101ede01dfd5072651965e974bc6e30c8d0748e2.tar.bz2
linux-101ede01dfd5072651965e974bc6e30c8d0748e2.zip
ipc/sem: avoid ipc_rcu_alloc()
Instead of using ipc_rcu_alloc() which only performs the refcount bump, open code it to perform better sem-specific checks. This also allows for sem_array structure layout to be randomized in the future. [manfred@colorfullife.com: Rediff, because the memset was temporarily inside ipc_rcu_alloc()] Link: http://lkml.kernel.org/r/20170525185107.12869-10-manfred@colorfullife.com Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'ipc')
-rw-r--r--ipc/sem.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/ipc/sem.c b/ipc/sem.c
index a04c4d6d120c..445a5b5eb88f 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -451,6 +451,25 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
ipc_rmid(&sem_ids(ns), &s->sem_perm);
}
+static struct sem_array *sem_alloc(size_t nsems)
+{
+ struct sem_array *sma;
+ size_t size;
+
+ if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
+ return NULL;
+
+ size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
+ sma = kvmalloc(size, GFP_KERNEL);
+ if (unlikely(!sma))
+ return NULL;
+
+ memset(sma, 0, size);
+ atomic_set(&sma->sem_perm.refcount, 1);
+
+ return sma;
+}
+
/**
* newary - Create a new semaphore set
* @ns: namespace
@@ -463,7 +482,6 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
int id;
int retval;
struct sem_array *sma;
- int size;
key_t key = params->key;
int nsems = params->u.nsems;
int semflg = params->flg;
@@ -474,10 +492,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
if (ns->used_sems + nsems > ns->sc_semmns)
return -ENOSPC;
- BUILD_BUG_ON(offsetof(struct sem_array, sem_perm) != 0);
-
- size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
- sma = container_of(ipc_rcu_alloc(size), struct sem_array, sem_perm);
+ sma = sem_alloc(nsems);
if (!sma)
return -ENOMEM;