Skip to content

Commit 7ae215d

Browse files
joestringerAlexei Starovoitov
authored andcommitted
bpf: Don't refcount LISTEN sockets in sk_assign()
Avoid taking a reference on listen sockets by checking the socket type in the sk_assign and in the corresponding skb_steal_sock() code in the the transport layer, and by ensuring that the prefetch free (sock_pfree) function uses the same logic to check whether the socket is refcounted. Suggested-by: Martin KaFai Lau <[email protected]> Signed-off-by: Joe Stringer <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 71489e2 commit 7ae215d

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

include/net/sock.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,21 @@ skb_sk_is_prefetched(struct sk_buff *skb)
25372537
#endif /* CONFIG_INET */
25382538
}
25392539

2540+
/* This helper checks if a socket is a full socket,
2541+
* ie _not_ a timewait or request socket.
2542+
*/
2543+
static inline bool sk_fullsock(const struct sock *sk)
2544+
{
2545+
return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
2546+
}
2547+
2548+
static inline bool
2549+
sk_is_refcounted(struct sock *sk)
2550+
{
2551+
/* Only full sockets have sk->sk_flags. */
2552+
return !sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE);
2553+
}
2554+
25402555
/**
25412556
* skb_steal_sock
25422557
* @skb to steal the socket from
@@ -2549,6 +2564,8 @@ skb_steal_sock(struct sk_buff *skb, bool *refcounted)
25492564
struct sock *sk = skb->sk;
25502565

25512566
*refcounted = true;
2567+
if (skb_sk_is_prefetched(skb))
2568+
*refcounted = sk_is_refcounted(sk);
25522569
skb->destructor = NULL;
25532570
skb->sk = NULL;
25542571
return sk;
@@ -2557,14 +2574,6 @@ skb_steal_sock(struct sk_buff *skb, bool *refcounted)
25572574
return NULL;
25582575
}
25592576

2560-
/* This helper checks if a socket is a full socket,
2561-
* ie _not_ a timewait or request socket.
2562-
*/
2563-
static inline bool sk_fullsock(const struct sock *sk)
2564-
{
2565-
return (1 << sk->sk_state) & ~(TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV);
2566-
}
2567-
25682577
/* Checks if this SKB belongs to an HW offloaded socket
25692578
* and whether any SW fallbacks are required based on dev.
25702579
* Check decrypted mark in case skb_orphan() cleared socket.

net/core/filter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5401,8 +5401,7 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
54015401

54025402
BPF_CALL_1(bpf_sk_release, struct sock *, sk)
54035403
{
5404-
/* Only full sockets have sk->sk_flags. */
5405-
if (!sk_fullsock(sk) || !sock_flag(sk, SOCK_RCU_FREE))
5404+
if (sk_is_refcounted(sk))
54065405
sock_gen_put(sk);
54075406
return 0;
54085407
}
@@ -5928,7 +5927,8 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
59285927
return -ENETUNREACH;
59295928
if (unlikely(sk->sk_reuseport))
59305929
return -ESOCKTNOSUPPORT;
5931-
if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
5930+
if (sk_is_refcounted(sk) &&
5931+
unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
59325932
return -ENOENT;
59335933

59345934
skb_orphan(skb);

net/core/sock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,8 @@ EXPORT_SYMBOL(sock_efree);
20772077
#ifdef CONFIG_INET
20782078
void sock_pfree(struct sk_buff *skb)
20792079
{
2080-
sock_gen_put(skb->sk);
2080+
if (sk_is_refcounted(skb->sk))
2081+
sock_gen_put(skb->sk);
20812082
}
20822083
EXPORT_SYMBOL(sock_pfree);
20832084
#endif /* CONFIG_INET */

0 commit comments

Comments
 (0)