Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-crypto@vger.kernel.org
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	James Hsiao <jhsiao@amcc.com>, Rosen Penev <rosenp@gmail.com>,
	Christian Lamparter <chunkeey@gmail.com>
Subject: [PATCH 3/8] crypto: amcc - allocate all algorithm wrappers before registering
Date: Thu, 26 Nov 2026 20:04:18 -0800	[thread overview]
Message-ID: <20261127040423.1023233-4-rosenp@gmail.com> (raw)
In-Reply-To: <20261127040423.1023233-1-rosenp@gmail.com>

crypto4xx_register_alg() allocates a wrapper and registers it, one
algorithm at a time. If an allocation fails partway, it returns
-ENOMEM with the earlier algorithms still registered. The probe error
path does not unregister them, but devres frees the device context and
descriptor rings they point to. The next request on one of them,
typically its own self-test, is a use-after-free. With an allocation
failure injected at the third algorithm on an APM82181:

  crypto4xx 400180000.crypto: probe with driver crypto4xx failed with error -12
  BUG: Unable to handle kernel data access on write at 0xe4fbd028
  NIP [e4f9bb4c] crypto4xx_build_pd+0x174/0x83c [crypto4xx]
  [c188b7b0] [e4f9c888] crypto4xx_ctr_crypt+0xc8/0x188 [crypto4xx]
  [c188b9e0] [c035f098] test_skcipher_vec_cfg+0x3bc/0x890

Unregistering the algorithms on that path is not safe either: with
self-tests enabled, a test that started on registration may still hold
the algorithm and have requests on the engine.

Allocate every wrapper first and only then register, so an allocation
failure leaves nothing registered.

Fixes: 049359d65527 ("crypto: amcc - Add crypt4xx driver")
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/crypto/amcc/crypto4xx_core.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
index e28e836c4f35..6131fd6c0607 100644
--- a/drivers/crypto/amcc/crypto4xx_core.c
+++ b/drivers/crypto/amcc/crypto4xx_core.c
@@ -982,15 +982,32 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
 				  struct crypto4xx_alg_common *crypto_alg,
 				  int array_size)
 {
-	struct crypto4xx_alg *alg;
+	struct crypto4xx_alg **algs, *alg;
 	int i;
 	int rc = 0;
 
+	/*
+	 * Allocate every wrapper before registering anything: a registered
+	 * algorithm may already have its self-test running on the engine,
+	 * so it cannot simply be unregistered again if a later allocation
+	 * fails.
+	 */
+	algs = kzalloc_objs(*algs, array_size);
+	if (!algs)
+		return -ENOMEM;
+
 	for (i = 0; i < array_size; i++) {
-		alg = kzalloc_obj(struct crypto4xx_alg);
-		if (!alg)
+		algs[i] = kzalloc_obj(*algs[i]);
+		if (!algs[i]) {
+			while (i--)
+				kfree(algs[i]);
+			kfree(algs);
 			return -ENOMEM;
+		}
+	}
 
+	for (i = 0; i < array_size; i++) {
+		alg = algs[i];
 		alg->alg = crypto_alg[i];
 		alg->dev = sec_dev;
 
@@ -1010,6 +1027,7 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
 			list_add_tail(&alg->entry, &sec_dev->alg_list);
 	}
 
+	kfree(algs);
 	return 0;
 }
 
-- 
2.56.1


  parent reply	other threads:[~2026-11-27  4:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  4:04 [PATCH 0/8] crypto: amcc - fixes for the crypto4xx driver Rosen Penev
2026-11-27  4:04 ` [PATCH 1/8] crypto: amcc - fix scatter ring full check Rosen Penev
2026-11-27  4:04 ` [PATCH 2/8] crypto: amcc - fix missing DMA memory barriers in descriptor handling Rosen Penev
2026-11-27  4:04 ` Rosen Penev [this message]
2026-11-27  4:04 ` [PATCH 4/8] crypto: amcc - set up the TRNG before registering the algorithms Rosen Penev
2026-11-27  4:04 ` [PATCH 5/8] crypto: amcc - don't use memcpy() on the coherent SA and state record Rosen Penev
2026-11-27  4:04 ` [PATCH 6/8] crypto: amcc - fix the DMA mappings of the source and destination Rosen Penev
2026-11-27  4:04 ` [PATCH 7/8] crypto: amcc - fix the inverted AEAD error ratelimit Rosen Penev
2026-11-27  4:04 ` [PATCH 8/8] crypto: amcc - wipe the SA key material when freeing it Rosen Penev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127040423.1023233-4-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=chunkeey@gmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=jhsiao@amcc.com \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox