Skip to contentSkip to conversation

crypto: fix crypto_alloc_{tfm,base}() return value

PATCH In review Akinobu Mita 2 participants 5 messages Oct 9, 2006 – Oct 10, 2006 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 crypto: fix crypto_alloc_{tfm,base}() return value

crypto_alloc_tfm() and crypto_alloc_base() are suppose to return error code as pointer on failures. But there are some cases where they return NULL (for example, crypto_alloc_tfm() returns NULL on kzalloc() failure)

This patch fixes that wrong return value, and also fixes tcrypt so that it can detect error code correctly.

Cc Herbert Xu <[email protected]>
Cc "David S. Miller" <[email protected]>
 crypto/api.c    |   47 ++++++++++++++++++++++++++++-------------------
 crypto/tcrypt.c |    5 +++--
 2 files changed, 31 insertions(+), 21 deletions(-)
crypto/tcrypt.c +3 -2
Index: work-fault-inject/crypto/tcrypt.c
===================================================================
@@ -766,8 +766,9 @@ static void test_deflate(void)
 	tv = (void *)tvmem;
 
 	tfm = crypto_alloc_tfm("deflate", 0);
-	if (tfm == NULL) {
-		printk("failed to load transform for deflate\n");
+	if (IS_ERR(tfm)) {
+		printk("failed to load transform for deflate: %ld\n",
+				PTR_ERR(tfm));
 		return;
 	}
 
crypto/api.c +28 -19

91 lines not on this page · raw message

#1 lore raw

PATCH 2/2 crypto: delete duplicated code

crypto_alloc_tfm() and crypto_alloc_base() are almost same.

Cc Herbert Xu <[email protected]>
Cc "David S. Miller" <[email protected]>
 crypto/api.c |   40 +++++++++-------------------------------
 1 file changed, 9 insertions(+), 31 deletions(-)
crypto/api.c +9 -31
Index: work-fault-inject/crypto/api.c
===================================================================
@@ -362,7 +362,8 @@ out:
 }
 EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
 
-struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
+static struct crypto_tfm *crypto_alloc_tfm_base(const char *name, u32 flags,
+						u32 type, u32 mask)
 {
 	struct crypto_tfm *tfm;
 	int err;
@@ -370,7 +371,7 @@ struct crypto_tfm *crypto_alloc_tfm(cons
 	for (;;) {
 		struct crypto_alg *alg;
 
-		alg = crypto_alg_mod_lookup(name, 0, CRYPTO_ALG_ASYNC);
+		alg = crypto_alg_mod_lookup(name, type, mask);
 		if (IS_ERR(alg)) {
 			err = PTR_ERR(alg);
 			goto err;
@@ -395,6 +396,11 @@ err:
 	return ERR_PTR(err);
 }
 
+struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
+{
+	return crypto_alloc_tfm_base(name, flags, 0, CRYPTO_ALG_ASYNC);
+}
+
 /*
  *	crypto_alloc_base - Locate algorithm and allocate transform
  *	@alg_name: Name of algorithm
@@ -416,35 +422,7 @@ err:
  */
 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
 {
-	struct crypto_tfm *tfm;
-	int err;
-
-	for (;;) {
-		struct crypto_alg *alg;
-
-		alg = crypto_alg_mod_lookup(alg_name, type, mask);
-		if (IS_ERR(alg)) {
-			err = PTR_ERR(alg);
-			goto err;
-		}
-
-		tfm = __crypto_alloc_tfm(alg, 0);
-		if (!IS_ERR(tfm))
-			return tfm;
-
-		crypto_mod_put(alg);
-		err = PTR_ERR(tfm);
-
-err:
-		if (err != -EAGAIN)
-			break;
-		if (signal_pending(current)) {
-			err = -EINTR;
-			break;
-		}
-	}
-
-	return ERR_PTR(err);
+	return crypto_alloc_tfm_base(alg_name, 0, type, mask);
 }
 EXPORT_SYMBOL_GPL(crypto_alloc_base);
  
maintainer
#2 lore raw
In reply to Akinobu Mita
Quoted 6 lines · Akinobu Mita

crypto_alloc_tfm() and crypto_alloc_base() are suppose to return error code as pointer on failures. But there are some cases where they return NULL (for example, crypto_alloc_tfm() returns NULL on kzalloc() failure)

This patch fixes that wrong return value, and also fixes tcrypt so that it can detect error code correctly.

Actually, crypto_alloc_tfm is an obsolete function which is supposed to maintain its previous semantics of returning NULL or success.

I don't quite see where the problem with crypto_alloc_base is.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 2 lines · Herbert Xu

Actually, crypto_alloc_tfm is an obsolete function which is supposed to maintain its previous semantics of returning NULL or success.

I misunderstood about crypto_alloc_tfm().

BTW, ecryptfs and reiser4 are still using crypto_alloc_tfm(). Should we mark it as __deprecated?

Quoted 1 line

I don't quite see where the problem with crypto_alloc_base is.

- __crypto_alloc_tfm() should return -ENOMEM on kzalloc() failure. But it returns NULL.

- crypto_alloc_base() may not return -EINTR on signal_pending()

I'll fix the patch and resend with more clear description later.

maintainer
#4 lore raw
Quoted 5 lines · Akinobu Mita

I misunderstood about crypto_alloc_tfm().

BTW, ecryptfs and reiser4 are still using crypto_alloc_tfm(). Should we mark it as __deprecated?

Probably. Although anybody using crypto_alloc_tfm will probably also use the old crypto interface which gives plenty of warnings already.

Quoted 6 lines

- __crypto_alloc_tfm() should return -ENOMEM on kzalloc() failure. But it returns NULL.

- crypto_alloc_base() may not return -EINTR on signal_pending()

I'll fix the patch and resend with more clear description later.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt