Skip to content

Commit c6d633a

Browse files
ebiggersherbertx
authored andcommitted
crypto: algapi - make unregistration functions return void
Some of the algorithm unregistration functions return -ENOENT when asked to unregister a non-registered algorithm, while others always return 0 or always return void. But no users check the return value, except for two of the bulk unregistration functions which print a message on error but still always return 0 to their caller, and crypto_del_alg() which calls crypto_unregister_instance() which always returns 0. Since unregistering a non-registered algorithm is always a kernel bug but there isn't anything callers should do to handle this situation at runtime, let's simplify things by making all the unregistration functions return void, and moving the error message into crypto_unregister_alg() and upgrading it to a WARN(). Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 0e89640 commit c6d633a

File tree

12 files changed

+42
-71
lines changed

12 files changed

+42
-71
lines changed

Documentation/crypto/devel-algos.rst

+12-22
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,18 @@ The counterparts to those functions are listed below.
3131

3232
::
3333

34-
int crypto_unregister_alg(struct crypto_alg *alg);
35-
int crypto_unregister_algs(struct crypto_alg *algs, int count);
34+
void crypto_unregister_alg(struct crypto_alg *alg);
35+
void crypto_unregister_algs(struct crypto_alg *algs, int count);
3636

3737

38-
Notice that both registration and unregistration functions do return a
39-
value, so make sure to handle errors. A return code of zero implies
40-
success. Any return code < 0 implies an error.
38+
The registration functions return 0 on success, or a negative errno
39+
value on failure. crypto_register_algs() succeeds only if it
40+
successfully registered all the given algorithms; if it fails partway
41+
through, then any changes are rolled back.
4142

42-
The bulk registration/unregistration functions register/unregister each
43-
transformation in the given array of length count. They handle errors as
44-
follows:
45-
46-
- crypto_register_algs() succeeds if and only if it successfully
47-
registers all the given transformations. If an error occurs partway
48-
through, then it rolls back successful registrations before returning
49-
the error code. Note that if a driver needs to handle registration
50-
errors for individual transformations, then it will need to use the
51-
non-bulk function crypto_register_alg() instead.
52-
53-
- crypto_unregister_algs() tries to unregister all the given
54-
transformations, continuing on error. It logs errors and always
55-
returns zero.
43+
The unregistration functions always succeed, so they don't have a
44+
return value. Don't try to unregister algorithms that aren't
45+
currently registered.
5646

5747
Single-Block Symmetric Ciphers [CIPHER]
5848
---------------------------------------
@@ -169,10 +159,10 @@ are as follows:
169159

170160
::
171161

172-
int crypto_unregister_ahash(struct ahash_alg *alg);
162+
void crypto_unregister_ahash(struct ahash_alg *alg);
173163

174-
int crypto_unregister_shash(struct shash_alg *alg);
175-
int crypto_unregister_shashes(struct shash_alg *algs, int count);
164+
void crypto_unregister_shash(struct shash_alg *alg);
165+
void crypto_unregister_shashes(struct shash_alg *algs, int count);
176166

177167

178168
Cipher Definition With struct shash_alg and ahash_alg

crypto/acompress.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ int crypto_register_acomp(struct acomp_alg *alg)
151151
}
152152
EXPORT_SYMBOL_GPL(crypto_register_acomp);
153153

154-
int crypto_unregister_acomp(struct acomp_alg *alg)
154+
void crypto_unregister_acomp(struct acomp_alg *alg)
155155
{
156-
return crypto_unregister_alg(&alg->base);
156+
crypto_unregister_alg(&alg->base);
157157
}
158158
EXPORT_SYMBOL_GPL(crypto_unregister_acomp);
159159

crypto/ahash.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ int crypto_register_ahash(struct ahash_alg *alg)
598598
}
599599
EXPORT_SYMBOL_GPL(crypto_register_ahash);
600600

601-
int crypto_unregister_ahash(struct ahash_alg *alg)
601+
void crypto_unregister_ahash(struct ahash_alg *alg)
602602
{
603-
return crypto_unregister_alg(&alg->halg.base);
603+
crypto_unregister_alg(&alg->halg.base);
604604
}
605605
EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
606606

crypto/algapi.c

+8-17
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
442442
return 0;
443443
}
444444

445-
int crypto_unregister_alg(struct crypto_alg *alg)
445+
void crypto_unregister_alg(struct crypto_alg *alg)
446446
{
447447
int ret;
448448
LIST_HEAD(list);
@@ -451,15 +451,14 @@ int crypto_unregister_alg(struct crypto_alg *alg)
451451
ret = crypto_remove_alg(alg, &list);
452452
up_write(&crypto_alg_sem);
453453

454-
if (ret)
455-
return ret;
454+
if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
455+
return;
456456

457457
BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
458458
if (alg->cra_destroy)
459459
alg->cra_destroy(alg);
460460

461461
crypto_remove_final(&list);
462-
return 0;
463462
}
464463
EXPORT_SYMBOL_GPL(crypto_unregister_alg);
465464

@@ -483,18 +482,12 @@ int crypto_register_algs(struct crypto_alg *algs, int count)
483482
}
484483
EXPORT_SYMBOL_GPL(crypto_register_algs);
485484

486-
int crypto_unregister_algs(struct crypto_alg *algs, int count)
485+
void crypto_unregister_algs(struct crypto_alg *algs, int count)
487486
{
488-
int i, ret;
489-
490-
for (i = 0; i < count; i++) {
491-
ret = crypto_unregister_alg(&algs[i]);
492-
if (ret)
493-
pr_err("Failed to unregister %s %s: %d\n",
494-
algs[i].cra_driver_name, algs[i].cra_name, ret);
495-
}
487+
int i;
496488

497-
return 0;
489+
for (i = 0; i < count; i++)
490+
crypto_unregister_alg(&algs[i]);
498491
}
499492
EXPORT_SYMBOL_GPL(crypto_unregister_algs);
500493

@@ -639,7 +632,7 @@ int crypto_register_instance(struct crypto_template *tmpl,
639632
}
640633
EXPORT_SYMBOL_GPL(crypto_register_instance);
641634

642-
int crypto_unregister_instance(struct crypto_instance *inst)
635+
void crypto_unregister_instance(struct crypto_instance *inst)
643636
{
644637
LIST_HEAD(list);
645638

@@ -651,8 +644,6 @@ int crypto_unregister_instance(struct crypto_instance *inst)
651644
up_write(&crypto_alg_sem);
652645

653646
crypto_remove_final(&list);
654-
655-
return 0;
656647
}
657648
EXPORT_SYMBOL_GPL(crypto_unregister_instance);
658649

crypto/crypto_user_base.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
323323
if (refcount_read(&alg->cra_refcnt) > 2)
324324
goto drop_alg;
325325

326-
err = crypto_unregister_instance((struct crypto_instance *)alg);
326+
crypto_unregister_instance((struct crypto_instance *)alg);
327+
err = 0;
327328

328329
drop_alg:
329330
crypto_mod_put(alg);

crypto/scompress.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ int crypto_register_scomp(struct scomp_alg *alg)
266266
}
267267
EXPORT_SYMBOL_GPL(crypto_register_scomp);
268268

269-
int crypto_unregister_scomp(struct scomp_alg *alg)
269+
void crypto_unregister_scomp(struct scomp_alg *alg)
270270
{
271-
return crypto_unregister_alg(&alg->base);
271+
crypto_unregister_alg(&alg->base);
272272
}
273273
EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
274274

crypto/shash.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ int crypto_register_shash(struct shash_alg *alg)
520520
}
521521
EXPORT_SYMBOL_GPL(crypto_register_shash);
522522

523-
int crypto_unregister_shash(struct shash_alg *alg)
523+
void crypto_unregister_shash(struct shash_alg *alg)
524524
{
525-
return crypto_unregister_alg(&alg->base);
525+
crypto_unregister_alg(&alg->base);
526526
}
527527
EXPORT_SYMBOL_GPL(crypto_unregister_shash);
528528

@@ -546,19 +546,12 @@ int crypto_register_shashes(struct shash_alg *algs, int count)
546546
}
547547
EXPORT_SYMBOL_GPL(crypto_register_shashes);
548548

549-
int crypto_unregister_shashes(struct shash_alg *algs, int count)
549+
void crypto_unregister_shashes(struct shash_alg *algs, int count)
550550
{
551-
int i, ret;
552-
553-
for (i = count - 1; i >= 0; --i) {
554-
ret = crypto_unregister_shash(&algs[i]);
555-
if (ret)
556-
pr_err("Failed to unregister %s %s: %d\n",
557-
algs[i].base.cra_driver_name,
558-
algs[i].base.cra_name, ret);
559-
}
551+
int i;
560552

561-
return 0;
553+
for (i = count - 1; i >= 0; --i)
554+
crypto_unregister_shash(&algs[i]);
562555
}
563556
EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
564557

include/crypto/algapi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct crypto_template *crypto_lookup_template(const char *name);
9696

9797
int crypto_register_instance(struct crypto_template *tmpl,
9898
struct crypto_instance *inst);
99-
int crypto_unregister_instance(struct crypto_instance *inst);
99+
void crypto_unregister_instance(struct crypto_instance *inst);
100100

101101
int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
102102
struct crypto_instance *inst, u32 mask);

include/crypto/internal/acompress.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ int crypto_register_acomp(struct acomp_alg *alg);
6868
* compression algorithm
6969
*
7070
* @alg: algorithm definition
71-
*
72-
* Return: zero on success; error code in case of error
7371
*/
74-
int crypto_unregister_acomp(struct acomp_alg *alg);
72+
void crypto_unregister_acomp(struct acomp_alg *alg);
7573

7674
int crypto_register_acomps(struct acomp_alg *algs, int count);
7775
void crypto_unregister_acomps(struct acomp_alg *algs, int count);

include/crypto/internal/hash.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static inline int crypto_ahash_walk_last(struct crypto_hash_walk *walk)
7070
}
7171

7272
int crypto_register_ahash(struct ahash_alg *alg);
73-
int crypto_unregister_ahash(struct ahash_alg *alg);
73+
void crypto_unregister_ahash(struct ahash_alg *alg);
7474
int crypto_register_ahashes(struct ahash_alg *algs, int count);
7575
void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
7676
int ahash_register_instance(struct crypto_template *tmpl,
@@ -105,9 +105,9 @@ static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
105105
struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask);
106106

107107
int crypto_register_shash(struct shash_alg *alg);
108-
int crypto_unregister_shash(struct shash_alg *alg);
108+
void crypto_unregister_shash(struct shash_alg *alg);
109109
int crypto_register_shashes(struct shash_alg *algs, int count);
110-
int crypto_unregister_shashes(struct shash_alg *algs, int count);
110+
void crypto_unregister_shashes(struct shash_alg *algs, int count);
111111
int shash_register_instance(struct crypto_template *tmpl,
112112
struct shash_instance *inst);
113113
void shash_free_instance(struct crypto_instance *inst);

include/crypto/internal/scompress.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ int crypto_register_scomp(struct scomp_alg *alg);
112112
* compression algorithm
113113
*
114114
* @alg: algorithm definition
115-
*
116-
* Return: zero on success; error code in case of error
117115
*/
118-
int crypto_unregister_scomp(struct scomp_alg *alg);
116+
void crypto_unregister_scomp(struct scomp_alg *alg);
119117

120118
int crypto_register_scomps(struct scomp_alg *algs, int count);
121119
void crypto_unregister_scomps(struct scomp_alg *algs, int count);

include/linux/crypto.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ static inline void crypto_init_wait(struct crypto_wait *wait)
584584
* Algorithm registration interface.
585585
*/
586586
int crypto_register_alg(struct crypto_alg *alg);
587-
int crypto_unregister_alg(struct crypto_alg *alg);
587+
void crypto_unregister_alg(struct crypto_alg *alg);
588588
int crypto_register_algs(struct crypto_alg *algs, int count);
589-
int crypto_unregister_algs(struct crypto_alg *algs, int count);
589+
void crypto_unregister_algs(struct crypto_alg *algs, int count);
590590

591591
/*
592592
* Algorithm query interface.

0 commit comments

Comments
 (0)