Skip to content

Commit ec69bbf

Browse files
committed
crypto: algif_skcipher - Do not assume that req is unchanged
The async path in algif_skcipher assumes that the crypto completion function will be called with the original request. This is not necessarily the case. In fact there is no need for this anyway since we already embed information into the request with struct skcipher_async_req. This patch adds a pointer to that struct and then passes it as the data to the callback function. Cc: stable@vger.kernel.org Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Tested-by: Tadeusz Struk <tadeusz.struk@intel.com>
1 parent 63e41eb commit ec69bbf

1 file changed

Lines changed: 28 additions & 32 deletions

File tree

‎crypto/algif_skcipher.c‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,10 @@ struct skcipher_async_req {
6565
struct skcipher_async_rsgl first_sgl;
6666
struct list_head list;
6767
struct scatterlist *tsg;
68-
char iv[];
68+
atomic_t *inflight;
69+
struct skcipher_request req;
6970
};
7071

71-
#define GET_SREQ(areq, ctx) (struct skcipher_async_req *)((char *)areq + \
72-
crypto_skcipher_reqsize(crypto_skcipher_reqtfm(&ctx->req)))
73-
74-
#define GET_REQ_SIZE(ctx) \
75-
crypto_skcipher_reqsize(crypto_skcipher_reqtfm(&ctx->req))
76-
77-
#define GET_IV_SIZE(ctx) \
78-
crypto_skcipher_ivsize(crypto_skcipher_reqtfm(&ctx->req))
79-
8072
#define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
8173
sizeof(struct scatterlist) - 1)
8274

@@ -102,15 +94,12 @@ static void skcipher_free_async_sgls(struct skcipher_async_req *sreq)
10294

10395
static void skcipher_async_cb(struct crypto_async_request *req, int err)
10496
{
105-
struct sock *sk = req->data;
106-
struct alg_sock *ask = alg_sk(sk);
107-
struct skcipher_ctx *ctx = ask->private;
108-
struct skcipher_async_req *sreq = GET_SREQ(req, ctx);
97+
struct skcipher_async_req *sreq = req->data;
10998
struct kiocb *iocb = sreq->iocb;
11099

111-
atomic_dec(&ctx->inflight);
100+
atomic_dec(sreq->inflight);
112101
skcipher_free_async_sgls(sreq);
113-
kfree(req);
102+
kzfree(sreq);
114103
iocb->ki_complete(iocb, err, err);
115104
}
116105

@@ -509,37 +498,42 @@ static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
509498
{
510499
struct sock *sk = sock->sk;
511500
struct alg_sock *ask = alg_sk(sk);
501+
struct sock *psk = ask->parent;
502+
struct alg_sock *pask = alg_sk(psk);
512503
struct skcipher_ctx *ctx = ask->private;
504+
struct skcipher_tfm *skc = pask->private;
505+
struct crypto_skcipher *tfm = skc->skcipher;
513506
struct skcipher_sg_list *sgl;
514507
struct scatterlist *sg;
515508
struct skcipher_async_req *sreq;
516509
struct skcipher_request *req;
517510
struct skcipher_async_rsgl *last_rsgl = NULL;
518511
unsigned int txbufs = 0, len = 0, tx_nents = skcipher_all_sg_nents(ctx);
519-
unsigned int reqlen = sizeof(struct skcipher_async_req) +
520-
GET_REQ_SIZE(ctx) + GET_IV_SIZE(ctx);
512+
unsigned int reqsize = crypto_skcipher_reqsize(tfm);
513+
unsigned int ivsize = crypto_skcipher_ivsize(tfm);
521514
int err = -ENOMEM;
522515
bool mark = false;
516+
char *iv;
523517

524-
lock_sock(sk);
525-
req = kmalloc(reqlen, GFP_KERNEL);
526-
if (unlikely(!req))
527-
goto unlock;
518+
sreq = kzalloc(sizeof(*sreq) + reqsize + ivsize, GFP_KERNEL);
519+
if (unlikely(!sreq))
520+
goto out;
528521

529-
sreq = GET_SREQ(req, ctx);
522+
req = &sreq->req;
523+
iv = (char *)(req + 1) + reqsize;
530524
sreq->iocb = msg->msg_iocb;
531-
memset(&sreq->first_sgl, '\0', sizeof(struct skcipher_async_rsgl));
532525
INIT_LIST_HEAD(&sreq->list);
526+
sreq->inflight = &ctx->inflight;
527+
528+
lock_sock(sk);
533529
sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL);
534-
if (unlikely(!sreq->tsg)) {
535-
kfree(req);
530+
if (unlikely(!sreq->tsg))
536531
goto unlock;
537-
}
538532
sg_init_table(sreq->tsg, tx_nents);
539-
memcpy(sreq->iv, ctx->iv, GET_IV_SIZE(ctx));
540-
skcipher_request_set_tfm(req, crypto_skcipher_reqtfm(&ctx->req));
533+
memcpy(iv, ctx->iv, ivsize);
534+
skcipher_request_set_tfm(req, tfm);
541535
skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
542-
skcipher_async_cb, sk);
536+
skcipher_async_cb, sreq);
543537

544538
while (iov_iter_count(&msg->msg_iter)) {
545539
struct skcipher_async_rsgl *rsgl;
@@ -615,20 +609,22 @@ static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg,
615609
sg_mark_end(sreq->tsg + txbufs - 1);
616610

617611
skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg,
618-
len, sreq->iv);
612+
len, iv);
619613
err = ctx->enc ? crypto_skcipher_encrypt(req) :
620614
crypto_skcipher_decrypt(req);
621615
if (err == -EINPROGRESS) {
622616
atomic_inc(&ctx->inflight);
623617
err = -EIOCBQUEUED;
618+
sreq = NULL;
624619
goto unlock;
625620
}
626621
free:
627622
skcipher_free_async_sgls(sreq);
628-
kfree(req);
629623
unlock:
630624
skcipher_wmem_wakeup(sk);
631625
release_sock(sk);
626+
kzfree(sreq);
627+
out:
632628
return err;
633629
}
634630

0 commit comments

Comments
 (0)