Skip to contentSkip to conversation

tls: rx: strp: fix inline crypto offload

PATCH Applied Jakub Kicinski 3 participants 16 messages May 17, 2023 – May 19, 2023

Subsystems (3)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover tls: rx: strp: fix inline crypto offload

The local strparser version I added to TLS does not preserve decryption status, which breaks inline crypto (NIC offload).

Jakub Kicinski (7):
tls: rx: device: fix checking decryption status
tls: rx: strp: set the skb->len of detached / CoW'ed skbs
tls: rx: strp: force mixed decrypted records into copy mode
tls: rx: strp: fix determining record length in copy mode
tls: rx: strp: factor out copying skb data
tls: rx: strp: preserve decryption status of skbs when needed
tls: rx: strp: don't use GFP_KERNEL in softirq context

 include/linux/skbuff.h |  10 +++
 include/net/tls.h      |   1 +
 net/tls/tls.h          |   5 ++
 net/tls/tls_device.c   |  22 ++---
 net/tls/tls_strp.c     | 185 +++++++++++++++++++++++++++++++++--------
 net/tls/tls_sw.c       |   4 +
 6 files changed, 177 insertions(+), 50 deletions(-)
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/7 tls: rx: device: fix checking decryption status

skb->len covers the entire skb, including the frag_list. In fact we're guaranteed that rxm->full_len <= skb->len, so since the change under Fixes we were not checking decrypt status of any skb but the first.

Note that the skb_pagelen() added here may feel a bit costly, but it's removed by subsequent fixes, anyway.

Fixes 86b259f6f888 ("tls: rx: device: bound the frag walk")
---
 net/tls/tls_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/tls/tls_device.c +1 -1
index a7cc4f9faac2..3b87c7b04ac8 100644
@@ -1012,7 +1012,7 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
 	struct sk_buff *skb_iter;
 	int left;
 
-	left = rxm->full_len - skb->len;
+	left = rxm->full_len + rxm->offset - skb_pagelen(skb);
 	/* Check if all the data is decrypted already */
 	skb_iter = skb_shinfo(skb)->frag_list;
 	while (skb_iter && left > 0) {
Signature · 1 line
2.40.1
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH net 1/7] tls: rx: device: fix checking decryption status

Quoted 5 lines · Jakub Kicinski

skb->len covers the entire skb, including the frag_list. In fact we're guaranteed that rxm->full_len <= skb->len, so since the change under Fixes we were not checking decrypt status of any skb but the first.

Quoted 7 lines

Note that the skb_pagelen() added here may feel a bit costly, but it's removed by subsequent fixes, anyway.

Fixes 86b259f6f888 ("tls: rx: device: bound the frag walk")
author maintainer
#3 lore raw

PATCH 2/7 tls: rx: strp: set the skb->len of detached / CoW'ed skbs

In reply to Jakub Kicinski

alloc_skb_with_frags() fills in page frag sizes but does not set skb->len and skb->data_len. Set those correctly otherwise device offload will most likely generate an empty skb and hit the BUG() at the end of __skb_nsg().

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
---
 net/tls/tls_strp.c | 2 ++
 1 file changed, 2 insertions(+)
net/tls/tls_strp.c +2 -0
index 955ac3e0bf4d..24016c865e00 100644
@@ -56,6 +56,8 @@ static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
 		offset += skb_frag_size(frag);
 	}
 
+	skb->len = strp->stm.full_len;
+	skb->data_len = strp->stm.full_len;
 	skb_copy_header(skb, strp->anchor);
 	rxm = strp_msg(skb);
 	rxm->offset = 0;
Signature · 1 line
2.40.1
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH net 2/7] tls: rx: strp: set the skb->len of detached / CoW'ed skbs

Quoted 8 lines · Jakub Kicinski

alloc_skb_with_frags() fills in page frag sizes but does not set skb->len and skb->data_len. Set those correctly otherwise device offload will most likely generate an empty skb and hit the BUG() at the end of __skb_nsg().

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
author maintainer
#5 lore raw

PATCH 3/7 tls: rx: strp: force mixed decrypted records into copy mode

In reply to Jakub Kicinski

If a record is partially decrypted we'll have to CoW it, anyway, so go into copy mode and allocate a writable skb right away.

This will make subsequent fix simpler because we won't have to teach tls_strp_msg_make_copy() how to copy skbs while preserving decrypt status.

---
 include/linux/skbuff.h | 10 ++++++++++
 net/tls/tls_strp.c     | 16 +++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)
include/linux/skbuff.h +10 -0
index 738776ab8838..0b40417457cd 100644
@@ -1587,6 +1587,16 @@ static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
 	to->l4_hash = from->l4_hash;
 };
 
+static inline int skb_cmp_decrypted(const struct sk_buff *skb1,
+				    const struct sk_buff *skb2)
+{
+#ifdef CONFIG_TLS_DEVICE
+	return skb2->decrypted - skb1->decrypted;
+#else
+	return 0;
+#endif
+}
+
 static inline void skb_copy_decrypted(struct sk_buff *to,
 				      const struct sk_buff *from)
 {
net/tls/tls_strp.c +11 -5
index 24016c865e00..2b6fa9855999 100644
@@ -317,15 +317,19 @@ static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)
 	return 0;
 }
 
-static bool tls_strp_check_no_dup(struct tls_strparser *strp)
+static bool tls_strp_check_queue_ok(struct tls_strparser *strp)
 {
 	unsigned int len = strp->stm.offset + strp->stm.full_len;
-	struct sk_buff *skb;
+	struct sk_buff *first, *skb;
 	u32 seq;
 
-	skb = skb_shinfo(strp->anchor)->frag_list;
-	seq = TCP_SKB_CB(skb)->seq;
+	first = skb_shinfo(strp->anchor)->frag_list;
+	skb = first;
+	seq = TCP_SKB_CB(first)->seq;
 
+	/* Make sure there's no duplicate data in the queue,
+	 * and the decrypted status matches.
+	 */
 	while (skb->len < len) {
 		seq += skb->len;
 		len -= skb->len;
@@ -333,6 +337,8 @@ static bool tls_strp_check_no_dup(struct tls_strparser *strp)
 
 		if (TCP_SKB_CB(skb)->seq != seq)
 			return false;
+		if (skb_cmp_decrypted(first, skb))
+			return false;
 	}
 
 	return true;
@@ -413,7 +419,7 @@ static int tls_strp_read_sock(struct tls_strparser *strp)
 			return tls_strp_read_copy(strp, true);
 	}
 
-	if (!tls_strp_check_no_dup(strp))
+	if (!tls_strp_check_queue_ok(strp))
 		return tls_strp_read_copy(strp, false);
 
 	strp->msg_ready = 1;
Signature · 1 line
2.40.1
Reviewed-by on v1 3/7
#6 lore raw

Re: [PATCH net 3/7] tls: rx: strp: force mixed decrypted records into copy mode

Quoted 6 lines · Jakub Kicinski

If a record is partially decrypted we'll have to CoW it, anyway, so go into copy mode and allocate a writable skb right away.

This will make subsequent fix simpler because we won't have to teach tls_strp_msg_make_copy() how to copy skbs while preserving decrypt status.

author maintainer
#7 lore raw

PATCH 4/7 tls: rx: strp: fix determining record length in copy mode

In reply to Jakub Kicinski

We call tls_rx_msg_size(skb) before doing skb->len += chunk. So the tls_rx_msg_size() code will see old skb->len, most likely leading to an over-read.

Worst case we will over read an entire record, next iteration will try to trim the skb but may end up turning frag len negative or discarding the subsequent record (since we already told TCP we've read it during previous read but now we'll trim it out of the skb).

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
---
 net/tls/tls_strp.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
net/tls/tls_strp.c +15 -6
index 2b6fa9855999..e2e48217e7ac 100644
@@ -210,19 +210,28 @@ static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
 					   skb_frag_size(frag),
 					   chunk));
 
-		sz = tls_rx_msg_size(strp, strp->anchor);
+		skb->len += chunk;
+		skb->data_len += chunk;
+		skb_frag_size_add(frag, chunk);
+
+		sz = tls_rx_msg_size(strp, skb);
 		if (sz < 0) {
 			desc->error = sz;
 			return 0;
 		}
 
 		/* We may have over-read, sz == 0 is guaranteed under-read */
-		if (sz > 0)
-			chunk =	min_t(size_t, chunk, sz - skb->len);
+		if (unlikely(sz && sz < skb->len)) {
+			int over = skb->len - sz;
+
+			WARN_ON_ONCE(over > chunk);
+			skb->len -= over;
+			skb->data_len -= over;
+			skb_frag_size_add(frag, -over);
+
+			chunk -= over;
+		}
 
-		skb->len += chunk;
-		skb->data_len += chunk;
-		skb_frag_size_add(frag, chunk);
 		frag++;
 		len -= chunk;
 		offset += chunk;
Signature · 1 line
2.40.1
Reviewed-by on v1 4/7
#8 lore raw

Re: [PATCH net 4/7] tls: rx: strp: fix determining record length in copy mode

Quoted 10 lines · Jakub Kicinski

We call tls_rx_msg_size(skb) before doing skb->len += chunk. So the tls_rx_msg_size() code will see old skb->len, most likely leading to an over-read.

Worst case we will over read an entire record, next iteration will try to trim the skb but may end up turning frag len negative or discarding the subsequent record (since we already told TCP we've read it during previous read but now we'll trim it out of the skb).

Quoted 3 lines
Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
author maintainer
#9 lore raw

PATCH 5/7 tls: rx: strp: factor out copying skb data

In reply to Jakub Kicinski

We'll need to copy input skbs individually in the next patch. Factor that code out (without assuming we're copying a full record).

---
 net/tls/tls_strp.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
net/tls/tls_strp.c +23 -10
index e2e48217e7ac..61fbf84baf9e 100644
@@ -34,31 +34,44 @@ static void tls_strp_anchor_free(struct tls_strparser *strp)
 	strp->anchor = NULL;
 }
 
-/* Create a new skb with the contents of input copied to its page frags */
-static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
+static struct sk_buff *
+tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
+		  int offset, int len)
 {
-	struct strp_msg *rxm;
 	struct sk_buff *skb;
-	int i, err, offset;
+	int i, err;
 
-	skb = alloc_skb_with_frags(0, strp->stm.full_len, TLS_PAGE_ORDER,
+	skb = alloc_skb_with_frags(0, len, TLS_PAGE_ORDER,
 				   &err, strp->sk->sk_allocation);
 	if (!skb)
 		return NULL;
 
-	offset = strp->stm.offset;
 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 
-		WARN_ON_ONCE(skb_copy_bits(strp->anchor, offset,
+		WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
 					   skb_frag_address(frag),
 					   skb_frag_size(frag)));
 		offset += skb_frag_size(frag);
 	}
 
-	skb->len = strp->stm.full_len;
-	skb->data_len = strp->stm.full_len;
-	skb_copy_header(skb, strp->anchor);
+	skb->len = len;
+	skb->data_len = len;
+	skb_copy_header(skb, in_skb);
+	return skb;
+}
+
+/* Create a new skb with the contents of input copied to its page frags */
+static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp)
+{
+	struct strp_msg *rxm;
+	struct sk_buff *skb;
+
+	skb = tls_strp_skb_copy(strp, strp->anchor, strp->stm.offset,
+				strp->stm.full_len);
+	if (!skb)
+		return NULL;
+
 	rxm = strp_msg(skb);
 	rxm->offset = 0;
 	return skb;
Signature · 1 line
2.40.1
author maintainer
#11 lore raw

PATCH 6/7 tls: rx: strp: preserve decryption status of skbs when needed

In reply to Jakub Kicinski

When receive buffer is small we try to copy out the data from TCP into a skb maintained by TLS to prevent connection from stalling. Unfortunately if a single record is made up of a mix of decrypted and non-decrypted skbs combining them into a single skb leads to loss of decryption status, resulting in decryption errors or data corruption.

Similarly when trying to use TCP receive queue directly we need to make sure that all the skbs within the record have the same status. If we don't the mixed status will be detected correctly but we'll CoW the anchor, again collapsing it into a single paged skb without decrypted status preserved. So the "fixup" code will not know which parts of skb to re-encrypt.

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
---
 include/net/tls.h    |   1 +
 net/tls/tls.h        |   5 ++
 net/tls/tls_device.c |  22 +++-----
 net/tls/tls_strp.c   | 117 ++++++++++++++++++++++++++++++++++++-------
 4 files changed, 114 insertions(+), 31 deletions(-)
include/net/tls.h +1 -0
index 6056ce5a2aa5..596595c4b1af 100644
@@ -126,6 +126,7 @@ struct tls_strparser {
 	u32 mark : 8;
 	u32 stopped : 1;
 	u32 copy_mode : 1;
+	u32 mixed_decrypted : 1;
 	u32 msg_ready : 1;
 
 	struct strp_msg stm;
net/tls/tls.h +5 -0
index 804c3880d028..0672acab2773 100644
@@ -167,6 +167,11 @@ static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)
 	return ctx->strp.msg_ready;
 }
 
+static inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx)
+{
+	return ctx->strp.mixed_decrypted;
+}
+
 #ifdef CONFIG_TLS_DEVICE
 int tls_device_init(void);
 void tls_device_cleanup(void);
net/tls/tls_device.c +8 -14
index 3b87c7b04ac8..bf69c9d6d06c 100644
@@ -1007,20 +1007,14 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
 	struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
 	struct sk_buff *skb = tls_strp_msg(sw_ctx);
 	struct strp_msg *rxm = strp_msg(skb);
-	int is_decrypted = skb->decrypted;
-	int is_encrypted = !is_decrypted;
-	struct sk_buff *skb_iter;
-	int left;
-
-	left = rxm->full_len + rxm->offset - skb_pagelen(skb);
-	/* Check if all the data is decrypted already */
-	skb_iter = skb_shinfo(skb)->frag_list;
-	while (skb_iter && left > 0) {
-		is_decrypted &= skb_iter->decrypted;
-		is_encrypted &= !skb_iter->decrypted;
-
-		left -= skb_iter->len;
-		skb_iter = skb_iter->next;
+	int is_decrypted, is_encrypted;
+
+	if (!tls_strp_msg_mixed_decrypted(sw_ctx)) {
+		is_decrypted = skb->decrypted;
+		is_encrypted = !is_decrypted;
+	} else {
+		is_decrypted = 0;
+		is_encrypted = 0;
 	}
 
 	trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
net/tls/tls_strp.c +100 -17

154 lines not on this page · raw message

Signature · 1 line
2.40.1
Reviewed-by on v1 6/7
#12 lore raw

Re: [PATCH net 6/7] tls: rx: strp: preserve decryption status of skbs when needed

Quoted 14 lines · Jakub Kicinski

When receive buffer is small we try to copy out the data from TCP into a skb maintained by TLS to prevent connection from stalling. Unfortunately if a single record is made up of a mix of decrypted and non-decrypted skbs combining them into a single skb leads to loss of decryption status, resulting in decryption errors or data corruption.

Similarly when trying to use TCP receive queue directly we need to make sure that all the skbs within the record have the same status. If we don't the mixed status will be detected correctly but we'll CoW the anchor, again collapsing it into a single paged skb without decrypted status preserved. So the "fixup" code will not know which parts of skb to re-encrypt.

Quoted 3 lines
Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
author maintainer
#13 lore raw

PATCH 7/7 tls: rx: strp: don't use GFP_KERNEL in softirq context

In reply to Jakub Kicinski

When receive buffer is small, or the TCP rx queue looks too complicated to bother using it directly - we allocate a new skb and copy data into it.

We already use sk->sk_allocation... but nothing actually sets it to GFP_ATOMIC on the ->sk_data_ready() path.

Users of HW offload are far more likely to experience problems due to scheduling while atomic. "Copy mode" is very rarely triggered with SW crypto.

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
---
 net/tls/tls_sw.c | 4 ++++
 1 file changed, 4 insertions(+)
net/tls/tls_sw.c +4 -0
index 635b8bf6b937..6e6a7c37d685 100644
@@ -2304,10 +2304,14 @@ static void tls_data_ready(struct sock *sk)
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
 	struct sk_psock *psock;
+	gfp_t alloc_save;
 
 	trace_sk_data_ready(sk);
 
+	alloc_save = sk->sk_allocation;
+	sk->sk_allocation = GFP_ATOMIC;
 	tls_strp_data_ready(&ctx->strp);
+	sk->sk_allocation = alloc_save;
 
 	psock = sk_psock_get(sk);
 	if (psock) {
Signature · 1 line
2.40.1
Reviewed-by on v1 7/7
#14 lore raw

Re: [PATCH net 7/7] tls: rx: strp: don't use GFP_KERNEL in softirq context

Quoted 7 lines · Jakub Kicinski

When receive buffer is small, or the TCP rx queue looks too complicated to bother using it directly - we allocate a new skb and copy data into it.

We already use sk->sk_allocation... but nothing actually sets it to GFP_ATOMIC on the ->sk_data_ready() path.

Quoted 7 lines

Users of HW offload are far more likely to experience problems due to scheduling while atomic. "Copy mode" is very rarely triggered with SW crypto.

Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")
#15 lore raw
In reply to Jakub Kicinski
This series was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 12 lines · On Tue, 16 May 2023 18:50:35 -0700 you wrote:

The local strparser version I added to TLS does not preserve decryption status, which breaks inline crypto (NIC offload).

Jakub Kicinski (7):
tls: rx: device: fix checking decryption status
tls: rx: strp: set the skb->len of detached / CoW'ed skbs
tls: rx: strp: force mixed decrypted records into copy mode
tls: rx: strp: fix determining record length in copy mode
tls: rx: strp: factor out copying skb data
tls: rx: strp: preserve decryption status of skbs when needed
tls: rx: strp: don't use GFP_KERNEL in softirq context

Quoted 1 line

[...]

Here is the summary with links:
- [net,1/7] tls: rx: device: fix checking decryption status

    https://git.kernel.org/netdev/net/c/b3a03b540e3c
  - [net,2/7] tls: rx: strp: set the skb->len of detached / CoW'ed skbs
    https://git.kernel.org/netdev/net/c/210620ae44a8
  - [net,3/7] tls: rx: strp: force mixed decrypted records into copy mode
    https://git.kernel.org/netdev/net/c/14c4be92ebb3
  - [net,4/7] tls: rx: strp: fix determining record length in copy mode
    https://git.kernel.org/netdev/net/c/8b0c0dc9fbbd
  - [net,5/7] tls: rx: strp: factor out copying skb data
    https://git.kernel.org/netdev/net/c/c1c607b1e5d5
  - [net,6/7] tls: rx: strp: preserve decryption status of skbs when needed
    https://git.kernel.org/netdev/net/c/eca9bfafee3a
  - [net,7/7] tls: rx: strp: don't use GFP_KERNEL in softirq context
    https://git.kernel.org/netdev/net/c/74836ec828fe

You are awesome, thank you!

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