ipc: convert kern_ipc_perm.refcount from atomic_t to refcount_t
authorElena Reshetova <elena.reshetova@intel.com>
Fri, 8 Sep 2017 23:17:45 +0000 (16:17 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 9 Sep 2017 01:26:51 +0000 (18:26 -0700)
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.

Link: http://lkml.kernel.org/r/1499417992-3238-4-git-send-email-elena.reshetova@intel.com
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David Windsor <dwindsor@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: <arozansk@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/ipc.h
ipc/util.c

index fadd579d577dc8aafd7c100ea51fe2bf630c76a0..ae68980e9d481fa66c68295c302a0309f5cceb75 100644 (file)
@@ -4,6 +4,7 @@
 #include <linux/spinlock.h>
 #include <linux/uidgid.h>
 #include <uapi/linux/ipc.h>
+#include <linux/refcount.h>
 
 #define IPCMNI 32768  /* <= MAX_INT limit for ipc arrays (including sysctl changes) */
 
@@ -22,7 +23,7 @@ struct kern_ipc_perm {
        void            *security;
 
        struct rcu_head rcu;
-       atomic_t refcount;
+       refcount_t refcount;
 } ____cacheline_aligned_in_smp __randomize_layout;
 
 #endif /* _LINUX_IPC_H */
index 1a2cb02467ab5f33f309a4542620cbec8d18361a..069bb22c9f642f0a82a4d075aff6e00043edc930 100644 (file)
@@ -232,7 +232,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
 
        idr_preload(GFP_KERNEL);
 
-       atomic_set(&new->refcount, 1);
+       refcount_set(&new->refcount, 1);
        spin_lock_init(&new->lock);
        new->deleted = false;
        rcu_read_lock();
@@ -397,13 +397,13 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
 
 int ipc_rcu_getref(struct kern_ipc_perm *ptr)
 {
-       return atomic_inc_not_zero(&ptr->refcount);
+       return refcount_inc_not_zero(&ptr->refcount);
 }
 
 void ipc_rcu_putref(struct kern_ipc_perm *ptr,
                        void (*func)(struct rcu_head *head))
 {
-       if (!atomic_dec_and_test(&ptr->refcount))
+       if (!refcount_dec_and_test(&ptr->refcount))
                return;
 
        call_rcu(&ptr->rcu, func);