Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 199354d7fb6e ("crypto: caam - Remove GFP_DMA and add DMA alignment padding")
Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Horia Geantă and committed by
Herbert Xu
80688afb 5ddfdcbe

+2 -1
+2 -1
drivers/crypto/caam/caamalg_qi2.c
··· 3326 3326 if (aligned_len < keylen) 3327 3327 return -EOVERFLOW; 3328 3328 3329 - hashed_key = kmemdup(key, aligned_len, GFP_KERNEL); 3329 + hashed_key = kmalloc(aligned_len, GFP_KERNEL); 3330 3330 if (!hashed_key) 3331 3331 return -ENOMEM; 3332 + memcpy(hashed_key, key, keylen); 3332 3333 ret = hash_digest_key(ctx, &keylen, hashed_key, digestsize); 3333 3334 if (ret) 3334 3335 goto bad_free_key;