Skip to content

Commit 3f683d6

Browse files
committed
crypto: api - Fix crypto_alloc_tfm/create_create_tfm return convention
This is based on a report and patch by Geert Uytterhoeven. The functions crypto_alloc_tfm and create_create_tfm return a pointer that needs to be adjusted by the caller when successful and otherwise an error value. This means that the caller has to check for the error and only perform the adjustment if the pointer returned is valid. Since all callers want to make the adjustment and we know how to adjust it ourselves, it's much easier to just return adjusted pointer directly. The only caveat is that we have to return a void * instead of struct crypto_tfm *. However, this isn't that bad because both of these functions are for internal use only (by types code like shash.c, not even algorithms code). This patch also moves crypto_alloc_tfm into crypto/internal.h (crypto_create_tfm is already there) to reflect this. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ff75330 commit 3f683d6

4 files changed

Lines changed: 16 additions & 26 deletions

File tree

‎crypto/api.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
453453
}
454454
EXPORT_SYMBOL_GPL(crypto_alloc_base);
455455

456-
struct crypto_tfm *crypto_create_tfm(struct crypto_alg *alg,
457-
const struct crypto_type *frontend)
456+
void *crypto_create_tfm(struct crypto_alg *alg,
457+
const struct crypto_type *frontend)
458458
{
459459
char *mem;
460460
struct crypto_tfm *tfm = NULL;
@@ -488,9 +488,9 @@ struct crypto_tfm *crypto_create_tfm(struct crypto_alg *alg,
488488
crypto_shoot_alg(alg);
489489
kfree(mem);
490490
out_err:
491-
tfm = ERR_PTR(err);
491+
mem = ERR_PTR(err);
492492
out:
493-
return tfm;
493+
return mem;
494494
}
495495
EXPORT_SYMBOL_GPL(crypto_create_tfm);
496496

@@ -514,12 +514,11 @@ EXPORT_SYMBOL_GPL(crypto_create_tfm);
514514
*
515515
* In case of error the return value is an error pointer.
516516
*/
517-
struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
518-
const struct crypto_type *frontend,
519-
u32 type, u32 mask)
517+
void *crypto_alloc_tfm(const char *alg_name,
518+
const struct crypto_type *frontend, u32 type, u32 mask)
520519
{
521520
struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
522-
struct crypto_tfm *tfm;
521+
void *tfm;
523522
int err;
524523

525524
type &= frontend->maskclear;

‎crypto/internal.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ void crypto_alg_tested(const char *name, int err);
109109
void crypto_shoot_alg(struct crypto_alg *alg);
110110
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
111111
u32 mask);
112-
struct crypto_tfm *crypto_create_tfm(struct crypto_alg *alg,
113-
const struct crypto_type *frontend);
112+
void *crypto_create_tfm(struct crypto_alg *alg,
113+
const struct crypto_type *frontend);
114+
void *crypto_alloc_tfm(const char *alg_name,
115+
const struct crypto_type *frontend, u32 type, u32 mask);
114116

115117
int crypto_register_instance(struct crypto_template *tmpl,
116118
struct crypto_instance *inst);

‎crypto/shash.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
#include <linux/slab.h>
1919
#include <linux/seq_file.h>
2020

21-
static const struct crypto_type crypto_shash_type;
22-
23-
static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
24-
{
25-
return container_of(tfm, struct crypto_shash, base);
26-
}
27-
2821
#include "internal.h"
2922

23+
static const struct crypto_type crypto_shash_type;
24+
3025
static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
3126
unsigned int keylen)
3227
{
@@ -282,8 +277,7 @@ static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
282277
if (!crypto_mod_get(calg))
283278
return -EAGAIN;
284279

285-
shash = __crypto_shash_cast(crypto_create_tfm(
286-
calg, &crypto_shash_type));
280+
shash = crypto_create_tfm(calg, &crypto_shash_type);
287281
if (IS_ERR(shash)) {
288282
crypto_mod_put(calg);
289283
return PTR_ERR(shash);
@@ -391,8 +385,7 @@ static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
391385
if (!crypto_mod_get(calg))
392386
return -EAGAIN;
393387

394-
shash = __crypto_shash_cast(crypto_create_tfm(
395-
calg, &crypto_shash_type));
388+
shash = crypto_create_tfm(calg, &crypto_shash_type);
396389
if (IS_ERR(shash)) {
397390
crypto_mod_put(calg);
398391
return PTR_ERR(shash);
@@ -480,8 +473,7 @@ static const struct crypto_type crypto_shash_type = {
480473
struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
481474
u32 mask)
482475
{
483-
return __crypto_shash_cast(
484-
crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask));
476+
return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
485477
}
486478
EXPORT_SYMBOL_GPL(crypto_alloc_shash);
487479

‎include/linux/crypto.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,6 @@ struct crypto_attr_u32 {
548548
* Transform user interface.
549549
*/
550550

551-
struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
552-
const struct crypto_type *frontend,
553-
u32 type, u32 mask);
554551
struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
555552
void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
556553

0 commit comments

Comments
 (0)