Skip to content

Commit a2e0602

Browse files
ereshetovatorvalds
authored andcommitted
ipc: convert ipc_namespace.count 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/[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]>
1 parent 7483e5d commit a2e0602

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

include/linux/ipc_namespace.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/notifier.h>
88
#include <linux/nsproxy.h>
99
#include <linux/ns_common.h>
10+
#include <linux/refcount.h>
1011

1112
struct user_namespace;
1213

@@ -19,7 +20,7 @@ struct ipc_ids {
1920
};
2021

2122
struct ipc_namespace {
22-
atomic_t count;
23+
refcount_t count;
2324
struct ipc_ids ids[3];
2425

2526
int sem_ctls[4];
@@ -118,7 +119,7 @@ extern struct ipc_namespace *copy_ipcs(unsigned long flags,
118119
static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
119120
{
120121
if (ns)
121-
atomic_inc(&ns->count);
122+
refcount_inc(&ns->count);
122123
return ns;
123124
}
124125

ipc/msgutil.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ DEFINE_SPINLOCK(mq_lock);
2929
* and not CONFIG_IPC_NS.
3030
*/
3131
struct ipc_namespace init_ipc_ns = {
32-
.count = ATOMIC_INIT(1),
32+
.count = REFCOUNT_INIT(1),
3333
.user_ns = &init_user_ns,
3434
.ns.inum = PROC_IPC_INIT_INO,
3535
#ifdef CONFIG_IPC_NS

ipc/namespace.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
5050
goto fail_free;
5151
ns->ns.ops = &ipcns_operations;
5252

53-
atomic_set(&ns->count, 1);
53+
refcount_set(&ns->count, 1);
5454
ns->user_ns = get_user_ns(user_ns);
5555
ns->ucounts = ucounts;
5656

@@ -144,7 +144,7 @@ static void free_ipc_ns(struct ipc_namespace *ns)
144144
*/
145145
void put_ipc_ns(struct ipc_namespace *ns)
146146
{
147-
if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
147+
if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
148148
mq_clear_sbinfo(ns);
149149
spin_unlock(&mq_lock);
150150
mq_put_mnt(ns);

0 commit comments

Comments
 (0)