Skip to content

Commit fbb1fb4

Browse files
edumazetdavem330
authored andcommitted
net: defer call to cgroup_sk_alloc()
sk_clone_lock() might run while TCP/DCCP listener already vanished. In order to prevent use after free, it is better to defer cgroup_sk_alloc() to the point we know both parent and child exist, and from process context. Fixes: e994b2f ("tcp: do not lock listener to process SYN packets") Signed-off-by: Eric Dumazet <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Tejun Heo <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 9f1c267 commit fbb1fb4

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

kernel/cgroup/cgroup.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5709,17 +5709,6 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
57095709
if (cgroup_sk_alloc_disabled)
57105710
return;
57115711

5712-
/* Socket clone path */
5713-
if (skcd->val) {
5714-
/*
5715-
* We might be cloning a socket which is left in an empty
5716-
* cgroup and the cgroup might have already been rmdir'd.
5717-
* Don't use cgroup_get_live().
5718-
*/
5719-
cgroup_get(sock_cgroup_ptr(skcd));
5720-
return;
5721-
}
5722-
57235712
rcu_read_lock();
57245713

57255714
while (true) {

net/core/sock.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
16801680

16811681
/* sk->sk_memcg will be populated at accept() time */
16821682
newsk->sk_memcg = NULL;
1683+
memset(&newsk->sk_cgrp_data, 0, sizeof(newsk->sk_cgrp_data));
16831684

16841685
atomic_set(&newsk->sk_drops, 0);
16851686
newsk->sk_send_head = NULL;
@@ -1718,8 +1719,6 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
17181719
newsk->sk_incoming_cpu = raw_smp_processor_id();
17191720
atomic64_set(&newsk->sk_cookie, 0);
17201721

1721-
cgroup_sk_alloc(&newsk->sk_cgrp_data);
1722-
17231722
/*
17241723
* Before updating sk_refcnt, we must commit prior changes to memory
17251724
* (Documentation/RCU/rculist_nulls.txt for details)

net/ipv4/inet_connection_sock.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <net/tcp.h>
2727
#include <net/sock_reuseport.h>
2828
#include <net/addrconf.h>
29+
#include <net/cls_cgroup.h>
30+
#include <net/netprio_cgroup.h>
2931

3032
#ifdef INET_CSK_DEBUG
3133
const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
@@ -476,6 +478,9 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
476478
spin_unlock_bh(&queue->fastopenq.lock);
477479
}
478480
mem_cgroup_sk_alloc(newsk);
481+
cgroup_sk_alloc(&newsk->sk_cgrp_data);
482+
sock_update_classid(&newsk->sk_cgrp_data);
483+
sock_update_netprioidx(&newsk->sk_cgrp_data);
479484
out:
480485
release_sock(sk);
481486
if (req)

0 commit comments

Comments
 (0)