BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect
@ 2026-11-30 18:24 Nikolay Aleksandrov
  2026-11-30 18:45 ` sashiko-bot
  2026-12-01  3:15 ` Alexei Starovoitov
  0 siblings, 2 replies; 4+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-30 18:24 UTC (permalink / raw)
  To: bpf
  Cc: Nikolay Aleksandrov, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Alexei Starovoitov,
	Martin KaFai Lau, Daniel Borkmann, Joe Stringer,
	Kuniyuki Iwashima, netdev

tc ingress can attach a prefetched socket that is borrowed under rcu.
A sync-cookie request can also carry a borrowed listener. Redirects that
queue the skb can outlive that protection and peer redirects can carry
the assigned socket into another netns.

In skb_do_redirect keep the assignment only for tc ingress-to-ingress
redirects within the socket's netns when the skb already owns a reference
to a full socket. Orphan other prefetched assignments before forwarding so
the receiving stack can perform its normal lookup.

The problem was found during reviews of patches that add bpf_sk_assign
egress support and is very easy to hit, here's one way:
 - TCP SYN arrives at tc ingress
 - bpf_sk_assign attaches the listening tcp socket
 - bpf_redirect sends the packet via iface egress
 - The listener closes while the packet is still queued
 - Releasing the packet accesses the freed socket

A resolved trace looks like:
 [   24.345648] BUG: KASAN: slab-use-after-free in sock_pfree (include/net/sock.h:2295 include/net/sock.h:3047 net/core/sock.c:2865)
 [   24.345657] Read of size 1 at addr ffff88810cdf1a12 by task swapper/2/0

 [   24.345663] CPU: 2 UID: 0 PID: 0 Comm: swapper/2 Not tainted 7.4.0-rc4-g7cd8495c0f16 #1 PREEMPT(full)
 [   24.345666] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
 [   24.345669] Call Trace:
 [   24.345672]  <IRQ>
 [   24.345676]  dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120)
 [   24.345681]  print_report (mm/kasan/report.c:378 mm/kasan/report.c:482)
 [   24.345684]  ? sock_pfree (include/net/sock.h:2295 include/net/sock.h:3047 net/core/sock.c:2865)
 [   24.345693]  kasan_report (mm/kasan/report.c:595)
 [   24.345697]  ? sock_pfree (include/net/sock.h:2295 include/net/sock.h:3047 net/core/sock.c:2865)
 [   24.345701]  ? ip_rcv_core (include/linux/skbuff.h:1330 net/ipv4/ip_input.c:601)
 [   24.345704]  sock_pfree (include/net/sock.h:2295 include/net/sock.h:3047 net/core/sock.c:2865)
 [   24.345707]  skb_release_head_state (net/core/skbuff.c:1167 (discriminator 8))
 [   24.345711]  sk_skb_reason_drop (net/core/skbuff.c:1187 net/core/skbuff.c:1203 net/core/skbuff.c:1242)
 [   24.345715]  ip_rcv_core (include/linux/skbuff.h:1330 net/ipv4/ip_input.c:601)


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>
---
This patch was taken out of the bpf_sk_assign set, re-worked and re-tested.

For Sashiko and the other bots since they will complain:
 Classic mirred redirects, bridge and OVS forwarding and tunnel
 decapsulation can also queue borrowed assignments or carry a prefetched
 socket across namespaces. Those paths need separate handling.

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: Alexei Starovoitov <ast@kernel.org>
CC: Martin KaFai Lau <kafai@fb.com>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Joe Stringer <joe@wand.net.nz>
CC: Kuniyuki Iwashima <kuniyu@google.com>
CC: netdev@vger.kernel.org

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

diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2be65c..9e7d6c03e542 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2553,6 +2553,21 @@ static struct net_device *skb_get_peer_dev(struct net_device *dev)
 	return NULL;
 }
 
+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;
+	if (!skb_at_tc_ingress(skb) || flags != BPF_F_INGRESS ||
+	    !net_eq(sock_net(sk), dev_net(dev)) || !sk_fullsock(sk) ||
+	    !sk_is_refcounted(sk))
+		skb_orphan(skb);
+}
+
 int skb_do_redirect(struct sk_buff *skb)
 {
 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
@@ -2565,6 +2580,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] 4+ messages in thread

* Re: [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect
  2026-11-30 18:24 [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect Nikolay Aleksandrov
@ 2026-11-30 18:45 ` sashiko-bot
  2026-11-30 18:54   ` Nikolay Aleksandrov
  2026-12-01  3:15 ` Alexei Starovoitov
  1 sibling, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-30 18:45 UTC (permalink / raw)
  To: Nikolay Aleksandrov, bpf

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] netfilter: nf_queue: use-after-free of syncookie reqsk listener
  in __nf_queue() [1]
--

commit db64d3de9cb065fcc63cc9c7da578879d98b49a3
Author: Nikolay Aleksandrov <razor@blackwall.org>

bpf: orphan unsafe prefetched sockets in skb_do_redirect

This commit orphans prefetched sockets in skb_do_redirect() unless the
redirect is tc ingress-to-ingress within the socket's netns and the skb
holds a reference to a full socket.

> diff --git a/net/core/filter.c b/net/core/filter.c
> index ea20fd2be65c0..9e7d6c03e5420 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -2553,6 +2553,21 @@ static struct net_device *skb_get_peer_dev(struct net_device *dev)
>  	return NULL;
>  }
>  
> +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;
> +	if (!skb_at_tc_ingress(skb) || flags != BPF_F_INGRESS ||
> +	    !net_eq(sock_net(sk), dev_net(dev)) || !sk_fullsock(sk) ||
> +	    !sk_is_refcounted(sk))
> +		skb_orphan(skb);
> +}

[Severity: High]
This problem wasn't introduced by this patch, but can __nf_queue() hit a
similar use-after-free on req->rsk_listener [1] when queuing a
packet whose skb->sk was set by bpf_sk_assign_tcp_reqsk()?

bpf_sk_assign_tcp_reqsk() attaches a syncookie request_sock with an
unreferenced req->rsk_listener and sets skb->destructor to sock_pfree.
When __nf_queue() queues the packet to userspace, it only checks
!sk_is_refcounted(sk):

net/netfilter/nf_queue.c:__nf_queue() {
    ...
	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;

			/* drop refcount on skb_orphan */
			skb->destructor = sock_edemux;
		}
	}
    ...
}

Because sk_fullsock(sk) is false for a request_sock, sk_is_refcounted(sk)
returns true and __nf_queue() leaves the skb unchanged across RCU grace
periods.  If the listener closes before nf_reinject() runs,
skb_steal_sock() in tcp_v4_rcv() or tcp_v6_rcv() returns the freed
req->rsk_listener pointer.

[1] https://sashiko.dev/#/bug/linux-e38dcdd4-fc77-4d07-a948-1840d055aa09

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130182424.2879469-1-razor@blackwall.org?part=1

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

* Re: [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect
  2026-11-30 18:45 ` sashiko-bot
@ 2026-11-30 18:54   ` Nikolay Aleksandrov
  0 siblings, 0 replies; 4+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-30 18:54 UTC (permalink / raw)
  To: sashiko-reviews, bpf

On 30/11/2026 20:45, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found no new issues
> in this patch, but noticed 1 related pre-existing issue it does not
> introduce:
> - [High] netfilter: nf_queue: use-after-free of syncookie reqsk listener
>    in __nf_queue() [1]

this is a pre-existing different problem that will be fixed in a different way
(already discussed in a separate thread)

Cheers,
  Nik


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

* Re: [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect
  2026-11-30 18:24 [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect Nikolay Aleksandrov
  2026-11-30 18:45 ` sashiko-bot
@ 2026-12-01  3:15 ` Alexei Starovoitov
  1 sibling, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2026-12-01  3:15 UTC (permalink / raw)
  To: Nikolay Aleksandrov, bpf
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Martin KaFai Lau, Daniel Borkmann, Joe Stringer,
	Kuniyuki Iwashima, netdev

On Mon, Nov 30, 2026 at 10:24 AM Nikolay Aleksandrov <razor@blackwall.org> wrote:
> +	sk = skb->sk;
> +	if (!skb_at_tc_ingress(skb) || flags != BPF_F_INGRESS ||
> +	    !net_eq(sock_net(sk), dev_net(dev)) || !sk_fullsock(sk) ||
> +	    !sk_is_refcounted(sk))
> +		skb_orphan(skb);

Can we orphan every prefetched socket instead?
tcf_bpf_act() does that for every verdict but TC_ACT_OK.

diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2..1f4641e 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2565,6 +2565,9 @@ int skb_do_redirect(struct sk_buff *skb)
 	ri->flags = 0;
 	if (unlikely(!dev))
 		goto out_drop;
+	/* sk_assign is for local delivery, same as in tcf_bpf_act() */
+	if (skb_sk_is_prefetched(skb))
+		skb_orphan(skb);
 	if (flags & BPF_F_PEER) {
 		dev = skb_get_peer_dev(dev);
 		if (unlikely(!dev ||

Also imo bpf-next is fine for this bug.

pw-bot: cr

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

end of thread, other threads:[~2026-12-01  3:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 18:24 [PATCH bpf] bpf: orphan unsafe prefetched sockets in skb_do_redirect Nikolay Aleksandrov
2026-11-30 18:45 ` sashiko-bot
2026-11-30 18:54   ` Nikolay Aleksandrov
2026-12-01  3:15 ` Alexei Starovoitov

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