Skip to content

Commit d979a39

Browse files
Johannes Weinertorvalds
authored andcommitted
cgroup: duplicate cgroup reference when cloning sockets
When a socket is cloned, the associated sock_cgroup_data is duplicated but not its reference on the cgroup. As a result, the cgroup reference count will underflow when both sockets are destroyed later on. Fixes: bd1060a ("sock, cgroup: add sock->sk_cgroup") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Johannes Weiner <[email protected]> Acked-by: Tejun Heo <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: <[email protected]> [4.5+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent db2ba40 commit d979a39

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

kernel/cgroup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6270,6 +6270,12 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
62706270
if (cgroup_sk_alloc_disabled)
62716271
return;
62726272

6273+
/* Socket clone path */
6274+
if (skcd->val) {
6275+
cgroup_get(sock_cgroup_ptr(skcd));
6276+
return;
6277+
}
6278+
62736279
rcu_read_lock();
62746280

62756281
while (true) {

net/core/sock.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,6 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
13621362
if (!try_module_get(prot->owner))
13631363
goto out_free_sec;
13641364
sk_tx_queue_clear(sk);
1365-
cgroup_sk_alloc(&sk->sk_cgrp_data);
13661365
}
13671366

13681367
return sk;
@@ -1422,6 +1421,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
14221421
sock_net_set(sk, net);
14231422
atomic_set(&sk->sk_wmem_alloc, 1);
14241423

1424+
cgroup_sk_alloc(&sk->sk_cgrp_data);
14251425
sock_update_classid(&sk->sk_cgrp_data);
14261426
sock_update_netprioidx(&sk->sk_cgrp_data);
14271427
}
@@ -1566,6 +1566,9 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
15661566
newsk->sk_priority = 0;
15671567
newsk->sk_incoming_cpu = raw_smp_processor_id();
15681568
atomic64_set(&newsk->sk_cookie, 0);
1569+
1570+
cgroup_sk_alloc(&newsk->sk_cgrp_data);
1571+
15691572
/*
15701573
* Before updating sk_refcnt, we must commit prior changes to memory
15711574
* (Documentation/RCU/rculist_nulls.txt for details)

0 commit comments

Comments
 (0)