Skip to content

Commit f7355a6

Browse files
iamkafaiborkmann
authored andcommitted
bpf: Check sk_fullsock() before returning from bpf_sk_lookup()
The BPF_FUNC_sk_lookup_xxx helpers return RET_PTR_TO_SOCKET_OR_NULL. Meaning a fullsock ptr and its fullsock's fields in bpf_sock can be accessed, e.g. type, protocol, mark and priority. Some new helper, like bpf_sk_storage_get(), also expects ARG_PTR_TO_SOCKET is a fullsock. bpf_sk_lookup() currently calls sk_to_full_sk() before returning. However, the ptr returned from sk_to_full_sk() is not guaranteed to be a fullsock. For example, it cannot get a fullsock if sk is in TCP_TIME_WAIT. This patch checks for sk_fullsock() before returning. If it is not a fullsock, sock_gen_put() is called if needed and then returns NULL. Fixes: 6acc9b4 ("bpf: Add helper to retrieve socket in BPF") Cc: Joe Stringer <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Joe Stringer <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 9b28ae2 commit f7355a6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

net/core/filter.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5343,8 +5343,14 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
53435343
struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
53445344
ifindex, proto, netns_id, flags);
53455345

5346-
if (sk)
5346+
if (sk) {
53475347
sk = sk_to_full_sk(sk);
5348+
if (!sk_fullsock(sk)) {
5349+
if (!sock_flag(sk, SOCK_RCU_FREE))
5350+
sock_gen_put(sk);
5351+
return NULL;
5352+
}
5353+
}
53485354

53495355
return sk;
53505356
}
@@ -5375,8 +5381,14 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
53755381
struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
53765382
flags);
53775383

5378-
if (sk)
5384+
if (sk) {
53795385
sk = sk_to_full_sk(sk);
5386+
if (!sk_fullsock(sk)) {
5387+
if (!sock_flag(sk, SOCK_RCU_FREE))
5388+
sock_gen_put(sk);
5389+
return NULL;
5390+
}
5391+
}
53805392

53815393
return sk;
53825394
}

0 commit comments

Comments
 (0)