Skip to content

Commit

Permalink
crypto: cryptd - Use request context instead of stack for sub-request
Browse files Browse the repository at this point in the history
[ Upstream commit 3a58c23 ]

cryptd is buggy as it tries to use sync_skcipher without going
through the proper sync_skcipher interface.  In fact it doesn't
even need sync_skcipher since it's already a proper skcipher and
can easily access the request context instead of using something
off the stack.

Fixes: 36b3875 ("crypto: cryptd - Remove VLA usage of skcipher")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
herbertx authored and gregkh committed Dec 31, 2022
1 parent 677f01f commit b5be672
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions crypto/cryptd.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ struct aead_instance_ctx {

struct cryptd_skcipher_ctx {
refcount_t refcnt;
struct crypto_sync_skcipher *child;
struct crypto_skcipher *child;
};

struct cryptd_skcipher_request_ctx {
crypto_completion_t complete;
struct skcipher_request req;
};

struct cryptd_hash_ctx {
Expand Down Expand Up @@ -227,13 +228,13 @@ static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
const u8 *key, unsigned int keylen)
{
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
struct crypto_sync_skcipher *child = ctx->child;
struct crypto_skcipher *child = ctx->child;

crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
crypto_sync_skcipher_set_flags(child,
crypto_skcipher_get_flags(parent) &
CRYPTO_TFM_REQ_MASK);
return crypto_sync_skcipher_setkey(child, key, keylen);
crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
crypto_skcipher_set_flags(child,
crypto_skcipher_get_flags(parent) &
CRYPTO_TFM_REQ_MASK);
return crypto_skcipher_setkey(child, key, keylen);
}

static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
Expand All @@ -258,13 +259,13 @@ static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
struct crypto_sync_skcipher *child = ctx->child;
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
struct skcipher_request *subreq = &rctx->req;
struct crypto_skcipher *child = ctx->child;

if (unlikely(err == -EINPROGRESS))
goto out;

skcipher_request_set_sync_tfm(subreq, child);
skcipher_request_set_tfm(subreq, child);
skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
NULL, NULL);
skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
Expand All @@ -286,13 +287,13 @@ static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
struct crypto_sync_skcipher *child = ctx->child;
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
struct skcipher_request *subreq = &rctx->req;
struct crypto_skcipher *child = ctx->child;

if (unlikely(err == -EINPROGRESS))
goto out;

skcipher_request_set_sync_tfm(subreq, child);
skcipher_request_set_tfm(subreq, child);
skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
NULL, NULL);
skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
Expand Down Expand Up @@ -343,17 +344,18 @@ static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
if (IS_ERR(cipher))
return PTR_ERR(cipher);

ctx->child = (struct crypto_sync_skcipher *)cipher;
ctx->child = cipher;
crypto_skcipher_set_reqsize(
tfm, sizeof(struct cryptd_skcipher_request_ctx));
tfm, sizeof(struct cryptd_skcipher_request_ctx) +
crypto_skcipher_reqsize(cipher));
return 0;
}

static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
{
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);

crypto_free_sync_skcipher(ctx->child);
crypto_free_skcipher(ctx->child);
}

static void cryptd_skcipher_free(struct skcipher_instance *inst)
Expand Down Expand Up @@ -931,7 +933,7 @@ struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
{
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);

return &ctx->child->base;
return ctx->child;
}
EXPORT_SYMBOL_GPL(cryptd_skcipher_child);

Expand Down

0 comments on commit b5be672

Please sign in to comment.