From: Nikolay Aleksandrov <razor@blackwall.org>
To: bpf@vger.kernel.org
Cc: Daniel Borkmann <daniel@iogearbox.net>,
Nikolay Aleksandrov <razor@blackwall.org>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Martin KaFai Lau <kafai@fb.com>, Joe Stringer <joe@wand.net.nz>,
Kuniyuki Iwashima <kuniyu@google.com>,
netdev@vger.kernel.org
Subject: [PATCH bpf-next v2] bpf: orphan prefetched sk on skb_do_redirect
Date: Tue, 1 Dec 2026 22:36:18 +0200 [thread overview]
Message-ID: <20261201203618.3146031-1-razor@blackwall.org> (raw)
From: Daniel Borkmann <daniel@iogearbox.net>
A prefetched skb socket can be an RCU-borrowed listener or a SYN-cookie
request carrying a borrowed listener. Redirects can retain the skb past
the RCU section or carry it into another netns. Orphan prefetched skbs
before redirect so later processing can't use a stale socket.
A socket assigned with bpf_sk_assign is no longer kept across
bpf_redirect, including BPF_F_INGRESS redirects, so the receiving
stack performs a regular socket lookup as with tcf_bpf_act.
Programs that need the assignment can make it from tc ingress on the
redirect target which runs after the skb is queued.
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: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
v2: - target bpf-next (Alexei)
- use Daniel's original patch that orphans all prefetched sockets but
orphan before peer redirect and adjust the commit message,
similar change was also suggested by Alexei in v1's review
v1: https://lore.kernel.org/bpf/20261130182424.2879469-1-razor@blackwall.org/
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 | 3 +++
1 file changed, 3 insertions(+)
diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2be65c..b87e99ec7441 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;
+ /* redirect may outlive rcu protection for a prefetched socket */
+ if (skb_sk_is_prefetched(skb))
+ skb_orphan(skb);
if (flags & BPF_F_PEER) {
dev = skb_get_peer_dev(dev);
if (unlikely(!dev ||
--
2.47.3
reply other threads:[~2026-12-01 20:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261201203618.3146031-1-razor@blackwall.org \
--to=razor@blackwall.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=joe@wand.net.nz \
--cc=kafai@fb.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/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