All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: algapi - Remove test larvals to fix error paths
@ 2022-01-26 14:53 Philipp Zabel
  2022-01-26 15:01 ` Philipp Zabel
  2022-01-27  4:41 ` Herbert Xu
  0 siblings, 2 replies; 8+ messages in thread
From: Philipp Zabel @ 2022-01-26 14:53 UTC (permalink / raw)
  To: linux-crypto; +Cc: Herbert Xu, David S. Miller, kernel

If crypto_unregister_alg() is called with an algorithm that still has a
pending test larval, the algorithm will have a reference count of 2 and
crypto_unregister_alg() will trigger a BUG(). This can happen during
cleanup if the error path is taken for a built-in algorithm, before
crypto_algapi_init() was called.

Kill test larvals for untested algorithms during removal to fix the
reference count.

Fixes: adad556efcdd ("crypto: api - Fix built-in testing dependency failures")
Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 crypto/algapi.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/crypto/algapi.c b/crypto/algapi.c
index a366cb3e8aa1..fc3b75e59d0e 100644
--- a/crypto/algapi.c
+++ b/crypto/algapi.c
@@ -458,6 +458,28 @@ void crypto_unregister_alg(struct crypto_alg *alg)
 	if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
 		return;
 
+	/* If there is a test larval holding a reference, remove it */
+	if (!(alg->cra_flags & CRYPTO_ALG_TESTED)) {
+		struct crypto_alg *q, *n;
+
+		list_for_each_entry_safe(q, n, &crypto_alg_list, cra_list) {
+			struct crypto_larval *l;
+
+			if (!crypto_is_larval(q))
+				continue;
+
+			l = (void *)q;
+
+			if (!crypto_is_test_larval(l))
+				continue;
+
+			if (l->adult != alg)
+				continue;
+
+			crypto_larval_kill(q);
+		}
+	}
+
 	BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
 	if (alg->cra_destroy)
 		alg->cra_destroy(alg);

base-commit: e783362eb54cd99b2cac8b3a9aeac942e6f6ac07
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-03-17  9:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-26 14:53 [PATCH] crypto: algapi - Remove test larvals to fix error paths Philipp Zabel
2022-01-26 15:01 ` Philipp Zabel
2022-01-27  4:37   ` Herbert Xu
2022-03-16  1:11   ` Herbert Xu
2022-03-16  7:23     ` Ard Biesheuvel
2022-03-16 22:10       ` Herbert Xu
2022-03-17  9:00         ` Philipp Zabel
2022-01-27  4:41 ` Herbert Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.