Skip to contentSkip to conversation

crypto: ghash - fix unaligned memory access in ghash_setkey()

PATCH In review Eric Biggers 4 participants 5 messages May 30, 2019 – Jun 6, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: ghash - fix unaligned memory access in ghash_setkey()

From: Eric Biggers <[email protected]>

Changing ghash_mod_init() to be subsys_initcall made it start running before the alignment fault handler has been installed on ARM. In kernel builds where the keys in the ghash test vectors happened to be misaligned in the kernel image, this exposed the longstanding bug that ghash_setkey() is incorrectly casting the key buffer (which can have any alignment) to be128 for passing to gf128mul_init_4k_lle().

Fix this by memcpy()ing the key to a temporary buffer.

Don't fix it by setting an alignmask on the algorithm instead because that would unnecessarily force alignment of the data too.

Fixes 2cdc6899a88e ("crypto: ghash - Add GHASH digest algorithm for GCM")
---
 crypto/ghash-generic.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
crypto/ghash-generic.c +7 -1
index e6307935413c1..c8a347798eae6 100644
@@ -34,6 +34,7 @@ static int ghash_setkey(struct crypto_shash *tfm,
 			const u8 *key, unsigned int keylen)
 {
 	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
+	be128 k;
 
 	if (keylen != GHASH_BLOCK_SIZE) {
 		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
@@ -42,7 +43,12 @@ static int ghash_setkey(struct crypto_shash *tfm,
 
 	if (ctx->gf128)
 		gf128mul_free_4k(ctx->gf128);
-	ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
+
+	BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
+	memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
+	ctx->gf128 = gf128mul_init_4k_lle(&k);
+	memzero_explicit(&k, GHASH_BLOCK_SIZE);
+
 	if (!ctx->gf128)
 		return -ENOMEM;
 
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Changing ghash_mod_init() to be subsys_initcall made it start running before the alignment fault handler has been installed on ARM. In kernel builds where the keys in the ghash test vectors happened to be misaligned in the kernel image, this exposed the longstanding bug that ghash_setkey() is incorrectly casting the key buffer (which can have any alignment) to be128 for passing to gf128mul_init_4k_lle().

Quoted 7 lines

Fix this by memcpy()ing the key to a temporary buffer.

Don't fix it by setting an alignmask on the algorithm instead because that would unnecessarily force alignment of the data too.

Fixes 2cdc6899a88e ("crypto: ghash - Add GHASH digest algorithm for GCM")

That fixes the problems I was seeing, thanks for the quick response/fix.

Peter

Quoted 35 lines
---
 crypto/ghash-generic.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
crypto/ghash-generic.c +7 -2
index e6307935413c1..c8a347798eae6 100644
@@ -34,6 +34,7 @@ static int ghash_setkey(struct crypto_shash *tfm,
                        const u8 *key, unsigned int keylen)
 {
        struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
+       be128 k;
 
        if (keylen != GHASH_BLOCK_SIZE) {
                crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
@@ -42,7 +43,12 @@ static int ghash_setkey(struct crypto_shash *tfm,
 
        if (ctx->gf128)
                gf128mul_free_4k(ctx->gf128);
-       ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
+
+       BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
+       memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
+       ctx->gf128 = gf128mul_init_4k_lle(&k);
+       memzero_explicit(&k, GHASH_BLOCK_SIZE);
+
        if (!ctx->gf128)
                return -ENOMEM;
 
--

2.22.0.rc1.257.g3120a18244-goog

#2 lore raw
In reply to Eric Biggers

Le 30/05/2019 à 19:50, Eric Biggers a écrit :

Quoted 10 lines

From: Eric Biggers <[email protected]>

Changing ghash_mod_init() to be subsys_initcall made it start running before the alignment fault handler has been installed on ARM. In kernel builds where the keys in the ghash test vectors happened to be misaligned in the kernel image, this exposed the longstanding bug that ghash_setkey() is incorrectly casting the key buffer (which can have any alignment) to be128 for passing to gf128mul_init_4k_lle().

Fix this by memcpy()ing the key to a temporary buffer.

Shouldn't we make it dependent on CONFIG_HAVE_64BIT_ALIGNED_ACCESS or !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ?

Christophe

Quoted 39 lines

Don't fix it by setting an alignmask on the algorithm instead because that would unnecessarily force alignment of the data too.

Fixes 2cdc6899a88e ("crypto: ghash - Add GHASH digest algorithm for GCM")
---
  crypto/ghash-generic.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
crypto/ghash-generic.c +7 -1
index e6307935413c1..c8a347798eae6 100644
@@ -34,6 +34,7 @@ static int ghash_setkey(struct crypto_shash *tfm,
  			const u8 *key, unsigned int keylen)
  {
  	struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
+	be128 k;
 
  	if (keylen != GHASH_BLOCK_SIZE) {
  		crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
@@ -42,7 +43,12 @@ static int ghash_setkey(struct crypto_shash *tfm,
 
  	if (ctx->gf128)
  		gf128mul_free_4k(ctx->gf128);
-	ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
+
+	BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
+	memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
+	ctx->gf128 = gf128mul_init_4k_lle(&k);
+	memzero_explicit(&k, GHASH_BLOCK_SIZE);
+
  	if (!ctx->gf128)
  		return -ENOMEM;
 
#3 lore raw
Quoted 12 lines · Christophe Leroy

Le 30/05/2019 à 19:50, Eric Biggers a écrit :

Quoted 9 lines

From: Eric Biggers <[email protected]>

Changing ghash_mod_init() to be subsys_initcall made it start running before the alignment fault handler has been installed on ARM. In kernel builds where the keys in the ghash test vectors happened to be misaligned in the kernel image, this exposed the longstanding bug that ghash_setkey() is incorrectly casting the key buffer (which can have any alignment) to be128 for passing to gf128mul_init_4k_lle().

Quoted 3 lines
Quoted 1 line

Fix this by memcpy()ing the key to a temporary buffer.

Shouldn't we make it dependent on CONFIG_HAVE_64BIT_ALIGNED_ACCESS

No, because the buffer can have as little as 1-byte alignment.

Quoted 1 line

or !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ?

I don't think that's a good idea because two code paths are harder to test than one, and also CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS only means that the CPU allows "regular" loads and stores to be misaligned. On some architectures the compiler can still generate load and store instructions that require alignment, e.g. 'ldrd' or 'ldm' on ARM.

We could change gf128mul_init_4k_lle() to take a byte array and make it use get_unaligned_be64(). But since it has to allocate and initialize a 4 KiB multiplication table anyway, that microoptimization would be lost in the noise.

- Eric

maintainer
#4 lore raw
In reply to Eric Biggers
Quoted 14 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Changing ghash_mod_init() to be subsys_initcall made it start running before the alignment fault handler has been installed on ARM. In kernel builds where the keys in the ghash test vectors happened to be misaligned in the kernel image, this exposed the longstanding bug that ghash_setkey() is incorrectly casting the key buffer (which can have any alignment) to be128 for passing to gf128mul_init_4k_lle().

Fix this by memcpy()ing the key to a temporary buffer.

Don't fix it by setting an alignmask on the algorithm instead because that would unnecessarily force alignment of the data too.

Quoted 8 lines
Fixes 2cdc6899a88e ("crypto: ghash - Add GHASH digest algorithm for GCM")
---
 crypto/ghash-generic.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt