Netdev List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress
@ 2026-11-19 14:22 Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect() Nikolay Aleksandrov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 14:22 UTC (permalink / raw)
  To: bpf
  Cc: Nikolay Aleksandrov, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, John Fastabend,
	Stanislav Fomichev, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Joe Stringer, Paul Chaignon,
	Alasdair McWilliam, netdev

Hi,
This patch-set is based on Daniel's [1] original set that adds support for
bpf_sk_assign on egress. It is useful for the TPROXY use case which needs
to assign a listening socket (e.g. Envoy) to an skb on egress, other
potential use cases are to attribute forwarded traffic to a specific
established socket for qdisc flow classification (fq, fq_codel) or cgroup
accounting. In order to support bpf_sk_assign on SOCK_RCU_FREE sockets, a
reference is taken and a specific destructor that unconditionally releases
it is installed (sock_pfree_ref). That destructor is used throughout to
recognize such skb. For more information please see the individual commit
messages.

There are a few limitations, also noted in the commit messages,
more importantly:
 - software GSO splitting after assignment is unsupported
 - decrypted egress skbs are unsupported
 - loopback discards egress assignments

Maybe this can be applied to a feature branch on bpf-next and then pulled to
bpf-next/master and net-next/main so both trees would have the original
commits and not duplicates.

With the kind help of friendly LLMs there're 19 new selftests that exercise
various sk assign (16 egress and 3 ingress) scenarios such as:
 - egress reuseport (4 tests)
 - ingress redirects (2 tests)
 - egress redirects and drops (4 tests)
 - classic tc actions (3 tests)
 - netkit proxy delivery (2 tests)
 - netkit with NFQUEUE (4 tests)

Thank you,
 Nik

[1] https://github.com/cilium/linux/commits/pr/bpf-tproxy/

CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Emil Tsalapatis <emil@etsalapatis.com>
CC: Ihor Solodrai <ihor.solodrai@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: Stanislav Fomichev <sdf@fomichev.me>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: Joe Stringer <joe@wand.net.nz>
CC: Paul Chaignon <paul@isovalent.com>
CC: Alasdair McWilliam <alasdair.mcwilliam@isovalent.com>
CC: netdev@vger.kernel.org

Daniel Borkmann (3):
  bpf: Orphan prefetched sk on skb_do_redirect()
  bpf: allow bpf_sk_assign on tc egress with refcounted sockets
  bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress

Nikolay Aleksandrov (5):
  net: track references owned by prefetched socket assignments
  selftests/bpf: Delete assign_reuse namespace when opening it fails
  selftests/bpf: Cover egress socket assignment with reuseport
  selftests/bpf: Exercise netkit socket assignment to a host proxy
  selftests/bpf: Check assigned socket lifetime across queues and drops

 .../chelsio/inline_crypto/ch_ktls/chcr_ktls.c |   5 +-
 include/net/inet6_hashtables.h                |  10 +-
 include/net/inet_hashtables.h                 |  10 +-
 include/net/request_sock.h                    |   8 +-
 include/net/sock.h                            |   4 +-
 include/uapi/linux/bpf.h                      |   9 +-
 net/core/filter.c                             |  22 +-
 net/core/sock.c                               |  11 +
 net/netfilter/nf_queue.c                      |  16 +-
 net/sched/act_bpf.c                           |   4 +-
 net/tls/tls_device_fallback.c                 |   5 +-
 tools/include/uapi/linux/bpf.h                |   9 +-
 tools/testing/selftests/bpf/config            |   5 +
 tools/testing/selftests/bpf/netlink_helpers.c |   3 +-
 .../selftests/bpf/prog_tests/assign_reuse.c   | 625 +++++++++++++++++-
 .../bpf/prog_tests/sk_assign_netkit.c         | 586 ++++++++++++++++
 .../selftests/bpf/progs/sk_assign_lifetime.c  | 182 +++++
 .../selftests/bpf/progs/sk_assign_netkit.c    | 153 +++++
 .../selftests/bpf/progs/test_assign_reuse.c   | 174 ++++-
 tools/testing/selftests/bpf/unpriv_helpers.c  |  16 +
 tools/testing/selftests/bpf/unpriv_helpers.h  |   2 +
 21 files changed, 1794 insertions(+), 65 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c
 create mode 100644 tools/testing/selftests/bpf/progs/sk_assign_lifetime.c
 create mode 100644 tools/testing/selftests/bpf/progs/sk_assign_netkit.c

-- 
2.47.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect()
  2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
@ 2026-11-19 14:22 ` Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 2/8] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 14:22 UTC (permalink / raw)
  To: bpf
  Cc: Daniel Borkmann, Nikolay Aleksandrov, Alexei Starovoitov,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, John Fastabend,
	Stanislav Fomichev, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Joe Stringer, Paul Chaignon,
	Alasdair McWilliam, netdev

From: Daniel Borkmann <daniel@iogearbox.net>

Ingress bpf_sk_assign() can borrow an RCU-protected listener. Redirecting
to a qdisc or receive backlog can outlive that protection.

Orphan prefetched assignments before potentially queued redirects,
including peer egress. Keep synchronous peer-ingress handoff unchanged.

Fixes: 7ae215d23c12 ("bpf: Don't refcount LISTEN sockets in sk_assign()")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Emil Tsalapatis <emil@etsalapatis.com>
CC: Ihor Solodrai <ihor.solodrai@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: Stanislav Fomichev <sdf@fomichev.me>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: Joe Stringer <joe@wand.net.nz>
CC: Paul Chaignon <paul@isovalent.com>
CC: Alasdair McWilliam <alasdair.mcwilliam@isovalent.com>
CC: netdev@vger.kernel.org

 net/core/filter.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/net/core/filter.c b/net/core/filter.c
index 946b07a17fce..1ad57a371534 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2558,6 +2558,15 @@ int skb_do_redirect(struct sk_buff *skb)
 	ri->flags = 0;
 	if (unlikely(!dev))
 		goto out_drop;
+
+	/*
+	 * Prefetched sockets may borrow a listener under RCU. Orphan them
+	 * before redirects that can outlive this section. Peer ingress
+	 * stays in the same RCU section.
+	 */
+	if (skb_sk_is_prefetched(skb) &&
+	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
+		skb_orphan(skb);
 	if (flags & BPF_F_PEER) {
 		dev = skb_get_peer_dev(dev);
 		if (unlikely(!dev ||
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 2/8] net: track references owned by prefetched socket assignments
  2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect() Nikolay Aleksandrov
@ 2026-11-19 14:22 ` Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress Nikolay Aleksandrov
  3 siblings, 0 replies; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 14:22 UTC (permalink / raw)
  To: bpf
  Cc: Nikolay Aleksandrov, Paul Chaignon, Alasdair McWilliam,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Kuniyuki Iwashima, Willem de Bruijn,
	Pablo Neira Ayuso, Florian Westphal, Phil Sutter, netdev,
	netfilter-devel, coreteam, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, John Fastabend,
	Stanislav Fomichev

Add sock_pfree_ref() for skb-owned references whose release must not
depend on SOCK_RCU_FREE. Teach receive lookup, NFQUEUE and reuseport
selection to preserve and transfer this ownership for egress assignment.
This ownership bookkeeping is needed for subsequent patches that allow
bpf_sk_assign to be used in egress paths.

Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
CC: Paul Chaignon <paul@isovalent.com>
CC: Alasdair McWilliam <alasdair.mcwilliam@isovalent.com>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: Kuniyuki Iwashima <kuniyu@google.com>
CC: Willem de Bruijn <willemb@google.com>
CC: Pablo Neira Ayuso <pablo@netfilter.org>
CC: Florian Westphal <fw@strlen.de>
CC: Phil Sutter <phil@nwl.cc>
CC: netdev@vger.kernel.org
CC: netfilter-devel@vger.kernel.org
CC: coreteam@netfilter.org
CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Emil Tsalapatis <emil@etsalapatis.com>
CC: Ihor Solodrai <ihor.solodrai@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: Stanislav Fomichev <sdf@fomichev.me>

 include/net/inet6_hashtables.h | 10 +++++++---
 include/net/inet_hashtables.h  | 10 +++++++---
 include/net/request_sock.h     |  8 +++++---
 include/net/sock.h             |  4 +++-
 net/core/sock.c                |  7 +++++++
 net/netfilter/nf_queue.c       | 16 +++++++---------
 6 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h
index 2cc5d416bbb5..5a109a48cc14 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.
+	/* egress bpf assignment can hold 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 6e2fe186d0dc..787bf9e394a8 100644
--- a/include/net/inet_hashtables.h
+++ b/include/net/inet_hashtables.h
@@ -474,10 +474,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.
+	/* egress bpf assignment can hold 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..1d1204e4f754 100644
--- a/include/net/request_sock.h
+++ b/include/net/request_sock.h
@@ -103,6 +103,8 @@ 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) {
@@ -114,10 +116,10 @@ static inline struct sock *skb_steal_sock(struct sk_buff *skb,
 			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..3b1f60eaa73e 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);
+
+/* variant of sock_pfree for sockets where a refcount was explicitly taken */
+void sock_pfree_ref(struct sk_buff *skb)
+{
+	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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets
  2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect() Nikolay Aleksandrov
  2026-11-19 14:22 ` [PATCH bpf-next 2/8] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
@ 2026-11-19 14:22 ` Nikolay Aleksandrov
  2026-11-19 15:19   ` bot+bpf-ci
  2026-11-19 14:22 ` [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress Nikolay Aleksandrov
  3 siblings, 1 reply; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 14:22 UTC (permalink / raw)
  To: bpf
  Cc: Daniel Borkmann, Nikolay Aleksandrov, Ayush Sawal, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Alexei Starovoitov, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
	Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
	John Fastabend, Stanislav Fomichev, Simon Horman,
	Kuniyuki Iwashima, Willem de Bruijn, Jamal Hadi Salim, Jiri Pirko,
	Sabrina Dubroca, Paul Chaignon, Alasdair McWilliam, netdev

From: Daniel Borkmann <daniel@iogearbox.net>

bpf_sk_assign() has been limited to tc ingress because the assigned
socket may be a SOCK_RCU_FREE listener held without a refcount, relying
on the current RCU read-side section for its lifetime. That assumption
breaks on egress where the skb can sit in a qdisc across grace periods.
However, the helper is also useful on the egress path, for example to
attribute forwarded traffic to a specific established socket for qdisc
flow classification (fq, fq_codel), cgroup accounting, or SO_PRIORITY
propagation. Established sockets are refcounted and therefore safe to
pin on an skb that may be queued.

Allow TC egress to assign full refcounted sockets, taking an skb-owned
reference after orphaning any previous send-side owner. Preserve it
across redirects, classic BPF verdicts and netem delay. Reject decrypted
egress skbs to preserve the original TLS transmit owner.

Using sock_pfree_ref as a destructor encodes the refcount state so:

  - sock_pfree_ref releases the refcount on drop (unlike sock_pfree
    which skips non-refcounted sockets).
  - skb_sk_is_prefetched recognizes both destructors so ip_rcv_core
    still skips the skb_orphan on the receive side.
  - skb_steal_sock sets refcounted=true for sock_pfree_ref so the
    transport layer calls sock_put after consuming the socket.
  - skb_do_redirect only orphans sock_pfree + !sk_is_refcounted (the
    true no-refcount case from ingress), preserving sock_pfree_ref
    sockets across redirects.

The refcount_inc_not_zero cost per bpf_sk_assign call is negligible
relative to the BPF program and the sk_lookup that precedes it, and
does not affect the UDP early demux path which continues to use
sock_pfree without a refcount.

Handle reference-only ownership in TLS software fallback and Chelsio
record reconstruction without sk_wmem_alloc charges or lost references.

Software GSO splitting after assignment is unsupported, including qdisc,
device/driver/tunnel fallback and netkit peer-side NFQUEUE segmentation.
Segments can lose the selected socket; intact GSO and assignment after
segmentation remain supported. Ordinary loopback and veth peer NAPI/GRO
preparation can also discard the assignment, even without GSO.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
CC: Ayush Sawal <ayush.sawal@chelsio.com>
CC: Andrew Lunn <andrew+netdev@lunn.ch>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Emil Tsalapatis <emil@etsalapatis.com>
CC: Ihor Solodrai <ihor.solodrai@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: Stanislav Fomichev <sdf@fomichev.me>
CC: Simon Horman <horms@kernel.org>
CC: Kuniyuki Iwashima <kuniyu@google.com>
CC: Willem de Bruijn <willemb@google.com>
CC: Jamal Hadi Salim <jhs@mojatatu.com>
CC: Jiri Pirko <jiri@resnulli.us>
CC: Sabrina Dubroca <sd@queasysnail.net>
CC: Paul Chaignon <paul@isovalent.com>
CC: Alasdair McWilliam <alasdair.mcwilliam@isovalent.com>
CC: netdev@vger.kernel.org

 .../chelsio/inline_crypto/ch_ktls/chcr_ktls.c |  5 ++++-
 include/uapi/linux/bpf.h                      |  8 ++++---
 net/core/filter.c                             | 22 +++++++++++++------
 net/core/sock.c                               |  4 ++++
 net/sched/act_bpf.c                           |  4 +++-
 net/tls/tls_device_fallback.c                 |  5 +++--
 tools/include/uapi/linux/bpf.h                |  8 ++++---
 7 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c b/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c
index 4d645771596a..796497e9d3b9 100644
--- a/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c
+++ b/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c
@@ -1669,7 +1669,10 @@ static void chcr_ktls_copy_record_in_skb(struct sk_buff *nskb,
 	nskb->truesize += record->len;
 	nskb->sk = skb->sk;
 	nskb->destructor = skb->destructor;
-	refcount_add(nskb->truesize, &nskb->sk->sk_wmem_alloc);
+	if (nskb->destructor == sock_pfree_ref)
+		sock_hold(nskb->sk);
+	else
+		refcount_add(nskb->truesize, &nskb->sk->sk_wmem_alloc);
 }
 
 /*
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de0919db72..45e3018fb2ae 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4611,7 +4611,9 @@ union bpf_attr {
  *		**bpf_clone_redirect**\ () or other methods outside of BPF may
  *		interfere with successful delivery to the socket.
  *
- *		This operation is only valid from TC ingress path.
+ *		This operation is valid from TC ingress and egress paths.
+ *		On egress, only full refcounted sockets are accepted.
+ *		Software GSO segmentation after assignment is unsupported.
  *
  *		The *flags* argument must be zero.
  *	Return
@@ -4623,8 +4625,8 @@ union bpf_attr {
  *
  *		**-ENETUNREACH** if the socket is unreachable (wrong netns).
  *
- *		**-EOPNOTSUPP** if the operation is not supported, for example
- *		a call from outside of TC ingress.
+ *		**-EOPNOTSUPP** if the socket is unhashed, or TC egress uses
+ *		a non-full or non-refcounted socket or a decrypted packet.
  *
  * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
  *	Description
diff --git a/net/core/filter.c b/net/core/filter.c
index 1ad57a371534..feeb752f7a00 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2559,14 +2559,17 @@ int skb_do_redirect(struct sk_buff *skb)
 	if (unlikely(!dev))
 		goto out_drop;
 
+#ifdef CONFIG_INET
 	/*
-	 * Prefetched sockets may borrow a listener under RCU. Orphan them
-	 * before redirects that can outlive this section. Peer ingress
-	 * stays in the same RCU section.
+	 * Prefetched listeners may be borrowed under RCU. Drop borrowed
+	 * assignments before redirects that can outlive this section.
+	 * Peer ingress stays in the same RCU section.
 	 */
-	if (skb_sk_is_prefetched(skb) &&
+	if (skb->destructor == sock_pfree &&
+	    !sk_is_refcounted(skb->sk) &&
 	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
 		skb_orphan(skb);
+#endif
 	if (flags & BPF_F_PEER) {
 		dev = skb_get_peer_dev(dev);
 		if (unlikely(!dev ||
@@ -8115,21 +8118,26 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
 
 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
 {
+	bool egress = !skb_at_tc_ingress(skb);
+	bool refcounted;
+
 	if (!sk || flags != 0)
 		return -EINVAL;
-	if (!skb_at_tc_ingress(skb))
+	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
+		       skb_is_decrypted(skb)))
 		return -EOPNOTSUPP;
+	refcounted = egress || sk_is_refcounted(sk);
 	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
 		return -ENETUNREACH;
 	if (sk_unhashed(sk))
 		return -EOPNOTSUPP;
-	if (sk_is_refcounted(sk) &&
+	if (refcounted &&
 	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
 		return -ENOENT;
 
 	skb_orphan(skb);
 	skb->sk = sk;
-	skb->destructor = sock_pfree;
+	skb->destructor = egress ? sock_pfree_ref : sock_pfree;
 
 	return 0;
 }
diff --git a/net/core/sock.c b/net/core/sock.c
index 3b1f60eaa73e..f4ec58a86056 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2818,6 +2818,10 @@ static bool can_skb_orphan_partial(const struct sk_buff *skb)
  */
 void skb_orphan_partial(struct sk_buff *skb)
 {
+#ifdef CONFIG_INET
+	if (skb->destructor == sock_pfree_ref)
+		return;
+#endif
 	if (skb_is_tcp_pure_ack(skb))
 		return;
 
diff --git a/net/sched/act_bpf.c b/net/sched/act_bpf.c
index 06d8f78b7368..a0918fe9a71a 100644
--- a/net/sched/act_bpf.c
+++ b/net/sched/act_bpf.c
@@ -54,8 +54,10 @@ TC_INDIRECT_SCOPE int tcf_bpf_act(struct sk_buff *skb,
 	}
 	if (unlikely(!skb->tstamp && skb->tstamp_type))
 		skb->tstamp_type = SKB_CLOCK_REALTIME;
-	if (skb_sk_is_prefetched(skb) && filter_res != TC_ACT_OK)
+#ifdef CONFIG_INET
+	if (skb->destructor == sock_pfree && filter_res != TC_ACT_OK)
 		skb_orphan(skb);
+#endif
 
 	/* A BPF program may overwrite the default action opcode.
 	 * Similarly as in cls_bpf, if filter_res == -1 we use the
diff --git a/net/tls/tls_device_fallback.c b/net/tls/tls_device_fallback.c
index b547027cc879..1450335a94dc 100644
--- a/net/tls/tls_device_fallback.c
+++ b/net/tls/tls_device_fallback.c
@@ -194,8 +194,9 @@ static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln)
 
 	update_chksum(nskb, headln);
 
-	/* sock_efree means skb must gone through skb_orphan_partial() */
-	if (nskb->destructor == sock_efree)
+	/* these skbs hold a socket ref but aren't charged to sk_wmem_alloc */
+	if (nskb->destructor == sock_efree ||
+	    nskb->destructor == sock_pfree_ref)
 		return;
 
 	delta = nskb->truesize - skb->truesize;
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 62de0919db72..45e3018fb2ae 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -4611,7 +4611,9 @@ union bpf_attr {
  *		**bpf_clone_redirect**\ () or other methods outside of BPF may
  *		interfere with successful delivery to the socket.
  *
- *		This operation is only valid from TC ingress path.
+ *		This operation is valid from TC ingress and egress paths.
+ *		On egress, only full refcounted sockets are accepted.
+ *		Software GSO segmentation after assignment is unsupported.
  *
  *		The *flags* argument must be zero.
  *	Return
@@ -4623,8 +4625,8 @@ union bpf_attr {
  *
  *		**-ENETUNREACH** if the socket is unreachable (wrong netns).
  *
- *		**-EOPNOTSUPP** if the operation is not supported, for example
- *		a call from outside of TC ingress.
+ *		**-EOPNOTSUPP** if the socket is unhashed, or TC egress uses
+ *		a non-full or non-refcounted socket or a decrypted packet.
  *
  * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
  *	Description
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress
  2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
                   ` (2 preceding siblings ...)
  2026-11-19 14:22 ` [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets Nikolay Aleksandrov
@ 2026-11-19 14:22 ` Nikolay Aleksandrov
  3 siblings, 0 replies; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 14:22 UTC (permalink / raw)
  To: bpf
  Cc: Daniel Borkmann, Nikolay Aleksandrov, Alexei Starovoitov,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, John Fastabend,
	Stanislav Fomichev, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Kuniyuki Iwashima, Paul Chaignon,
	Alasdair McWilliam, netdev

From: Daniel Borkmann <daniel@iogearbox.net>

Accept full SOCK_RCU_FREE sockets, including listeners, on TC egress,
taking an skb-owned reference. Request and time-wait sockets remain
unsupported. The BPF TPROXY use case needs to assign a listening socket
(e.g. Envoy proxy) to an skb from tc egress. With netkit, the BPF program
runs on the container's egress side and returns NETKIT_PASS, which delivers
the skb to the host via __netif_rx -> backlog.

The fq qdisc discards listener assignments; ordinary loopback also drops
egress assignments. Both can miss the selected proxy even without GSO.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Eduard Zingerman <eddyz87@gmail.com>
CC: Kumar Kartikeya Dwivedi <memxor@gmail.com>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: Jiri Olsa <jolsa@kernel.org>
CC: Emil Tsalapatis <emil@etsalapatis.com>
CC: Ihor Solodrai <ihor.solodrai@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: Stanislav Fomichev <sdf@fomichev.me>
CC: David S. Miller <davem@davemloft.net>
CC: Eric Dumazet <edumazet@kernel.org>
CC: Jakub Kicinski <kuba@kernel.org>
CC: Paolo Abeni <pabeni@redhat.com>
CC: Simon Horman <horms@kernel.org>
CC: Kuniyuki Iwashima <kuniyu@google.com>
CC: Paul Chaignon <paul@isovalent.com>
CC: Alasdair McWilliam <alasdair.mcwilliam@isovalent.com>
CC: netdev@vger.kernel.org

 include/uapi/linux/bpf.h       | 5 +++--
 net/core/filter.c              | 3 +--
 tools/include/uapi/linux/bpf.h | 5 +++--
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 45e3018fb2ae..e8a7ebee305c 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4612,7 +4612,8 @@ union bpf_attr {
  *		interfere with successful delivery to the socket.
  *
  *		This operation is valid from TC ingress and egress paths.
- *		On egress, only full refcounted sockets are accepted.
+ *		On egress, only full sockets are accepted. Assignment takes an
+ *		skb-owned reference and releases any previous send-side owner.
  *		Software GSO segmentation after assignment is unsupported.
  *
  *		The *flags* argument must be zero.
@@ -4626,7 +4627,7 @@ union bpf_attr {
  *		**-ENETUNREACH** if the socket is unreachable (wrong netns).
  *
  *		**-EOPNOTSUPP** if the socket is unhashed, or TC egress uses
- *		a non-full or non-refcounted socket or a decrypted packet.
+ *		a non-full socket or a decrypted packet.
  *
  * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
  *	Description
diff --git a/net/core/filter.c b/net/core/filter.c
index feeb752f7a00..4dea2cc84712 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -8123,8 +8123,7 @@ BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
 
 	if (!sk || flags != 0)
 		return -EINVAL;
-	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
-		       skb_is_decrypted(skb)))
+	if (egress && (!sk_fullsock(sk) || skb_is_decrypted(skb)))
 		return -EOPNOTSUPP;
 	refcounted = egress || sk_is_refcounted(sk);
 	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 45e3018fb2ae..e8a7ebee305c 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -4612,7 +4612,8 @@ union bpf_attr {
  *		interfere with successful delivery to the socket.
  *
  *		This operation is valid from TC ingress and egress paths.
- *		On egress, only full refcounted sockets are accepted.
+ *		On egress, only full sockets are accepted. Assignment takes an
+ *		skb-owned reference and releases any previous send-side owner.
  *		Software GSO segmentation after assignment is unsupported.
  *
  *		The *flags* argument must be zero.
@@ -4626,7 +4627,7 @@ union bpf_attr {
  *		**-ENETUNREACH** if the socket is unreachable (wrong netns).
  *
  *		**-EOPNOTSUPP** if the socket is unhashed, or TC egress uses
- *		a non-full or non-refcounted socket or a decrypted packet.
+ *		a non-full socket or a decrypted packet.
  *
  * long bpf_sk_assign(struct bpf_sk_lookup *ctx, struct bpf_sock *sk, u64 flags)
  *	Description
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets
  2026-11-19 14:22 ` [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets Nikolay Aleksandrov
@ 2026-11-19 15:19   ` bot+bpf-ci
  2026-11-19 15:36     ` Nikolay Aleksandrov
  0 siblings, 1 reply; 7+ messages in thread
From: bot+bpf-ci @ 2026-11-19 15:19 UTC (permalink / raw)
  To: razor, bpf
  Cc: daniel, razor, ayush.sawal, andrew+netdev, davem, edumazet, kuba,
	pabeni, ast, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, john.fastabend, sdf,
	horms, kuniyu, willemb, jhs, jiri, sd, paul, alasdair.mcwilliam,
	netdev, ast, andrii, daniel, martin.lau, eddyz87, yonghong.song,
	mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 5812 bytes --]

> bpf: allow bpf_sk_assign on tc egress with refcounted sockets

TL;DR:
- bpf_sk_assign() on egress may hit BUG_ON in skb_orphan() for test-run skbs
  with skb->sk but no destructor
- skb_do_redirect() may keep a borrowed listener via syncookie reqsk, a
  possible use-after-free; also possible socket double-put

> diff --git a/net/core/filter.c b/net/core/filter.c
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -2559,14 +2559,17 @@ int skb_do_redirect(struct sk_buff *skb)
>  	if (unlikely(!dev))
>  		goto out_drop;
>
> +#ifdef CONFIG_INET
>  	/*
> -	 * Prefetched sockets may borrow a listener under RCU. Orphan them
> -	 * before redirects that can outlive this section. Peer ingress
> -	 * stays in the same RCU section.
> +	 * Prefetched listeners may be borrowed under RCU. Drop borrowed
> +	 * assignments before redirects that can outlive this section.
> +	 * Peer ingress stays in the same RCU section.
>  	 */
> -	if (skb_sk_is_prefetched(skb) &&
> +	if (skb->destructor == sock_pfree &&
> +	    !sk_is_refcounted(skb->sk) &&
>  	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
>  		skb_orphan(skb);
> +#endif

Can this leave a borrowed listener behind a redirect?

bpf_sk_assign_tcp_reqsk() installs a syncookie request sock whose
rsk_listener is a borrowed SOCK_RCU_FREE listener:

    req = inet_reqsk_alloc(ops, sk, false);   /* no ref on sk */
    req->rsk_listener = sk;
    skb->sk = req_to_sk(req);
    skb->destructor = sock_pfree;

skb->sk is a TCP_NEW_SYN_RECV request sock, so sk_is_refcounted(skb->sk)
is true and the new condition is false.  The skb then keeps the reqsk
across bpf_redirect(ifindex, BPF_F_INGRESS) and non-peer egress
redirects.  For example:

    sch_handle_ingress()
      skb_do_redirect()                    /* no orphan */
        __bpf_redirect_common()
          __bpf_rx_skb()
            dev_forward_skb_nomtu()
              netif_rx_internal()          /* queued past the RCU section */

Later tcp_v4_rcv() -> __inet_lookup_skb() -> inet_steal_sock() ->
skb_steal_sock() takes the syncookie branch:

    *refcounted = false;
    sk = req->rsk_listener;
    req->rsk_listener = NULL;
    return sk;

and inet_steal_sock() dereferences that listener.  If the listener was
closed and its SOCK_RCU_FREE grace period elapsed while the skb sat in
the backlog or a qdisc, is this a use-after-free?

The updated comment says borrowed listener assignments are dropped, and the
commit message describes "sock_pfree + !sk_is_refcounted" as "the true
no-refcount case from ingress", but the reqsk case above is not covered.

Separately, refcounted sock_pfree assignments made at ingress now survive
non-peer redirects into egress.  complete_skb() and
chcr_ktls_copy_record_in_skb() treat any destructor other than
sock_efree/sock_pfree_ref as charged to sk_wmem_alloc, and chcr copies
sock_pfree to nskb without sock_hold().  Would that double-put the socket
when both skbs are freed?  The commit message says only sock_pfree_ref
sockets are meant to be preserved; would testing
skb->destructor == sock_pfree alone, as before, keep that and close both
problems?

[ ... ]

> @@ -8115,21 +8118,26 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
>
>  BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
>  {
> +	bool egress = !skb_at_tc_ingress(skb);
> +	bool refcounted;
> +
>  	if (!sk || flags != 0)
>  		return -EINVAL;
> -	if (!skb_at_tc_ingress(skb))
> +	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
> +		       skb_is_decrypted(skb)))
>  		return -EOPNOTSUPP;
> +	refcounted = egress || sk_is_refcounted(sk);
>  	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
>  		return -ENETUNREACH;
>  	if (sk_unhashed(sk))
>  		return -EOPNOTSUPP;
> -	if (sk_is_refcounted(sk) &&
> +	if (refcounted &&
>  	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
>  		return -ENOENT;
>
>  	skb_orphan(skb);
>  	skb->sk = sk;
> -	skb->destructor = sock_pfree;
> +	skb->destructor = egress ? sock_pfree_ref : sock_pfree;

Can skb_orphan() hit BUG_ON(skb->sk) here?

Removing the !skb_at_tc_ingress() rejection lets every non-ingress skb
reach skb_orphan(), including skbs that have skb->sk set and no
destructor.  bpf_prog_test_run_skb() builds exactly such an skb:

    sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
    skb = slab_build_skb(data);
    skb->sk = sk;          /* no destructor */

tc_at_ingress is zero there and convert___skb_to_skb() never sets it, so
skb_at_tc_ingress() is false and the new egress branch is taken.  A tc
program that does bpf_sk_lookup_tcp(skb, &tuple, ..., BPF_F_CURRENT_NETNS,
0) on an established socket and then bpf_sk_assign(skb, sk, 0) passes
sk_fullsock(), sk_is_refcounted(), skb_is_decrypted(), the netns check and
sk_unhashed(), and refcount_inc_not_zero_acquire() succeeds.  Then:

    static __always_inline void skb_orphan(struct sk_buff *skb)
    {
        if (skb->destructor) {
            ...
        } else {
            BUG_ON(skb->sk);   /* skb->sk == dummy sk */
        }
    }

Before this patch the same program got -EOPNOTSUPP and never reached
skb_orphan().  The selftest added later in the series (probe_timewait in
sk_assign_netkit) goes through bpf_prog_test_run_opts() but only avoids
the BUG because its socket is in TIME_WAIT and fails sk_fullsock().  A
later patch in the series that accepts listeners does not add a guard
either.

Could the egress branch reject skbs that have skb->sk set but no
destructor (or the test-run context) before calling skb_orphan()?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44548126067

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets
  2026-11-19 15:19   ` bot+bpf-ci
@ 2026-11-19 15:36     ` Nikolay Aleksandrov
  0 siblings, 0 replies; 7+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-19 15:36 UTC (permalink / raw)
  To: bot+bpf-ci, bpf
  Cc: daniel, ayush.sawal, andrew+netdev, davem, edumazet, kuba, pabeni,
	ast, andrii, eddyz87, memxor, martin.lau, song, yonghong.song,
	jolsa, emil, ihor.solodrai, john.fastabend, sdf, horms, kuniyu,
	willemb, jhs, jiri, sd, paul, alasdair.mcwilliam, netdev,
	martin.lau, mason

On 19/11/2026 17:19, bot+bpf-ci@kernel.org wrote:
>> bpf: allow bpf_sk_assign on tc egress with refcounted sockets
> 
> TL;DR:
> - bpf_sk_assign() on egress may hit BUG_ON in skb_orphan() for test-run skbs
>    with skb->sk but no destructor
> - skb_do_redirect() may keep a borrowed listener via syncookie reqsk, a
>    possible use-after-free; also possible socket double-put
> 

oh well, both are good and valid catches, I will take care of them for v2
my local Sashiko run missed these too

Thanks,
  Nik

>> diff --git a/net/core/filter.c b/net/core/filter.c
>> --- a/net/core/filter.c
>> +++ b/net/core/filter.c
>> @@ -2559,14 +2559,17 @@ int skb_do_redirect(struct sk_buff *skb)
>>   	if (unlikely(!dev))
>>   		goto out_drop;
>>
>> +#ifdef CONFIG_INET
>>   	/*
>> -	 * Prefetched sockets may borrow a listener under RCU. Orphan them
>> -	 * before redirects that can outlive this section. Peer ingress
>> -	 * stays in the same RCU section.
>> +	 * Prefetched listeners may be borrowed under RCU. Drop borrowed
>> +	 * assignments before redirects that can outlive this section.
>> +	 * Peer ingress stays in the same RCU section.
>>   	 */
>> -	if (skb_sk_is_prefetched(skb) &&
>> +	if (skb->destructor == sock_pfree &&
>> +	    !sk_is_refcounted(skb->sk) &&
>>   	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
>>   		skb_orphan(skb);
>> +#endif
> 
> Can this leave a borrowed listener behind a redirect?
> 
> bpf_sk_assign_tcp_reqsk() installs a syncookie request sock whose
> rsk_listener is a borrowed SOCK_RCU_FREE listener:
> 
>      req = inet_reqsk_alloc(ops, sk, false);   /* no ref on sk */
>      req->rsk_listener = sk;
>      skb->sk = req_to_sk(req);
>      skb->destructor = sock_pfree;
> 
> skb->sk is a TCP_NEW_SYN_RECV request sock, so sk_is_refcounted(skb->sk)
> is true and the new condition is false.  The skb then keeps the reqsk
> across bpf_redirect(ifindex, BPF_F_INGRESS) and non-peer egress
> redirects.  For example:
> 
>      sch_handle_ingress()
>        skb_do_redirect()                    /* no orphan */
>          __bpf_redirect_common()
>            __bpf_rx_skb()
>              dev_forward_skb_nomtu()
>                netif_rx_internal()          /* queued past the RCU section */
> 
> Later tcp_v4_rcv() -> __inet_lookup_skb() -> inet_steal_sock() ->
> skb_steal_sock() takes the syncookie branch:
> 
>      *refcounted = false;
>      sk = req->rsk_listener;
>      req->rsk_listener = NULL;
>      return sk;
> 
> and inet_steal_sock() dereferences that listener.  If the listener was
> closed and its SOCK_RCU_FREE grace period elapsed while the skb sat in
> the backlog or a qdisc, is this a use-after-free?
> 
> The updated comment says borrowed listener assignments are dropped, and the
> commit message describes "sock_pfree + !sk_is_refcounted" as "the true
> no-refcount case from ingress", but the reqsk case above is not covered.
> 
> Separately, refcounted sock_pfree assignments made at ingress now survive
> non-peer redirects into egress.  complete_skb() and
> chcr_ktls_copy_record_in_skb() treat any destructor other than
> sock_efree/sock_pfree_ref as charged to sk_wmem_alloc, and chcr copies
> sock_pfree to nskb without sock_hold().  Would that double-put the socket
> when both skbs are freed?  The commit message says only sock_pfree_ref
> sockets are meant to be preserved; would testing
> skb->destructor == sock_pfree alone, as before, keep that and close both
> problems?
> 
> [ ... ]
> 
>> @@ -8115,21 +8118,26 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
>>
>>   BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
>>   {
>> +	bool egress = !skb_at_tc_ingress(skb);
>> +	bool refcounted;
>> +
>>   	if (!sk || flags != 0)
>>   		return -EINVAL;
>> -	if (!skb_at_tc_ingress(skb))
>> +	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
>> +		       skb_is_decrypted(skb)))
>>   		return -EOPNOTSUPP;
>> +	refcounted = egress || sk_is_refcounted(sk);
>>   	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
>>   		return -ENETUNREACH;
>>   	if (sk_unhashed(sk))
>>   		return -EOPNOTSUPP;
>> -	if (sk_is_refcounted(sk) &&
>> +	if (refcounted &&
>>   	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
>>   		return -ENOENT;
>>
>>   	skb_orphan(skb);
>>   	skb->sk = sk;
>> -	skb->destructor = sock_pfree;
>> +	skb->destructor = egress ? sock_pfree_ref : sock_pfree;
> 
> Can skb_orphan() hit BUG_ON(skb->sk) here?
> 
> Removing the !skb_at_tc_ingress() rejection lets every non-ingress skb
> reach skb_orphan(), including skbs that have skb->sk set and no
> destructor.  bpf_prog_test_run_skb() builds exactly such an skb:
> 
>      sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
>      skb = slab_build_skb(data);
>      skb->sk = sk;          /* no destructor */
> 
> tc_at_ingress is zero there and convert___skb_to_skb() never sets it, so
> skb_at_tc_ingress() is false and the new egress branch is taken.  A tc
> program that does bpf_sk_lookup_tcp(skb, &tuple, ..., BPF_F_CURRENT_NETNS,
> 0) on an established socket and then bpf_sk_assign(skb, sk, 0) passes
> sk_fullsock(), sk_is_refcounted(), skb_is_decrypted(), the netns check and
> sk_unhashed(), and refcount_inc_not_zero_acquire() succeeds.  Then:
> 
>      static __always_inline void skb_orphan(struct sk_buff *skb)
>      {
>          if (skb->destructor) {
>              ...
>          } else {
>              BUG_ON(skb->sk);   /* skb->sk == dummy sk */
>          }
>      }
> 
> Before this patch the same program got -EOPNOTSUPP and never reached
> skb_orphan().  The selftest added later in the series (probe_timewait in
> sk_assign_netkit) goes through bpf_prog_test_run_opts() but only avoids
> the BUG because its socket is in TIME_WAIT and fails sk_fullsock().  A
> later patch in the series that accepts listeners does not add a guard
> either.
> 
> Could the egress branch reject skbs that have skb->sk set but no
> destructor (or the test-run context) before calling skb_orphan()?
> 
> 
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
> 
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44548126067


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-19 15:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 14:22 [PATCH bpf-next 0/8] bpf: add bpf_sk_assign support on egress Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 1/8] bpf: Orphan prefetched sk on skb_do_redirect() Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 2/8] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets Nikolay Aleksandrov
2026-11-19 15:19   ` bot+bpf-ci
2026-11-19 15:36     ` Nikolay Aleksandrov
2026-11-19 14:22 ` [PATCH bpf-next 4/8] bpf: support SOCK_RCU_FREE sockets in bpf_sk_assign on egress Nikolay Aleksandrov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox