Skip to contentSkip to conversation

net: don't redirect decrypted skbs to ingress, psp time-wait validator

PATCH In review Todd Freed 4 participants 8 messages Nov 17 – Nov 20

Subsystems (7)
lore
Share & tools
lore

Todd Freed

author
#0 lore raw

v2 cover net: don't redirect decrypted skbs to ingress, psp time-wait validator

Three fixes that are relevant for the net-next series, "psp: introduce socket-less associations". These were previously patches 6-8 of its v1:

https://lore.kernel.org/netdev/[email protected]

The first two patches prevent sending decrypted skbs from egress to ingress, since the meaning of skb->decrypted depends on the direction. This affects the packets of kTLS and PSP device offload sockets.

The third patch makes psp_twsk_init() give a validator only to the time-wait sockets of PSP connections with a Tx key, instead of to every TCP time-wait socket. It has no effect today, but the net-next series will rely on the presence of the validator to check if the socket has an encryption offload.

Testing, on two hosts with mlx5 (ConnectX-7), with and without the
patches: an egress rule on a connection's flow sends its packets to the device's ingress, with mirred (mirror and redirect) or bpf (bpf_clone_redirect() and bpf_redirect() with BPF_F_INGRESS), for a PSP device offload socket, a kTLS device offload socket and a plain TCP socket.

Without the patches, the PSP and kTLS sockets' packets arrive on ingress in all four cases. With them, none do: mirrored and cloned packets still go out, encrypted by the device, and redirected ones are dropped. The plain TCP socket's packets arrive on ingress in all four cases, with and without the patches. The PSP and plain TCP cases give the same results on netdevsim.

drivers/net psp.py and hw/tls_hw_offload.py pass, with and without the patches, as does tc-testing mirred.json on netdevsim (39 pass, 7 skipped by their dependsOn probes). Each patch builds without warnings (gcc and clang, allmodconfig, W=1).

v2 was re-tested as above, plus bpf selftests lwt_redirect, lwt_reroute and lwt_misc

v2:
- Cc: stable on patches 1 and 2 (Jamal)
- patch 2: also clear tc_at_ingress before running an LWT xmit program. Sashiko found this in its review of the same patch in the net-next series v1:
https://lore.kernel.org/netdev/[email protected]
- drop commit message references to the net-next series (Daniel)
- rebased on net/main

v1: https://lore.kernel.org/netdev/[email protected]

Todd Freed (3):
net/sched: act_mirred: don't send decrypted skbs from egress to
ingress
bpf: don't redirect decrypted skbs from egress to ingress
psp: only give time-wait sockets of PSP connections a validator

 net/core/filter.c      | 10 ++++++++++
 net/core/lwt_bpf.c     |  8 ++++++++
 net/psp/psp_sock.c     |  6 ++++--
 net/sched/act_mirred.c |  6 ++++++
 4 files changed, 28 insertions(+), 2 deletions(-)

base-commit: 5e9b2ad9b8384cd3c417cd5e2c3bc7eee4a72791

Signature · 1 line
2.53.1-Meta
author
#1 lore raw

PATCH v2 1/3 net/sched: act_mirred: don't send decrypted skbs from egress to ingress

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()).

A packet which mirred redirects or mirrors from egress to ingress keeps the bit. On ingress, it then claims to have been decrypted by the device, although it was never encrypted, and its payload, which was to be encrypted before leaving the host, could then be forwarded in cleartext.

Clearing skb->decrypted would not work, because that is what indicates intent to encrypt the packet.

Don't send decrypted skbs from egress to ingress: a redirected one is dropped, a mirrored one isn't copied.

Fixes 414776621d10 ("net/tls: prevent skb_orphan() from leaking TLS plain text with offload")
---
 net/sched/act_mirred.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/sched/act_mirred.c +6 -0
index 1c98e4d81ebe..52c3eeab381a 100644
@@ -285,6 +285,12 @@ static int tcf_mirred_to_dev(struct sk_buff *skb, struct tcf_mirred *m,
 		goto err_cant_do;
 	}
 
+	/* On transmit, decrypted asks the device to encrypt the packet. Sent
+	 * to ingress, the packet would read as decrypted by the device.
+	 */
+	if (unlikely(want_ingress && !at_ingress && skb_is_decrypted(skb)))
+		goto err_cant_do;
+
 	/* we could easily avoid the clone only if called by ingress and clsact;
 	 * since we can't easily detect the clsact caller, skip clone only for
 	 * ingress - that covers the TC S/W datapath.
Signature · 1 line
2.53.1-Meta
author
#2 lore raw

PATCH v2 2/3 bpf: don't redirect decrypted skbs from egress to ingress

In reply to Todd Freed

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")
---
 net/core/filter.c  | 10 ++++++++++
 net/core/lwt_bpf.c |  8 ++++++++
 2 files changed, 18 insertions(+)
net/core/filter.c +10 -0
index 946b07a17fce..defbef3a9f75 100644
@@ -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
net/core/lwt_bpf.c +8 -0
index 72c86ff2a847..4861fe2b4959 100644
@@ -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:
Signature · 1 line
2.53.1-Meta
#3 lore raw

Re: [PATCH net v2 2/3] bpf: don't redirect decrypted skbs from egress to ingress

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] bpf: multi-line comment style in bpf_xmit() in net/core/lwt_bpf.c - [Low] bpf: inaccurate commit…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] bpf: multi-line comment style in bpf_xmit() in net/core/lwt_bpf.c
- [Low] bpf: inaccurate commit message claim about LWT xmit __sk_buff->tstamp access
--

commit f0449c06845a54209a334370bea65b715330a2a6
Author: Todd Freed <[email protected]>

bpf: don't redirect decrypted skbs from egress to ingress

This commit drops decrypted skbs redirected from egress to ingress in __bpf_redirect() and clears tc_at_ingress before running LWT xmit BPF programs.

Quoted 4 lines

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.

[Severity: Low]
Is the commit message accurate about LWT xmit programs accessing __sk_buff->tstamp?

lwt_xmit_verifier_ops uses lwt_is_valid_access(), which explicitly rejects bpf_ctx_range(struct __sk_buff, tstamp), and lwt_xmit_func_proto() does not expose bpf_skb_set_tstamp().

Quoted 4 lines
net/core/lwt_bpf.c +0 -0
index 72c86ff2a8472..4861fe2b4959a 100644

[ ... ]

net/core/lwt_bpf.c static int bpf_xmit(struct sk_buff *skb) · line 337

Quoted 11 lines
net/core/lwt_bpf.c +7 -0
@@ -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);

[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of a multi-line comment on its own line, with the text starting on the next line.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

PATCH v2 3/3 psp: only give time-wait sockets of PSP connections a validator

In reply to Todd Freed

psp_twsk_init() sets tw_validate_xmit_skb on TCP time-wait sockets. Today, that becomes set whether or not the socket has PSP offload, which is not needed but is harmless. psp_validate_xmit() passes such packets.

Set the validator only when the socket has a PSP association with a Tx key, as psp_sock_set_tx_key() does for full sockets.

Fixes 6b46ca260e22 ("net: psp: add socket security association code")
---
 net/psp/psp_sock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/psp/psp_sock.c +4 -2
index a6b1c42dd626..5043df0bf711 100644
@@ -314,10 +314,12 @@ void psp_twsk_init(struct inet_timewait_sock *tw, const struct sock *sk)
 {
 	struct psp_assoc *pas = psp_sk_assoc(sk);
 
-	if (pas)
+	if (pas) {
 		refcount_inc(&pas->refcnt);
+		if (pas->tx.spi)
+			tw->tw_validate_xmit_skb = psp_validate_xmit;
+	}
 	rcu_assign_pointer(tw->psp_assoc, pas);
-	tw->tw_validate_xmit_skb = psp_validate_xmit;
 }
 
 void psp_twsk_assoc_free(struct inet_timewait_sock *tw)
Signature · 1 line
2.53.1-Meta
maintainer
#5 lore raw
In reply to Todd Freed
Quoted 5 lines · On Tue, 17 Nov 2026 11:24:08 -0800 Todd Freed wrote:

The third patch makes psp_twsk_init() give a validator only to the time-wait sockets of PSP connections with a Tx key, instead of to every TCP time-wait socket. It has no effect today, but the net-next series will rely on the presence of the validator to check if the socket has an encryption offload.

Ah, I was commenting on v1 yesterday, there was a kernel.org outage over the weekend so series tracking from last week to this week didn't work :(

For future reference - there needs to be a path in net that hits the bug for something to be considered a net-worthy fix. Otherwise no Fixes tag and to net-next it goes.

#6 lore raw
Quoted 3 lines

For future reference - there needs to be a path in net that hits the bug for something to be considered a net-worthy fix. Otherwise no Fixes tag and to net-next it goes.

Makes sense, I will keep this in mind.

-Todd

#7 lore raw
In reply to Todd Freed
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Tue, 17 Nov 2026 11:24:08 -0800 you wrote:

Three fixes that are relevant for the net-next series, "psp: introduce socket-less associations". These were previously patches 6-8 of its v1:

https://lore.kernel.org/netdev/[email protected]

The first two patches prevent sending decrypted skbs from egress to ingress, since the meaning of skb->decrypted depends on the direction. This affects the packets of kTLS and PSP device offload sockets.

[...]

Here is the summary with links:
- [net,v2,1/3] net/sched: act_mirred: don't send decrypted skbs from egress to ingress (no matching commit)
- [net,v2,2/3] bpf: don't redirect decrypted skbs from egress to ingress (no matching commit)
- [net,v2,3/3] psp: only give time-wait sockets of PSP connections a validator

    https://git.kernel.org/netdev/net-next/c/82290fab668c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html