Commit 80688afb authored by Horia Geantă's avatar Horia Geantă Committed by Herbert Xu
Browse files

crypto: caam - fix overflow on long hmac keys



When a key longer than block size is supplied, it is copied and then
hashed into the real key.  The memory allocated for the copy needs to
be rounded to DMA cache alignment, as otherwise the hashed key may
corrupt neighbouring memory.

The copying is performed using kmemdup, however this leads to an overflow:
reading more bytes (aligned_len - keylen) from the keylen source buffer.
Fix this by replacing kmemdup with kmalloc, followed by memcpy.

Fixes: 199354d7 ("crypto: caam - Remove GFP_DMA and add DMA alignment padding")
Signed-off-by: default avatarHoria Geantă <horia.geanta@nxp.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 5ddfdcbe
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -3326,9 +3326,10 @@ static int ahash_setkey(struct crypto_ahash *ahash, const u8 *key,
		if (aligned_len < keylen)
			return -EOVERFLOW;

		hashed_key = kmemdup(key, aligned_len, GFP_KERNEL);
		hashed_key = kmalloc(aligned_len, GFP_KERNEL);
		if (!hashed_key)
			return -ENOMEM;
		memcpy(hashed_key, key, keylen);
		ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize);
		if (ret)
			goto bad_free_key;