Netdev List
 help / color / mirror / Atom feed
From: Todd Freed <toddfreed@meta.com>
To: <netdev@vger.kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Daniel Zahka <daniel.zahka@gmail.com>,
	Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Wei Wang <weiwan@google.com>, Jamal Hadi Salim <jhs@mojatatu.com>,
	Jiri Pirko <jiri@resnulli.us>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Sabrina Dubroca <sd@queasysnail.net>,
	Boris Pismenny <borisp@nvidia.com>, <bpf@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <stable@vger.kernel.org>
Subject: [PATCH net v2 2/3] bpf: don't redirect decrypted skbs from egress to ingress
Date: Tue, 17 Nov 2026 11:24:10 -0800	[thread overview]
Message-ID: <20261117192411.247117-3-toddfreed@meta.com> (raw)
In-Reply-To: <20261117192411.247117-1-toddfreed@meta.com>

On transmit, skb->decrypted means that the device must encrypt the
packet, e.g. a packet of a kTLS or PSP device offload socket.

On receive, skb->decrypted means that the device decrypted the packet,
and kTLS device offload trusts it as such (tls_device_decrypted()).

bpf_redirect() and bpf_clone_redirect() with BPF_F_INGRESS send the
packet to a device's receive path with the bit still set, where it claims
to have been decrypted by the device, although it was never encrypted.

Drop decrypted skbs which bpf redirects from egress to ingress, as mirred
does since the previous patch.

__bpf_redirect() tells egress from ingress by tc_at_ingress, which is
only kept up to date in tc. It is also reached from LWT xmit programs
(ip route ... encap bpf xmit), which run in ip_finish_output2(), before
__dev_queue_xmit() clears the bit. A forwarded packet can still have it
set there by the tc ingress hook of the device it came in on, and would
pass the check: whether the packet is dropped would depend on unrelated
tc configuration of its input device.

Clear the bit before running an LWT xmit program, as __dev_queue_xmit()
would right after. This also gives the program's other uses of the bit,
in the redirect helpers and in its __sk_buff->tstamp access, the egress
view, which matches where it runs.

Sashiko found the LWT case when reviewing this patch as patch 7 of v1
of the net-next series "psp: introduce socket-less associations", which
it was split out of.

Fixes: 414776621d10 ("net/tls: prevent skb_orphan() from leaking TLS plain text with offload")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/netdev/179471270091.2199510.4320994626979041884@kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Todd Freed <toddfreed@meta.com>
---
 net/core/filter.c  | 10 ++++++++++
 net/core/lwt_bpf.c |  8 ++++++++
 2 files changed, 18 insertions(+)

diff --git a/net/core/filter.c b/net/core/filter.c
index 946b07a17fce..defbef3a9f75 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -2226,6 +2226,16 @@ static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
 			  u32 flags)
 {
+	/*
+	 * On transmit, decrypted asks the device to encrypt the packet. Sent
+	 * to ingress, the packet would read as decrypted by the device.
+	 */
+	if (unlikely((flags & BPF_F_INGRESS) && !skb_at_tc_ingress(skb) &&
+		     skb_is_decrypted(skb))) {
+		kfree_skb(skb);
+		return -EINVAL;
+	}
+
 	if (dev_is_mac_header_xmit(dev))
 		return __bpf_redirect_common(skb, dev, flags);
 	else
diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c
index 72c86ff2a847..4861fe2b4959 100644
--- a/net/core/lwt_bpf.c
+++ b/net/core/lwt_bpf.c
@@ -13,6 +13,7 @@
 #include <net/gre.h>
 #include <net/ip.h>
 #include <net/ip6_route.h>
+#include <net/tcx.h>
 
 struct bpf_lwt_prog {
 	struct bpf_prog *prog;
@@ -327,6 +328,13 @@ static int bpf_xmit(struct sk_buff *skb)
 		__be16 proto = skb->protocol;
 		int ret;
 
+		/* The packet is on egress, but a forwarded one can still have
+		 * tc_at_ingress set by the tc ingress hook of the device it
+		 * came in on, until __dev_queue_xmit() clears it. Clear it
+		 * now, so that the redirect helpers see the direction of this
+		 * hook.
+		 */
+		tcx_set_ingress(skb, false);
 		ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
 		switch (ret) {
 		case BPF_OK:
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-11-17 19:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 19:24 [PATCH net v2 0/3] net: don't redirect decrypted skbs to ingress, psp time-wait validator Todd Freed
2026-11-17 19:24 ` [PATCH net v2 1/3] net/sched: act_mirred: don't send decrypted skbs from egress to ingress Todd Freed
2026-11-17 19:24 ` Todd Freed [this message]
2026-11-17 19:24 ` [PATCH net v2 3/3] psp: only give time-wait sockets of PSP connections a validator Todd Freed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117192411.247117-3-toddfreed@meta.com \
    --to=toddfreed@meta.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=borisp@nvidia.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel.zahka@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=sdf@fomichev.me \
    --cc=stable@vger.kernel.org \
    --cc=weiwan@google.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox