netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 0/7] net: tls: fix some issues with async encryption
@ 2024-02-07  1:18 Jakub Kicinski
  2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
                   ` (8 more replies)
  0 siblings, 9 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem; +Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski

Hi!

valis was reporting a race on socket close so I sat down to try to fix it.
I used Sabrina's async crypto debug patch to test... and in the process
run into some of the same issues, and created very similar fixes :(
I didn't realize how many of those patches weren't applied. Once I found
Sabrina's code [1] it turned out to be so similar in fact that I added
her S-o-b's and Co-develop'eds in a semi-haphazard way.

With this series in place all expected tests pass with async crypto.
Sabrina had a few more fixes, but I'll leave those to her, things are
not crashing anymore.

[1] https://lore.kernel.org/netdev/cover.1694018970.git.sd@queasysnail.net/

Jakub Kicinski (6):
  net: tls: factor out tls_*crypt_async_wait()
  tls: fix race between async notify and socket close
  tls: fix race between tx work scheduling and socket close
  net: tls: handle backlogging of crypto requests
  selftests: tls: use exact comparison in recv_partial
  net: tls: fix returned read length with async decrypt

Sabrina Dubroca (1):
  net: tls: fix use-after-free with partial reads and async decrypt

 include/net/tls.h                 |   5 --
 net/tls/tls_sw.c                  | 135 ++++++++++++++----------------
 tools/testing/selftests/net/tls.c |   8 +-
 3 files changed, 66 insertions(+), 82 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait()
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:23   ` Simon Horman
  2024-02-10  9:08   ` Sabrina Dubroca
  2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
                   ` (7 subsequent siblings)
  8 siblings, 2 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	borisp, john.fastabend

Factor out waiting for async encrypt and decrypt to finish.
There are already multiple copies and a subsequent fix will
need more. No functional changes.

Note that crypto_wait_req() returns wait->err

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
---
 net/tls/tls_sw.c | 96 +++++++++++++++++++++++-------------------------
 1 file changed, 45 insertions(+), 51 deletions(-)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 31e8a94dfc11..6a73714f34cc 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -230,6 +230,20 @@ static void tls_decrypt_done(void *data, int err)
 	spin_unlock_bh(&ctx->decrypt_compl_lock);
 }
 
+static int tls_decrypt_async_wait(struct tls_sw_context_rx *ctx)
+{
+	int pending;
+
+	spin_lock_bh(&ctx->decrypt_compl_lock);
+	reinit_completion(&ctx->async_wait.completion);
+	pending = atomic_read(&ctx->decrypt_pending);
+	spin_unlock_bh(&ctx->decrypt_compl_lock);
+	if (pending)
+		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
+
+	return ctx->async_wait.err;
+}
+
 static int tls_do_decryption(struct sock *sk,
 			     struct scatterlist *sgin,
 			     struct scatterlist *sgout,
@@ -495,6 +509,28 @@ static void tls_encrypt_done(void *data, int err)
 		schedule_delayed_work(&ctx->tx_work.work, 1);
 }
 
+static int tls_encrypt_async_wait(struct tls_sw_context_tx *ctx)
+{
+	int pending;
+
+	spin_lock_bh(&ctx->encrypt_compl_lock);
+	ctx->async_notify = true;
+
+	pending = atomic_read(&ctx->encrypt_pending);
+	spin_unlock_bh(&ctx->encrypt_compl_lock);
+	if (pending)
+		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
+	else
+		reinit_completion(&ctx->async_wait.completion);
+
+	/* There can be no concurrent accesses, since we have no
+	 * pending encrypt operations
+	 */
+	WRITE_ONCE(ctx->async_notify, false);
+
+	return ctx->async_wait.err;
+}
+
 static int tls_do_encryption(struct sock *sk,
 			     struct tls_context *tls_ctx,
 			     struct tls_sw_context_tx *ctx,
@@ -984,7 +1020,6 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
 	int num_zc = 0;
 	int orig_size;
 	int ret = 0;
-	int pending;
 
 	if (!eor && (msg->msg_flags & MSG_EOR))
 		return -EINVAL;
@@ -1163,24 +1198,12 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
 	if (!num_async) {
 		goto send_end;
 	} else if (num_zc) {
+		int err;
+
 		/* Wait for pending encryptions to get completed */
-		spin_lock_bh(&ctx->encrypt_compl_lock);
-		ctx->async_notify = true;
-
-		pending = atomic_read(&ctx->encrypt_pending);
-		spin_unlock_bh(&ctx->encrypt_compl_lock);
-		if (pending)
-			crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
-		else
-			reinit_completion(&ctx->async_wait.completion);
-
-		/* There can be no concurrent accesses, since we have no
-		 * pending encrypt operations
-		 */
-		WRITE_ONCE(ctx->async_notify, false);
-
-		if (ctx->async_wait.err) {
-			ret = ctx->async_wait.err;
+		err = tls_encrypt_async_wait(ctx);
+		if (err) {
+			ret = err;
 			copied = 0;
 		}
 	}
@@ -1229,7 +1252,6 @@ void tls_sw_splice_eof(struct socket *sock)
 	ssize_t copied = 0;
 	bool retrying = false;
 	int ret = 0;
-	int pending;
 
 	if (!ctx->open_rec)
 		return;
@@ -1264,22 +1286,7 @@ void tls_sw_splice_eof(struct socket *sock)
 	}
 
 	/* Wait for pending encryptions to get completed */
-	spin_lock_bh(&ctx->encrypt_compl_lock);
-	ctx->async_notify = true;
-
-	pending = atomic_read(&ctx->encrypt_pending);
-	spin_unlock_bh(&ctx->encrypt_compl_lock);
-	if (pending)
-		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
-	else
-		reinit_completion(&ctx->async_wait.completion);
-
-	/* There can be no concurrent accesses, since we have no pending
-	 * encrypt operations
-	 */
-	WRITE_ONCE(ctx->async_notify, false);
-
-	if (ctx->async_wait.err)
+	if (tls_encrypt_async_wait(ctx))
 		goto unlock;
 
 	/* Transmit if any encryptions have completed */
@@ -2109,16 +2116,10 @@ int tls_sw_recvmsg(struct sock *sk,
 
 recv_end:
 	if (async) {
-		int ret, pending;
+		int ret;
 
 		/* Wait for all previously submitted records to be decrypted */
-		spin_lock_bh(&ctx->decrypt_compl_lock);
-		reinit_completion(&ctx->async_wait.completion);
-		pending = atomic_read(&ctx->decrypt_pending);
-		spin_unlock_bh(&ctx->decrypt_compl_lock);
-		ret = 0;
-		if (pending)
-			ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
+		ret = tls_decrypt_async_wait(ctx);
 		__skb_queue_purge(&ctx->async_hold);
 
 		if (ret) {
@@ -2435,16 +2436,9 @@ void tls_sw_release_resources_tx(struct sock *sk)
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
 	struct tls_rec *rec, *tmp;
-	int pending;
 
 	/* Wait for any pending async encryptions to complete */
-	spin_lock_bh(&ctx->encrypt_compl_lock);
-	ctx->async_notify = true;
-	pending = atomic_read(&ctx->encrypt_pending);
-	spin_unlock_bh(&ctx->encrypt_compl_lock);
-
-	if (pending)
-		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
+	tls_encrypt_async_wait(ctx);
 
 	tls_tx_records(sk, -1);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 2/7] tls: fix race between async notify and socket close
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
  2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:24   ` Simon Horman
                     ` (2 more replies)
  2024-02-07  1:18 ` [PATCH net 3/7] tls: fix race between tx work scheduling " Jakub Kicinski
                   ` (6 subsequent siblings)
  8 siblings, 3 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	valis, borisp, john.fastabend, vinay.yadav

The submitting thread (one which called recvmsg/sendmsg)
may exit as soon as the async crypto handler calls complete()
so any code past that point risks touching already freed data.

Try to avoid the locking and extra flags altogether.
Have the main thread hold an extra reference, this way
we can depend solely on the atomic ref counter for
synchronization.

Don't futz with reiniting the completion, either, we are now
tightly controlling when completion fires.

Reported-by: valis <sec@valis.email>
Fixes: 0cada33241d9 ("net/tls: fix race condition causing kernel panic")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
CC: vinay.yadav@chelsio.com
---
 include/net/tls.h |  5 -----
 net/tls/tls_sw.c  | 43 ++++++++++---------------------------------
 2 files changed, 10 insertions(+), 38 deletions(-)

diff --git a/include/net/tls.h b/include/net/tls.h
index 962f0c501111..340ad43971e4 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -97,9 +97,6 @@ struct tls_sw_context_tx {
 	struct tls_rec *open_rec;
 	struct list_head tx_list;
 	atomic_t encrypt_pending;
-	/* protect crypto_wait with encrypt_pending */
-	spinlock_t encrypt_compl_lock;
-	int async_notify;
 	u8 async_capable:1;
 
 #define BIT_TX_SCHEDULED	0
@@ -136,8 +133,6 @@ struct tls_sw_context_rx {
 	struct tls_strparser strp;
 
 	atomic_t decrypt_pending;
-	/* protect crypto_wait with decrypt_pending*/
-	spinlock_t decrypt_compl_lock;
 	struct sk_buff_head async_hold;
 	struct wait_queue_head wq;
 };
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 6a73714f34cc..635305bebfef 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -224,22 +224,15 @@ static void tls_decrypt_done(void *data, int err)
 
 	kfree(aead_req);
 
-	spin_lock_bh(&ctx->decrypt_compl_lock);
-	if (!atomic_dec_return(&ctx->decrypt_pending))
+	if (atomic_dec_and_test(&ctx->decrypt_pending))
 		complete(&ctx->async_wait.completion);
-	spin_unlock_bh(&ctx->decrypt_compl_lock);
 }
 
 static int tls_decrypt_async_wait(struct tls_sw_context_rx *ctx)
 {
-	int pending;
-
-	spin_lock_bh(&ctx->decrypt_compl_lock);
-	reinit_completion(&ctx->async_wait.completion);
-	pending = atomic_read(&ctx->decrypt_pending);
-	spin_unlock_bh(&ctx->decrypt_compl_lock);
-	if (pending)
+	if (!atomic_dec_and_test(&ctx->decrypt_pending))
 		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
+	atomic_inc(&ctx->decrypt_pending);
 
 	return ctx->async_wait.err;
 }
@@ -267,6 +260,7 @@ static int tls_do_decryption(struct sock *sk,
 		aead_request_set_callback(aead_req,
 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
 					  tls_decrypt_done, aead_req);
+		DEBUG_NET_WARN_ON_ONCE(atomic_read(&ctx->decrypt_pending) < 1);
 		atomic_inc(&ctx->decrypt_pending);
 	} else {
 		aead_request_set_callback(aead_req,
@@ -455,7 +449,6 @@ static void tls_encrypt_done(void *data, int err)
 	struct sk_msg *msg_en;
 	bool ready = false;
 	struct sock *sk;
-	int pending;
 
 	msg_en = &rec->msg_encrypted;
 
@@ -494,12 +487,8 @@ static void tls_encrypt_done(void *data, int err)
 			ready = true;
 	}
 
-	spin_lock_bh(&ctx->encrypt_compl_lock);
-	pending = atomic_dec_return(&ctx->encrypt_pending);
-
-	if (!pending && ctx->async_notify)
+	if (atomic_dec_and_test(&ctx->encrypt_pending))
 		complete(&ctx->async_wait.completion);
-	spin_unlock_bh(&ctx->encrypt_compl_lock);
 
 	if (!ready)
 		return;
@@ -511,22 +500,9 @@ static void tls_encrypt_done(void *data, int err)
 
 static int tls_encrypt_async_wait(struct tls_sw_context_tx *ctx)
 {
-	int pending;
-
-	spin_lock_bh(&ctx->encrypt_compl_lock);
-	ctx->async_notify = true;
-
-	pending = atomic_read(&ctx->encrypt_pending);
-	spin_unlock_bh(&ctx->encrypt_compl_lock);
-	if (pending)
+	if (!atomic_dec_and_test(&ctx->encrypt_pending))
 		crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
-	else
-		reinit_completion(&ctx->async_wait.completion);
-
-	/* There can be no concurrent accesses, since we have no
-	 * pending encrypt operations
-	 */
-	WRITE_ONCE(ctx->async_notify, false);
+	atomic_inc(&ctx->encrypt_pending);
 
 	return ctx->async_wait.err;
 }
@@ -577,6 +553,7 @@ static int tls_do_encryption(struct sock *sk,
 
 	/* Add the record in tx_list */
 	list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
+	DEBUG_NET_WARN_ON_ONCE(atomic_read(&ctx->encrypt_pending) < 1);
 	atomic_inc(&ctx->encrypt_pending);
 
 	rc = crypto_aead_encrypt(aead_req);
@@ -2601,7 +2578,7 @@ static struct tls_sw_context_tx *init_ctx_tx(struct tls_context *ctx, struct soc
 	}
 
 	crypto_init_wait(&sw_ctx_tx->async_wait);
-	spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
+	atomic_set(&sw_ctx_tx->encrypt_pending, 1);
 	INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
 	INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
 	sw_ctx_tx->tx_work.sk = sk;
@@ -2622,7 +2599,7 @@ static struct tls_sw_context_rx *init_ctx_rx(struct tls_context *ctx)
 	}
 
 	crypto_init_wait(&sw_ctx_rx->async_wait);
-	spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
+	atomic_set(&sw_ctx_rx->decrypt_pending, 1);
 	init_waitqueue_head(&sw_ctx_rx->wq);
 	skb_queue_head_init(&sw_ctx_rx->rx_list);
 	skb_queue_head_init(&sw_ctx_rx->async_hold);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 3/7] tls: fix race between tx work scheduling and socket close
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
  2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
  2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:24   ` Simon Horman
  2024-02-10  9:12   ` Sabrina Dubroca
  2024-02-07  1:18 ` [PATCH net 4/7] net: tls: handle backlogging of crypto requests Jakub Kicinski
                   ` (5 subsequent siblings)
  8 siblings, 2 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	valis, borisp, john.fastabend, vakul.garg

Similarly to previous commit, the submitting thread (recvmsg/sendmsg)
may exit as soon as the async crypto handler calls complete().
Reorder scheduling the work before calling complete().
This seems more logical in the first place, as it's
the inverse order of what the submitting thread will do.

Reported-by: valis <sec@valis.email>
Fixes: a42055e8d2c3 ("net/tls: Add support for async encryption of records for performance")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
CC: vakul.garg@nxp.com
---
 net/tls/tls_sw.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 635305bebfef..9374a61cef00 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -447,7 +447,6 @@ static void tls_encrypt_done(void *data, int err)
 	struct tls_rec *rec = data;
 	struct scatterlist *sge;
 	struct sk_msg *msg_en;
-	bool ready = false;
 	struct sock *sk;
 
 	msg_en = &rec->msg_encrypted;
@@ -483,19 +482,16 @@ static void tls_encrypt_done(void *data, int err)
 		/* If received record is at head of tx_list, schedule tx */
 		first_rec = list_first_entry(&ctx->tx_list,
 					     struct tls_rec, list);
-		if (rec == first_rec)
-			ready = true;
+		if (rec == first_rec) {
+			/* Schedule the transmission */
+			if (!test_and_set_bit(BIT_TX_SCHEDULED,
+					      &ctx->tx_bitmask))
+				schedule_delayed_work(&ctx->tx_work.work, 1);
+		}
 	}
 
 	if (atomic_dec_and_test(&ctx->encrypt_pending))
 		complete(&ctx->async_wait.completion);
-
-	if (!ready)
-		return;
-
-	/* Schedule the transmission */
-	if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
-		schedule_delayed_work(&ctx->tx_work.work, 1);
 }
 
 static int tls_encrypt_async_wait(struct tls_sw_context_tx *ctx)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 4/7] net: tls: handle backlogging of crypto requests
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (2 preceding siblings ...)
  2024-02-07  1:18 ` [PATCH net 3/7] tls: fix race between tx work scheduling " Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:25   ` Simon Horman
  2024-02-07  1:18 ` [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt Jakub Kicinski
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	borisp, john.fastabend, vakul.garg, davejwatson

Since we're setting the CRYPTO_TFM_REQ_MAY_BACKLOG flag on our
requests to the crypto API, crypto_aead_{encrypt,decrypt} can return
 -EBUSY instead of -EINPROGRESS in valid situations. For example, when
the cryptd queue for AESNI is full (easy to trigger with an
artificially low cryptd.cryptd_max_cpu_qlen), requests will be enqueued
to the backlog but still processed. In that case, the async callback
will also be called twice: first with err == -EINPROGRESS, which it
seems we can just ignore, then with err == 0.

Compared to Sabrina's original patch this version uses the new
tls_*crypt_async_wait() helpers and converts the EBUSY to
EINPROGRESS to avoid having to modify all the error handling
paths. The handling is identical.

Fixes: a54667f6728c ("tls: Add support for encryption using async offload accelerator")
Fixes: 94524d8fc965 ("net/tls: Add support for async decryption of tls records")
Co-developed-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Link: https://lore.kernel.org/netdev/9681d1febfec295449a62300938ed2ae66983f28.1694018970.git.sd@queasysnail.net/
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
CC: vakul.garg@nxp.com
CC: davejwatson@fb.com
---
 net/tls/tls_sw.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 9374a61cef00..63bef5666e36 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -196,6 +196,17 @@ static void tls_decrypt_done(void *data, int err)
 	struct sock *sk;
 	int aead_size;
 
+	/* If requests get too backlogged crypto API returns -EBUSY and calls
+	 * ->complete(-EINPROGRESS) immediately followed by ->complete(0)
+	 * to make waiting for backlog to flush with crypto_wait_req() easier.
+	 * First wait converts -EBUSY -> -EINPROGRESS, and the second one
+	 * -EINPROGRESS -> 0.
+	 * We have a single struct crypto_async_request per direction, this
+	 * scheme doesn't help us, so just ignore the first ->complete().
+	 */
+	if (err == -EINPROGRESS)
+		return;
+
 	aead_size = sizeof(*aead_req) + crypto_aead_reqsize(aead);
 	aead_size = ALIGN(aead_size, __alignof__(*dctx));
 	dctx = (void *)((u8 *)aead_req + aead_size);
@@ -269,6 +280,10 @@ static int tls_do_decryption(struct sock *sk,
 	}
 
 	ret = crypto_aead_decrypt(aead_req);
+	if (ret == -EBUSY) {
+		ret = tls_decrypt_async_wait(ctx);
+		ret = ret ?: -EINPROGRESS;
+	}
 	if (ret == -EINPROGRESS) {
 		if (darg->async)
 			return 0;
@@ -449,6 +464,9 @@ static void tls_encrypt_done(void *data, int err)
 	struct sk_msg *msg_en;
 	struct sock *sk;
 
+	if (err == -EINPROGRESS) /* see the comment in tls_decrypt_done() */
+		return;
+
 	msg_en = &rec->msg_encrypted;
 
 	sk = rec->sk;
@@ -553,6 +571,10 @@ static int tls_do_encryption(struct sock *sk,
 	atomic_inc(&ctx->encrypt_pending);
 
 	rc = crypto_aead_encrypt(aead_req);
+	if (rc == -EBUSY) {
+		rc = tls_encrypt_async_wait(ctx);
+		rc = rc ?: -EINPROGRESS;
+	}
 	if (!rc || rc != -EINPROGRESS) {
 		atomic_dec(&ctx->encrypt_pending);
 		sge->offset -= prot->prepend_size;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (3 preceding siblings ...)
  2024-02-07  1:18 ` [PATCH net 4/7] net: tls: handle backlogging of crypto requests Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:25   ` Simon Horman
  2024-02-07  1:18 ` [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial Jakub Kicinski
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	borisp, john.fastabend

From: Sabrina Dubroca <sd@queasysnail.net>

tls_decrypt_sg doesn't take a reference on the pages from clear_skb,
so the put_page() in tls_decrypt_done releases them, and we trigger
a use-after-free in process_rx_list when we try to read from the
partially-read skb.

Fixes: fd31f3996af2 ("tls: rx: decrypt into a fresh skb")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
This is pretty much Sabrina's patch just addressing my own
feedback, so I'm keeping her as the author.
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
---
 net/tls/tls_sw.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 63bef5666e36..a6eff21ade23 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -63,6 +63,7 @@ struct tls_decrypt_ctx {
 	u8 iv[TLS_MAX_IV_SIZE];
 	u8 aad[TLS_MAX_AAD_SIZE];
 	u8 tail;
+	bool free_sgout;
 	struct scatterlist sg[];
 };
 
@@ -187,7 +188,6 @@ static void tls_decrypt_done(void *data, int err)
 	struct aead_request *aead_req = data;
 	struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
 	struct scatterlist *sgout = aead_req->dst;
-	struct scatterlist *sgin = aead_req->src;
 	struct tls_sw_context_rx *ctx;
 	struct tls_decrypt_ctx *dctx;
 	struct tls_context *tls_ctx;
@@ -224,7 +224,7 @@ static void tls_decrypt_done(void *data, int err)
 	}
 
 	/* Free the destination pages if skb was not decrypted inplace */
-	if (sgout != sgin) {
+	if (dctx->free_sgout) {
 		/* Skip the first S/G entry as it points to AAD */
 		for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
 			if (!sg)
@@ -1583,6 +1583,7 @@ static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
 	} else if (out_sg) {
 		memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
 	}
+	dctx->free_sgout = !!pages;
 
 	/* Prepare and submit AEAD request */
 	err = tls_do_decryption(sk, sgin, sgout, dctx->iv,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (4 preceding siblings ...)
  2024-02-07  1:18 ` [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:25   ` Simon Horman
  2024-02-07  1:18 ` [PATCH net 7/7] net: tls: fix returned read length with async decrypt Jakub Kicinski
                   ` (2 subsequent siblings)
  8 siblings, 1 reply; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	shuah, linux-kselftest

This exact case was fail for async crypto and we weren't
catching it.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: shuah@kernel.org
CC: linux-kselftest@vger.kernel.org
---
 tools/testing/selftests/net/tls.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/net/tls.c
index 7799e042a971..bc36c91c4480 100644
--- a/tools/testing/selftests/net/tls.c
+++ b/tools/testing/selftests/net/tls.c
@@ -1002,12 +1002,12 @@ TEST_F(tls, recv_partial)
 
 	memset(recv_mem, 0, sizeof(recv_mem));
 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
-	EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_first),
-		       MSG_WAITALL), -1);
+	EXPECT_EQ(recv(self->cfd, recv_mem, strlen(test_str_first),
+		       MSG_WAITALL), strlen(test_str_first));
 	EXPECT_EQ(memcmp(test_str_first, recv_mem, strlen(test_str_first)), 0);
 	memset(recv_mem, 0, sizeof(recv_mem));
-	EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_second),
-		       MSG_WAITALL), -1);
+	EXPECT_EQ(recv(self->cfd, recv_mem, strlen(test_str_second),
+		       MSG_WAITALL), strlen(test_str_second));
 	EXPECT_EQ(memcmp(test_str_second, recv_mem, strlen(test_str_second)),
 		  0);
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH net 7/7] net: tls: fix returned read length with async decrypt
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (5 preceding siblings ...)
  2024-02-07  1:18 ` [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial Jakub Kicinski
@ 2024-02-07  1:18 ` Jakub Kicinski
  2024-02-09  9:22   ` Simon Horman
  2024-02-10  9:02   ` Sabrina Dubroca
  2024-02-10  9:05 ` [PATCH net 0/7] net: tls: fix some issues with async encryption Sabrina Dubroca
  2024-02-10 21:40 ` patchwork-bot+netdevbpf
  8 siblings, 2 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-07  1:18 UTC (permalink / raw)
  To: davem
  Cc: netdev, edumazet, pabeni, sd, vadim.fedorenko, Jakub Kicinski,
	borisp, john.fastabend, horms

We double count async, non-zc rx data. The previous fix was
lucky because if we fully zc async_copy_bytes is 0 so we add 0.
Decrypted already has all the bytes we handled, in all cases.
We don't have to adjust anything, delete the erroneous line.

Fixes: 4d42cd6bc2ac ("tls: rx: fix return value for async crypto")
Co-developed-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
CC: borisp@nvidia.com
CC: john.fastabend@gmail.com
CC: horms@kernel.org
---
 net/tls/tls_sw.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index a6eff21ade23..9fbc70200cd0 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -2132,7 +2132,6 @@ int tls_sw_recvmsg(struct sock *sk,
 		else
 			err = process_rx_list(ctx, msg, &control, 0,
 					      async_copy_bytes, is_peek);
-		decrypted += max(err, 0);
 	}
 
 	copied += decrypted;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH net 7/7] net: tls: fix returned read length with async decrypt
  2024-02-07  1:18 ` [PATCH net 7/7] net: tls: fix returned read length with async decrypt Jakub Kicinski
@ 2024-02-09  9:22   ` Simon Horman
  2024-02-10  9:02   ` Sabrina Dubroca
  1 sibling, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:22 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, borisp,
	john.fastabend

On Tue, Feb 06, 2024 at 05:18:24PM -0800, Jakub Kicinski wrote:
> We double count async, non-zc rx data. The previous fix was
> lucky because if we fully zc async_copy_bytes is 0 so we add 0.
> Decrypted already has all the bytes we handled, in all cases.
> We don't have to adjust anything, delete the erroneous line.
> 
> Fixes: 4d42cd6bc2ac ("tls: rx: fix return value for async crypto")
> Co-developed-by: Sabrina Dubroca <sd@queasysnail.net>
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait()
  2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
@ 2024-02-09  9:23   ` Simon Horman
  2024-02-10  9:08   ` Sabrina Dubroca
  1 sibling, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:23 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, borisp,
	john.fastabend

On Tue, Feb 06, 2024 at 05:18:18PM -0800, Jakub Kicinski wrote:
> Factor out waiting for async encrypt and decrypt to finish.
> There are already multiple copies and a subsequent fix will
> need more. No functional changes.
> 
> Note that crypto_wait_req() returns wait->err
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 2/7] tls: fix race between async notify and socket close
  2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
@ 2024-02-09  9:24   ` Simon Horman
  2024-02-09  9:47   ` Eric Dumazet
  2024-02-10  9:11   ` Sabrina Dubroca
  2 siblings, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:24 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, valis,
	borisp, john.fastabend, vinay.yadav

On Tue, Feb 06, 2024 at 05:18:19PM -0800, Jakub Kicinski wrote:
> The submitting thread (one which called recvmsg/sendmsg)
> may exit as soon as the async crypto handler calls complete()
> so any code past that point risks touching already freed data.
> 
> Try to avoid the locking and extra flags altogether.
> Have the main thread hold an extra reference, this way
> we can depend solely on the atomic ref counter for
> synchronization.
> 
> Don't futz with reiniting the completion, either, we are now
> tightly controlling when completion fires.
> 
> Reported-by: valis <sec@valis.email>
> Fixes: 0cada33241d9 ("net/tls: fix race condition causing kernel panic")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 3/7] tls: fix race between tx work scheduling and socket close
  2024-02-07  1:18 ` [PATCH net 3/7] tls: fix race between tx work scheduling " Jakub Kicinski
@ 2024-02-09  9:24   ` Simon Horman
  2024-02-10  9:12   ` Sabrina Dubroca
  1 sibling, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:24 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, valis,
	borisp, john.fastabend, vakul.garg

On Tue, Feb 06, 2024 at 05:18:20PM -0800, Jakub Kicinski wrote:
> Similarly to previous commit, the submitting thread (recvmsg/sendmsg)
> may exit as soon as the async crypto handler calls complete().
> Reorder scheduling the work before calling complete().
> This seems more logical in the first place, as it's
> the inverse order of what the submitting thread will do.
> 
> Reported-by: valis <sec@valis.email>
> Fixes: a42055e8d2c3 ("net/tls: Add support for async encryption of records for performance")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 4/7] net: tls: handle backlogging of crypto requests
  2024-02-07  1:18 ` [PATCH net 4/7] net: tls: handle backlogging of crypto requests Jakub Kicinski
@ 2024-02-09  9:25   ` Simon Horman
  0 siblings, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:25 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, borisp,
	john.fastabend, vakul.garg, davejwatson

On Tue, Feb 06, 2024 at 05:18:21PM -0800, Jakub Kicinski wrote:
> Since we're setting the CRYPTO_TFM_REQ_MAY_BACKLOG flag on our
> requests to the crypto API, crypto_aead_{encrypt,decrypt} can return
>  -EBUSY instead of -EINPROGRESS in valid situations. For example, when
> the cryptd queue for AESNI is full (easy to trigger with an
> artificially low cryptd.cryptd_max_cpu_qlen), requests will be enqueued
> to the backlog but still processed. In that case, the async callback
> will also be called twice: first with err == -EINPROGRESS, which it
> seems we can just ignore, then with err == 0.
> 
> Compared to Sabrina's original patch this version uses the new
> tls_*crypt_async_wait() helpers and converts the EBUSY to
> EINPROGRESS to avoid having to modify all the error handling
> paths. The handling is identical.
> 
> Fixes: a54667f6728c ("tls: Add support for encryption using async offload accelerator")
> Fixes: 94524d8fc965 ("net/tls: Add support for async decryption of tls records")
> Co-developed-by: Sabrina Dubroca <sd@queasysnail.net>
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> Link: https://lore.kernel.org/netdev/9681d1febfec295449a62300938ed2ae66983f28.1694018970.git.sd@queasysnail.net/
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt
  2024-02-07  1:18 ` [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt Jakub Kicinski
@ 2024-02-09  9:25   ` Simon Horman
  0 siblings, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:25 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, borisp,
	john.fastabend

On Tue, Feb 06, 2024 at 05:18:22PM -0800, Jakub Kicinski wrote:
> From: Sabrina Dubroca <sd@queasysnail.net>
> 
> tls_decrypt_sg doesn't take a reference on the pages from clear_skb,
> so the put_page() in tls_decrypt_done releases them, and we trigger
> a use-after-free in process_rx_list when we try to read from the
> partially-read skb.
> 
> Fixes: fd31f3996af2 ("tls: rx: decrypt into a fresh skb")
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> This is pretty much Sabrina's patch just addressing my own
> feedback, so I'm keeping her as the author.

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial
  2024-02-07  1:18 ` [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial Jakub Kicinski
@ 2024-02-09  9:25   ` Simon Horman
  0 siblings, 0 replies; 23+ messages in thread
From: Simon Horman @ 2024-02-09  9:25 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko, shuah,
	linux-kselftest

On Tue, Feb 06, 2024 at 05:18:23PM -0800, Jakub Kicinski wrote:
> This exact case was fail for async crypto and we weren't
> catching it.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 2/7] tls: fix race between async notify and socket close
  2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
  2024-02-09  9:24   ` Simon Horman
@ 2024-02-09  9:47   ` Eric Dumazet
  2024-02-10  9:11   ` Sabrina Dubroca
  2 siblings, 0 replies; 23+ messages in thread
From: Eric Dumazet @ 2024-02-09  9:47 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, pabeni, sd, vadim.fedorenko, valis, borisp,
	john.fastabend, vinay.yadav

On Wed, Feb 7, 2024 at 2:19 AM Jakub Kicinski <kuba@kernel.org> wrote:
>
> The submitting thread (one which called recvmsg/sendmsg)
> may exit as soon as the async crypto handler calls complete()
> so any code past that point risks touching already freed data.
>
> Try to avoid the locking and extra flags altogether.
> Have the main thread hold an extra reference, this way
> we can depend solely on the atomic ref counter for
> synchronization.
>
> Don't futz with reiniting the completion, either, we are now
> tightly controlling when completion fires.
>
> Reported-by: valis <sec@valis.email>
> Fixes: 0cada33241d9 ("net/tls: fix race condition causing kernel panic")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> CC: borisp@nvidia.com
> CC: john.fastabend@gmail.com
> CC: vinay.yadav@chelsio.com

Thanks Jakub, this looks much nicer indeed.

Reviewed-by: Eric Dumazet <edumazet@google.com>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 7/7] net: tls: fix returned read length with async decrypt
  2024-02-07  1:18 ` [PATCH net 7/7] net: tls: fix returned read length with async decrypt Jakub Kicinski
  2024-02-09  9:22   ` Simon Horman
@ 2024-02-10  9:02   ` Sabrina Dubroca
  2024-02-12 17:11     ` Jakub Kicinski
  1 sibling, 1 reply; 23+ messages in thread
From: Sabrina Dubroca @ 2024-02-10  9:02 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko, borisp,
	john.fastabend, horms

2024-02-06, 17:18:24 -0800, Jakub Kicinski wrote:
> We double count async, non-zc rx data. The previous fix was
> lucky because if we fully zc async_copy_bytes is 0 so we add 0.
> Decrypted already has all the bytes we handled, in all cases.
> We don't have to adjust anything, delete the erroneous line.

I had an adjustment there which I thought was necessary, but I can't
remember why, so let's go with this.

Possibly had to do with partial async cases, since I also played with
a hack to only go async for every other request (or every N requests).

-- 
Sabrina


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 0/7] net: tls: fix some issues with async encryption
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (6 preceding siblings ...)
  2024-02-07  1:18 ` [PATCH net 7/7] net: tls: fix returned read length with async decrypt Jakub Kicinski
@ 2024-02-10  9:05 ` Sabrina Dubroca
  2024-02-10 21:40 ` patchwork-bot+netdevbpf
  8 siblings, 0 replies; 23+ messages in thread
From: Sabrina Dubroca @ 2024-02-10  9:05 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko

2024-02-06, 17:18:17 -0800, Jakub Kicinski wrote:
> Hi!
> 
> valis was reporting a race on socket close so I sat down to try to fix it.
> I used Sabrina's async crypto debug patch to test... and in the process
> run into some of the same issues, and created very similar fixes :(
> I didn't realize how many of those patches weren't applied. Once I found
> Sabrina's code [1] it turned out to be so similar in fact that I added
> her S-o-b's and Co-develop'eds in a semi-haphazard way.
> 
> With this series in place all expected tests pass with async crypto.
> Sabrina had a few more fixes, but I'll leave those to her, things are
> not crashing anymore.

Sorry :(
I got stuck trying to fix a race condition (probably one of those
you're fixing in this series, I tried something similar to patch 3 but
that wasn't enough), and then got distracted. I had a v2 ready and
never posted it :/

Thanks for taking over, and sorry for the duplicate effort. I'll go
back to my old series and see if anything is still relevant on top of
this.

-- 
Sabrina


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait()
  2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
  2024-02-09  9:23   ` Simon Horman
@ 2024-02-10  9:08   ` Sabrina Dubroca
  1 sibling, 0 replies; 23+ messages in thread
From: Sabrina Dubroca @ 2024-02-10  9:08 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko, borisp,
	john.fastabend

2024-02-06, 17:18:18 -0800, Jakub Kicinski wrote:
> Factor out waiting for async encrypt and decrypt to finish.
> There are already multiple copies and a subsequent fix will
> need more. No functional changes.
> 
> Note that crypto_wait_req() returns wait->err
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>

-- 
Sabrina


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 2/7] tls: fix race between async notify and socket close
  2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
  2024-02-09  9:24   ` Simon Horman
  2024-02-09  9:47   ` Eric Dumazet
@ 2024-02-10  9:11   ` Sabrina Dubroca
  2 siblings, 0 replies; 23+ messages in thread
From: Sabrina Dubroca @ 2024-02-10  9:11 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko, valis, borisp,
	john.fastabend, vinay.yadav

2024-02-06, 17:18:19 -0800, Jakub Kicinski wrote:
> The submitting thread (one which called recvmsg/sendmsg)
> may exit as soon as the async crypto handler calls complete()
> so any code past that point risks touching already freed data.
> 
> Try to avoid the locking and extra flags altogether.
> Have the main thread hold an extra reference, this way
> we can depend solely on the atomic ref counter for
> synchronization.
> 
> Don't futz with reiniting the completion, either, we are now
> tightly controlling when completion fires.
> 
> Reported-by: valis <sec@valis.email>
> Fixes: 0cada33241d9 ("net/tls: fix race condition causing kernel panic")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>

-- 
Sabrina


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 3/7] tls: fix race between tx work scheduling and socket close
  2024-02-07  1:18 ` [PATCH net 3/7] tls: fix race between tx work scheduling " Jakub Kicinski
  2024-02-09  9:24   ` Simon Horman
@ 2024-02-10  9:12   ` Sabrina Dubroca
  1 sibling, 0 replies; 23+ messages in thread
From: Sabrina Dubroca @ 2024-02-10  9:12 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko, valis, borisp,
	john.fastabend, vakul.garg

2024-02-06, 17:18:20 -0800, Jakub Kicinski wrote:
> Similarly to previous commit, the submitting thread (recvmsg/sendmsg)
> may exit as soon as the async crypto handler calls complete().
> Reorder scheduling the work before calling complete().
> This seems more logical in the first place, as it's
> the inverse order of what the submitting thread will do.
> 
> Reported-by: valis <sec@valis.email>
> Fixes: a42055e8d2c3 ("net/tls: Add support for async encryption of records for performance")
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>

Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>

-- 
Sabrina


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 0/7] net: tls: fix some issues with async encryption
  2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
                   ` (7 preceding siblings ...)
  2024-02-10  9:05 ` [PATCH net 0/7] net: tls: fix some issues with async encryption Sabrina Dubroca
@ 2024-02-10 21:40 ` patchwork-bot+netdevbpf
  8 siblings, 0 replies; 23+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-10 21:40 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: davem, netdev, edumazet, pabeni, sd, vadim.fedorenko

Hello:

This series was applied to netdev/net.git (main)
by David S. Miller <davem@davemloft.net>:

On Tue,  6 Feb 2024 17:18:17 -0800 you wrote:
> Hi!
> 
> valis was reporting a race on socket close so I sat down to try to fix it.
> I used Sabrina's async crypto debug patch to test... and in the process
> run into some of the same issues, and created very similar fixes :(
> I didn't realize how many of those patches weren't applied. Once I found
> Sabrina's code [1] it turned out to be so similar in fact that I added
> her S-o-b's and Co-develop'eds in a semi-haphazard way.
> 
> [...]

Here is the summary with links:
  - [net,1/7] net: tls: factor out tls_*crypt_async_wait()
    https://git.kernel.org/netdev/net/c/c57ca512f3b6
  - [net,2/7] tls: fix race between async notify and socket close
    https://git.kernel.org/netdev/net/c/aec7961916f3
  - [net,3/7] tls: fix race between tx work scheduling and socket close
    https://git.kernel.org/netdev/net/c/e01e3934a1b2
  - [net,4/7] net: tls: handle backlogging of crypto requests
    https://git.kernel.org/netdev/net/c/859054147318
  - [net,5/7] net: tls: fix use-after-free with partial reads and async decrypt
    https://git.kernel.org/netdev/net/c/32b55c5ff910
  - [net,6/7] selftests: tls: use exact comparison in recv_partial
    https://git.kernel.org/netdev/net/c/49d821064c44
  - [net,7/7] net: tls: fix returned read length with async decrypt
    https://git.kernel.org/netdev/net/c/ac437a51ce66

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH net 7/7] net: tls: fix returned read length with async decrypt
  2024-02-10  9:02   ` Sabrina Dubroca
@ 2024-02-12 17:11     ` Jakub Kicinski
  0 siblings, 0 replies; 23+ messages in thread
From: Jakub Kicinski @ 2024-02-12 17:11 UTC (permalink / raw)
  To: Sabrina Dubroca
  Cc: davem, netdev, edumazet, pabeni, vadim.fedorenko, borisp,
	john.fastabend, horms

On Sat, 10 Feb 2024 10:02:07 +0100 Sabrina Dubroca wrote:
> 2024-02-06, 17:18:24 -0800, Jakub Kicinski wrote:
> > We double count async, non-zc rx data. The previous fix was
> > lucky because if we fully zc async_copy_bytes is 0 so we add 0.
> > Decrypted already has all the bytes we handled, in all cases.
> > We don't have to adjust anything, delete the erroneous line.  
> 
> I had an adjustment there which I thought was necessary, but I can't
> remember why, so let's go with this.
> 
> Possibly had to do with partial async cases, since I also played with
> a hack to only go async for every other request (or every N requests).

I must have seen some reason to keep it back in 4d42cd6bc2ac as well :S
Let's see if someone complains..

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2024-02-12 17:11 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-07  1:18 [PATCH net 0/7] net: tls: fix some issues with async encryption Jakub Kicinski
2024-02-07  1:18 ` [PATCH net 1/7] net: tls: factor out tls_*crypt_async_wait() Jakub Kicinski
2024-02-09  9:23   ` Simon Horman
2024-02-10  9:08   ` Sabrina Dubroca
2024-02-07  1:18 ` [PATCH net 2/7] tls: fix race between async notify and socket close Jakub Kicinski
2024-02-09  9:24   ` Simon Horman
2024-02-09  9:47   ` Eric Dumazet
2024-02-10  9:11   ` Sabrina Dubroca
2024-02-07  1:18 ` [PATCH net 3/7] tls: fix race between tx work scheduling " Jakub Kicinski
2024-02-09  9:24   ` Simon Horman
2024-02-10  9:12   ` Sabrina Dubroca
2024-02-07  1:18 ` [PATCH net 4/7] net: tls: handle backlogging of crypto requests Jakub Kicinski
2024-02-09  9:25   ` Simon Horman
2024-02-07  1:18 ` [PATCH net 5/7] net: tls: fix use-after-free with partial reads and async decrypt Jakub Kicinski
2024-02-09  9:25   ` Simon Horman
2024-02-07  1:18 ` [PATCH net 6/7] selftests: tls: use exact comparison in recv_partial Jakub Kicinski
2024-02-09  9:25   ` Simon Horman
2024-02-07  1:18 ` [PATCH net 7/7] net: tls: fix returned read length with async decrypt Jakub Kicinski
2024-02-09  9:22   ` Simon Horman
2024-02-10  9:02   ` Sabrina Dubroca
2024-02-12 17:11     ` Jakub Kicinski
2024-02-10  9:05 ` [PATCH net 0/7] net: tls: fix some issues with async encryption Sabrina Dubroca
2024-02-10 21:40 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).