BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Nikolay Aleksandrov" <razor@blackwall.org>, bpf@vger.kernel.org
Subject: Re: [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path
Date: Tue, 24 Nov 2026 15:59:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-231505@kernel.org> (raw)
In-Reply-To: <20261124151244.1423343-4-razor@blackwall.org>

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

  reply	other threads:[~2026-11-24 15:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [PATCH RFC bpf-next 3/4] bpf: support bpf_sk_assign in netkit tx path Nikolay Aleksandrov
2026-11-24 15:59   ` sashiko-bot [this message]
2026-11-24 16:04     ` Nikolay Aleksandrov
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-231505@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=razor@blackwall.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox