crypto: algapi - enforce that all instances have a ->free() method
authorEric Biggers <ebiggers@google.com>
Fri, 3 Jan 2020 04:04:40 +0000 (20:04 -0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 9 Jan 2020 03:30:58 +0000 (11:30 +0800)
All instances need to have a ->free() method, but people could forget to
set it and then not notice if the instance is never unregistered.  To
help detect this bug earlier, don't allow an instance without a ->free()
method to be registered, and complain loudly if someone tries to do it.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/aead.c
crypto/ahash.c
crypto/akcipher.c
crypto/shash.c
crypto/skcipher.c

index 7707d32..1699109 100644 (file)
@@ -288,6 +288,9 @@ int aead_register_instance(struct crypto_template *tmpl,
 {
        int err;
 
+       if (WARN_ON(!inst->free))
+               return -EINVAL;
+
        err = aead_prepare_alg(&inst->alg);
        if (err)
                return err;
index cd5d984..68a0f0c 100644 (file)
@@ -656,6 +656,9 @@ int ahash_register_instance(struct crypto_template *tmpl,
 {
        int err;
 
+       if (WARN_ON(!inst->free))
+               return -EINVAL;
+
        err = ahash_prepare_alg(&inst->alg);
        if (err)
                return err;
index eeed6c1..f866085 100644 (file)
@@ -147,6 +147,8 @@ EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
 int akcipher_register_instance(struct crypto_template *tmpl,
                               struct akcipher_instance *inst)
 {
+       if (WARN_ON(!inst->free))
+               return -EINVAL;
        akcipher_prepare_alg(&inst->alg);
        return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
 }
index 70faf28..c075b26 100644 (file)
@@ -577,6 +577,9 @@ int shash_register_instance(struct crypto_template *tmpl,
 {
        int err;
 
+       if (WARN_ON(!inst->free))
+               return -EINVAL;
+
        err = shash_prepare_alg(&inst->alg);
        if (err)
                return err;
index 42add1e..7221def 100644 (file)
@@ -865,6 +865,9 @@ int skcipher_register_instance(struct crypto_template *tmpl,
 {
        int err;
 
+       if (WARN_ON(!inst->free))
+               return -EINVAL;
+
        err = skcipher_prepare_alg(&inst->alg);
        if (err)
                return err;