Skip to content

Commit 101ede0

Browse files
keestorvalds
authored andcommitted
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>
1 parent 5ccc8fb commit 101ede0

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

‎ipc/sem.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,25 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
451451
ipc_rmid(&sem_ids(ns), &s->sem_perm);
452452
}
453453

454+
static struct sem_array *sem_alloc(size_t nsems)
455+
{
456+
struct sem_array *sma;
457+
size_t size;
458+
459+
if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
460+
return NULL;
461+
462+
size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
463+
sma = kvmalloc(size, GFP_KERNEL);
464+
if (unlikely(!sma))
465+
return NULL;
466+
467+
memset(sma, 0, size);
468+
atomic_set(&sma->sem_perm.refcount, 1);
469+
470+
return sma;
471+
}
472+
454473
/**
455474
* newary - Create a new semaphore set
456475
* @ns: namespace
@@ -463,7 +482,6 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
463482
int id;
464483
int retval;
465484
struct sem_array *sma;
466-
int size;
467485
key_t key = params->key;
468486
int nsems = params->u.nsems;
469487
int semflg = params->flg;
@@ -474,10 +492,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
474492
if (ns->used_sems + nsems > ns->sc_semmns)
475493
return -ENOSPC;
476494

477-
BUILD_BUG_ON(offsetof(struct sem_array, sem_perm) != 0);
478-
479-
size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
480-
sma = container_of(ipc_rcu_alloc(size), struct sem_array, sem_perm);
495+
sma = sem_alloc(nsems);
481496
if (!sma)
482497
return -ENOMEM;
483498

0 commit comments

Comments
 (0)