Skip to contentSkip to conversation

crypto: arm/aes-neonbs - fix usage of cbc(aes) fallback

PATCH In review Horia Geantă 3 participants 5 messages Oct 28, 2020 – Nov 6, 2020

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

PATCH 1/1 crypto: arm/aes-neonbs - fix usage of cbc(aes) fallback

Loading the module deadlocks since:
-local cbc(aes) implementation needs a fallback and
-crypto API tries to find one but the request_module() resolves back to the same module

Fix this by changing the module alias for cbc(aes) and using the NEED_FALLBACK flag when requesting for a fallback algorithm.

Fixes 00b99ad2bac2 ("crypto: arm/aes-neonbs - Use generic cbc encryption path")
---
 arch/arm/crypto/aes-neonbs-glue.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm/crypto/aes-neonbs-glue.c +5 -3
index bda8bf17631e..f70af1d0514b 100644
@@ -19,7 +19,7 @@ MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
 MODULE_LICENSE("GPL v2");
 
 MODULE_ALIAS_CRYPTO("ecb(aes)");
-MODULE_ALIAS_CRYPTO("cbc(aes)");
+MODULE_ALIAS_CRYPTO("cbc(aes)-all");
 MODULE_ALIAS_CRYPTO("ctr(aes)");
 MODULE_ALIAS_CRYPTO("xts(aes)");
 
@@ -191,7 +191,8 @@ static int cbc_init(struct crypto_skcipher *tfm)
 	struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
 	unsigned int reqsize;
 
-	ctx->enc_tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
+	ctx->enc_tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC |
+					     CRYPTO_ALG_NEED_FALLBACK);
 	if (IS_ERR(ctx->enc_tfm))
 		return PTR_ERR(ctx->enc_tfm);
 
@@ -441,7 +442,8 @@ static struct skcipher_alg aes_algs[] = { {
 	.base.cra_blocksize	= AES_BLOCK_SIZE,
 	.base.cra_ctxsize	= sizeof(struct aesbs_cbc_ctx),
 	.base.cra_module	= THIS_MODULE,
-	.base.cra_flags		= CRYPTO_ALG_INTERNAL,
+	.base.cra_flags		= CRYPTO_ALG_INTERNAL |
+				  CRYPTO_ALG_NEED_FALLBACK,
 
 	.min_keysize		= AES_MIN_KEY_SIZE,
 	.max_keysize		= AES_MAX_KEY_SIZE,
Signature · 7 lines
2.17.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw
Quoted 11 lines · Horia Geantă

Loading the module deadlocks since:

(quoted diff) +0 -2
-local cbc(aes) implementation needs a fallback and
-crypto API tries to find one but the request_module() resolves back to

the same module

Fix this by changing the module alias for cbc(aes) and using the NEED_FALLBACK flag when requesting for a fallback algorithm.

Fixes 00b99ad2bac2 ("crypto: arm/aes-neonbs - Use generic cbc encryption path")

Not sure what is happening here: IIRC the intention was to rely on the fact that only the sync cbc(aes) implementation needs the fallback, and therefore, allocating a sync skcipher explicitly would avoid this recursion.

Herbert?

Quoted 38 lines
---
 arch/arm/crypto/aes-neonbs-glue.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm/crypto/aes-neonbs-glue.c +5 -4
index bda8bf17631e..f70af1d0514b 100644
@@ -19,7 +19,7 @@ MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
 MODULE_LICENSE("GPL v2");
 
 MODULE_ALIAS_CRYPTO("ecb(aes)");
-MODULE_ALIAS_CRYPTO("cbc(aes)");
+MODULE_ALIAS_CRYPTO("cbc(aes)-all");
 MODULE_ALIAS_CRYPTO("ctr(aes)");
 MODULE_ALIAS_CRYPTO("xts(aes)");
 
@@ -191,7 +191,8 @@ static int cbc_init(struct crypto_skcipher *tfm)
        struct aesbs_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
        unsigned int reqsize;
 
-       ctx->enc_tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
+       ctx->enc_tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC |
+                                            CRYPTO_ALG_NEED_FALLBACK);
        if (IS_ERR(ctx->enc_tfm))
                return PTR_ERR(ctx->enc_tfm);
 
@@ -441,7 +442,8 @@ static struct skcipher_alg aes_algs[] = { {
        .base.cra_blocksize     = AES_BLOCK_SIZE,
        .base.cra_ctxsize       = sizeof(struct aesbs_cbc_ctx),
        .base.cra_module        = THIS_MODULE,
-       .base.cra_flags         = CRYPTO_ALG_INTERNAL,
+       .base.cra_flags         = CRYPTO_ALG_INTERNAL |
+                                 CRYPTO_ALG_NEED_FALLBACK,
 
        .min_keysize            = AES_MIN_KEY_SIZE,
        .max_keysize            = AES_MAX_KEY_SIZE,
--
Quoted 2 lines

2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw
Quoted 10 lines · Ard Biesheuvel
Quoted 9 lines · Horia Geantă

Loading the module deadlocks since:

(quoted diff) +0 -2
-local cbc(aes) implementation needs a fallback and
-crypto API tries to find one but the request_module() resolves back to

the same module

Fix this by changing the module alias for cbc(aes) and using the NEED_FALLBACK flag when requesting for a fallback algorithm.

Quoted 8 lines
Quoted 2 lines
Fixes 00b99ad2bac2 ("crypto: arm/aes-neonbs - Use generic cbc encryption path")

Not sure what is happening here: IIRC the intention was to rely on the fact that only the sync cbc(aes) implementation needs the fallback, and therefore, allocating a sync skcipher explicitly would avoid this recursion.

My understanding is the following:

1. Local cbc_init() tries to allocate a fallback tfm for cbc(aes)

2. crypto API cbc(aes) tries to find a cbc(aes) algorithm implementation crypto_alloc_skcipher ->

	crypto_alloc_tfm ->
		crypto_alloc_tfm_node ->
			crypto_find_alg ->
				crypto_alg_mod_lookup ->
					crypto_larval_lookup

Here crypto_alg_lookup() fails to find a cbc(aes) implementation.

3. Next step is to try to dynamically load a module (request_module) that supports this implementation. And here it deadlocks, since it tries to load aes-arm-bs module...

The fix is providing a way to (partially) skip the dynamic module loading in crypto_alg_lookup() and allow for the last method of finding an algorithm implementation, which is creating the cbc(aes) on the fly
via the cbc template - see:

	ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);

in crypto_alg_mod_lookup().

Horia

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#3 lore raw
In reply to Ard Biesheuvel
Quoted 7 lines · Ard Biesheuvel

Not sure what is happening here: IIRC the intention was to rely on the fact that only the sync cbc(aes) implementation needs the fallback, and therefore, allocating a sync skcipher explicitly would avoid this recursion.

Herbert?

It works only if everything is built in. If cbc is built as a module then you need Horia's patch to prevent a loop.

Cheers,

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#4 lore raw
In reply to Horia Geantă
Quoted 5 lines · Horia Geantă

Loading the module deadlocks since:

(quoted diff) +0 -2
-local cbc(aes) implementation needs a fallback and
-crypto API tries to find one but the request_module() resolves back to

the same module

Quoted 8 lines

Fix this by changing the module alias for cbc(aes) and using the NEED_FALLBACK flag when requesting for a fallback algorithm.

Fixes 00b99ad2bac2 ("crypto: arm/aes-neonbs - Use generic cbc encryption path")
---
 arch/arm/crypto/aes-neonbs-glue.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

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