Skip to content
/ linux Public
forked from torvalds/linux

Commit

Permalink
ipc: convert kern_ipc_perm.refcount from atomic_t to refcount_t
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Elena Reshetova <[email protected]>
Signed-off-by: Hans Liljestrand <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Signed-off-by: David Windsor <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Eric W. Biederman" <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Alexey Dobriyan <[email protected]>
Cc: Serge Hallyn <[email protected]>
Cc: <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: Manfred Spraul <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
ereshetova authored and torvalds committed Sep 9, 2017
1 parent f74370b commit 9405c03
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion include/linux/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) */

Expand All @@ -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 */
6 changes: 3 additions & 3 deletions ipc/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9405c03

Please sign in to comment.