Skip to content

Commit ad0f75e

Browse files
congwangdavem330
authored andcommitted
cgroup: fix cgroup_sk_alloc() for sk_clone_lock()
When we clone a socket in sk_clone_lock(), its sk_cgrp_data is copied, so the cgroup refcnt must be taken too. And, unlike the sk_alloc() path, sock_update_netprioidx() is not called here. Therefore, it is safe and necessary to grab the cgroup refcnt even when cgroup_sk_alloc is disabled. sk_clone_lock() is in BH context anyway, the in_interrupt() would terminate this function if called there. And for sk_alloc() skcd->val is always zero. So it's safe to factor out the code to make it more readable. The global variable 'cgroup_sk_alloc_disabled' is used to determine whether to take these reference counts. It is impossible to make the reference counting correct unless we save this bit of information in skcd->val. So, add a new bit there to record whether the socket has already taken the reference counts. This obviously relies on kmalloc() to align cgroup pointers to at least 4 bytes, ARCH_KMALLOC_MINALIGN is certainly larger than that. This bug seems to be introduced since the beginning, commit d979a39 ("cgroup: duplicate cgroup reference when cloning sockets") tried to fix it but not compeletely. It seems not easy to trigger until the recent commit 090e28b ("netprio_cgroup: Fix unlimited memory leak of v2 cgroups") was merged. Fixes: bd1060a ("sock, cgroup: add sock->sk_cgroup") Reported-by: Cameron Berkenpas <[email protected]> Reported-by: Peter Geis <[email protected]> Reported-by: Lu Fengqi <[email protected]> Reported-by: Daniël Sonck <[email protected]> Reported-by: Zhang Qiang <[email protected]> Tested-by: Cameron Berkenpas <[email protected]> Tested-by: Peter Geis <[email protected]> Tested-by: Thomas Lamprecht <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Zefan Li <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Roman Gushchin <[email protected]> Signed-off-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent aea23c3 commit ad0f75e

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

include/linux/cgroup-defs.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ struct sock_cgroup_data {
790790
union {
791791
#ifdef __LITTLE_ENDIAN
792792
struct {
793-
u8 is_data;
793+
u8 is_data : 1;
794+
u8 no_refcnt : 1;
794795
u8 padding;
795796
u16 prioidx;
796797
u32 classid;
@@ -800,7 +801,8 @@ struct sock_cgroup_data {
800801
u32 classid;
801802
u16 prioidx;
802803
u8 padding;
803-
u8 is_data;
804+
u8 no_refcnt : 1;
805+
u8 is_data : 1;
804806
} __packed;
805807
#endif
806808
u64 val;

include/linux/cgroup.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ extern spinlock_t cgroup_sk_update_lock;
822822

823823
void cgroup_sk_alloc_disable(void);
824824
void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
825+
void cgroup_sk_clone(struct sock_cgroup_data *skcd);
825826
void cgroup_sk_free(struct sock_cgroup_data *skcd);
826827

827828
static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
@@ -835,7 +836,7 @@ static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
835836
*/
836837
v = READ_ONCE(skcd->val);
837838

838-
if (v & 1)
839+
if (v & 3)
839840
return &cgrp_dfl_root.cgrp;
840841

841842
return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
@@ -847,6 +848,7 @@ static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
847848
#else /* CONFIG_CGROUP_DATA */
848849

849850
static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
851+
static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
850852
static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
851853

852854
#endif /* CONFIG_CGROUP_DATA */

kernel/cgroup/cgroup.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6439,18 +6439,8 @@ void cgroup_sk_alloc_disable(void)
64396439

64406440
void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
64416441
{
6442-
if (cgroup_sk_alloc_disabled)
6443-
return;
6444-
6445-
/* Socket clone path */
6446-
if (skcd->val) {
6447-
/*
6448-
* We might be cloning a socket which is left in an empty
6449-
* cgroup and the cgroup might have already been rmdir'd.
6450-
* Don't use cgroup_get_live().
6451-
*/
6452-
cgroup_get(sock_cgroup_ptr(skcd));
6453-
cgroup_bpf_get(sock_cgroup_ptr(skcd));
6442+
if (cgroup_sk_alloc_disabled) {
6443+
skcd->no_refcnt = 1;
64546444
return;
64556445
}
64566446

@@ -6475,10 +6465,27 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
64756465
rcu_read_unlock();
64766466
}
64776467

6468+
void cgroup_sk_clone(struct sock_cgroup_data *skcd)
6469+
{
6470+
if (skcd->val) {
6471+
if (skcd->no_refcnt)
6472+
return;
6473+
/*
6474+
* We might be cloning a socket which is left in an empty
6475+
* cgroup and the cgroup might have already been rmdir'd.
6476+
* Don't use cgroup_get_live().
6477+
*/
6478+
cgroup_get(sock_cgroup_ptr(skcd));
6479+
cgroup_bpf_get(sock_cgroup_ptr(skcd));
6480+
}
6481+
}
6482+
64786483
void cgroup_sk_free(struct sock_cgroup_data *skcd)
64796484
{
64806485
struct cgroup *cgrp = sock_cgroup_ptr(skcd);
64816486

6487+
if (skcd->no_refcnt)
6488+
return;
64826489
cgroup_bpf_put(cgrp);
64836490
cgroup_put(cgrp);
64846491
}

net/core/sock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
19261926
/* sk->sk_memcg will be populated at accept() time */
19271927
newsk->sk_memcg = NULL;
19281928

1929-
cgroup_sk_alloc(&newsk->sk_cgrp_data);
1929+
cgroup_sk_clone(&newsk->sk_cgrp_data);
19301930

19311931
rcu_read_lock();
19321932
filter = rcu_dereference(sk->sk_filter);

0 commit comments

Comments
 (0)