2023-02-06 12:03:43

by Herbert Xu

[permalink] [raw]
Subject: [PATCH 8/17] tls: Only use data field in crypto completion function

The crypto_async_request passed to the completion is not guaranteed
to be the original request object. Only the data field can be relied
upon.

Fix this by storing the socket pointer with the AEAD request.

Signed-off-by: Herbert Xu <[email protected]>
---

net/tls/tls.h | 2 ++
net/tls/tls_sw.c | 40 +++++++++++++++++++++++++++++-----------
2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/net/tls/tls.h b/net/tls/tls.h
index 0e840a0c3437..804c3880d028 100644
--- a/net/tls/tls.h
+++ b/net/tls/tls.h
@@ -70,6 +70,8 @@ struct tls_rec {
char content_type;
struct scatterlist sg_content_type;

+ struct sock *sk;
+
char aad_space[TLS_AAD_SPACE_SIZE];
u8 iv_data[MAX_IV_SIZE];
struct aead_request aead_req;
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 9ed978634125..5b7f67a7d394 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -38,6 +38,7 @@
#include <linux/bug.h>
#include <linux/sched/signal.h>
#include <linux/module.h>
+#include <linux/kernel.h>
#include <linux/splice.h>
#include <crypto/aead.h>

@@ -57,6 +58,7 @@ struct tls_decrypt_arg {
};

struct tls_decrypt_ctx {
+ struct sock *sk;
u8 iv[MAX_IV_SIZE];
u8 aad[TLS_MAX_AAD_SIZE];
u8 tail;
@@ -177,18 +179,25 @@ static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
return sub;
}

-static void tls_decrypt_done(struct crypto_async_request *req, int err)
+static void tls_decrypt_done(crypto_completion_data_t *data, int err)
{
- struct aead_request *aead_req = (struct aead_request *)req;
+ struct aead_request *aead_req = crypto_get_completion_data(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;
struct scatterlist *sg;
unsigned int pages;
struct sock *sk;
+ int aead_size;

- sk = (struct sock *)req->data;
+ aead_size = sizeof(*aead_req) + crypto_aead_reqsize(aead);
+ aead_size = ALIGN(aead_size, __alignof__(*dctx));
+ dctx = (void *)((u8 *)aead_req + aead_size);
+
+ sk = dctx->sk;
tls_ctx = tls_get_ctx(sk);
ctx = tls_sw_ctx_rx(tls_ctx);

@@ -240,7 +249,7 @@ static int tls_do_decryption(struct sock *sk,
if (darg->async) {
aead_request_set_callback(aead_req,
CRYPTO_TFM_REQ_MAY_BACKLOG,
- tls_decrypt_done, sk);
+ tls_decrypt_done, aead_req);
atomic_inc(&ctx->decrypt_pending);
} else {
aead_request_set_callback(aead_req,
@@ -336,6 +345,8 @@ static struct tls_rec *tls_get_rec(struct sock *sk)
sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
sg_unmark_end(&rec->sg_aead_out[1]);

+ rec->sk = sk;
+
return rec;
}

@@ -417,22 +428,27 @@ int tls_tx_records(struct sock *sk, int flags)
return rc;
}

-static void tls_encrypt_done(struct crypto_async_request *req, int err)
+static void tls_encrypt_done(crypto_completion_data_t *data, int err)
{
- struct aead_request *aead_req = (struct aead_request *)req;
- struct sock *sk = req->data;
- struct tls_context *tls_ctx = tls_get_ctx(sk);
- struct tls_prot_info *prot = &tls_ctx->prot_info;
- struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+ struct aead_request *aead_req = crypto_get_completion_data(data);
+ struct tls_sw_context_tx *ctx;
+ struct tls_context *tls_ctx;
+ struct tls_prot_info *prot;
struct scatterlist *sge;
struct sk_msg *msg_en;
struct tls_rec *rec;
bool ready = false;
+ struct sock *sk;
int pending;

rec = container_of(aead_req, struct tls_rec, aead_req);
msg_en = &rec->msg_encrypted;

+ sk = rec->sk;
+ tls_ctx = tls_get_ctx(sk);
+ prot = &tls_ctx->prot_info;
+ ctx = tls_sw_ctx_tx(tls_ctx);
+
sge = sk_msg_elem(msg_en, msg_en->sg.curr);
sge->offset -= prot->prepend_size;
sge->length += prot->prepend_size;
@@ -520,7 +536,7 @@ static int tls_do_encryption(struct sock *sk,
data_len, rec->iv_data);

aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
- tls_encrypt_done, sk);
+ tls_encrypt_done, aead_req);

/* Add the record in tx_list */
list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
@@ -1485,6 +1501,7 @@ static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
* Both structs are variable length.
*/
aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
+ aead_size = ALIGN(aead_size, __alignof__(*dctx));
mem = kmalloc(aead_size + struct_size(dctx, sg, n_sgin + n_sgout),
sk->sk_allocation);
if (!mem) {
@@ -1495,6 +1512,7 @@ static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
/* Segment the allocated memory */
aead_req = (struct aead_request *)mem;
dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
+ dctx->sk = sk;
sgin = &dctx->sg[0];
sgout = &dctx->sg[n_sgin];



2023-02-07 07:15:27

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH 8/17] tls: Only use data field in crypto completion function

On Mon, 06 Feb 2023 18:22:27 +0800 Herbert Xu wrote:
> -static void tls_encrypt_done(struct crypto_async_request *req, int err)
> +static void tls_encrypt_done(crypto_completion_data_t *data, int err)
> {
> - struct aead_request *aead_req = (struct aead_request *)req;
> - struct sock *sk = req->data;
> - struct tls_context *tls_ctx = tls_get_ctx(sk);
> - struct tls_prot_info *prot = &tls_ctx->prot_info;
> - struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
> + struct aead_request *aead_req = crypto_get_completion_data(data);

All we use aead_req for in this function now is to find rec...

> + struct tls_sw_context_tx *ctx;
> + struct tls_context *tls_ctx;
> + struct tls_prot_info *prot;
> struct scatterlist *sge;
> struct sk_msg *msg_en;
> struct tls_rec *rec;
> bool ready = false;
> + struct sock *sk;
> int pending;
>
> rec = container_of(aead_req, struct tls_rec, aead_req);
> msg_en = &rec->msg_encrypted;
>
> + sk = rec->sk;
> + tls_ctx = tls_get_ctx(sk);
> + prot = &tls_ctx->prot_info;
> + ctx = tls_sw_ctx_tx(tls_ctx);
> +
> sge = sk_msg_elem(msg_en, msg_en->sg.curr);
> sge->offset -= prot->prepend_size;
> sge->length += prot->prepend_size;
> @@ -520,7 +536,7 @@ static int tls_do_encryption(struct sock *sk,
> data_len, rec->iv_data);
>
> aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
> - tls_encrypt_done, sk);
> + tls_encrypt_done, aead_req);

... let's just pass rec instead of aead_req here, then?

> /* Add the record in tx_list */
> list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);

2023-02-07 08:19:12

by Herbert Xu

[permalink] [raw]
Subject: [PATCH] tls: Pass rec instead of aead_req into tls_encrypt_done

On Mon, Feb 06, 2023 at 11:15:21PM -0800, Jakub Kicinski wrote:
>
> > aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
> > - tls_encrypt_done, sk);
> > + tls_encrypt_done, aead_req);
>
> ... let's just pass rec instead of aead_req here, then?

Good point. Could we do this as a follow-up patch? Reposting
the whole series would disturb a lot of people. Of course if
other major issues crop up I can fold this into the existing
patch.

Thanks!

---8<---
The function tls_encrypt_done only uses aead_req to get ahold of
the tls_rec object. So we could pass that in instead of aead_req
to simplify the code.

Suggested-by: Jakub Kicinski <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>

diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 0515cda32fe2..6dfec2e8fdfa 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -430,18 +430,16 @@ int tls_tx_records(struct sock *sk, int flags)

static void tls_encrypt_done(void *data, int err)
{
- struct aead_request *aead_req = data;
struct tls_sw_context_tx *ctx;
struct tls_context *tls_ctx;
struct tls_prot_info *prot;
+ struct tls_rec *rec = data;
struct scatterlist *sge;
struct sk_msg *msg_en;
- struct tls_rec *rec;
bool ready = false;
struct sock *sk;
int pending;

- rec = container_of(aead_req, struct tls_rec, aead_req);
msg_en = &rec->msg_encrypted;

sk = rec->sk;
@@ -536,7 +534,7 @@ static int tls_do_encryption(struct sock *sk,
data_len, rec->iv_data);

aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
- tls_encrypt_done, aead_req);
+ tls_encrypt_done, rec);

/* Add the record in tx_list */
list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
--
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2023-02-07 08:41:54

by bluez.test.bot

[permalink] [raw]
Subject: RE: tls: Pass rec instead of aead_req into tls_encrypt_done

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: net/tls/tls_sw.c:430
error: net/tls/tls_sw.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.


---
Regards,
Linux Bluetooth

2023-02-07 18:50:42

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH] tls: Pass rec instead of aead_req into tls_encrypt_done

On Tue, 7 Feb 2023 16:18:36 +0800 Herbert Xu wrote:
> > > aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
> > > - tls_encrypt_done, sk);
> > > + tls_encrypt_done, aead_req);
> >
> > ... let's just pass rec instead of aead_req here, then?
>
> Good point. Could we do this as a follow-up patch? Reposting
> the whole series would disturb a lot of people. Of course if
> other major issues crop up I can fold this into the existing
> patch.

Whatever works best!

Reviewed-by: Jakub Kicinski <[email protected]>