Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 0 · Low: 1
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] On IPv6 TCP sockets, TCP still keeps a clone of the received skb
that pins the xfrm states, so the fix does not release them.
- [High] On MPTCP subflows, and on any skb carrying SKB_EXT_PSP,
tcp_cleanup_skb() still calls only secpath_reset().
- [Low] The new comment in tcp_add_backlog() gives the wrong reason for
skipping tcp_cleanup_skb().
--
tcp: drop skb extensions before queueing a received skb
This patch changes tcp_cleanup_skb() so that it drops all skb extensions
before a received skb is queued, instead of only the secpath. The aim is
to stop a queued skb from keeping an IPv6 tunnel xfrm state alive past
its netns. MPTCP sockets and skbs with a PSP extension keep the old
secpath_reset()-only behaviour, and tcp_add_backlog() now drops the dst
and calls secpath_reset() directly.
> diff --git a/include/net/tcp.h b/include/net/tcp.h
> index 869e780fcb482..3f14ea632486e 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;
[Severity: High]
Can MPTCP subflows still hit the leak described in the commit message?
This check looks at the socket type, not at whether the skb actually
carries SKB_EXT_MPTCP. When it returns true, tcp_cleanup_skb() only calls
secpath_reset(), which ends up here:
net/core/skbuff.c:__skb_ext_del() {
...
if (refcount_read(&ext->refcnt) > 1)
return;
#ifdef CONFIG_XFRM
if (id == SKB_EXT_SEC_PATH)
skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
#endif
...
}
The comment's reason ("MPTCP may just have attached its own extension")
only makes the block private when mptcp_incoming_options() reaches
skb_ext_add(), because skb_ext_maybe_cow() copies the block on add. But
there are early returns that the peer controls and that add no extension:
net/mptcp/options.c:mptcp_incoming_options() {
...
if (__mptcp_check_fallback(msk)) {
...
return !mptcp_over_limit(subflow->conn, sk, skb);
}
...
if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
return true;
...
mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
...
}
In those cases, under the commit's own precondition (another extension
active and the block shared with a clone), the secpath and its xfrm_state
references stay with the skb queued on the subflow sk_receive_queue or
out_of_order_queue. mptcp_init_skb() only resets them later. That can take
a long time if the msk receive buffer is full or a sequence hole is never
filled.
tcp_fastopen_add_skb() always passes a fresh skb_clone() to
tcp_cleanup_skb(), so MPTCP children take this path with a shared block
too.
The PSP branch below has the same gap if a PSP skb also carries a shared
secpath, though that case is more speculative.
Would it work to test skb_ext_exist(skb, SKB_EXT_MPTCP) instead of
sk_is_mptcp(sk)? Other options are to unshare the block before deleting
SKB_EXT_SEC_PATH, or to drop everything except the MPTCP/PSP extensions,
similar to what skb_ext_scrub() does for PSP.
> +#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);
[Severity: High]
Does this actually release the xfrm states on IPv6 sockets that have rx
options set?
In tcp_v6_do_rcv(), the skb is cloned before tcp_rcv_established() or
tcp_rcv_state_process() runs, and the clone is later stored on the
socket:
net/ipv6/tcp_ipv6.c:tcp_v6_do_rcv() {
...
if (np->rxopt.all &&
!((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
opt_skb = skb_clone_and_charge_r(skb, sk);
...
if (ipv6_opt_accepted(sk, opt_skb, &TCP_SKB_CB(opt_skb)->header.h6)) {
tcp_v6_restore_cb(opt_skb);
opt_skb = xchg(&np->pktoptions, opt_skb);
...
}
Through __copy_skb_header()->__skb_ext_copy(), the clone shares
skb->extensions, secpath included. The skb_ext_reset() here only drops
the original's reference. The block and its xfrm_state references then
stay alive through np->pktoptions, and nothing resets its extensions.
The listener path looks similar. When rxinfo, rxoinfo, rxhlim, rxohlim or
REPFLOW is set, tcp_v6_init_req() keeps the SYN itself:
refcount_inc(&skb->users);
ireq->pktopts = skb;
and tcp_v6_syn_recv_sock() clones it into the child:
newnp->pktoptions = skb_clone_and_charge_r(ireq->pktopts, newsk);
No secpath_reset() ever runs on that SYN. The child keeps the SYN's
sec_path until pktoptions is replaced or cleared, or until the socket is
destroyed.
Suppose the packet was decapsulated by an xfrm6 tunnel or IPcomp state in
another netns. Destroying that netns would then seem to give the same
xfrm6_tunnel_net_exit() warning and xfrm6_tunnel_destroy() use-after-free
quoted in the commit message.
Storing skbs in pktoptions is older than this patch, but it is the same
class of problem this commit is meant to close. The opt_skb clone is also
one source of the shared-extension condition the fix relies on.
> + }
> }
>
> static inline void tcp_add_receive_queue(struct sock *sk, struct sk_buff *skb)
[ ... ]
> diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
> index 2fc92a7884fc8..014364828e0cf 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);
[Severity: Low]
This isn't a bug, but is the reason given in this comment accurate?
tcp_skb_keep_ext() already returns true when skb_ext_exist(skb,
SKB_EXT_PSP) is true, and tcp_cleanup_skb() then only calls
secpath_reset(). Calling tcp_cleanup_skb(skb, sk) here would therefore
also leave the PSP extension in place for psp_skb_coalesce_diff() and
__psp_sk_rx_policy_check().
The commit message uses the same reasoning:
"tcp_add_backlog() keeps secpath_reset(), as PSP needs its extension
until tcp_v{4,6}_do_rcv()."
The code itself works, because backlog skbs are cleaned up again in
tcp_rcv_established() or tcp_data_queue().
Could the comment and the changelog be updated to give the actual reason
this cleanup is done by hand here?
>
> if (unlikely(tcp_checksum_complete(skb))) {
> bh_unlock_sock(sk);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/bd16…%40mid.andrii.net