From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752974AbdBTLeM (ORCPT ); Mon, 20 Feb 2017 06:34:12 -0500 Received: from mga11.intel.com ([192.55.52.93]:19520 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752927AbdBTLeL (ORCPT ); Mon, 20 Feb 2017 06:34:11 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,186,1484035200"; d="scan'208";a="826332823" From: Elena Reshetova To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, gregkh@linuxfoundation.org, akpm@linux-foundation.org, ebiederm@xmission.com, mingo@redhat.com, adobriyan@gmail.com, serge@hallyn.com, arozansk@redhat.com, dave@stgolabs.net, Elena Reshetova , Hans Liljestrand , Kees Cook , David Windsor Subject: [PATCH 3/3] ipc: convert ipc_rcu.refcount from atomic_t to refcount_t Date: Mon, 20 Feb 2017 13:29:49 +0200 Message-Id: <1487590189-18151-4-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1487590189-18151-1-git-send-email-elena.reshetova@intel.com> References: <1487590189-18151-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org refcount_t type and corresponding API should be used instead of atomic_t when the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Signed-off-by: Elena Reshetova Signed-off-by: Hans Liljestrand Signed-off-by: Kees Cook Signed-off-by: David Windsor --- ipc/util.c | 6 +++--- ipc/util.h | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ipc/util.c b/ipc/util.c index 798cad1..24484a6 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -437,7 +437,7 @@ void *ipc_rcu_alloc(int size) struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size); if (unlikely(!out)) return NULL; - atomic_set(&out->refcount, 1); + refcount_set(&out->refcount, 1); return out + 1; } @@ -445,14 +445,14 @@ int ipc_rcu_getref(void *ptr) { struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1; - return atomic_inc_not_zero(&p->refcount); + return refcount_inc_not_zero(&p->refcount); } void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head)) { struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1; - if (!atomic_dec_and_test(&p->refcount)) + if (!refcount_dec_and_test(&p->refcount)) return; call_rcu(&p->rcu, func); diff --git a/ipc/util.h b/ipc/util.h index 51f7ca5..274ec9b 100644 --- a/ipc/util.h +++ b/ipc/util.h @@ -12,6 +12,7 @@ #include #include +#include #define SEQ_MULTIPLIER (IPCMNI) @@ -49,7 +50,7 @@ static inline void shm_exit_ns(struct ipc_namespace *ns) { } struct ipc_rcu { struct rcu_head rcu; - atomic_t refcount; + refcount_t refcount; } ____cacheline_aligned_in_smp; #define ipc_rcu_to_struct(p) ((void *)(p+1)) -- 2.7.4