Add sock_pfree_ref for skb-owned references whose release mustn't depend on SOCK_RCU_FREE. Teach receive lookup, NFQUEUE and reuseport selection to preserve and transfer this ownership. A following patch lets netkit programs assign sockets. The skb is then queued to the backlog before the receive path consumes the socket so a borrowed RCU reference isn't enough and the skb must own a reference. The reference must be on a full socket. skb_steal_sock returns the listener of a syncookie request socket before it looks at the destructor so a reference taken on such a request socket would leak. DEBUG_NET builds warn if sock_pfree_ref meets a socket that is not full either there or when the skb is freed. Signed-off-by: Nikolay Aleksandrov --- include/net/inet6_hashtables.h | 10 +++++++--- include/net/inet_hashtables.h | 10 +++++++--- include/net/request_sock.h | 10 +++++++--- include/net/sock.h | 4 +++- net/core/sock.c | 7 +++++++ net/netfilter/nf_queue.c | 16 +++++++--------- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h index 2cc5d416bbb5..e461a86f8eaf 100644 --- a/include/net/inet6_hashtables.h +++ b/include/net/inet6_hashtables.h @@ -134,10 +134,14 @@ struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff, if (!reuse_sk) return sk; - /* We've chosen a new reuseport sock which is never refcounted. This - * implies that sk also isn't refcounted. + /* netkit bpf assignment holds a reference on the prefetched + * socket and reuseport returns an rcu protected socket so if it + * differs we have to release the original reference */ - WARN_ON_ONCE(*refcounted); + if (reuse_sk != sk && *refcounted) { + sock_put(sk); + *refcounted = false; + } return reuse_sk; } diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h index 44b7e39a1245..d4dd9c3487d8 100644 --- a/include/net/inet_hashtables.h +++ b/include/net/inet_hashtables.h @@ -476,10 +476,14 @@ struct sock *inet_steal_sock(struct net *net, struct sk_buff *skb, int doff, if (!reuse_sk) return sk; - /* We've chosen a new reuseport sock which is never refcounted. This - * implies that sk also isn't refcounted. + /* netkit bpf assignment holds a reference on the prefetched + * socket and reuseport returns an rcu protected socket so if it + * differs we have to release the original reference */ - WARN_ON_ONCE(*refcounted); + if (reuse_sk != sk && *refcounted) { + sock_put(sk); + *refcounted = false; + } return reuse_sk; } diff --git a/include/net/request_sock.h b/include/net/request_sock.h index faa6dc49cea8..72fdbf0dccbc 100644 --- a/include/net/request_sock.h +++ b/include/net/request_sock.h @@ -103,21 +103,25 @@ static inline struct sock *skb_steal_sock(struct sk_buff *skb, } *prefetched = skb_sk_is_prefetched(skb); + *refcounted = true; +#ifdef CONFIG_INET if (*prefetched) { #if IS_ENABLED(CONFIG_SYN_COOKIES) if (sk->sk_state == TCP_NEW_SYN_RECV && inet_reqsk(sk)->syncookie) { struct request_sock *req = inet_reqsk(sk); + DEBUG_NET_WARN_ON_ONCE(skb->destructor == + sock_pfree_ref); *refcounted = false; sk = req->rsk_listener; req->rsk_listener = NULL; return sk; } #endif - *refcounted = sk_is_refcounted(sk); - } else { - *refcounted = true; + *refcounted = skb->destructor == sock_pfree_ref || + sk_is_refcounted(sk); } +#endif skb->destructor = NULL; skb->sk = NULL; diff --git a/include/net/sock.h b/include/net/sock.h index 3144efad8c36..33cc361e941f 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1874,6 +1874,7 @@ void sock_efree(struct sk_buff *skb); #ifdef CONFIG_INET void sock_edemux(struct sk_buff *skb); void sock_pfree(struct sk_buff *skb); +void sock_pfree_ref(struct sk_buff *skb); static inline void skb_set_owner_edemux(struct sk_buff *skb, struct sock *sk) { @@ -3034,7 +3035,8 @@ static inline bool skb_sk_is_prefetched(struct sk_buff *skb) { #ifdef CONFIG_INET - return skb->destructor == sock_pfree; + return skb->destructor == sock_pfree || + skb->destructor == sock_pfree_ref; #else return false; #endif /* CONFIG_INET */ diff --git a/net/core/sock.c b/net/core/sock.c index ad414878dc5b..11d5d221a2fe 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -2871,6 +2871,13 @@ void sock_pfree(struct sk_buff *skb) sock_gen_put(sk); } EXPORT_SYMBOL(sock_pfree); + +void sock_pfree_ref(struct sk_buff *skb) +{ + DEBUG_NET_WARN_ON_ONCE(!sk_fullsock(skb->sk)); + sock_gen_put(skb->sk); +} +EXPORT_SYMBOL(sock_pfree_ref); #endif /* CONFIG_INET */ /* diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c index bd603e3a99a5..d920a67b6fa0 100644 --- a/net/netfilter/nf_queue.c +++ b/net/netfilter/nf_queue.c @@ -195,17 +195,15 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state, break; } - if (skb_sk_is_prefetched(skb)) { - struct sock *sk = skb->sk; - - if (!sk_is_refcounted(sk)) { - if (!refcount_inc_not_zero(&sk->sk_refcnt)) - return -ENOTCONN; +#ifdef CONFIG_INET + if (skb->destructor == sock_pfree && !sk_is_refcounted(skb->sk)) { + if (!refcount_inc_not_zero(&skb->sk->sk_refcnt)) + return -ENOTCONN; - /* drop refcount on skb_orphan */ - skb->destructor = sock_edemux; - } + /* drop refcount on skb_orphan */ + skb->destructor = sock_edemux; } +#endif entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC); if (!entry) -- 2.47.3