Skip to contentSkip to conversation

psp, tls: don't send offloaded contents in cleartext

PATCH In review Todd Freed 3 participants 19 messages Nov 17 – Nov 18

Subsystems (10)
lore
Share & tools
lore

Todd Freed

author
#0 lore raw

v6 cover psp, tls: don't send offloaded contents in cleartext

While working on adding support for socket-less psp associations, I noticed the case of ipv6 extension headers was not being handled properly for psp packets. It will be relevant with that series, because it adds udp support, for which the inner packet needs to be inspected to set the psp nexthdr.

That led me to various other ways the encryption offload path could transmit packets unencrypted.

Since the psp/tls association encodes intent to send encrypted packets, but these various cases circumvent that, sending these as fixes.

The cases are,

1/ ipv6 extension headers (patch 2; patch 1 makes mlx5e flush its tx
queue when the resulting drop happens),
2/ fragments (patch 3),
3/ icmp errors (patch 4),
4/ icmpv6 errors (patch 5),

Cases 2, 3 and 4 are relevant for ktls as well as psp.

patch 8 fixes an mlx5e bug in tso checksum calculation for psp packets when the l3 dest is an intermediate hop (ipv4 source routing). It's not about preventing unencrypted tx, so could be considered separate.

netfilter refragments bridged packets and builds reject replies itself, outside these paths. A separate nf-next series applies the same rules there.

Testing:

hardware testing was done on connectx-7 (fw 28.48), between two x86 hosts, plus netdevsim.

- drivers/net psp.py: on netdevsim, all 36 tests pass. on mlx5, 40 pass, 4 skip (gmac versions the device doesn't support), and 2 xfail (netdevsim only). the gmac variants of data_basic_send_netkit_psp_assoc fail: that test doesn't skip versions the device doesn't support, which is fixed separately.

- drivers/net/hw tls_hw_offload.py on mlx5: all 8 tests pass.

- tso checksum seed: a psp tcp socket with an ipv4 loose source route, the peer checking tcp checksums in software. hundreds of checksum errors before the fix, none after.

- fragments, with tls offload and psp:
- ipv4 (route mtu above the device mtu, IP_PMTUDISC_OMIT): without the fix, some fragments after the first go out in cleartext. with it they're dropped.
- ipv6, through an ipvs dr service with a local client: without the fix, fragments after the first go out in cleartext, and the peer reassembles them. with it they're dropped.

- icmp and icmpv6 errors, on netdevsim psp, with an nft reject rule (icmpx port-unreachable) in the inet family:
- in an input chain, of received decrypted packets: without the fix, the errors go out on the wire, quoting the decrypted packets. with it, no error reaches the wire.
- in an output chain, of a psp socket's packets: the error is delivered to the local socket, with and without the fix.
- a PKT_TOOBIG from ip6_fragment() about a psp socket's packets is delivered to the socket, with and without the fix.

v6:
- patches 4 and 5 - since "net: Make PSP skb extension survive packet scrubbing", some of the comments on patches 4 and 5 no longer make sense (psp skb and ktls skb can now be reliably distinguished). the behavior from v5 is retained, the rationale has been updated.
- patches 4 and 5 - clarified which reject rules go through icmp_send() and icmp6_send(), so that it's more obvious how the nf-next series applies (Sashiko)
- patch 5 - icmpv6: explain why the check of a locally originated skb comes after the rate limit (Sashiko)
- patch 6 - remove the socket option and close the connection in separate try blocks, and don't overstate the variants (Sashiko)
- patch 7 - use ctl_file_write() to turn off tcp_mtu_probing, including 1 (Sashiko)
- Cc: stable on the fixes (patches 1-5 and 8)
- rebase on net/main

v5: https://lore.kernel.org/netdev/[email protected]
- new patch 1: mlx5e rings the tx doorbell when an accel offload drops a packet, and checks the stop room after the nop it posts (Sashiko)
- ipv6 extension headers: drop them in psp_dev_encapsulate() again, instead of in mlx5e. it's the helper which can't encapsulate them, for every driver (netdevsim sent them malformed). unlike the routing header rejection in v2, this isn't a device limitation
- don't fragment decrypted skbs in ip6_fragment() either
- icmp and icmpv6: send no error at all about a received decrypted skb, instead of quoting up to the tcp header for tls offload
- icmpv6: decide whether an error about a locally originated skb goes to a local address from the error's route (RTF_LOCAL), like icmp (RTCF_LOCAL), instead of the address list (Sashiko)
- selftests: the extension header test runs on netdevsim too, and removes the socket option before closing. the fragment test uses the source address of the route, and turns off tcp_mtu_probing

v4: https://lore.kernel.org/netdev/[email protected]
- back to dropping ipv6 packets with extension headers, but moved into mlx5e instead of psp_dev_encapsulate()
- drop the psp core changes to rx and tx ipv6 extension headers
- new patch 2 to drop decrypted skbs instead of fragmenting
- new patches 3 and 4 to prevent quoting just-decrypted packet contents in icmp errors, v4 and v6 respectively (Jakub, Bjorn, Sashiko)
- new selftest: ipv4 fragments of a psp tcp socket

v3: https://lore.kernel.org/netdev/[email protected]
- new patch 1 to fix tso checksum seed calculation in mlx5e (Daniel)
- accept routing headers and reject ah (Daniel)
- split the psp_dev_rcv() change into its own patch (Daniel)
- return -EINVAL instead of -1 (Daniel)
- selftest: one variant per extension header type (Daniel)

v2: https://lore.kernel.org/netdev/[email protected]
- walk the extension headers instead of dropping (Daniel)
- reject fragment headers (Bjorn, Daniel)
- reject routing headers
- psp_dev_rcv() handle the rx case as well

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

Todd Freed (8):
net/mlx5e: ring the tx doorbell when an accel offload drops a packet
psp: don't encapsulate ipv6 packets with extension headers
net: don't fragment decrypted skbs
ipv4: icmp: don't quote decrypted payload in icmp errors
ipv6: icmpv6: don't quote decrypted payload in icmpv6 errors
selftests: drv-net: psp: test ipv6 tcp socket with extension headers
selftests: drv-net: psp: test that ipv4 psp packets aren't fragmented net/mlx5e: psp: tso: replace only the length in checksum seed

 .../mellanox/mlx5/core/en_accel/psp_rxtx.c    |  21 +-
 .../net/ethernet/mellanox/mlx5/core/en_tx.c   |   8 +-
 net/ipv4/icmp.c                               |  12 +
 net/ipv4/ip_output.c                          |   8 +
 net/ipv6/icmp.c                               |  18 ++
 net/ipv6/ip6_output.c                         |   9 +
 net/psp/psp_main.c                            |  21 +-
 tools/testing/selftests/drivers/net/config    |   1 +
 tools/testing/selftests/drivers/net/psp.py    | 245 +++++++++++++++++-
 9 files changed, 324 insertions(+), 19 deletions(-)

base-commit: 5e9b2ad9b8384cd3c417cd5e2c3bc7eee4a72791

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

PATCH v6 1/8 net/mlx5e: ring the tx doorbell when an accel offload drops a packet

when mlx5e_accel_tx_begin() drops the skb, e.g. because psp can't encapsulate it, mlx5e_xmit() returns without posting anything. the skbs before it in a bulk dequeue may have been posted with xmit_more, which defers the doorbell to the next one, and an mpwqe session may be open. the device then doesn't see them until the next transmit on that sq, and since the queue isn't stopped, the tx watchdog doesn't fire.

flush the sq on this drop like the dma error paths do: close the mpwqe session, post a nop and ring the doorbell.

mlx5e_tx_flush() doesn't check whether the nop leaves less than stop_room in the sq. for the dma error paths that is rare, but a run of offload drops would post nops without stopping the queue, and could overrun wqes in flight. so check it in mlx5e_tx_flush(), like the other producers do. the completion of the nop wakes the queue again.

the next patch makes the psp drop reachable from user space, with an ipv6 extension header set on a psp socket.

Fixes 2ac9cfe78223 ("net/mlx5e: IPSec, Add Innova IPSec offload TX data path")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_tx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_tx.c +7 -1
index 14479da98f42..006bf976def1 100644
@@ -365,6 +365,7 @@ static void mlx5e_tx_flush(struct mlx5e_txqsq *sq)
 	};
 
 	wqe = mlx5e_post_nop(&sq->wq, sq->sqn, &sq->pc);
+	mlx5e_tx_check_stop(sq);
 	mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &wqe->ctrl);
 }
 
@@ -672,8 +673,13 @@ netdev_tx_t mlx5e_xmit(struct sk_buff *skb, struct net_device *dev)
 	}
 
 	/* May send SKBs and WQEs. */
-	if (unlikely(!mlx5e_accel_tx_begin(dev, sq, skb, &accel)))
+	if (unlikely(!mlx5e_accel_tx_begin(dev, sq, skb, &accel))) {
+		/* The skb was dropped: ring the doorbell which the previous
+		 * ones may have deferred to it
+		 */
+		mlx5e_tx_flush(sq);
 		return NETDEV_TX_OK;
+	}
 
 	mlx5e_sq_xmit_prepare(sq, skb, &accel, &attr);
 
Signature · 1 line
2.53.1-Meta
#2 lore raw

Re: [PATCH net v6 1/8] net/mlx5e: ring the tx doorbell when an accel offload drops a packet

Sashiko has reviewed this patch and found no issues.
Quoted 16 lines

when mlx5e_accel_tx_begin() drops the skb, e.g. because psp can't encapsulate it, mlx5e_xmit() returns without posting anything. the skbs before it in a bulk dequeue may have been posted with xmit_more, which defers the doorbell to the next one, and an mpwqe session may be open. the device then doesn't see them until the next transmit on that sq, and since the queue isn't stopped, the tx watchdog doesn't fire.

flush the sq on this drop like the dma error paths do: close the mpwqe session, post a nop and ring the doorbell.

mlx5e_tx_flush() doesn't check whether the nop leaves less than stop_room in the sq. for the dma error paths that is rare, but a run of offload drops would post nops without stopping the queue, and could overrun wqes in flight. so check it in mlx5e_tx_flush(), like the other producers do. the completion of the nop wakes the queue again.

Quoted 6 lines

the next patch makes the psp drop reachable from user space, with an ipv6 extension header set on a psp socket.

Fixes 2ac9cfe78223 ("net/mlx5e: IPSec, Add Innova IPSec offload TX data path")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v6 1/8
#3 lore raw

Re: [PATCH net v6 1/8] net/mlx5e: ring the tx doorbell when an accel offload drops a packet

In reply to Todd Freed
Quoted 16 lines · Todd Freed

when mlx5e_accel_tx_begin() drops the skb, e.g. because psp can't encapsulate it, mlx5e_xmit() returns without posting anything. the skbs before it in a bulk dequeue may have been posted with xmit_more, which defers the doorbell to the next one, and an mpwqe session may be open. the device then doesn't see them until the next transmit on that sq, and since the queue isn't stopped, the tx watchdog doesn't fire.

flush the sq on this drop like the dma error paths do: close the mpwqe session, post a nop and ring the doorbell.

mlx5e_tx_flush() doesn't check whether the nop leaves less than stop_room in the sq. for the dma error paths that is rare, but a run of offload drops would post nops without stopping the queue, and could overrun wqes in flight. so check it in mlx5e_tx_flush(), like the other producers do. the completion of the nop wakes the queue again.

Quoted 6 lines

the next patch makes the psp drop reachable from user space, with an ipv6 extension header set on a psp socket.

Fixes 2ac9cfe78223 ("net/mlx5e: IPSec, Add Innova IPSec offload TX data path")

LGTM. Couldn't find any side effects besides the additonal NOP + db dance which is ok.

Thanks,
Dragos

author
#4 lore raw

PATCH v6 2/8 psp: don't encapsulate ipv6 packets with extension headers

In reply to Todd Freed

psp_dev_encapsulate() inserts the udp+psp headers at the transport header, rewrites the protocol of the fixed ip header to udp, and writes tcp as the psp next header. so tcp must directly follow the fixed header.

if the packet is an ipv6 packet with extension headers, udp+psp is placed after them, but the next header indicating the l4 protocol is on the last extension header in the chain, not in the fixed header. therefore this produces a malformed packet.

in addition to being malformed, with mlx5 this kind of packet goes out unencrypted. since the tx association encodes intent to send encrypted packets, drop such packets instead.

a psp tcp socket emits such packets if certain socket options are set on it (e.g. IPV6_DSTOPTS). refusing the combination of such socket options with psp association would be helpful but is not implemented here. since extension headers can also come from a route the datapath check is needed anyway.

the check also covers ipv4: the l4 protocol must be tcp, and the transport header must follow the ip header and its options. packets of a psp tcp socket always pass it.

Fixes fc724515741a ("psp: provide encapsulation helper for drivers")
---
 net/psp/psp_main.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
net/psp/psp_main.c +19 -2
index 273b010d2355..c071f507261b 100644
@@ -245,6 +245,24 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
 	memset(&psph->iv, 0, sizeof(psph->iv));
 }
 
+/* UDP+PSP goes in at the transport header, the protocol of the fixed IP
+ * header becomes UDP, and TCP the PSP next header: TCP must directly follow
+ * the fixed header. It doesn't with IPv6 extension headers, whose last one
+ * holds the L4 protocol.
+ */
+static bool psp_dev_encap_ok(const struct sk_buff *skb)
+{
+	u32 network_len = skb_network_header_len(skb);
+
+	if (skb->protocol == htons(ETH_P_IP))
+		return ip_hdr(skb)->protocol == IPPROTO_TCP &&
+		       network_len == ip_hdrlen(skb);
+	if (skb->protocol == htons(ETH_P_IPV6))
+		return ipv6_hdr(skb)->nexthdr == IPPROTO_TCP &&
+		       network_len == sizeof(struct ipv6hdr);
+	return false;
+}
+
 /* Encapsulate a TCP packet with PSP by adding the UDP+PSP headers and filling
  * them in.
  */
@@ -255,8 +273,7 @@ bool psp_dev_encapsulate(struct net *net, struct sk_buff *skb, __be32 spi,
 	u32 ethr_len = skb_mac_header_len(skb);
 	u32 bufflen = ethr_len + network_len;
 
-	if (skb->protocol != htons(ETH_P_IP) &&
-	    skb->protocol != htons(ETH_P_IPV6))
+	if (!psp_dev_encap_ok(skb))
 		return false;
 
 	if (skb_cow_head(skb, PSP_ENCAP_HLEN))
Signature · 1 line
2.53.1-Meta
#5 lore raw

Re: [PATCH net v6 2/8] psp: don't encapsulate ipv6 packets with extension headers

Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

psp_dev_encapsulate() inserts the udp+psp headers at the transport header, rewrites the protocol of the fixed ip header to udp, and writes tcp as the psp next header. so tcp must directly follow the fixed header.

if the packet is an ipv6 packet with extension headers, udp+psp is placed after them, but the next header indicating the l4 protocol is on the last extension header in the chain, not in the fixed header. therefore this produces a malformed packet.

in addition to being malformed, with mlx5 this kind of packet goes out unencrypted. since the tx association encodes intent to send encrypted packets, drop such packets instead.

a psp tcp socket emits such packets if certain socket options are set on it (e.g. IPV6_DSTOPTS). refusing the combination of such socket options with psp association would be helpful but is not implemented here. since extension headers can also come from a route the datapath check is needed anyway.

Quoted 7 lines

the check also covers ipv4: the l4 protocol must be tcp, and the transport header must follow the ip header and its options. packets of a psp tcp socket always pass it.

Fixes fc724515741a ("psp: provide encapsulation helper for drivers")

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 3/8 net: don't fragment decrypted skbs

In reply to Todd Freed

on transmit, skb->decrypted means the device must encrypt the skb. if it can't do that, then the packet should be dropped, not transmitted in the clear.

ip_do_fragment() and ip6_fragment() on a decrypted skb produce fragments which have skb->decrypted unset, and can be sent out unencrypted.

tx of individually encrypted fragments would not work anyway, because encrypting needs the L4 header, which non-initial fragments do not have.

ktls could encrypt in sw before fragmenting. that would not work for psp offload which does not have a software fallback path.

tcp sizes segments to the mtu, so psp and ktls socket packets normally won't reach this path. an ipv4 tcp socket can be made to send fragments by setting a route mtu above the device mtu and disabling pmtud on the socket (IP_PMTUDISC_OMIT). the selftest later in the series does this.

emitting fragments from an ipv6 tcp socket requires more steps, but is also possible. the test setup here uses ipvs, which sets ignore_df on forwarded packets. in ip6_fragment(), check skb->decrypted after the too-big checks, so that a local socket still gets PKT_TOOBIG for path mtu discovery.

tested with tls tx offload on mlx5 (connectx-7), over ipv4. without this patch some fragments after the first go out in cleartext, with it they're dropped (IpFragFails) and the connection stalls.

tested over ipv6, with tls tx offload and psp on mlx5 (connectx-7), through an ipvs dr service with a local client. without this patch, fragments after the first go out in cleartext, and the peer reassembles them; with it they're dropped (Ip6FragFails).

Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
Fixes 659a2899a57d ("tcp: add datapath logic for PSP with inline key exchange")
---
 net/ipv4/ip_output.c  | 8 ++++++++
 net/ipv6/ip6_output.c | 9 +++++++++
 2 files changed, 17 insertions(+)
net/ipv4/ip_output.c +8 -0
index d42d776af273..94a40e7ed534 100644
@@ -770,6 +770,14 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	struct ip_frag_state state;
 	int err = 0;
 
+	/* PSP and TLS offload need the packet's L4 header, which non-initial
+	 * fragments do not have.
+	 */
+	if (unlikely(skb_is_decrypted(skb))) {
+		err = -EMSGSIZE;
+		goto fail;
+	}
+
 	/* for offloaded checksums cleanup checksum before fragmentation */
 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
 		err = ip_check_csum_start(skb);
net/ipv6/ip6_output.c +9 -0
index cb648c839431..5b19683f08e0 100644
@@ -929,6 +929,15 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 			mtu = IPV6_MIN_MTU;
 	}
 
+	/* PSP and TLS offload need the packet's L4 header, which non-initial
+	 * fragments do not have. Checked after the too-big checks above,
+	 * so that PMTUD for a local socket works.
+	 */
+	if (unlikely(skb_is_decrypted(skb))) {
+		err = -EMSGSIZE;
+		goto fail;
+	}
+
 	if (np) {
 		u32 frag_size = READ_ONCE(np->frag_size);
 
Signature · 1 line
2.53.1-Meta
#7 lore raw

Re: [PATCH net v6 3/8] net: don't fragment decrypted skbs

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

on transmit, skb->decrypted means the device must encrypt the skb. if it can't do that, then the packet should be dropped, not transmitted in the clear.

ip_do_fragment() and ip6_fragment() on a decrypted skb produce fragments which have skb->decrypted unset, and can be sent out unencrypted.

tx of individually encrypted fragments would not work anyway, because encrypting needs the L4 header, which non-initial fragments do not have.

ktls could encrypt in sw before fragmenting. that would not work for psp offload which does not have a software fallback path.

Quoted 7 lines

tcp sizes segments to the mtu, so psp and ktls socket packets normally [ ... ]

Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
Fixes 659a2899a57d ("tcp: add datapath logic for PSP with inline key exchange")

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 4/8 ipv4: icmp: don't quote decrypted payload in icmp errors

In reply to Todd Freed

an icmp error quotes as much of the invoking packet as fits in 576 bytes. if the device had decrypted the packet (as indicated by skb->decrypted) then the icmp error is sending part of it back out onto the network in cleartext.

the two offloads encrypt different parts of the packet:

- ktls encrypts the tcp payload. the tcp header is in the clear on the wire, so a quote of the ip header and the tcp header, without the payload, would be fine.

- psp encryption also covers the tcp header. the quote could include just the ip header, but then that's not really useful. linux receivers want to look at the l4 layer on the quoted packet to look up the related flow, so such a truncated icmp error packet is just dropped (icmp_socket_deliver()).

a decrypted psp skb carries SKB_EXT_PSP, so the two could be told apart, and an error about a ktls skb could still quote up to the tcp header. but then whether the tcp header of a psp packet goes out in cleartext would depend on the extension not being lost anywhere between the device and here, while skb->decrypted is what the device itself set. so don't send an error about a received decrypted skb at all.

on tx, skb->decrypted is also set. an error about a locally originated skb (skb_iif is 0) is sent in full as before, if it is routed to a local address (RTCF_LOCAL on the route to the packet's source address).

iptables REJECT, and nft reject in the ip and inet families, send their icmp error with icmp_send(), through nf_send_unreach(). so now, a received packet which the device decrypted, and which hits such a reject rule in the INPUT or FORWARD chain, is dropped without a reply, like a drop rule. nft reject in the netdev and bridge families builds its own error (nf_reject_skb_v4_unreach()); a separate nf-next patch applies the same rule there.

packets which weren't decrypted are rejected as before, and so are locally originated ones in the OUTPUT chain, if the error goes to a local address as above.

the next patch does the same for icmpv6.

---
 net/ipv4/icmp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
net/ipv4/icmp.c +12 -0
index 90c0e22c29be..e11008aa4cec 100644
@@ -979,6 +979,18 @@ void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
 	if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
 		goto ende;
 
+	/* Don't send parts of a decrypted skb onto the network. A received skb
+	 * was decrypted by the device, and the transport header may have been
+	 * encrypted. The different offloads can't be distinguished in all cases
+	 * so simply don't send the error.
+	 *
+	 * A locally originated skb to be encrypted may be quoted if its source
+	 * address (target of the error) is local.
+	 */
+	if (unlikely(skb_is_decrypted(skb_in)) &&
+	    (skb_in->skb_iif || !(rt->rt_flags & RTCF_LOCAL)))
+		goto ende;
+
 	/* peer icmp_ratelimit */
 	if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
 		goto ende;
Signature · 1 line
2.53.1-Meta
#9 lore raw

Re: [PATCH net v6 4/8] ipv4: icmp: don't quote decrypted payload in icmp errors

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

an icmp error quotes as much of the invoking packet as fits in 576 bytes. if the device had decrypted the packet (as indicated by skb->decrypted) then the icmp error is sending part of it back out onto the network in cleartext.

the two offloads encrypt different parts of the packet:

- ktls encrypts the tcp payload. the tcp header is in the clear on the wire, so a quote of the ip header and the tcp header, without the payload, would be fine.

- psp encryption also covers the tcp header. the quote could include just the ip header, but then that's not really useful. linux receivers want to look at the l4 layer on the quoted packet to look up the related flow, so such a truncated icmp error packet is just dropped

Quoted 6 lines
[ ... ]
Fixes 784abe24c903 ("net: Add decrypted field to skb")

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 5/8 ipv6: icmpv6: don't quote decrypted payload in icmpv6 errors

In reply to Todd Freed

an icmpv6 error quotes as much of the invoking packet as fits in the minimum ipv6 mtu. if the device had decrypted the packet (as indicated by skb->decrypted) then the icmpv6 error is sending part of it back out onto the network in cleartext.

as in the previous patch for icmp, don't send an error about a received decrypted skb at all: psp also encrypts the l4 header, and telling it apart from ktls rx device offload would rely on SKB_EXT_PSP not being lost on the way. linux receivers would drop an error whose quote stops before the l4 header anyway (icmpv6_notify()). this covers ip6tables REJECT and nft reject in the ip6 and inet families; the netdev and bridge families are left to the nf-next patch, as for icmp.

on tx, skb->decrypted is also set. an error about a locally originated skb (skb_iif is 0) is sent in full as before, if its route goes to a local address (RTF_LOCAL), like RTCF_LOCAL in the previous patch.

an error about a received decrypted skb is dropped before the rate limit, so that it doesn't use up the peer's and the global icmpv6 credit. the check of a locally originated skb needs the route of the error, which is looked up after the rate limit.

---
 net/ipv6/icmp.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
net/ipv6/icmp.c +18 -0
index d8203ebad882..574a42d279e0 100644
@@ -778,6 +778,15 @@ void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
 
 	np = inet6_sk(sk);
 
+	/* Don't send parts of a decrypted skb onto the network, see
+	 * __icmp_send(): no error about a received skb. Before the rate
+	 * limit, so that such an error costs no credit. The check of a
+	 * locally originated skb below needs the route of the error, which
+	 * is looked up after the rate limit.
+	 */
+	if (unlikely(skb_is_decrypted(skb) && skb->skb_iif))
+		goto out_unlock;
+
 	if (!icmpv6_xrlim_allow(sk, type, &fl6, apply_ratelimit))
 		goto out_unlock;
 
@@ -799,6 +808,15 @@ void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info,
 	if (IS_ERR(dst))
 		goto out_unlock;
 
+	/* An error about a locally originated decrypted skb is sent only if
+	 * it is routed to a local address.
+	 */
+	if (unlikely(skb_is_decrypted(skb)) &&
+	    !(dst_rt6_info(dst)->rt6i_flags & RTF_LOCAL)) {
+		dst_release(dst);
+		goto out_unlock;
+	}
+
 	ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
 
 	msg.skb = skb;
Signature · 1 line
2.53.1-Meta
#11 lore raw

Re: [PATCH net v6 5/8] ipv6: icmpv6: don't quote decrypted payload in icmpv6 errors

Sashiko has reviewed this patch and found no issues.
Quoted 22 lines

an icmpv6 error quotes as much of the invoking packet as fits in the minimum ipv6 mtu. if the device had decrypted the packet (as indicated by skb->decrypted) then the icmpv6 error is sending part of it back out onto the network in cleartext.

as in the previous patch for icmp, don't send an error about a received decrypted skb at all: psp also encrypts the l4 header, and telling it apart from ktls rx device offload would rely on SKB_EXT_PSP not being lost on the way. linux receivers would drop an error whose quote stops before the l4 header anyway (icmpv6_notify()). this covers ip6tables REJECT and nft reject in the ip6 and inet families; the netdev and bridge families are left to the nf-next patch, as for icmp.

on tx, skb->decrypted is also set. an error about a locally originated skb (skb_iif is 0) is sent in full as before, if its route goes to a local address (RTF_LOCAL), like RTCF_LOCAL in the previous patch.

an error about a received decrypted skb is dropped before the rate limit, so that it doesn't use up the peer's and the global icmpv6 credit. the check of a locally originated skb needs the route of the error, which is looked up after the rate limit.

Quoted 7 lines

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 6/8 selftests: drv-net: psp: test ipv6 tcp socket with extension headers

In reply to Todd Freed

send on a psp tcp socket in three phases, checking the device's tx-packets and what the peer received after each:

1/ no socket options. the data is sent, counted, and received.

2/ an extension header set on the socket. the packets are dropped,
tx-packets is unchanged, and nothing reaches the peer. the data stays queued.

3/ option removed. the queued data, then new data, is sent, counted,
and received.

the test has three variants: a destination options header (IPV6_DSTOPTS) and a hop-by-hop options header (IPV6_HOPOPTS), each holding one padn option, and a segment routing header (IPV6_RTHDR).

a software filter on the peer also counts, and drops, udp packets from the socket's address to the peer's. psp packets reach the peer as decrypted tcp packets, as expected. packets which were encapsulated but are malformed or unencrypted (this is what mlx5 does) show up as udp, and the test asserts there are none. if the filter cannot be set up the test is skipped.

tx-packets is read after it has quiesced, because some devices refresh counters periodically (mlx5 takes about a second).

without the fix in "psp: don't encapsulate ipv6 packets with extension headers", netdevsim encapsulates the packets in a malformed way and counts them in tx-packets, and the test fails on that. with mlx5 (connectx-7), the packets go out unencrypted and aren't counted in tx-packets. the test fails on the udp packets which reach the peer. capturing on the peer,

tcpdump -i <dev> -nn -A 'ip6 and src host <sender> and not tcp'

shows them as udp, with the test's payload readable.

---
 tools/testing/selftests/drivers/net/config |   1 +
 tools/testing/selftests/drivers/net/psp.py | 150 ++++++++++++++++++++-
 2 files changed, 148 insertions(+), 3 deletions(-)
tools/testing/selftests/drivers/net/config +1 -0
index 4838adf27fa1..409f9a96a102 100644
@@ -4,6 +4,7 @@ CONFIG_DEBUG_INFO_BTF_MODULES=n
 CONFIG_INET_PSP=y
 CONFIG_IPV6=y
 CONFIG_MACSEC=m
+CONFIG_NET_ACT_GACT=m
 CONFIG_NET_ACT_SKBEDIT=m
 CONFIG_NET_CLS_ACT=y
 CONFIG_NET_CLS_BPF=y
tools/testing/selftests/drivers/net/psp.py +147 -3

178 lines not on this page · raw message

Signature · 1 line
2.53.1-Meta
#13 lore raw

Re: [PATCH net v6 6/8] selftests: drv-net: psp: test ipv6 tcp socket with extension headers

Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

send on a psp tcp socket in three phases, checking the device's tx-packets and what the peer received after each:

1/ no socket options. the data is sent, counted, and received.

2/ an extension header set on the socket. the packets are dropped,
tx-packets is unchanged, and nothing reaches the peer. the data stays queued.

3/ option removed. the queued data, then new data, is sent, counted,
and received.

the test has three variants: a destination options header (IPV6_DSTOPTS) and a hop-by-hop options header (IPV6_HOPOPTS), each holding one padn option, and a segment routing header (IPV6_RTHDR). [ ... ]
tcpdump -i <dev> -nn -A 'ip6 and src host <sender> and not tcp'

Quoted 3 lines

shows them as udp, with the test's payload readable.

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 7/8 selftests: drv-net: psp: test that ipv4 psp packets aren't fragmented

In reply to Todd Freed

make the ipv4 packets of a psp tcp socket need fragmenting, and check that they're dropped (IpFragFails) and not transmitted unencrypted.

tcp sizes its segments from the route mtu. with IP_PMTUDISC_OMIT, ip fragments them to the device mtu, and doesn't set df. so the test sets a route mtu of twice the larger of the two device mtus (at most 65535) on both ends (the peer's mss limits ours too), keeping the device and source address of the current route, and IP_PMTUDISC_OMIT on the socket. tcp mtu probing (tcp_mtu_probing 2, or 1 once a black hole is detected) can cap the mss at the probing base, so the test turns it off. if the peer isn't on-link, or the socket's mss doesn't need fragmenting, the test is skipped.

a software filter on the peer counts, and drops, the ipv4 fragments which reach it from the socket's address, and the test asserts there are none. IpFragFails is per netns, so it only confirms that the packets were dropped.

while both route mtus are in place, a new ssh connection to the remote over these addresses can stall: its segments are sized from the route mtu, and sent with df set. so the local route is added last, and removed before the next remote command.

no ipv6 test: the stack only fragments ipv6 tcp packets when something like ipvs sets ignore_df on them (see "net: don't fragment decrypted skbs"), which this test doesn't set up.

without the fix in "net: don't fragment decrypted skbs", the packets are fragmented, and the fragments go out unencrypted. they reach the peer, IpFragFails doesn't move, and the test fails.

---
 tools/testing/selftests/drivers/net/psp.py | 95 +++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)
tools/testing/selftests/drivers/net/psp.py +94 -1
index cae369d8ab63..9c1d4c59b1ac 100755
@@ -22,10 +22,12 @@ from lib.py import KsftSkipEx, KsftFailEx, CmdExitFailure
 from lib.py import NetDrvEpEnv, NetDrvContEnv
 from lib.py import Netlink, NlError, PSPFamily, RtnlFamily
 from lib.py import NetNSEnter
-from lib.py import bkg, rand_port, wait_port_listen
+from lib.py import bkg, ctl_file_write, rand_port, wait_port_listen
 from lib.py import ip, tc
 
 TCP_ULP = 31
+IP_MTU_DISCOVER = 10
+IP_PMTUDISC_OMIT = 5
 
 
 def _get_outq(s):
@@ -822,6 +824,97 @@ def data_send_ipv6_exthdr(cfg, opt, val):
             _close_psp_conn(cfg, s)
 
 
+def _ipv4_snmp(key):
+    """ An Ip: counter of this netns, from /proc/net/snmp """
+    with open("/proc/net/snmp", encoding="utf-8") as f:
+        ip_lines = [line.split() for line in f if line.startswith("Ip:")]
+    return int(ip_lines[1][ip_lines[0].index(key)])
+
+
+def _ipv4_route_mtu(dst, mtu, host=None):
+    """ Add a route to @dst with @mtu, keeping the device and source of
+    the current one, which must be on-link. Returns the defer removing it.
+    """
+    route = ip(f"-4 route get {dst}", json=True, host=host)[0]
+    if route.get("gateway"):
+        raise KsftSkipEx(f"{dst} isn't on-link")
+    try:
+        ip(f"route add {dst}/32 dev {route['dev']} src {route['prefsrc']} "
+           f"mtu {mtu}", host=host)
+    except CmdExitFailure as e:
+        raise KsftSkipEx(f"can't set the route mtu to {dst}: {e}") from e
+    return defer(ip, f"route del {dst}/32 dev {route['dev']}", host=host)
+
+
+def data_send_ipv4_frags(cfg):
+    """ Test that IPv4 packets of a PSP TCP socket aren't fragmented """
+    cfg.require_ipver("4")
+    _init_psp_dev(cfg)
+
+    # Fragments which get out reach the remote. Set up the count before
+    # connecting, so a skip doesn't leave the responder mid key exchange.
+    # The socket's source is the one the kernel picks, which may not be
+    # LOCAL_V4, and the fragments would leave through the route's device
+    rem = cfg.remote_addr_v["4"]
+    route = ip(f"-4 route get {rem}", json=True)[0]
+    if route["dev"] != cfg.ifname:
+        raise KsftSkipEx(f"{rem} isn't routed through {cfg.ifname}")
+    loc = route["prefsrc"]
+    remote_frags = _remote_count(cfg, "ip",
+                                 f"src_ip {loc} dst_ip {rem} ip_flags frag")
+    if not remote_frags:
+        raise KsftSkipEx("can't count fragments on the remote")
+
+    # TCP sizes its segments from the route MTU. With IP_PMTUDISC_OMIT, IP
+    # fragments them to the device MTU. Set a route MTU above the device
+    # MTU on both ends, since the peer's MSS limits ours too. While both
+    # are in place, a new ssh connection to the remote over these addresses
+    # can stall, so add the local one last, and remove it first.
+    mtu = min(2 * max(cfg.dev['mtu'], cfg.remote_dev['mtu']), 65535)
+    _ipv4_route_mtu(loc, mtu, host=cfg.remote)
+    local_route = _ipv4_route_mtu(rem, mtu)
+
+    # With MTU probing on, TCP can cap the MSS at the probing base, whatever
+    # the route MTU
+    ctl_file_write("/proc/sys/net/ipv4/tcp_mtu_probing", 0)
+
+    s = _make_psp_conn(cfg, 0, "4")
+    try:
+        rx_assoc = cfg.pspnl.rx_assoc({"version": 0,
+                                       "dev-id": cfg.psp_dev_id,
+                                       "sock-fd": s.fileno()})
+        tx = _spi_xchg(s, rx_assoc['rx-key'])
+        cfg.pspnl.tx_assoc({"dev-id": cfg.psp_dev_id,
+                            "version": 0,
+                            "tx-key": tx,
+                            "sock-fd": s.fileno()})
+        s.setsockopt(socket.IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT)
+        mss = s.getsockopt(socket.IPPROTO_TCP, socket.TCP_MAXSEG)
+        if mss + 40 <= cfg.dev['mtu']:
+            raise KsftSkipEx(f"mss {mss} needs no fragmenting")
+
+        fails = _ipv4_snmp("FragFails")
+        # The packets are dropped, so the send may not complete (with a
+        # jumbo device MTU, the socket buffer can take all of it)
+        s.settimeout(3)
+        try:
+            s.sendall(b'f' * 200000)
+        except socket.timeout:
+            pass
+        fails = _ipv4_snmp("FragFails") - fails
+
+        # Before the next ssh to the remote, see above
+        local_route.exec()
+
+        # Fragments would go out unencrypted: the packets must be dropped
+        # instead of fragmented. FragFails is per netns, so it only
+        # confirms the drop: the remote count is what decides.
+        ksft_eq(remote_frags(), 0, comment="fragments reached the remote")
+        ksft_gt(fails, 0, comment="packets dropped instead of fragmented")
+    finally:
+        _close_psp_conn(cfg, s)
+
+
 def _get_psp_ver_ip_variants():
     for ver in range(4):
         for ipv in ("4", "6"):
Signature · 1 line
2.53.1-Meta
#15 lore raw

Re: [PATCH net v6 7/8] selftests: drv-net: psp: test that ipv4 psp packets aren't fragmented

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

make the ipv4 packets of a psp tcp socket need fragmenting, and check that they're dropped (IpFragFails) and not transmitted unencrypted.

tcp sizes its segments from the route mtu. with IP_PMTUDISC_OMIT, ip fragments them to the device mtu, and doesn't set df. so the test sets a route mtu of twice the larger of the two device mtus (at most 65535) on both ends (the peer's mss limits ours too), keeping the device and source address of the current route, and IP_PMTUDISC_OMIT on the socket. tcp mtu probing (tcp_mtu_probing 2, or 1 once a black hole is detected) can cap the mss at the probing base, so the test turns it off. if the peer isn't on-link, or the socket's mss doesn't need fragmenting, the test is skipped.

Quoted 8 lines

a software filter on the peer counts, and drops, the ipv4 fragments which reach it from the socket's address, and the test asserts there are [ ... ]
without the fix in "net: don't fragment decrypted skbs", the packets are fragmented, and the fragments go out unencrypted. they reach the peer, IpFragFails doesn't move, and the test fails.

Sashiko has reviewed this patch and found no issues.

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

PATCH v6 8/8 net/mlx5e: psp: tso: replace only the length in checksum seed

In reply to Todd Freed

for psp tcp with tso,

- __tcp_transmit_skb() -> tcp_v6_send_check() or tcp_v4_send_check() computes the checksum seed using the addresses on the socket and the total length, then

- mlx5e_psp_handle_tx_skb() recomputes the checksum seed using addresses from the packet's l3 header, and the length of one segment

the length difference is expected by the device. however, the difference in addresses is incorrect in cases where the l3 header daddr is an intermediate hop, for example,

- ipv4 source route option (IP_OPTIONS with lsrr or ssrr)
- ipv6 IPV6_RTHDR header (conceptually ; ipv6 extension headers are not properly supported on this path yet)

in these cases every segment of the tso packet to go out with a bad tcp checksum

the fix is to replace only the length in the seed which tcp had already computed. this is also what tcp_gso_segment() does.

tested on connectx-7, with a psp tcp socket whose ipv4 loose source route's first hop isn't the final destination, and the peer checking tcp checksums in software.

Fixes e5a1861a298e ("net/mlx5e: Implement PSP Tx data path")
---
 .../mellanox/mlx5/core/en_accel/psp_rxtx.c    | 21 ++++++++-----------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/psp_rxtx.c +9 -12
index 348fd7a96261..d9c2742b3c24 100644
@@ -177,18 +177,15 @@ bool mlx5e_psp_handle_tx_skb(struct net_device *netdev,
 		return false;
 	}
 	if (skb_is_gso(skb)) {
-		int len = skb_shinfo(skb)->gso_size + inner_tcp_hdrlen(skb);
-		struct tcphdr *th = inner_tcp_hdr(skb);
-
-		if (skb->protocol == htons(ETH_P_IP)) {
-			const struct iphdr *ip = ip_hdr(skb);
-
-			th->check = ~tcp_v4_check(len, ip->saddr, ip->daddr, 0);
-		} else {
-			const struct ipv6hdr *ip6 = ipv6_hdr(skb);
-
-			th->check = ~tcp_v6_check(len, &ip6->saddr, &ip6->daddr, 0);
-		}
+		u32 seg_len = skb_shinfo(skb)->gso_size + inner_tcp_hdrlen(skb);
+		u32 len = skb->len - skb_inner_transport_offset(skb);
+
+		/* update just the length component of the checksum seed
+		 * tcp computed the seed with the total length, but mlx5
+		 * devices are expecting the segment length
+		 */
+		inet_proto_csum_replace4(&inner_tcp_hdr(skb)->check, skb,
+					 htonl(len), htonl(seg_len), true);
 	}
 
 	return true;
Signature · 1 line
2.53.1-Meta
#17 lore raw

Re: [PATCH net v6 8/8] net/mlx5e: psp: tso: replace only the length in checksum seed

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

for psp tcp with tso,

- __tcp_transmit_skb() -> tcp_v6_send_check() or tcp_v4_send_check() computes the checksum seed using the addresses on the socket and the total length, then

- mlx5e_psp_handle_tx_skb() recomputes the checksum seed using addresses from the packet's l3 header, and the length of one segment

the length difference is expected by the device. however, the difference in addresses is incorrect in cases where the l3 header daddr is an intermediate hop, for example,

Quoted 8 lines

- ipv4 source route option (IP_OPTIONS with lsrr or ssrr)
- ipv6 IPV6_RTHDR header (conceptually ; ipv6 extension headers are not
[ ... ]

Fixes e5a1861a298e ("net/mlx5e: Implement PSP Tx data path")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Reviewed-by on v6 8/8
#18 lore raw

Re: [PATCH net v6 8/8] net/mlx5e: psp: tso: replace only the length in checksum seed

In reply to Todd Freed
Quoted 10 lines · Todd Freed

for psp tcp with tso,

- __tcp_transmit_skb() -> tcp_v6_send_check() or tcp_v4_send_check() computes the checksum seed using the addresses on the socket and the total length, then

- mlx5e_psp_handle_tx_skb() recomputes the checksum seed using addresses from the packet's l3 header, and the length of one segment

the length difference is expected by the device. however, the difference

No cap in first char of sentence makes reading the commit message harder. For example is this sentence standalone or a continuation? Sorry for cramping your style.

Quoted 9 lines

in addresses is incorrect in cases where the l3 header daddr is an intermediate hop, for example,

- ipv4 source route option (IP_OPTIONS with lsrr or ssrr)
- ipv6 IPV6_RTHDR header (conceptually ; ipv6 extension headers are not properly supported on this path yet)

in these cases every segment of the tso packet to go out with a bad tcp checksum

What did you mean with "to go out" here? Goes out? Or is a verb missing?

Quoted 5 lines

the fix is to replace only the length in the seed which tcp had already

computed. this is also what tcp_gso_segment() does.

Quoted 8 lines

tested on connectx-7, with a psp tcp socket whose ipv4 loose source route's first hop isn't the final destination, and the peer checking tcp checksums in software.

Fixes e5a1861a298e ("net/mlx5e: Implement PSP Tx data path")

Other than that:

Thanks,
Dragos