Skip to contentSkip to conversation

crypto: Demote BUG_ON() in crypto_unregister_alg() to a WARN_ON()

PATCH In review Toke Høiland-Jørgensen 2 participants 2 messages Mar 13, 2023 – Mar 24, 2023

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

PATCH v2 1/1 crypto: Demote BUG_ON() in crypto_unregister_alg() to a WARN_ON()

The crypto_unregister_alg() function expects callers to ensure that any algorithm that is unregistered has a refcnt of exactly 1, and issues a BUG_ON() if this is not the case. However, there are in fact drivers that will call crypto_unregister_alg() without ensuring that the refcnt has been lowered first, most notably on system shutdown. This causes the BUG_ON() to trigger, which prevents a clean shutdown and hangs the system.

To avoid such hangs on shutdown, demote the BUG_ON() in crypto_unregister_alg() to a WARN_ON() with early return. Cc stable because this problem was observed on a 6.2 kernel, cf the link below.

---
v2:
- Return early if the WARN_ON() triggers

 crypto/algapi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
crypto/algapi.c +3 -1
index d08f864f08be..9de0677b3643 100644
@@ -493,7 +493,9 @@ void crypto_unregister_alg(struct crypto_alg *alg)
 	if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
 		return;
 
-	BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
+	if (WARN_ON(refcount_read(&alg->cra_refcnt) != 1))
+		return;
+
 	if (alg->cra_destroy)
 		alg->cra_destroy(alg);
 
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 14 lines · Toke Høiland-Jørgensen

The crypto_unregister_alg() function expects callers to ensure that any algorithm that is unregistered has a refcnt of exactly 1, and issues a BUG_ON() if this is not the case. However, there are in fact drivers that will call crypto_unregister_alg() without ensuring that the refcnt has been lowered first, most notably on system shutdown. This causes the BUG_ON() to trigger, which prevents a clean shutdown and hangs the system.

To avoid such hangs on shutdown, demote the BUG_ON() in crypto_unregister_alg() to a WARN_ON() with early return. Cc stable because this problem was observed on a 6.2 kernel, cf the link below.

Quoted 6 lines

---
v2:
- Return early if the WARN_ON() triggers

 crypto/algapi.c | 4 +++-
 1 file changed, 3 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