* [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro
@ 2026-11-21 16:02 Nikolay Aleksandrov
2026-11-22 14:21 ` Nikolay Aleksandrov
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-21 16:02 UTC (permalink / raw)
To: netdev
Cc: Nikolay Aleksandrov, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Alexei Starovoitov,
Martin KaFai Lau, Joe Stringer, Daniel Borkmann, bpf
bpf_sk_assign at tc ingress holds a reference on a socket that is not
rcu freed, such as an established tcp socket, until the receive path
steals it. ip_rcv_core keeps a prefetched socket so when the assigned
packet is an ipip, gre or esp packet the socket stays attached after
decap and their rx paths pass the skb to gro_cells_receive. GRO merges
skbs with the same skb->sk into one flow and clears skb->sk and
skb->destructor of each merged skb without calling the destructor so
every merged skb leaks a socket ref.
Send skbs with a prefetched socket to netif_rx instead as
gro_cells_receive already does for cloned skbs, where each skb is
consumed on its own and no reference is lost.
skb_sk_is_prefetched tests skb->destructor which shares a cache line
with skb->cloned that is read just before so the common case adds a single
cheap test.
Reproduced with a tcx ingress program on a veth in a receiver netns with
an ipip tunnel which assigns an established tcp socket to ipip packets.
The sender sends 32 tcp segments of one flow inside ipip through a netem
slot qdisc so they are decapsulated in one batch and merged by gro.
Then the socket is closed and the receiver netns is deleted. The leaked
socket holds a ref on the netns so it is never torn down and its devices
are never unregistered:
Before:
bpf_sk_assign() calls 32, sk_refcnt 32 after the burst
receiver netns deleted, 30s later its veth peer still exists
After:
bpf_sk_assign() calls 32, sk_refcnt 1 after the burst
receiver netns deleted, its veth peer is gone
The problem was found during reviews of patches that add bpf_sk_assign
egress support.
Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
---
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: Joe Stringer <joe@wand.net.nz>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: bpf@vger.kernel.org
net/core/gro_cells.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/net/core/gro_cells.c b/net/core/gro_cells.c
index 31352cac355c..8c74b2a1227b 100644
--- a/net/core/gro_cells.c
+++ b/net/core/gro_cells.c
@@ -4,6 +4,7 @@
#include <linux/netdevice.h>
#include <net/gro_cells.h>
#include <net/hotdata.h>
+#include <net/sock.h>
struct gro_cell {
struct sk_buff_head napi_skbs;
@@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
skb_unset_transport_header(skb);
- if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
+ if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
+ unlikely(skb_sk_is_prefetched(skb))) {
res = netif_rx(skb);
goto unlock;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro
2026-11-21 16:02 [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro Nikolay Aleksandrov
@ 2026-11-22 14:21 ` Nikolay Aleksandrov
2026-11-23 10:49 ` Eric Dumazet
2026-11-24 3:03 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: Nikolay Aleksandrov @ 2026-11-22 14:21 UTC (permalink / raw)
To: netdev
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Alexei Starovoitov, Martin KaFai Lau, Joe Stringer,
Daniel Borkmann, bpf
On 21/11/2026 18:02, Nikolay Aleksandrov wrote:
> bpf_sk_assign at tc ingress holds a reference on a socket that is not
> rcu freed, such as an established tcp socket, until the receive path
> steals it. ip_rcv_core keeps a prefetched socket so when the assigned
> packet is an ipip, gre or esp packet the socket stays attached after
> decap and their rx paths pass the skb to gro_cells_receive. GRO merges
> skbs with the same skb->sk into one flow and clears skb->sk and
> skb->destructor of each merged skb without calling the destructor so
> every merged skb leaks a socket ref.
>
> Send skbs with a prefetched socket to netif_rx instead as
> gro_cells_receive already does for cloned skbs, where each skb is
> consumed on its own and no reference is lost.
>
> skb_sk_is_prefetched tests skb->destructor which shares a cache line
> with skb->cloned that is read just before so the common case adds a single
> cheap test.
>
> Reproduced with a tcx ingress program on a veth in a receiver netns with
> an ipip tunnel which assigns an established tcp socket to ipip packets.
> The sender sends 32 tcp segments of one flow inside ipip through a netem
> slot qdisc so they are decapsulated in one batch and merged by gro.
> Then the socket is closed and the receiver netns is deleted. The leaked
> socket holds a ref on the netns so it is never torn down and its devices
> are never unregistered:
>
> Before:
> bpf_sk_assign() calls 32, sk_refcnt 32 after the burst
> receiver netns deleted, 30s later its veth peer still exists
>
> After:
> bpf_sk_assign() calls 32, sk_refcnt 1 after the burst
> receiver netns deleted, its veth peer is gone
>
> The problem was found during reviews of patches that add bpf_sk_assign
> egress support.
>
> Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
> Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
> ---
> 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: Joe Stringer <joe@wand.net.nz>
> CC: Daniel Borkmann <daniel@iogearbox.net>
> CC: bpf@vger.kernel.org
>
> net/core/gro_cells.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
TBH it can be applied to next, this is an old problem ("never worked") that is
a corner case found by LLM reviews of a patch-set, which would make it easier to
reach. I've tested it with net-next as well.
Cheers,
Nik
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro
2026-11-21 16:02 [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro Nikolay Aleksandrov
2026-11-22 14:21 ` Nikolay Aleksandrov
@ 2026-11-23 10:49 ` Eric Dumazet
2026-11-24 3:03 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: Eric Dumazet @ 2026-11-23 10:49 UTC (permalink / raw)
To: Nikolay Aleksandrov
Cc: netdev, David S. Miller, Jakub Kicinski, Paolo Abeni,
Simon Horman, Alexei Starovoitov, Martin KaFai Lau, Joe Stringer,
Daniel Borkmann, bpf
Le sam. 21 nov. 2026 à 17:04, Nikolay Aleksandrov
<razor@blackwall.org> a écrit :
>
> bpf_sk_assign at tc ingress holds a reference on a socket that is not
> rcu freed, such as an established tcp socket, until the receive path
> steals it. ip_rcv_core keeps a prefetched socket so when the assigned
> packet is an ipip, gre or esp packet the socket stays attached after
> decap and their rx paths pass the skb to gro_cells_receive. GRO merges
> skbs with the same skb->sk into one flow and clears skb->sk and
> skb->destructor of each merged skb without calling the destructor so
> every merged skb leaks a socket ref.
>
> Send skbs with a prefetched socket to netif_rx instead as
> gro_cells_receive already does for cloned skbs, where each skb is
> consumed on its own and no reference is lost.
>
> skb_sk_is_prefetched tests skb->destructor which shares a cache line
> with skb->cloned that is read just before so the common case adds a single
> cheap test.
>
> Reproduced with a tcx ingress program on a veth in a receiver netns with
> an ipip tunnel which assigns an established tcp socket to ipip packets.
> The sender sends 32 tcp segments of one flow inside ipip through a netem
> slot qdisc so they are decapsulated in one batch and merged by gro.
> Then the socket is closed and the receiver netns is deleted. The leaked
> socket holds a ref on the netns so it is never torn down and its devices
> are never unregistered:
>
> Before:
> bpf_sk_assign() calls 32, sk_refcnt 32 after the burst
> receiver netns deleted, 30s later its veth peer still exists
>
> After:
> bpf_sk_assign() calls 32, sk_refcnt 1 after the burst
> receiver netns deleted, its veth peer is gone
>
> The problem was found during reviews of patches that add bpf_sk_assign
> egress support.
>
> Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
> Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
Right, GRO callers are supposed to only feed skbs with no socket, or
sock_wfree() ones (see skb_prepare_for_gro() used by veth), and
gro_cells never enforced this.
Reviewed-by: Eric Dumazet <edumazet@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro
2026-11-21 16:02 [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro Nikolay Aleksandrov
2026-11-22 14:21 ` Nikolay Aleksandrov
2026-11-23 10:49 ` Eric Dumazet
@ 2026-11-24 3:03 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 3:03 UTC (permalink / raw)
To: razor
Cc: netdev, davem, edumazet, kuba, pabeni, horms, ast, kafai, joe,
daniel, bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete.
--
net: keep skbs with a prefetched socket out of gro_cells gro
This patch makes gro_cells_receive() send skbs that carry a prefetched
socket from bpf_sk_assign() to netif_rx() instead of the GRO cell. The
goal is to stop GRO from clearing skb->sk and skb->destructor on merged
skbs, which leaks one socket reference per merged skb.
> diff --git a/net/core/gro_cells.c b/net/core/gro_cells.c
> index 31352cac355ce..4e8b9c3cd7da0 100644
> --- a/net/core/gro_cells.c
> +++ b/net/core/gro_cells.c
[ ... ]
> @@ -26,7 +27,8 @@ int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
>
> skb_unset_transport_header(skb);
>
> - if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
> + if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev) ||
> + unlikely(skb_sk_is_prefetched(skb))) {
[Severity: High]
Is matching only skb->destructor == sock_pfree enough here? An skb that
got its socket from bpf_sk_assign() can still reach gro_cells_receive()
with a different destructor that owns one sk_refcnt reference per skb.
One path: bpf_sk_assign() at tc ingress attaches a non-refcounted socket,
such as a TCP listener or a UDP socket. It sets sock_pfree and takes no
reference. ip_rcv_core() keeps it. Then an NFQUEUE rule matches the outer
ipip/gre/esp packet, and __nf_queue() converts it:
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;
}
}
...
}
After an NF_ACCEPT reinject, the packet is decapsulated. With !xnet,
skb_scrub_packet() does not orphan it, so the skb arrives here with
skb->sk set and destructor == sock_edemux. skb_sk_is_prefetched() now
returns false, and the skb is queued for GRO.
skb_gro_receive() then clears both fields on each merged skb without
calling sock_edemux():
net/core/gro.c:skb_gro_receive() {
...
/* sk ownership - if any - completely transferred to the aggregated packet */
skb->destructor = NULL;
skb->sk = NULL;
...
}
The frag_list merge path in the same function does the same thing.
gro_list_prepare() still allows the merge because p->sk == skb->sk.
Wouldn't each merged skb then leak the reference that __nf_queue() took?
That is the same socket and netns teardown leak described in the commit
message.
The tree already has a general rule for this in skb_prepare_for_gro():
include/net/sock.h:skb_prepare_for_gro() {
if (skb->destructor != sock_wfree) {
skb_orphan(skb);
return;
}
skb->slow_gro = 1;
}
veth_xdp_rcv_skb() calls it before napi_gro_receive(). Could
gro_cells_receive() divert or orphan any skb that still has skb->sk set
with a destructor other than sock_wfree, rather than checking for
sock_pfree only? Checking skb->sk directly would be one way to do it.
Separately, the "sk ownership - if any - completely transferred to the
aggregated packet" comments in skb_gro_receive() look wrong for
destructors that hold one reference per skb (sock_pfree on refcounted
sockets, sock_edemux, sock_efree).
> res = netif_rx(skb);
> goto unlock;
> }
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121160204.4024204-1-razor%40blackwall.org
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 3:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 16:02 [PATCH net] net: keep skbs with a prefetched socket out of gro_cells gro Nikolay Aleksandrov
2026-11-22 14:21 ` Nikolay Aleksandrov
2026-11-23 10:49 ` Eric Dumazet
2026-11-24 3:03 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox