From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1424938AbdEYSvd (ORCPT ); Thu, 25 May 2017 14:51:33 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34374 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1036466AbdEYSv0 (ORCPT ); Thu, 25 May 2017 14:51:26 -0400 From: Manfred Spraul To: mtk.manpages@gmail.com, Andrew Morton , Kees Cook Cc: LKML , 1vier1@web.de, Davidlohr Bueso , mingo@kernel.org, peterz@infradead.org, fabf@skynet.be, Manfred Spraul Subject: [PATCH 09/20] ipc/sem: Avoid ipc_rcu_alloc() Date: Thu, 25 May 2017 20:50:56 +0200 Message-Id: <20170525185107.12869-10-manfred@colorfullife.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170525185107.12869-1-manfred@colorfullife.com> References: <20170508222345.GA52073@beast> <20170525185107.12869-1-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook 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. Signed-off-by: Kees Cook [manfred@colorfullife.com: Rediff, because the memset was temporarily inside ipc_rcu_alloc()] Signed-off-by: Manfred Spraul --- ipc/sem.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ipc/sem.c b/ipc/sem.c index a04c4d6..445a5b5 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; -- 2.9.3