Skip to content

Commit 9405c03

Browse files
ereshetovatorvalds
authored andcommitted
ipc: convert kern_ipc_perm.refcount from atomic_t to refcount_t
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>
1 parent f74370b commit 9405c03

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

‎include/linux/ipc.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/spinlock.h>
55
#include <linux/uidgid.h>
66
#include <uapi/linux/ipc.h>
7+
#include <linux/refcount.h>
78

89
#define IPCMNI 32768 /* <= MAX_INT limit for ipc arrays (including sysctl changes) */
910

@@ -22,7 +23,7 @@ struct kern_ipc_perm {
2223
void *security;
2324

2425
struct rcu_head rcu;
25-
atomic_t refcount;
26+
refcount_t refcount;
2627
} ____cacheline_aligned_in_smp __randomize_layout;
2728

2829
#endif /* _LINUX_IPC_H */

‎ipc/util.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
232232

233233
idr_preload(GFP_KERNEL);
234234

235-
atomic_set(&new->refcount, 1);
235+
refcount_set(&new->refcount, 1);
236236
spin_lock_init(&new->lock);
237237
new->deleted = false;
238238
rcu_read_lock();
@@ -397,13 +397,13 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
397397

398398
int ipc_rcu_getref(struct kern_ipc_perm *ptr)
399399
{
400-
return atomic_inc_not_zero(&ptr->refcount);
400+
return refcount_inc_not_zero(&ptr->refcount);
401401
}
402402

403403
void ipc_rcu_putref(struct kern_ipc_perm *ptr,
404404
void (*func)(struct rcu_head *head))
405405
{
406-
if (!atomic_dec_and_test(&ptr->refcount))
406+
if (!refcount_dec_and_test(&ptr->refcount))
407407
return;
408408

409409
call_rcu(&ptr->rcu, func);

0 commit comments

Comments
 (0)