Skip to content

Commit

Permalink
crypto: ecc - Remove stack VLA usage
Browse files Browse the repository at this point in the history
On the quest to remove all VLAs from the kernel[1], this switches to
a pair of kmalloc regions instead of using the stack. This also moves
the get_random_bytes() after all allocations (and drops the needless
"nbytes" variable).

[1] https://lkml.org/lkml/2018/3/7/621

Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Tudor Ambarus <tudor.ambarus@microchip.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
kees authored and herbertx committed Mar 16, 2018
1 parent b698a9f commit 14de521
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions crypto/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,24 +1025,30 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
{
int ret = 0;
struct ecc_point *product, *pk;
u64 priv[ndigits];
u64 rand_z[ndigits];
unsigned int nbytes;
u64 *priv, *rand_z;
const struct ecc_curve *curve = ecc_get_curve(curve_id);

if (!private_key || !public_key || !curve) {
ret = -EINVAL;
goto out;
}

nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
priv = kmalloc_array(ndigits, sizeof(*priv), GFP_KERNEL);
if (!priv) {
ret = -ENOMEM;
goto out;
}

get_random_bytes(rand_z, nbytes);
rand_z = kmalloc_array(ndigits, sizeof(*rand_z), GFP_KERNEL);
if (!rand_z) {
ret = -ENOMEM;
goto kfree_out;
}

pk = ecc_alloc_point(ndigits);
if (!pk) {
ret = -ENOMEM;
goto out;
goto kfree_out;
}

product = ecc_alloc_point(ndigits);
Expand All @@ -1051,6 +1057,8 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
goto err_alloc_product;
}

get_random_bytes(rand_z, ndigits << ECC_DIGITS_TO_BYTES_SHIFT);

ecc_swap_digits(public_key, pk->x, ndigits);
ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
ecc_swap_digits(private_key, priv, ndigits);
Expand All @@ -1065,6 +1073,9 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
ecc_free_point(product);
err_alloc_product:
ecc_free_point(pk);
kfree_out:
kzfree(priv);
kzfree(rand_z);
out:
return ret;
}

0 comments on commit 14de521

Please sign in to comment.