Skip to content

Commit b68a7ec

Browse files
keesherbertx
authored andcommitted
crypto: hash - Remove VLA usage
In the quest to remove all stack VLA usage from the kernel[1], this removes the VLAs in SHASH_DESC_ON_STACK (via crypto_shash_descsize()) by using the maximum allowable size (which is now more clearly captured in a macro), along with a few other cases. Similar limits are turned into macros as well. A review of existing sizes shows that SHA512_DIGEST_SIZE (64) is the largest digest size and that sizeof(struct sha3_state) (360) is the largest descriptor size. The corresponding maximums are reduced. [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ebf533a commit b68a7ec

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

crypto/ahash.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ static int ahash_prepare_alg(struct ahash_alg *alg)
550550
{
551551
struct crypto_alg *base = &alg->halg.base;
552552

553-
if (alg->halg.digestsize > PAGE_SIZE / 8 ||
554-
alg->halg.statesize > PAGE_SIZE / 8 ||
553+
if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE ||
554+
alg->halg.statesize > HASH_MAX_STATESIZE ||
555555
alg->halg.statesize == 0)
556556
return -EINVAL;
557557

crypto/algif_hash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static int hash_accept(struct socket *sock, struct socket *newsock, int flags,
239239
struct alg_sock *ask = alg_sk(sk);
240240
struct hash_ctx *ctx = ask->private;
241241
struct ahash_request *req = &ctx->req;
242-
char state[crypto_ahash_statesize(crypto_ahash_reqtfm(req)) ? : 1];
242+
char state[HASH_MAX_STATESIZE];
243243
struct sock *sk2;
244244
struct alg_sock *ask2;
245245
struct hash_ctx *ctx2;

crypto/shash.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,9 @@ static int shash_prepare_alg(struct shash_alg *alg)
458458
{
459459
struct crypto_alg *base = &alg->base;
460460

461-
if (alg->digestsize > PAGE_SIZE / 8 ||
462-
alg->descsize > PAGE_SIZE / 8 ||
463-
alg->statesize > PAGE_SIZE / 8)
461+
if (alg->digestsize > HASH_MAX_DIGESTSIZE ||
462+
alg->descsize > HASH_MAX_DESCSIZE ||
463+
alg->statesize > HASH_MAX_STATESIZE)
464464
return -EINVAL;
465465

466466
base->cra_type = &crypto_shash_type;

include/crypto/hash.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ struct shash_desc {
151151
void *__ctx[] CRYPTO_MINALIGN_ATTR;
152152
};
153153

154+
#define HASH_MAX_DIGESTSIZE 64
155+
#define HASH_MAX_DESCSIZE 360
156+
#define HASH_MAX_STATESIZE 512
157+
154158
#define SHASH_DESC_ON_STACK(shash, ctx) \
155159
char __##shash##_desc[sizeof(struct shash_desc) + \
156-
crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \
160+
HASH_MAX_DESCSIZE] CRYPTO_MINALIGN_ATTR; \
157161
struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
158162

159163
/**

0 commit comments

Comments
 (0)