Skip to content

Commit eefca20

Browse files
Eric Dumazetdavem330
authored andcommitted
socket, bpf: fix possible use after free
Starting from linux-4.4, 3WHS no longer takes the listener lock. Since this time, we might hit a use-after-free in sk_filter_charge(), if the filter we got in the memcpy() of the listener content just happened to be replaced by a thread changing listener BPF filter. To fix this, we need to make sure the filter refcount is not already zero before incrementing it again. Fixes: e994b2f ("tcp: do not lock listener to process SYN packets") Signed-off-by: Eric Dumazet <edumazet@google.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4ee4553 commit eefca20

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

net/core/filter.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,14 @@ static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
989989

990990
bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
991991
{
992-
bool ret = __sk_filter_charge(sk, fp);
993-
if (ret)
994-
refcount_inc(&fp->refcnt);
995-
return ret;
992+
if (!refcount_inc_not_zero(&fp->refcnt))
993+
return false;
994+
995+
if (!__sk_filter_charge(sk, fp)) {
996+
sk_filter_release(fp);
997+
return false;
998+
}
999+
return true;
9961000
}
9971001

9981002
static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)

net/core/sock.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,13 +1684,16 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
16841684

16851685
sock_reset_flag(newsk, SOCK_DONE);
16861686

1687-
filter = rcu_dereference_protected(newsk->sk_filter, 1);
1687+
rcu_read_lock();
1688+
filter = rcu_dereference(sk->sk_filter);
16881689
if (filter != NULL)
16891690
/* though it's an empty new sock, the charging may fail
16901691
* if sysctl_optmem_max was changed between creation of
16911692
* original socket and cloning
16921693
*/
16931694
is_charged = sk_filter_charge(newsk, filter);
1695+
RCU_INIT_POINTER(newsk->sk_filter, filter);
1696+
rcu_read_unlock();
16941697

16951698
if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
16961699
/* We need to make sure that we don't uncharge the new

0 commit comments

Comments
 (0)