Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 482BC56C63D for ; Tue, 22 Sep 2026 16:48:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790095735; cv=none; b=uMDnuz0/kxKYvwdq3d/IoGTGcxJwDEy5ZszS8zxha7lFrV2Pa1n9PFYqmY9eGjAjr5XSJ6z5lUrDIzi7WhuAGbJjXPiAk7l8geCe+sE16/74mjuY3XVOrxDrCkmdEPKEEiQsWka63U14Wa2d0EpEhXewk7k8XgBldHow7O1pluM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790095735; c=relaxed/simple; bh=0BRk0OQsm1p2cLeVfbDb4qREQTt0gwai3AMK7biqK8o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GrRUNgZ4hVENbpmqaNsSyGol31eIOWQlQeIpCJQ9hYCcOJ0G4psaK/KKgieCVc0swEQBQS8K2dN7ZEcnrIMoh2kGez9udFKB+g7UzmzrVycAXR/EA/VSift4MRAGtBXX3HBmLeUSJGfOHcpX6Rtw85d79bmm6LtnnfVfoFBOjv4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LCWQnEmj; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LCWQnEmj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0C4D1F00899; Tue, 22 Sep 2026 16:48:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790095726; bh=IoPRmHHUMB9skjLu+LEFHEAmZvChuV3o2VBl/Y7TAW0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LCWQnEmjO87o09MLcdcIL8PzCsIv/JS64dLBCcBt0gI2+3gRLNAKaIL+SlYoC/Kyy /gN4tZmeMLEKKWv5Tx6Oh+hEHbuEX5+zzPK43vqOHkU5FkgSYIrUVmO2GEiohsHLcV s5H4Xg3jj7clvyKXPouuB1PZ0m0NpUndHScmFOawlZsti67FXdFPnNu/PjS1RqByPR U8lM7dH07JG8/5vy6wgTyYPcE7WT57fJVHkZ5KzNWAIGioJWz8Si/aNMfJ2mypVpQP KkNtVOq79KackM3BdICXtt/ufC8oaDClvMfdH7q0sapI+tkw+asGx6hV1fQnNHrzaO aAO2mqJPd4ZOA== From: Sasha Levin To: stable@vger.kernel.org Cc: Zhiling Zou , Vega , Steffen Klassert , Sasha Levin Subject: [PATCH 6.12.y 3/3] xfrm: save input state data before secpath resets Date: Tue, 22 Sep 2026 12:48:42 -0400 Message-ID: <20260922164842.3125154-3-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260922164842.3125154-1-sashal@kernel.org> References: <2026092226-ebony-armband-9098@gregkh> <20260922164842.3125154-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Zhiling Zou [ Upstream commit 3cf5cdecd99c9c186a5ea518d93bbf3045b6e3aa ] xfrm_input() stores the current xfrm_state in the skb secpath while it continues receive-side processing. Some input paths can reset that secpath before xfrm_input() has finished dereferencing the state. Receive callback users such as VTI and XFRM interfaces can reset the secpath. The VTI receive path does so before checking whether the packet crosses network namespaces, while the XFRM interface path does so only for cross-network-namespace packets. The XFRM_MAX_DEPTH error path can also reset the secpath before the final drop callback reports the current state's protocol. If secpath_reset() drops the last state reference while the state is concurrently deleted, xfrm_input() can still dereference the freed state when selecting transport_finish() or reporting the drop callback protocol. Save the state protocol on the stack while the state is still valid, and use the already saved address family for transport_finish(). A larval XFRM_STATE_ACQ state has no type, so retain nexthdr as its protocol. This preserves the existing drop-path fallback while avoiding the post-reset state dereferences without adding an extra state reference to every received packet. Fixes: df3893c176e9 ("vti: Update the ipv4 side to use it's own receive hook.") Cc: stable@vger.kernel.org Reported-by: Vega Signed-off-by: Zhiling Zou Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- net/xfrm/xfrm_input.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c index 9f98811d7815a..2562248408be3 100644 --- a/net/xfrm/xfrm_input.c +++ b/net/xfrm/xfrm_input.c @@ -464,6 +464,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) struct xfrm_state *x = NULL; xfrm_address_t *daddr; u32 mark = skb->mark; + u8 xfrm_proto = nexthdr; unsigned int family = AF_UNSPEC; int decaps = 0; int async = 0; @@ -475,6 +476,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) if (encap_type < 0 || (xo && (xo->flags & XFRM_GRO || encap_type == 0 || encap_type == UDP_ENCAP_ESPINUDP))) { x = xfrm_input_state(skb); + xfrm_proto = x->type ? x->type->proto : nexthdr; if (unlikely(x->km.state != XFRM_STATE_VALID)) { if (x->km.state == XFRM_STATE_ACQ) @@ -578,11 +580,13 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) x = xfrm_input_state_lookup(net, mark, daddr, spi, nexthdr, family); if (x == NULL) { + xfrm_proto = nexthdr; secpath_reset(skb); XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); xfrm_audit_state_notfound(skb, family, spi, seq); goto drop; } + xfrm_proto = x->type ? x->type->proto : nexthdr; if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) { secpath_reset(skb); @@ -590,6 +594,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) xfrm_audit_state_notfound(skb, family, spi, seq); xfrm_state_put(x); x = NULL; + xfrm_proto = nexthdr; goto drop; } @@ -709,7 +714,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) } while (!err); rcu_read_lock(); - err = xfrm_rcv_cb(skb, family, x->type->proto, 0); + err = xfrm_rcv_cb(skb, family, xfrm_proto, 0); if (err) { rcu_read_unlock(); goto drop; @@ -734,7 +739,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) xfrm_gro = xo->flags & XFRM_GRO; err = -EAFNOSUPPORT; - afinfo = xfrm_state_afinfo_get_rcu(x->props.family); + afinfo = xfrm_state_afinfo_get_rcu(family); if (likely(afinfo)) err = afinfo->transport_finish(skb, xfrm_gro || async); if (xfrm_gro) { @@ -757,7 +762,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) drop: if (async) dev_put(dev); - xfrm_rcv_cb(skb, family, x && x->type ? x->type->proto : nexthdr, -1); + xfrm_rcv_cb(skb, family, xfrm_proto, -1); kfree_skb(skb); return 0; } -- 2.53.0