From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384338; b=olIcMsXGxOmOz3bCNIjbX8jFUw1goxLaGDKlpAJJdrIxZ/e0I+IL34bCj+eUYtQT2DvG 99xbB9NHfopXhuQY2VmrNjxAkNQn8E+lkJdL0vjmyBxHaGgv/7akkts+CwPh2rpTnqAlN TLkFrD1dx8Xfyllvpwz+wV0r2L8QCNGLM3gUAk/nrD2cP3hZ9recid/BPSOUZidFe03VQ KmS2xRhxeIgRhXE5wydsrxzMErzgb1uhU6GhxYQfak325FiBcXUEx8DWjKOHNCDYvRl2u 6z8H+kyRRWSwqgSBFOQv6T1a02IHf7EE0IG3sEJXdUykVEqhZfRq0E1xQ1ntrA6uikw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384338; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=xmjsTmorRPT0EewMAva+JiKEejsnVgvdq7HYZLzF5Ko=; b=QeUYVEiTuYoMRP/sxFpyKaWO1MpXfBDy9XJ8tBxcKSu3JcC+6WkTUWNhcczk/KsINj7F J3Yx5juJiH3AAzverFYj0K57i4h1hdw7z7+6L94CeP0c/xZ0n/uUZVGUKYeRUCunUus35 lOLKgbAe0/iXf82GPJREejeVTWu8EkEeisHKTi9nJT4xGcCRGerZlpAZHrVx5Motgvj0N lccFKO7AjMEaDP/NQW3RQIqGi24Pzu1lgBnpUiqCUFGSpfTzErelpGudk8acpKaxc7NLF 702rSfOATb6ZWmkp68rcnzHc0YOO8nvH8cZJMeGKH7zTXC1O7M5JzDIk+6Zm2nHGuEg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uqXfkKIo; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13C54606A322; Fri, 4 Dec 2026 11:38:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uqXfkKIo Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D2711F000C2; Fri, 4 Dec 2026 11:38:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384337; bh=xmjsTmorRPT0EewMAva+JiKEejsnVgvdq7HYZLzF5Ko=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=uqXfkKIo4+zxeqo1iClMPB49ai7GZzg5xXVz5BWLad/Hbj273CuDLmSTMP5z0VR7F gZtHTTuvakwd1bQQWNr/dWnTpvoFURtQwtkfpdC4j/NxxW0t3OOPyvHBGV0GCwwZIv rktvSNx9IuOc9CgDd3W2dbJFudhAsvfPkRizm7v0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Xiang Mei , Daniel Borkmann , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.6 0075/1167] bpf: Reset skb->cb after an lwt prog pushes an IP header Date: Fri, 4 Dec 2026 18:51:42 +0900 Message-ID: <20261204100704.033368411@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit b3ad42f6183286ab376389a5d26edf7ee3bdba27 ] IPCB(skb)->opt holds the offsets from the network header to the IPv4 options. But bpf_lwt_push_ip_encap() moves the network header and does not change IPCB(skb). Then ip_forward_options() and __ip_options_echo() use the old offsets on the pushed header. Thus the kernel corrupts the skb and the stack. If the family changes, the kernel also reads IP6CB(skb) as IPCB(skb), or the reverse. With CONFIG_IPV6_MIP6, it reads IPSKB_FORWARDED as frag_max_size and adds a needless fragment header. Reset skb->cb after the prog. bpf_lwt_push_ip_encap() cannot do it, because bpf_prog_run_save_cb() can restore skb->cb. For IPv4 in IPv4, clear IPCB(skb)->opt as ip_tunnel_xmit() does. If the family changes, clear the cb of the new family but keep iif, because ip6_forward() gets the input device from it. Behind a VRF, skb->skb_iif is the VRF device and cannot replace iif. Do nothing for IPv6 in IPv6, as in seg6_do_srh_encap(). Do the reset in bpf_input() only after a push. Without a push, ip_forward_options() needs the offsets. Do it in bpf_xmit() before each reroute, because an lwt_xmit prog can also change the options. Thus after a reroute without a push, ip_options_fragment() leaves the options in all fragments, and ICMP errors echo none. After BPF_OK or BPF_REDIRECT the skb goes to a device, thus bpf_xmit() does no reset. The inner IPv4 checksum can still be wrong after ip_rcv_options() changes an option. Fixes: 52f278774e79 ("bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap") Reported-by: Xiang Mei Suggested-by: Daniel Borkmann Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261101190535.1454146-23-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- net/core/lwt_bpf.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c index ed5bafe584128..fde1b4a2f4aac 100644 --- a/net/core/lwt_bpf.c +++ b/net/core/lwt_bpf.c @@ -84,6 +84,32 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt, return ret; } +/* + * skb->cb still describes the IP header that was the first one before the + * prog ran. The option offsets in IPCB(skb)->opt are wrong for a pushed + * header, and a header of the other family needs the other layout. iif + * stays: behind a VRF skb->skb_iif is the L3 master and not the port. + * bpf_lwt_push_encap() cannot do this, because bpf_prog_run_save_cb() can + * restore skb->cb after the prog. + */ +static void bpf_lwt_reset_cb(struct sk_buff *skb, __be16 proto) +{ + int iif; + + if (skb->protocol == proto) { + if (proto == htons(ETH_P_IP)) + memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt)); + } else if (skb->protocol == htons(ETH_P_IP)) { + iif = IP6CB(skb)->iif; + memset(IPCB(skb), 0, sizeof(*IPCB(skb))); + IPCB(skb)->iif = iif; + } else { + iif = IPCB(skb)->iif; + memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); + IP6CB(skb)->iif = iif; + } +} + static int bpf_lwt_input_reroute(struct sk_buff *skb) { int err = -EINVAL; @@ -116,6 +142,8 @@ static int bpf_lwt_input_reroute(struct sk_buff *skb) static int bpf_input(struct sk_buff *skb) { struct dst_entry *dst = skb_dst(skb); + __be16 proto = skb->protocol; + unsigned int len = skb->len; struct bpf_lwt *bpf; int ret; @@ -124,6 +152,9 @@ static int bpf_input(struct sk_buff *skb) ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT); if (ret < 0) return ret; + /* An lwt_in prog can change the length only with a push. */ + if (skb->len != len) + bpf_lwt_reset_cb(skb, proto); if (ret == BPF_LWT_REROUTE) return bpf_lwt_input_reroute(skb); } @@ -303,6 +334,8 @@ static int bpf_xmit(struct sk_buff *skb) case BPF_REDIRECT: return LWTUNNEL_XMIT_DONE; case BPF_LWT_REROUTE: + /* An lwt_xmit prog can also write to the options. */ + bpf_lwt_reset_cb(skb, proto); return bpf_lwt_xmit_reroute(skb); default: return ret; -- 2.53.0