Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=62.96.220.36 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796129057; b=KnanPtiQOrPmksi9Wtj2UodLVBRqFQyaa4U0PIwWuGMs7Abcb3kbNj4LT6eJcPVg0+Jf B+83aXRVq3EQlEesZxAqQbjTj/TAjiM0kx7dSv0S6nuqdH6r1dlGXj+nFu98yBoUJT3em jnETfm5Fwgv318fJvh3UKmaBWwxqqsp/HjTCkflEcbuoGP9QV6kBoKnTONhOawfi25XPW GggGuIEKP1DWKmxCgmwgFVOymsSsat5n/VtUnIiNQwacJ5vRtuPwGFWWwhhm2Ac0l1X3M zjR+wmyNMqGXfm2OYBGul/bYINafh6qHjnALuG4OlZvUO3CnCE9BBsYDkkbyA8dn3Hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796129057; h=DMARC-Filter:Received:X-Virus-Scanned:Received:Received:DKIM-Filter: DKIM-Signature:Received:Received:From:To:CC:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:Content-Type:X-ClientProxiedBy; bh=st8itqixbvyszAdDVFztyOSqnDYuywlItFze1hKGseM=; b=YxHFhHJhSaiE/urSEjW/SjW1tSPMPPQF6keIlj6C4nwXIXa/uTzef0Afaj+YkhRt26Fo Tp2HEGWz3vq6sCEamvQWdrdIbcuHcHPfUBekI86uyV1achB62icoFo8e/gDJABupM5O5r E12ctQuTCJN3Fe/9hEhPRUKwPQk8R4nWPUyFAyHNxl3g16tpXprgMwmXcsCAaVZcK/CzH +IierInyWhUek0nV4h8zpuEa2crtbh5xuJUguzj5od/nR7rDhA4EofkgmpSSA4kf4wMjO CvLNJGPzTmwx1o0bqm+y3HHnoB01+bLhXmvc02fyZXIjbV5e3YskCWrT5W5BMTUSPzQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=secunet.com; spf=pass smtp.mailfrom=secunet.com; dkim=pass header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=oU5rXjYX; arc=none smtp.remote-ip=62.96.220.36 Received: from mx1.secunet.com (mx1.secunet.com [62.96.220.36]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B594606A688 for ; Tue, 1 Dec 2026 12:44:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1B594606A688 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=secunet.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=secunet.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=secunet.com header.i=@secunet.com header.a=rsa-sha256 header.s=202301 header.b=oU5rXjYX Received: from localhost (localhost [127.0.0.1]) by mx1.secunet.com (Postfix) with ESMTP id DE40420820; Tue, 1 Dec 2026 13:44:15 +0100 (CET) X-Virus-Scanned: by secunet Received: from mx1.secunet.com ([127.0.0.1]) by localhost (mx1.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Na1RODhIm021; Tue, 1 Dec 2026 13:44:14 +0100 (CET) Received: from EXCH-01.secunet.de (rl1.secunet.de [10.32.0.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.secunet.com (Postfix) with ESMTPS id 8429A2069C; Tue, 1 Dec 2026 13:44:14 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.secunet.com 8429A2069C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secunet.com; s=202301; t=1796129054; bh=st8itqixbvyszAdDVFztyOSqnDYuywlItFze1hKGseM=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=oU5rXjYXYg6ax9+tK5ZlZazVdM67qGyR+daN5Eh2bDuoy59HiMopkLWh8gPvQzo2r Y4onqkZvpPaTzW/KvrGnlpHSLCBCbIgwLVCVYdGn/sTj/oF2zg7el7BUeljcl1Yfr2 40zqDnabPp2J3udT65WaVknwKo6M9vmhniAGDhLjZe3TPbQa939EP0BO2LLgQk7lnA uIxFEMapQ6QyKzDMkMQCTkjpBWkUkgvFA2mXk1DjjtZx5KUrH4zb1ihmYqTZxntTnu sutgWUzuI0JxmAylb14Sy1FqjbfUqt08ulU+thcUjioHjJ3QQ5l+4d+GRcJAG1LiwS TiXM74YfNKVfg== Received: from gauss4 (10.182.7.102) by EXCH-01.secunet.de (10.32.0.171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Tue, 1 Dec 2026 13:44:14 +0100 Received: (nullmailer pid 1165813 invoked by uid 1000); Tue, 01 Dec 2026 12:43:56 -0000 From: Steffen Klassert To: David Miller , Jakub Kicinski CC: Herbert Xu , Steffen Klassert , Subject: [PATCH 15/22] net: xfrm: interface: restore dst_orig on every xfrm interface Date: Tue, 1 Dec 2026 13:41:41 +0100 Message-ID: <20261201124252.1163254-16-steffen.klassert@secunet.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201124252.1163254-1-steffen.klassert@secunet.com> References: <20261201124252.1163254-1-steffen.klassert@secunet.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EXCH-01.secunet.de (10.32.0.171) To EXCH-01.secunet.de (10.32.0.171) From: Jamal Hadi Salim Found while auditing the xfrm metadata consumers of a steered packet. One of them is an xfrm interface, which restores the original route before the xfrm lookup; the swap warned on the metadata dst still installed in the skb, and only the collect_md arm restored the route at all. The issue: An xfrm interface takes the if_id and the underlying device either from its configuration or, in collect_md mode, from the xfrm metadata on the skb. When that metadata also carries the route the packet arrived through, xfrmi_xmit2() puts the route back on the skb, because the xfrm lookup takes the address family from it. skb_dst_set() expects the dst slot to be empty for a reference counted dst and warns when it is not, so the route landed over a metadata dst that was still there; a kernel built with CONFIG_DEBUG_NET=y panics on the first packet that takes this path when panic_on_warn is set. Two more defects sit in the same place. An interface with a configured if_id never restores the route: it takes the metadata dst as it is, whose address family is AF_UNSPEC, so the lookup finds no policy and the packet is dropped as a link failure instead of being transformed. And the restore moved the route out of the metadata dst, which a clone of the skb shares, so the clone that reached the interface first left the original packet with nothing to restore. The same slot overwrite is in the unstable helper a tc-bpf program uses to attach that metadata: bpf_skb_set_xfrm_info() records the route as dst_orig and installs a per-cpu metadata dst over it. The per-cpu dst is shared by every invocation of the program, so the restore cannot keep anything in it. The Fix: Restore dst_orig in both arms of xfrmi_xmit2(), taking a reference with dst_clone() so the metadata dst keeps its own, and empty the counted slot with skb_dst_drop() before the route is installed. The helper allocates a metadata dst per skb instead of a per-cpu one, so the route it records belongs to that skb and is released with it; the per-cpu allocation is no longer used and goes away. Conditions to recreate the bug: A tc-bpf program that records the route as dst_orig with bpf_skb_set_xfrm_info() is self-contained: the overwrite is in the helper, so the first packet that calls it panics inside bpf_skb_set_xfrm_info() on a CONFIG_DEBUG_NET=y build with panic_on_warn=1. An interface with a configured if_id shows its own defect without any other patch: it drops the packet as a link failure without consulting the outbound policy. And a clone made after the metadata was attached consumes the route before the original packet arrives. Fixes: 94151f5aa966 ("xfrm: interface: Add unstable helpers for setting/getting XFRM metadata from TC-BPF") Link: https://lore.kernel.org/netdev/DMZJS8O5A4YX.9T2P4DT0DAB7@gmail.com/ Cc: stable@vger.kernel.org Reviewed-by: Victor Nogueira Signed-off-by: Jamal Hadi Salim Reviewed-by: Eyal Birger Signed-off-by: Steffen Klassert --- include/net/xfrm.h | 2 -- net/core/filter.c | 8 -------- net/xfrm/xfrm_interface_bpf.c | 19 ++++++++----------- net/xfrm/xfrm_interface_core.c | 22 ++++++++++++---------- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/include/net/xfrm.h b/include/net/xfrm.h index 445223a8dc6f..1866e5a3b911 100644 --- a/include/net/xfrm.h +++ b/include/net/xfrm.h @@ -2370,8 +2370,6 @@ static inline bool xfrm6_local_dontfrag(const struct sock *sk) #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \ (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) -extern struct metadata_dst __percpu *xfrm_bpf_md_dst; - int register_xfrm_interface_bpf(void); #else diff --git a/net/core/filter.c b/net/core/filter.c index d832d3332c6f..0dd2312521a9 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6271,14 +6271,6 @@ static const struct bpf_func_proto bpf_bind_proto = { #ifdef CONFIG_XFRM -#if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \ - (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) - -struct metadata_dst __percpu *xfrm_bpf_md_dst; -EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst); - -#endif - BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index, struct bpf_xfrm_state *, to, u32, size, u64, flags) { diff --git a/net/xfrm/xfrm_interface_bpf.c b/net/xfrm/xfrm_interface_bpf.c index 5ea15037ebd1..7afbdbd11145 100644 --- a/net/xfrm/xfrm_interface_bpf.c +++ b/net/xfrm/xfrm_interface_bpf.c @@ -68,16 +68,9 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bp if (unlikely(skb_metadata_dst(skb))) return -EINVAL; - if (!xfrm_bpf_md_dst) { - struct metadata_dst __percpu *tmp; - - tmp = metadata_dst_alloc_percpu(0, METADATA_XFRM, GFP_ATOMIC); - if (!tmp) - return -ENOMEM; - if (cmpxchg(&xfrm_bpf_md_dst, NULL, tmp)) - metadata_dst_free_percpu(tmp); - } - md_dst = this_cpu_ptr(xfrm_bpf_md_dst); + md_dst = metadata_dst_alloc(0, METADATA_XFRM, GFP_ATOMIC); + if (unlikely(!md_dst)) + return -ENOMEM; info = &md_dst->u.xfrm_info; @@ -86,7 +79,11 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bp skb_dst_force(skb); info->dst_orig = skb_dst(skb); - dst_hold((struct dst_entry *)md_dst); + /* The metadata dst releases dst_orig when it is freed, so the + * reference the skb held is stolen rather than dropped. The dst + * allocated above is handed to the skb with its single reference. + */ + skb_dstref_steal(skb); skb_dst_set(skb, (struct dst_entry *)md_dst); return 0; } diff --git a/net/xfrm/xfrm_interface_core.c b/net/xfrm/xfrm_interface_core.c index 9835fbe63ccb..896180f8d171 100644 --- a/net/xfrm/xfrm_interface_core.c +++ b/net/xfrm/xfrm_interface_core.c @@ -423,6 +423,7 @@ static int xfrmi_rcv_cb(struct sk_buff *skb, int err) static int xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl) { + struct xfrm_md_info *md_info = skb_xfrm_md_info(skb); struct xfrm_if *xi = netdev_priv(dev); struct dst_entry *dst = skb_dst(skb); unsigned int length = skb->len; @@ -433,25 +434,26 @@ xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl) int mtu; if (xi->p.collect_md) { - struct xfrm_md_info *md_info = skb_xfrm_md_info(skb); - if (unlikely(!md_info)) return -EINVAL; if_id = md_info->if_id; fl->flowi_oif = md_info->link; - if (md_info->dst_orig) { - struct dst_entry *tmp_dst = dst; - - dst = md_info->dst_orig; - skb_dst_set(skb, dst); - md_info->dst_orig = NULL; - dst_release(tmp_dst); - } } else { if_id = xi->p.if_id; } + /* The metadata dst carries the route the packet is still to be + * transformed for, and the lookup below takes the address family from + * the dst it is given. Put that route back, taking our own reference: + * the metadata dst keeps its own, as a clone of this skb shares it. + */ + if (md_info && md_info->dst_orig) { + dst = dst_clone(md_info->dst_orig); + skb_dst_drop(skb); + skb_dst_set(skb, dst); + } + dst_hold(dst); dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, if_id); if (IS_ERR(dst)) { -- 2.53.0