bpf: add bpf_sk_assign support on netkit egress
Subsystems (10)
Nikolay Aleksandrov
maintainercover bpf: add bpf_sk_assign support on netkit egress
Hi,
This RFC narrows the earlier egress series [1] to netkit's transmit path. NETKIT_PASS queues the assigned skb directly to the peer's receive backlog. NETKIT_REDIRECT clears the assignment before forwarding.
General TC egress support would require changes across the networking stack. Many transmit paths rely on skb->sk and skb->destructor for send-side ownership and socket write-memory accounting, and would need to handle an assigned socket reference in place of the transmit owner.
There is a new sk_assign_skb added to bpf_net_context that is used to record the exact skb netkit is running BPF on, bpf_sk_assign uses that identity to allow egress assignment for that skb. It is valid only when the new kern_flags bit is set, that is used to avoid always initializing it to NULL in bpf_net_ctx_set.
A nested tc egress program running on a clone still gets -EOPNOTSUPP.
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.
The selftests exercise the new netkit sk assign for both IPv4/IPv6 and TCP/UDP.
Feedback is most welcome, more importantly if the approach taken in patch 03 is acceptable.
Changes since the original posting:
- Use a new approach to limit the egress sk assign only to netkit
- Reduce the number of selftests
- Drop some unnecessary fixes of pre-existing problems
Pre-existing problems found during reviews that will be dealt with separately:
- Receive lookup could use a prefetched socket whose protocol, family or network namespace did not match the packet
- GRO via gro_cells can merge an assigned skb after tunnel decapsulation and discard its socket reference without calling the destructor
- When a reuseport program rejects an assigned UDP socket, receive can report an error pointer as rx_sk to the kfree_skb tracepoint
- A generic XDP program, an rx_handler or a classic mirred action on the receiving device can forward the assigned skb to another network namespace. Generic XDP can also drop the assignment when it copies the skb, so the packet falls back to normal socket lookup
- nsim_forward_skb() queues the skb for napi_gro_receive() without orphaning it. GRO can discard its socket reference when merging it
Thanks,
Nik
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://github.com/cilium/linux/commits/pr/bpf-tproxy
Nikolay Aleksandrov (4):
net: track references owned by prefetched socket assignments
bpf: preserve owned socket assignments on ingress redirects
bpf: support bpf_sk_assign in netkit tx path selftests/bpf: test netkit socket assignment to a transparent proxy
drivers/net/netkit.c | 10 + include/linux/filter.h | 4 + include/net/inet6_hashtables.h | 10 +- include/net/inet_hashtables.h | 10 +- include/net/netkit.h | 15 ++ include/net/request_sock.h | 10 +- include/net/sock.h | 4 +- include/uapi/linux/bpf.h | 9 +- net/core/filter.c | 50 ++++- net/core/sock.c | 7 + net/netfilter/nf_queue.c | 16 +- tools/include/uapi/linux/bpf.h | 9 +- .../bpf/prog_tests/sk_assign_netkit.c | 189 ++++++++++++++++++ .../selftests/bpf/progs/sk_assign_netkit.c | 95 +++++++++ 14 files changed, 412 insertions(+), 26 deletions(-) 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
Signature · 1 line
2.47.3
Nikolay Aleksandrov
maintainerRFC PATCH 1/4 net: track references owned by prefetched socket assignments
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.
--- 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(-)
include/net/inet6_hashtables.h +7 -3
@@ -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; }
include/net/inet_hashtables.h +7 -3
@@ -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; }
include/net/request_sock.h +7 -3
@@ -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 ;
include/net/sock.h +3 -1
@@ -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 */
net/core/sock.c +7 -0
@@ -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 */ /*
net/netfilter/nf_queue.c +7 -9
@@ -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)
Signature · 1 line
2.47.3
Nikolay Aleksandrov
maintainerRFC PATCH 2/4 bpf: preserve owned socket assignments on ingress redirects
In reply to Nikolay AleksandrovIngress 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.
--- net/core/filter.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
net/core/filter.c +37 -0
@@ -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 ||
Signature · 1 line
2.47.3
Nikolay Aleksandrov
maintainerRFC PATCH 3/4 bpf: support bpf_sk_assign in netkit tx path
In reply to Nikolay AleksandrovLet 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.
--- 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(-)
drivers/net/netkit.c +10 -0
@@ -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; }
include/linux/filter.h +4 -0
@@ -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)
include/net/netkit.h +15 -0
@@ -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 */
include/uapi/linux/bpf.h +7 -2
@@ -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
net/core/filter.c +10 -3
@@ -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 ; }
tools/include/uapi/linux/bpf.h +7 -2
@@ -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
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
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…
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 <[email protected]>
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.
Quoted 19 lines
drivers/net/netkit.c +6
-0@@ -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);
drivers/net/netkit.c static __always_inline int · line 76
Quoted 10 lines
drivers/net/netkit.c +4
-0ret =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.
Quoted 10 lines
net/core/filter.c +2
-0@@ -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); +
net/core/filter.c static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = { · line 8145
Quoted 10 lines
net/core/filter.c +6
-1if (!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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Nikolay Aleksandrov
maintainerRe: [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
Quoted 7 lines · On 24/11/2026 17:59, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] bpf: missing L3/L4 protocol validation inbpf_sk_assign()andinet_steal_sock()
- [High] bpf: prefetched socket innetkit_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
Nikolay Aleksandrov
maintainerRFC PATCH 4/4 selftests/bpf: test netkit socket assignment to a transparent proxy
In reply to Nikolay AleksandrovAttach 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.
--- .../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
tools/testing/selftests/bpf/prog_tests/sk_assign_netkit.c new file +189 -0
@@ -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 ); +}
tools/testing/selftests/bpf/progs/sk_assign_netkit.c new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.47.3
Quoted 4 lines · Nikolay Aleksandrov
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_FREElisteners.
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