]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
crypto: ecdh - avoid unaligned accesses in ecdh_set_secret()
authorArd Biesheuvel <ardb@kernel.org>
Tue, 24 Nov 2020 10:47:19 +0000 (11:47 +0100)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 20 Jan 2021 13:26:22 +0000 (14:26 +0100)
BugLink: https://bugs.launchpad.net/bugs/1910822
commit 17858b140bf49961b71d4e73f1c3ea9bc8e7dda0 upstream.

ecdh_set_secret() casts a void* pointer to a const u64* in order to
feed it into ecc_is_key_valid(). This is not generally permitted by
the C standard, and leads to actual misalignment faults on ARMv6
cores. In some cases, these are fixed up in software, but this still
leads to performance hits that are entirely avoidable.

So let's copy the key into the ctx buffer first, which we will do
anyway in the common case, and which guarantees correct alignment.

Cc: <stable@vger.kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kelsey Skunberg <kelsey.skunberg@canonical.com>
crypto/ecdh.c

index bd599053a8c4bd4a0ba7975e6a9b790a3094102d..efa4ee72301f825ca8a1f73f03b3a21b60904fcb 100644 (file)
@@ -53,12 +53,13 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
                return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
                                       ctx->private_key);
 
-       if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
-                            (const u64 *)params.key, params.key_size) < 0)
-               return -EINVAL;
-
        memcpy(ctx->private_key, params.key, params.key_size);
 
+       if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
+                            ctx->private_key, params.key_size) < 0) {
+               memzero_explicit(ctx->private_key, params.key_size);
+               return -EINVAL;
+       }
        return 0;
 }