* [PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
@ 2026-11-24 15:12 ` Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 2/4] bpf: preserve owned socket assignments on ingress redirects Nikolay Aleksandrov
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-24 15:12 UTC (permalink / raw)
To: bpf
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
Alexei Starovoitov, Daniel Borkmann, Stanislav Fomichev,
Nikolay Aleksandrov
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 <razor@blackwall.org>
---
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
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH RFC bpf-next 2/4] bpf: preserve owned socket assignments on ingress redirects
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
@ 2026-11-24 15:12 ` Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path Nikolay Aleksandrov
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-24 15:12 UTC (permalink / raw)
To: bpf
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
Alexei Starovoitov, Daniel Borkmann, Stanislav Fomichev,
Nikolay Aleksandrov
Ingress assignments can borrow an RCU-protected listener, directly or
through a syncookie request. Redirects can queue the skb beyond that
protection. Peer redirects also cross into another network namespace.
Keep an assignment on a redirect from TC ingress to ingress in the same
namespace when the skb already owns a full socket reference. Acquire a
reference for a borrowed full socket before queuing it and use
sock_pfree_ref to release that reference. If the reference cannot be
acquired or the socket is a request socket, orphan the assignment.
Orphan assignments on peer and egress redirects.
Fixes: 7ae215d23c12 ("bpf: Don't refcount LISTEN sockets in sk_assign()")
Fixes: 9aa1206e8f48 ("bpf: Add redirect_peer helper")
Fixes: e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
net/core/filter.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/net/core/filter.c b/net/core/filter.c
index 53c042b267ab..3206b40887c5 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2546,6 +2546,41 @@ static struct net_device *skb_get_peer_dev(struct net_device *dev)
return NULL;
}
+#if IS_ENABLED(CONFIG_INET)
+static void skb_redirect_prefetched_sk(struct sk_buff *skb,
+ const struct net_device *dev, u32 flags)
+{
+ struct sock *sk;
+
+ if (!skb_sk_is_prefetched(skb))
+ return;
+
+ sk = skb->sk;
+ /*
+ * tc ingress redirects to ingress in the same namespace keep a
+ * prefetched socket if the skb owns a reference or can acquire one.
+ * Other redirects and request sockets lose the assignment.
+ */
+ if (!skb_at_tc_ingress(skb) || flags != BPF_F_INGRESS ||
+ !net_eq(sock_net(sk), dev_net(dev)) || !sk_fullsock(sk))
+ goto orphan;
+ if (skb->destructor == sock_pfree_ref || sk_is_refcounted(sk))
+ return;
+ if (refcount_inc_not_zero_acquire(&sk->sk_refcnt)) {
+ skb->destructor = sock_pfree_ref;
+ return;
+ }
+
+orphan:
+ skb_orphan(skb);
+}
+#else
+static inline void skb_redirect_prefetched_sk(struct sk_buff *skb,
+ const struct net_device *dev, u32 flags)
+{
+}
+#endif
+
int skb_do_redirect(struct sk_buff *skb)
{
struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
@@ -2558,6 +2593,8 @@ int skb_do_redirect(struct sk_buff *skb)
ri->flags = 0;
if (unlikely(!dev))
goto out_drop;
+
+ skb_redirect_prefetched_sk(skb, dev, flags);
if (flags & BPF_F_PEER) {
dev = skb_get_peer_dev(dev);
if (unlikely(!dev ||
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 1/4] net: track references owned by prefetched socket assignments Nikolay Aleksandrov
2026-11-24 15:12 ` [PATCH RFC bpf-next 2/4] bpf: preserve owned socket assignments on ingress redirects Nikolay Aleksandrov
@ 2026-11-24 15:12 ` Nikolay Aleksandrov
2026-11-24 15:59 ` sashiko-bot
2026-11-24 15:12 ` [PATCH RFC bpf-next 4/4] selftests/bpf: test netkit socket assignment to a transparent proxy Nikolay Aleksandrov
2026-11-24 21:02 ` [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Alexei Starovoitov
4 siblings, 1 reply; 8+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-24 15:12 UTC (permalink / raw)
To: bpf
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
Alexei Starovoitov, Daniel Borkmann, Stanislav Fomichev,
Nikolay Aleksandrov
Let netkit programs assign a socket in the receiving network namespace,
for example a transparent proxy listener on the host for traffic coming
from a pod.
A netkit program runs with skb->dev already set to the peer and on
NETKIT_PASS the skb goes straight to __netif_rx() on that peer. The
assigned skb therefore doesn't travel the tx path of the sending side, so
transmit code doesn't need to understand the new ownership.
General tc egress assignment stays unsupported.
netkit records the skb it is running programs for in the BPF net
context and the helper only takes the netkit path for that skb.
A clone made with bpf_clone_redirect(), test runs, cls_bpf and act_bpf on
egress still get -EOPNOTSUPP.
The backlog queue outlives the RCU section so the skb takes a reference
with sock_pfree_ref even for SOCK_RCU_FREE sockets such as tcp
listeners.
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
drivers/net/netkit.c | 10 ++++++++++
include/linux/filter.h | 4 ++++
include/net/netkit.h | 15 +++++++++++++++
include/uapi/linux/bpf.h | 9 +++++++--
net/core/filter.c | 13 ++++++++++---
tools/include/uapi/linux/bpf.h | 9 +++++++--
6 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c
index 75a1a561e5aa..d32d98cad6f6 100644
--- a/drivers/net/netkit.c
+++ b/drivers/net/netkit.c
@@ -53,15 +53,25 @@ static __always_inline int
netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
enum netkit_action ret)
{
+ struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
const struct bpf_mprog_fp *fp;
const struct bpf_prog *prog;
+ struct sk_buff *prev = NULL;
+ if (bpf_net_ctx->ri.kern_flags & BPF_RI_F_NETKIT_SK_ASSIGN)
+ prev = bpf_net_ctx->sk_assign_skb;
+ bpf_net_ctx->sk_assign_skb = skb;
+ bpf_net_ctx->ri.kern_flags |= BPF_RI_F_NETKIT_SK_ASSIGN;
bpf_mprog_foreach_prog(entry, fp, prog) {
bpf_compute_data_pointers(skb);
ret = bpf_prog_run(prog, skb);
if (ret != NETKIT_NEXT)
break;
}
+ if (prev)
+ bpf_net_ctx->sk_assign_skb = prev;
+ else
+ bpf_net_ctx->ri.kern_flags &= ~BPF_RI_F_NETKIT_SK_ASSIGN;
return ret;
}
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 61448be53f8d..ddc4e6912e0c 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -981,6 +981,7 @@ struct bpf_nh_params {
#define BPF_RI_F_CPU_MAP_INIT BIT(2)
#define BPF_RI_F_DEV_MAP_INIT BIT(3)
#define BPF_RI_F_XSK_MAP_INIT BIT(4)
+#define BPF_RI_F_NETKIT_SK_ASSIGN BIT(5)
struct bpf_redirect_info {
u64 tgt_index;
@@ -998,6 +999,9 @@ struct bpf_net_context {
struct list_head cpu_map_flush_list;
struct list_head dev_map_flush_list;
struct list_head xskmap_map_flush_list;
+#ifdef CONFIG_NETKIT
+ struct sk_buff *sk_assign_skb;
+#endif
};
static inline struct bpf_net_context *bpf_net_ctx_set(struct bpf_net_context *bpf_net_ctx)
diff --git a/include/net/netkit.h b/include/net/netkit.h
index 9ec0163739f4..70992f145a63 100644
--- a/include/net/netkit.h
+++ b/include/net/netkit.h
@@ -4,6 +4,7 @@
#define __NET_NETKIT_H
#include <linux/bpf.h>
+#include <linux/filter.h>
#ifdef CONFIG_NETKIT
int netkit_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog);
@@ -11,6 +12,15 @@ int netkit_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
int netkit_prog_detach(const union bpf_attr *attr, struct bpf_prog *prog);
int netkit_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr);
INDIRECT_CALLABLE_DECLARE(struct net_device *netkit_peer_dev(struct net_device *dev));
+
+static inline bool netkit_runs_skb(const struct sk_buff *skb)
+{
+ struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
+
+ return bpf_net_ctx &&
+ (bpf_net_ctx->ri.kern_flags & BPF_RI_F_NETKIT_SK_ASSIGN) &&
+ bpf_net_ctx->sk_assign_skb == skb;
+}
#else
static inline int netkit_prog_attach(const union bpf_attr *attr,
struct bpf_prog *prog)
@@ -40,5 +50,10 @@ static inline struct net_device *netkit_peer_dev(struct net_device *dev)
{
return NULL;
}
+
+static inline bool netkit_runs_skb(const struct sk_buff *skb)
+{
+ return false;
+}
#endif /* CONFIG_NETKIT */
#endif /* __NET_NETKIT_H */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de0919db72..25f89f3cca1e 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -4611,7 +4611,10 @@ 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 from netkit
+ * programs. In netkit programs, only full TCP and UDP sockets
+ * are accepted, and the *skb* then holds a reference on *sk*
+ * until the receive path consumes it or the *skb* is freed.
*
* The *flags* argument must be zero.
* Return
@@ -4624,7 +4627,9 @@ 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.
+ * a call from outside of TC ingress or netkit programs, or, in a
+ * netkit program, a socket other than a full TCP or UDP socket or
+ * an *skb* that already carries a socket it does not own.
*
* 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 3206b40887c5..a8e7b46bd648 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -8132,21 +8132,28 @@ 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 from_netkit = netkit_runs_skb(skb);
+
if (!sk || flags != 0)
return -EINVAL;
- if (!skb_at_tc_ingress(skb))
+ if (from_netkit) {
+ if (!sk_fullsock(sk) || (!sk_is_tcp(sk) && !sk_is_udp(sk)) ||
+ (skb->sk && !skb->destructor))
+ return -EOPNOTSUPP;
+ } else if (!skb_at_tc_ingress(skb)) {
return -EOPNOTSUPP;
+ }
if (unlikely(dev_net(skb->dev) != sock_net(sk)))
return -ENETUNREACH;
if (sk_unhashed(sk))
return -EOPNOTSUPP;
- if (sk_is_refcounted(sk) &&
+ if ((from_netkit || sk_is_refcounted(sk)) &&
unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
return -ENOENT;
skb_orphan(skb);
skb->sk = sk;
- skb->destructor = sock_pfree;
+ skb->destructor = from_netkit ? sock_pfree_ref : sock_pfree;
return 0;
}
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 62de0919db72..25f89f3cca1e 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -4611,7 +4611,10 @@ 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 from netkit
+ * programs. In netkit programs, only full TCP and UDP sockets
+ * are accepted, and the *skb* then holds a reference on *sk*
+ * until the receive path consumes it or the *skb* is freed.
*
* The *flags* argument must be zero.
* Return
@@ -4624,7 +4627,9 @@ 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.
+ * a call from outside of TC ingress or netkit programs, or, in a
+ * netkit program, a socket other than a full TCP or UDP socket or
+ * an *skb* that already carries a socket it does not own.
*
* 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] 8+ messages in thread* Re: [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
2026-11-24 15:12 ` [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path Nikolay Aleksandrov
@ 2026-11-24 15:59 ` sashiko-bot
2026-11-24 16:04 ` Nikolay Aleksandrov
0 siblings, 1 reply; 8+ messages in thread
From: sashiko-bot @ 2026-11-24 15:59 UTC (permalink / raw)
To: Nikolay Aleksandrov, bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: missing L3/L4 protocol validation in bpf_sk_assign() and
inet_steal_sock()
- [High] bpf: prefetched socket in netkit_xmit() not orphaned on forward
or xnet
--
commit 7a81b1ade7d996de1fa2508363693501dc7fc20e
Author: Nikolay Aleksandrov <razor@blackwall.org>
bpf: support bpf_sk_assign in netkit tx path
This commit enables bpf_sk_assign() for BPF programs running in the netkit
transmit path by tracking the active skb in bpf_net_context and taking a
reference on the assigned socket via sock_pfree_ref.
> diff --git a/drivers/net/netkit.c b/drivers/net/netkit.c
> index 75a1a561e5aab..d32d98cad6f65 100644
> --- a/drivers/net/netkit.c
> +++ b/drivers/net/netkit.c
> @@ -53,15 +53,25 @@ static __always_inline int
> netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
> enum netkit_action ret)
> {
> + struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
> const struct bpf_mprog_fp *fp;
> const struct bpf_prog *prog;
> + struct sk_buff *prev = NULL;
>
> + if (bpf_net_ctx->ri.kern_flags & BPF_RI_F_NETKIT_SK_ASSIGN)
> + prev = bpf_net_ctx->sk_assign_skb;
> + bpf_net_ctx->sk_assign_skb = skb;
> + bpf_net_ctx->ri.kern_flags |= BPF_RI_F_NETKIT_SK_ASSIGN;
> bpf_mprog_foreach_prog(entry, fp, prog) {
> bpf_compute_data_pointers(skb);
> ret = bpf_prog_run(prog, skb);
> if (ret != NETKIT_NEXT)
> break;
> }
> + if (prev)
> + bpf_net_ctx->sk_assign_skb = prev;
> + else
> + bpf_net_ctx->ri.kern_flags &= ~BPF_RI_F_NETKIT_SK_ASSIGN;
> return ret;
> }
[Severity: High]
What happens if an skb with a prefetched socket is forwarded before
reaching the peer's local IP receive path, or enters netkit_xmit()
already carrying a prefetched socket?
On NETKIT_PASS, __netif_rx(skb) feeds __netif_receive_skb_core() on the
peer, which runs generic XDP, TC ingress (such as act_mirred or
sch_frag), VLANs, and rx_handlers (such as bridge or macvlan) before
ip_rcv(). Neither skb_scrub_packet(), __dev_queue_xmit(), nor
netkit_prep_forward() orphans a prefetched socket.
If the peer forwards the skb into another network namespace (for example
via a macvlan, VLAN, or act_mirred), inet_steal_sock() and
inet6_steal_sock() steal skb->sk without checking net_eq(sock_net(sk),
net), delivering the packet to a socket in the wrong namespace. If a
frag_list skb carrying a prefetched socket reaches ip_do_fragment() or
ip6_fragment() (via bridge netfilter or sch_frag), fragmentation sets
frag->destructor = sock_wfree without charging sk_wmem_alloc,
underflowing sk_wmem_alloc and freeing sk while still in use. And if TC
ingress assigns an unreferenced sock_pfree socket and forwards the skb
into netkit_xmit(), __netif_rx() queues it to the backlog past the RCU
read-side critical section, causing a use-after-free.
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 3206b40887c5e..a8e7b46bd648e 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -8132,21 +8132,28 @@ 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 from_netkit = netkit_runs_skb(skb);
> +
> if (!sk || flags != 0)
> return -EINVAL;
> - if (!skb_at_tc_ingress(skb))
> + if (from_netkit) {
> + if (!sk_fullsock(sk) || (!sk_is_tcp(sk) && !sk_is_udp(sk)) ||
> + (skb->sk && !skb->destructor))
> + return -EOPNOTSUPP;
> + } else if (!skb_at_tc_ingress(skb)) {
> return -EOPNOTSUPP;
> + }
[Severity: High]
Can bpf_sk_assign() attach a socket whose address family or L4 protocol
does not match the packet in skb?
The from_netkit check allows any full TCP or UDP socket (AF_INET or
AF_INET6), and when the packet enters ip_rcv() or ipv6_rcv() on the peer,
the IP layer dispatches to tcp_v4_rcv(), udp_rcv(), tcp_v6_rcv(), or
udpv6_rcv() based on the packet header. Those handlers call
inet_steal_sock() or inet6_steal_sock(), which return the prefetched
fullsock without checking sk_family or sk_protocol:
include/net/inet6_hashtables.h:inet6_steal_sock() {
...
sk = skb_steal_sock(skb, refcounted, &prefetched);
if (!sk)
return NULL;
if (!prefetched || !sk_fullsock(sk))
return sk;
...
}
If a netkit program assigns an AF_INET TCP socket or a UDP socket to an
IPv6 TCP packet, tcp_v6_rcv() -> tcp_v6_do_rcv() steals it and computes
tcp_inet6_sk(sk), causing type confusion and out-of-bounds slab accesses.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124151244.1423343-1-razor@blackwall.org?part=3
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
2026-11-24 15:59 ` sashiko-bot
@ 2026-11-24 16:04 ` Nikolay Aleksandrov
0 siblings, 0 replies; 8+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-24 16:04 UTC (permalink / raw)
To: sashiko-reviews, bpf
On 24/11/2026 17:59, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] bpf: missing L3/L4 protocol validation in bpf_sk_assign() and
> inet_steal_sock()
> - [High] bpf: prefetched socket in netkit_xmit() not orphaned on forward
> or xnet
> --
[snip]
Both are pre-existing and noted in the cover letter. They're not introduced by
this set.
Cheers,
Nik
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH RFC bpf-next 4/4] selftests/bpf: test netkit socket assignment to a transparent proxy
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
` (2 preceding siblings ...)
2026-11-24 15:12 ` [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path Nikolay Aleksandrov
@ 2026-11-24 15:12 ` Nikolay Aleksandrov
2026-11-24 21:02 ` [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Alexei Starovoitov
4 siblings, 0 replies; 8+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-24 15:12 UTC (permalink / raw)
To: bpf
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
Alexei Starovoitov, Daniel Borkmann, Stanislav Fomichev,
Nikolay Aleksandrov
Attach a netkit peer program that assigns TCP SYNs and UDP datagrams for
a non-local service address to a transparent host socket on another
port. Check for IPv4 and IPv6 that the datagram reaches the proxy socket
and that the connection is accepted with the original destination. Only
the SYN is assigned, the rest of a TCP connection uses the normal
lookup, so no TCP data is exchanged.
Clone each assigned packet to the host netkit device while the netkit
program is still running. A tcx egress program on that device tries to
assign the proxy socket to the clone and checks for -EOPNOTSUPP. The
clone has no socket, so if the netkit gate accepted any skb while the
netkit program runs, the helper would return 0. Skip the subtests when
the kernel does not provide netkit.
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
.../bpf/prog_tests/sk_assign_netkit.c | 189 ++++++++++++++++++
.../selftests/bpf/progs/sk_assign_netkit.c | 95 +++++++++
2 files changed, 284 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c
create mode 100644 tools/testing/selftests/bpf/progs/sk_assign_netkit.c
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c b/tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c
new file mode 100644
index 000000000000..59dc204318ca
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <uapi/linux/if_link.h>
+#include <net/if.h>
+#include <test_progs.h>
+#include "network_helpers.h"
+#include "netlink_helpers.h"
+#include "sk_assign_netkit.skel.h"
+
+#define NS_HOST "sk_assign_host"
+#define NS_POD "sk_assign_pod"
+#define HOST_DEV "nk_host"
+#define POD_DEV "nk0"
+#define SERVICE_ADDR4 "198.18.0.99"
+#define SERVICE_ADDR6 "2001:db8:99::99"
+#define SERVICE_PORT 18080
+#define PROXY_PORT 15001
+#define TEST_DATA "test"
+#define IO_TIMEOUT_MS 1000
+
+struct iplink_req {
+ struct nlmsghdr n;
+ struct ifinfomsg i;
+ char buf[512];
+};
+
+static int transparent_socket(int fd, void *opts)
+{
+ int one = 1;
+
+ return setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one));
+}
+
+static int create_netkit(void)
+{
+ struct rtnl_handle rth = { .fd = -1 };
+ struct rtattr *linkinfo, *data;
+ struct iplink_req req = {};
+ int err;
+
+ err = rtnl_open(&rth, 0);
+ if (err)
+ return err;
+
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
+ req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
+ req.n.nlmsg_type = RTM_NEWLINK;
+ addattrstrz(&req.n, sizeof(req), IFLA_IFNAME, HOST_DEV);
+ linkinfo = addattr_nest(&req.n, sizeof(req), IFLA_LINKINFO);
+ addattrstrz(&req.n, sizeof(req), IFLA_INFO_KIND, "netkit");
+ data = addattr_nest(&req.n, sizeof(req), IFLA_INFO_DATA);
+ addattr32(&req.n, sizeof(req), IFLA_NETKIT_MODE, NETKIT_L2);
+ addattr_nest_end(&req.n, data);
+ addattr_nest_end(&req.n, linkinfo);
+
+ errno = 0;
+ err = rtnl_talk(&rth, &req.n, NULL);
+ if (err && errno)
+ err = -errno;
+ rtnl_close(&rth);
+ return err;
+}
+
+static bool setup_netkit(int family, const char *dest)
+{
+ const char *host = family == AF_INET ? "10.88.0.1" : "fd88::1";
+ const char *pod = family == AF_INET ? "10.88.0.2" : "fd88::2";
+ const char *dad = family == AF_INET6 ? "nodad" : "";
+ int prefix = family == AF_INET ? 24 : 64;
+
+ SYS(fail, "ip link set %s netns %s", POD_DEV, NS_POD);
+ SYS(fail, "ip link set %s up", HOST_DEV);
+ SYS(fail, "ip -n %s link set %s up", NS_POD, POD_DEV);
+ SYS(fail, "ip addr add %s/%d dev %s %s", host, prefix, HOST_DEV, dad);
+ SYS(fail, "ip -n %s addr add %s/%d dev %s %s", NS_POD, pod, prefix,
+ POD_DEV, dad);
+ SYS(fail, "ip -n %s route add default via %s", NS_POD, host);
+ SYS(fail, "ip route add local %s dev lo", dest);
+ return true;
+fail:
+ return false;
+}
+
+static void run_netkit(int family, int type)
+{
+ static const struct network_helper_opts opts = {
+ .timeout_ms = IO_TIMEOUT_MS,
+ .post_socket_cb = transparent_socket,
+ };
+ const char *dest = family == AF_INET ? SERVICE_ADDR4 : SERVICE_ADDR6;
+ int server = -1, client = -1, accepted = -1;
+ struct netns_obj *host = NULL, *pod = NULL;
+ struct sk_assign_netkit *skel = NULL;
+ struct sockaddr_storage expected;
+ unsigned int ifindex;
+ struct nstoken *tok;
+ socklen_t len;
+ int err;
+
+ host = netns_new(NS_HOST, true);
+ if (!ASSERT_OK_PTR(host, "host_netns"))
+ return;
+ pod = netns_new(NS_POD, false);
+ if (!ASSERT_OK_PTR(pod, "pod_netns"))
+ goto cleanup;
+ err = create_netkit();
+ if (err == -EOPNOTSUPP || err == -ENODEV) {
+ printf("%s:SKIP: CONFIG_NETKIT is unavailable\n", __func__);
+ test__skip();
+ goto cleanup;
+ }
+ if (!ASSERT_OK(err, "create_netkit") || !setup_netkit(family, dest))
+ goto cleanup;
+
+ server = start_server_str(family, type, NULL, PROXY_PORT, &opts);
+ if (!ASSERT_OK_FD(server, "start_server"))
+ goto cleanup;
+ skel = sk_assign_netkit__open();
+ if (!ASSERT_OK_PTR(skel, "skel_open"))
+ goto cleanup;
+ skel->rodata->service_port = SERVICE_PORT;
+ skel->rodata->proxy_port = PROXY_PORT;
+ if (!ASSERT_OK(sk_assign_netkit__load(skel), "skel_load"))
+ goto cleanup;
+ ifindex = if_nametoindex(HOST_DEV);
+ skel->links.assign_proxy = bpf_program__attach_netkit(
+ skel->progs.assign_proxy, ifindex, NULL);
+ if (!ASSERT_OK_PTR(skel->links.assign_proxy, "attach_netkit"))
+ goto cleanup;
+ skel->links.egress_assign = bpf_program__attach_tcx(
+ skel->progs.egress_assign, ifindex, NULL);
+ if (!ASSERT_OK_PTR(skel->links.egress_assign, "attach_tcx_egress"))
+ goto cleanup;
+ if (!ASSERT_OK(make_sockaddr(family, dest, SERVICE_PORT, &expected,
+ &len), "make_sockaddr"))
+ goto cleanup;
+
+ tok = open_netns(NS_POD);
+ if (!ASSERT_OK_PTR(tok, "open_pod"))
+ goto cleanup;
+ client = connect_to_addr(type, &expected, len, NULL);
+ close_netns(tok);
+ if (!ASSERT_OK_FD(client, "connect_client"))
+ goto cleanup;
+ if (type == SOCK_DGRAM) {
+ char buf[sizeof(TEST_DATA) - 1];
+
+ if (!ASSERT_EQ(send(client, TEST_DATA, sizeof(buf), 0),
+ sizeof(buf), "send_client") ||
+ !ASSERT_EQ(recv(server, buf, sizeof(buf), 0),
+ sizeof(buf), "recv_server") ||
+ !ASSERT_MEMEQ(buf, TEST_DATA, sizeof(buf), "proxy_payload"))
+ goto cleanup;
+ } else {
+ socklen_t actual_len = sizeof(struct sockaddr_storage);
+ struct sockaddr_storage actual = {};
+
+ accepted = accept(server, NULL, NULL);
+ if (!ASSERT_OK_FD(accepted, "accept") ||
+ !ASSERT_OK(getsockname(accepted, (void *)&actual,
+ &actual_len), "getsockname") ||
+ !ASSERT_EQ(actual_len, len, "original_address_len") ||
+ !ASSERT_MEMEQ(&actual, &expected, len, "original_dst"))
+ goto cleanup;
+ }
+ ASSERT_OK(skel->bss->proxy_err, "assign_and_clone");
+ ASSERT_EQ(skel->data->egress_ret, -EOPNOTSUPP, "egress_rejected");
+cleanup:
+ sk_assign_netkit__destroy(skel);
+ if (accepted >= 0)
+ close(accepted);
+ if (client >= 0)
+ close(client);
+ if (server >= 0)
+ close(server);
+ netns_free(pod);
+ netns_free(host);
+}
+
+void test_sk_assign_netkit(void)
+{
+ if (test__start_subtest("tcpv4"))
+ run_netkit(AF_INET, SOCK_STREAM);
+ if (test__start_subtest("tcpv6"))
+ run_netkit(AF_INET6, SOCK_STREAM);
+ if (test__start_subtest("udpv4"))
+ run_netkit(AF_INET, SOCK_DGRAM);
+ if (test__start_subtest("udpv6"))
+ run_netkit(AF_INET6, SOCK_DGRAM);
+}
diff --git a/tools/testing/selftests/bpf/progs/sk_assign_netkit.c b/tools/testing/selftests/bpf/progs/sk_assign_netkit.c
new file mode 100644
index 000000000000..1837486de4b3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/sk_assign_netkit.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <asm-generic/errno-base.h>
+#include <bpf/bpf_endian.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_tracing_net.h"
+
+#define SKIP_PACKET 1
+
+const volatile __u16 service_port;
+const volatile __u16 proxy_port;
+int proxy_err;
+int egress_ret = SKIP_PACKET;
+
+static __always_inline int assign_proxy_sock(struct __sk_buff *skb)
+{
+ void *data_end = (void *)(long)skb->data_end;
+ void *data = (void *)(long)skb->data;
+ struct bpf_sock_tuple tuple = {};
+ struct bpf_sock *sk;
+ __u8 proto;
+ __u32 len;
+ void *l4;
+ int ret;
+
+ if (skb->protocol == bpf_htons(ETH_P_IP)) {
+ struct iphdr *iph = data + ETH_HLEN;
+
+ if (iph + 1 > data_end || iph->ihl != 5)
+ return SKIP_PACKET;
+ proto = iph->protocol;
+ l4 = iph + 1;
+ tuple.ipv4.dport = bpf_htons(proxy_port);
+ len = sizeof(tuple.ipv4);
+ } else if (skb->protocol == bpf_htons(ETH_P_IPV6)) {
+ struct ipv6hdr *ip6h = data + ETH_HLEN;
+
+ if (ip6h + 1 > data_end)
+ return SKIP_PACKET;
+ proto = ip6h->nexthdr;
+ l4 = ip6h + 1;
+ tuple.ipv6.dport = bpf_htons(proxy_port);
+ len = sizeof(tuple.ipv6);
+ } else {
+ return SKIP_PACKET;
+ }
+ if (proto == IPPROTO_TCP) {
+ struct tcphdr *th = l4;
+
+ if (th + 1 > data_end || th->dest != bpf_htons(service_port) ||
+ !th->syn || th->ack)
+ return SKIP_PACKET;
+ sk = bpf_sk_lookup_tcp(skb, &tuple, len, BPF_F_CURRENT_NETNS, 0);
+ } else if (proto == IPPROTO_UDP) {
+ struct udphdr *uh = l4;
+
+ if (uh + 1 > data_end || uh->dest != bpf_htons(service_port))
+ return SKIP_PACKET;
+ sk = bpf_sk_lookup_udp(skb, &tuple, len, BPF_F_CURRENT_NETNS, 0);
+ } else {
+ return SKIP_PACKET;
+ }
+ if (!sk)
+ return -ENOENT;
+ ret = bpf_sk_assign(skb, sk, 0);
+ bpf_sk_release(sk);
+ return ret;
+}
+
+SEC("netkit/peer")
+int assign_proxy(struct __sk_buff *skb)
+{
+ int ret;
+
+ ret = assign_proxy_sock(skb);
+ if (ret == SKIP_PACKET)
+ return NETKIT_PASS;
+ if (!ret)
+ ret = bpf_clone_redirect(skb, skb->ifindex, 0);
+ if (ret) {
+ proxy_err = ret;
+ return NETKIT_DROP;
+ }
+ return NETKIT_PASS;
+}
+
+SEC("tcx/egress")
+int egress_assign(struct __sk_buff *skb)
+{
+ if (egress_ret == SKIP_PACKET)
+ egress_ret = assign_proxy_sock(skb);
+ return TCX_PASS;
+}
+
+char LICENSE[] SEC("license") = "GPL";
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress
2026-11-24 15:12 [PATCH RFC bpf-next 0/4] bpf: add bpf_sk_assign support on netkit egress Nikolay Aleksandrov
` (3 preceding siblings ...)
2026-11-24 15:12 ` [PATCH RFC bpf-next 4/4] selftests/bpf: test netkit socket assignment to a transparent proxy Nikolay Aleksandrov
@ 2026-11-24 21:02 ` Alexei Starovoitov
4 siblings, 0 replies; 8+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 21:02 UTC (permalink / raw)
To: Nikolay Aleksandrov, bpf
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev,
Daniel Borkmann, Stanislav Fomichev
On Tue, Nov 24, 2026 at 07:12 AM Nikolay Aleksandrov <razor@blackwall.org> wrote:
> This series builds on Daniel's work [2] to steer netkit traffic to a
> transparent proxy in the receiving namespace. The receive backlog can
> outlive the transmit path's RCU read-side section, so the skb takes its
> own socket reference even for SOCK_RCU_FREE listeners.
can we do the assign at tcx ingress of the receiving netkit device ?
That works today. After NETKIT_PASS the skb goes through
sch_handle_ingress() of that device. It runs after the backlog, so
the listener stays borrowed under RCU.
tc_netkit_pkt_type already runs such a pair: tc1 in netkit, tc7 at
tcx ingress of the peer.
netkit scrubs skb->mark before the prog runs. So the netkit prog can
pass its verdict to the tcx prog in the mark.
assign_proxy_sock() from patch 4 needs no change for that, I think:
SEC("tcx/ingress")
int assign_proxy(struct __sk_buff *skb)
{
int ret = assign_proxy_sock(skb);
return ret && ret != SKIP_PACKET ? TCX_DROP : TCX_PASS;
}
bpf_program__attach_tcx(skel->progs.assign_proxy,
if_nametoindex(HOST_DEV), NULL);
The two ways:
netkit prog tcx ingress prog
(this series) on nk_host (today)
------------------ ---------------------- -----------------------
kernel changes patches 1 and 3 none
listener, UDP sk refcount inc and put no refcount
per skb
other netkit skbs netkit_run() sets and one more prog, only
clears sk_assign_skb after NETKIT_PASS
who sees the skb backlog, generic XDP, same as today
with assigned sk tcx ingress, the rest
as today
With a UDP proxy every datagram does an atomic inc and dec on one
socket, from all cpus.
commit 7ae215d23c12 ("bpf: Don't refcount LISTEN sockets in sk_assign()")
removed that for tc ingress.
Then there is no need for sock_pfree_ref, sk_assign_skb,
BPF_RI_F_NETKIT_SK_ASSIGN and the second set of rules in the helper doc.
pw-bot: cr
^ permalink raw reply [flat|nested] 8+ messages in thread