secpath_reset() keeps the xfrm states when another extension is active and a clone shares the extensions, so a queued skb can keep an IPv6 IPcomp tunnel state alive past its network namespace: WARNING: net/ipv6/xfrm6_tunnel.c:341 at xfrm6_tunnel_net_exit+0xb7/0x180 BUG: KASAN: slab-use-after-free in xfrm6_tunnel_destroy+0x41b/0x4c0 Drop all extensions in tcp_cleanup_skb() before the skb is queued, except MPTCP's and PSP's, which TCP still needs there; with those, only the secpath goes, as before. tcp_add_backlog() keeps secpath_reset(), as PSP needs its extension until tcp_v{4,6}_do_rcv(). Made with an AI coding assistant, reviewed by the submitter. Fixes: 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst") Suggested-by: Eric Dumazet Cc: stable@vger.kernel.org Signed-off-by: Andrii Tereshchenko --- Sabrina Dubroca's first version of 9b6412e6979f dropped the extensions in tcp_eat_recv_skb(); Eric Dumazet asked for the drop at enqueue instead, which is where this patch does it, and Paolo Abeni noted MPTCP's extension, which it keeps: https://lore.kernel.org/netdev/879a4592e4e4bd0c30dbe29ca189e224ec1739a5.1739201151.git.sd@queasysnail.net/ Related, not a dependency: Henry Martin's "net: fix xfrm_state refcount underflow and leak in skb_ext COW" (net, v3, 2026-11-25) fixes the refcounts of an inactive secpath slot in skb_ext_maybe_cow(); this patch does not touch skb_ext and applies with or without it. https://lore.kernel.org/netdev/20261125020718.261041-1-bsdhenrymartin@gmail.com/ Found by an AI coding assistant, checking TCP for the shared-extension case of the UDP fix posted alongside. It reproduces on unmodified net and 6.18.y without fault injection; a reproducer is available to the maintainers on request. Tested with it, and with the TCP, TLS, MPTCP and PSP selftests: no regressions. include/net/tcp.h | 26 ++++++++++++++++++++++++-- net/ipv4/tcp_fastopen.c | 2 +- net/ipv4/tcp_input.c | 4 ++-- net/ipv4/tcp_ipv4.c | 6 +++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index 869e780fcb48..3f14ea632486 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -829,10 +829,32 @@ static inline void tcp_check_space(struct sock *sk) void tcp_sack_compress_send_ack(struct sock *sk); -static inline void tcp_cleanup_skb(struct sk_buff *skb) +/* Drop what the receive queue does not need, here on the RX CPU: dst and + * skb extensions. MPTCP may just have attached its own extension, and PSP + * checks queued skbs against a new association (psp_sock_set_tx_key()), + * so with either one only the secpath goes. + */ +static inline bool tcp_skb_keep_ext(const struct sk_buff *skb, + const struct sock *sk) +{ + if (sk_is_mptcp(sk)) + return true; +#if IS_ENABLED(CONFIG_INET_PSP) + if (skb_ext_exist(skb, SKB_EXT_PSP)) + return true; +#endif + return false; +} + +static inline void tcp_cleanup_skb(struct sk_buff *skb, const struct sock *sk) { skb_dst_drop(skb); - secpath_reset(skb); + if (unlikely(skb_has_extensions(skb))) { + if (tcp_skb_keep_ext(skb, sk)) + secpath_reset(skb); + else + skb_ext_reset(skb); + } } static inline void tcp_add_receive_queue(struct sock *sk, struct sk_buff *skb) diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c index 7f7eb3cc1c04..8558c8415e91 100644 --- a/net/ipv4/tcp_fastopen.c +++ b/net/ipv4/tcp_fastopen.c @@ -265,7 +265,7 @@ void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb) if (!skb) return; - tcp_cleanup_skb(skb); + tcp_cleanup_skb(skb, sk); /* segs_in has been initialized to 1 in tcp_create_openreq_child(). * Hence, reset segs_in to 0 before calling tcp_segs_in() * to avoid double counting. Also, tcp_segs_in() expects diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index d9a66a1342ef..50f3f2760c94 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -5696,7 +5696,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb) __kfree_skb(skb); return; } - tcp_cleanup_skb(skb); + tcp_cleanup_skb(skb, sk); __skb_pull(skb, tcp_hdr(skb)->doff * 4); reason = SKB_DROP_REASON_NOT_SPECIFIED; @@ -6731,7 +6731,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb) NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS); /* Bulk data transfer: receiver */ - tcp_cleanup_skb(skb); + tcp_cleanup_skb(skb, sk); __skb_pull(skb, tcp_header_len); tcp_ecn_received_counters(sk, skb, len - tcp_header_len); diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 2fc92a7884fc..014364828e0c 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1926,7 +1926,11 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb) */ skb_condense(skb); - tcp_cleanup_skb(skb); + /* Only the secpath: PSP needs its extension until tcp_v4_do_rcv(), + * and the full cleanup runs when the backlog is processed. + */ + skb_dst_drop(skb); + secpath_reset(skb); if (unlikely(tcp_checksum_complete(skb))) { bh_unlock_sock(sk); base-commit: a8710eac64316fc48f29eb3c2ae0746af2100e2e -- 2.53.0