Skip to content

Commit

Permalink
crypto: algapi - enforce that all instances have a ->free() method
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
ebiggers authored and herbertx committed Jan 9, 2020
1 parent a24a1fd commit d4fdc2d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crypto/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions crypto/ahash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions crypto/akcipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
3 changes: 3 additions & 0 deletions crypto/shash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions crypto/skcipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d4fdc2d

Please sign in to comment.