From: akpm@linux-foundation.org Subject: [patch 40/96] ipc/sem: avoid ipc_rcu_alloc() Date: Wed, 12 Jul 2017 14:35:02 -0700 Message-ID: <20170712213502.0x3yQAhOO%akpm@linux-foundation.org> Reply-To: linux-kernel@vger.kernel.org Return-path: Received: from mail.linuxfoundation.org ([140.211.169.12]:53570 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753452AbdGLVfD (ORCPT ); Wed, 12 Jul 2017 17:35:03 -0400 Sender: mm-commits-owner@vger.kernel.org List-Id: mm-commits@vger.kernel.org To: akpm@linux-foundation.org, dave@stgolabs.net, keescook@chromium.org, manfred@colorfullife.com, mm-commits@vger.kernel.org, torvalds@linux-foundation.org From: Kees Cook Subject: 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 Signed-off-by: Manfred Spraul Cc: Davidlohr Bueso Signed-off-by: Andrew Morton --- ipc/sem.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff -puN ipc/sem.c~ipc-sem-avoid-ipc_rcu_alloc ipc/sem.c --- a/ipc/sem.c~ipc-sem-avoid-ipc_rcu_alloc +++ a/ipc/sem.c @@ -451,6 +451,25 @@ static inline void sem_rmid(struct ipc_n 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 * 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 * if (ns->used_sems + nsems > ns->sc_semmns) return -ENOSPC; - BUILD_BUG_ON(offsetof(struct sem_array, sem_perm) != 0);