Skip to contentSkip to conversation

crypto: x86/curve25519 - fix cpu feature checking logic in mod_exit

PATCH In review Hangbin Liu 3 participants 6 messages Jun 3, 2021 – Jun 11, 2021

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

PATCH 1/1 crypto: x86/curve25519 - fix cpu feature checking logic in mod_exit

In curve25519_mod_init() the curve25519_alg will be registered only when (X86_FEATURE_BMI2 && X86_FEATURE_ADX). But in curve25519_mod_exit() it still checks (X86_FEATURE_BMI2 || X86_FEATURE_ADX) when do crypto unregister. This will trigger a BUG_ON in crypto_unregister_alg() as alg->cra_refcnt is 0 if the cpu only supports one of X86_FEATURE_BMI2 and X86_FEATURE_ADX.

Fixes 07b586fe0662 ("crypto: x86/curve25519 - replace with formally verified implementation")
---
 arch/x86/crypto/curve25519-x86_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/crypto/curve25519-x86_64.c +1 -1
index 6706b6cb1d0f..38caf61cd5b7 100644
@@ -1500,7 +1500,7 @@ static int __init curve25519_mod_init(void)
 static void __exit curve25519_mod_exit(void)
 {
 	if (IS_REACHABLE(CONFIG_CRYPTO_KPP) &&
-	    (boot_cpu_has(X86_FEATURE_BMI2) || boot_cpu_has(X86_FEATURE_ADX)))
+	    static_branch_likely(&curve25519_use_bmi2_adx))
 		crypto_unregister_kpp(&curve25519_alg);
 }
 
Signature · 1 line
2.26.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Hangbin Liu

In curve25519_mod_init() the curve25519_alg will be registered only when (X86_FEATURE_BMI2 && X86_FEATURE_ADX). But in curve25519_mod_exit() it still checks (X86_FEATURE_BMI2 || X86_FEATURE_ADX) when do crypto unregister. This will trigger a BUG_ON in crypto_unregister_alg() as alg->cra_refcnt is 0 if the cpu only supports one of X86_FEATURE_BMI2 and X86_FEATURE_ADX.

Fixes 07b586fe0662 ("crypto: x86/curve25519 - replace with formally verified implementation")
---
 arch/x86/crypto/curve25519-x86_64.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/crypto/curve25519-x86_64.c +0 -0
index 6706b6cb1d0f..38caf61cd5b7 100644

arch/x86/crypto/curve25519-x86_64.c static int __init curve25519_mod_init(void) · line 1505

Quoted 7 lines
arch/x86/crypto/curve25519-x86_64.c +1 -1
@@ -1500,7 +1500,7 @@ static int __init curve25519_mod_init(void)
 static void __exit curve25519_mod_exit(void)
 {
        if (IS_REACHABLE(CONFIG_CRYPTO_KPP) &&
-           (boot_cpu_has(X86_FEATURE_BMI2) || boot_cpu_has(X86_FEATURE_ADX)))
+           static_branch_likely(&curve25519_use_bmi2_adx))
                crypto_unregister_kpp(&curve25519_alg);
 }

Looks like the error is actually that the || should be a &&. But if you'd like to branch on that static key instead, that's fine.

#2 lore raw
Quoted 8 lines · Jason A. Donenfeld

arch/x86/crypto/curve25519-x86_64.c static int __init curve25519_mod_init(void) · line 1505

Quoted 5 lines
        if (IS_REACHABLE(CONFIG_CRYPTO_KPP) &&
arch/x86/crypto/curve25519-x86_64.c +1 -1
-           (boot_cpu_has(X86_FEATURE_BMI2) || boot_cpu_has(X86_FEATURE_ADX)))
+           static_branch_likely(&curve25519_use_bmi2_adx))
                crypto_unregister_kpp(&curve25519_alg);
 }

Looks like the error is actually that the || should be a &&. But if you'd like to branch on that static key instead, that's fine.

Yes, the code would be shorter by checking the static key :)

Thanks
hangbin

maintainer
#3 lore raw
In reply to Hangbin Liu
Quoted 7 lines · Hangbin Liu

In curve25519_mod_init() the curve25519_alg will be registered only when (X86_FEATURE_BMI2 && X86_FEATURE_ADX). But in curve25519_mod_exit() it still checks (X86_FEATURE_BMI2 || X86_FEATURE_ADX) when do crypto unregister. This will trigger a BUG_ON in crypto_unregister_alg() as alg->cra_refcnt is 0 if the cpu only supports one of X86_FEATURE_BMI2 and X86_FEATURE_ADX.

Quoted 5 lines
Fixes 07b586fe0662 ("crypto: x86/curve25519 - replace with formally verified implementation")
---
 arch/x86/crypto/curve25519-x86_64.c | 2 +-
 1 file changed, 1 insertion(+), 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
maintainer
#5 lore raw
Quoted 7 lines · Jason A. Donenfeld

Hi Herbert,

Is there a reason why in
https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git/patch/?id=1b82435d17774f3eaab35dce239d354548aa9da2
you didn't mark it with the Cc: stable@ line that I included above my Reviewed-by? Netdev no longer has their own stable process. Do you have something else in mind for this?

Hi Jason:

This patch has a Fixes header set and it'll be automatically pushed to stable.

Cheers,

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