Skip to content

Commit a73e699

Browse files
committed
[CRYPTO] api: Do not remove users unless new algorithm matches
As it is whenever a new algorithm with the same name is registered users of the old algorithm will be removed so that they can take advantage of the new algorithm. This presents a problem when the new algorithm is not equivalent to the old algorithm. In particular, the new algorithm might only function on top of the existing one. Hence we should not remove users unless they can make use of the new algorithm. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent cf02f5d commit a73e699

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,47 @@ static void crypto_destroy_instance(struct crypto_alg *alg)
8484
crypto_tmpl_put(tmpl);
8585
}
8686

87-
static void crypto_remove_spawns(struct list_head *spawns,
88-
struct list_head *list)
87+
static void crypto_remove_spawn(struct crypto_spawn *spawn,
88+
struct list_head *list,
89+
struct list_head *secondary_spawns)
8990
{
90-
struct crypto_spawn *spawn, *n;
91+
struct crypto_instance *inst = spawn->inst;
92+
struct crypto_template *tmpl = inst->tmpl;
9193

92-
list_for_each_entry_safe(spawn, n, spawns, list) {
93-
struct crypto_instance *inst = spawn->inst;
94-
struct crypto_template *tmpl = inst->tmpl;
94+
list_del_init(&spawn->list);
95+
spawn->alg = NULL;
9596

96-
list_del_init(&spawn->list);
97-
spawn->alg = NULL;
97+
if (crypto_is_dead(&inst->alg))
98+
return;
9899

99-
if (crypto_is_dead(&inst->alg))
100-
continue;
100+
inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
101+
if (!tmpl || !crypto_tmpl_get(tmpl))
102+
return;
103+
104+
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
105+
list_move(&inst->alg.cra_list, list);
106+
hlist_del(&inst->list);
107+
inst->alg.cra_destroy = crypto_destroy_instance;
108+
109+
list_splice(&inst->alg.cra_users, secondary_spawns);
110+
}
111+
112+
static void crypto_remove_spawns(struct list_head *spawns,
113+
struct list_head *list, u32 new_type)
114+
{
115+
struct crypto_spawn *spawn, *n;
116+
LIST_HEAD(secondary_spawns);
101117

102-
inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
103-
if (!tmpl || !crypto_tmpl_get(tmpl))
118+
list_for_each_entry_safe(spawn, n, spawns, list) {
119+
if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
104120
continue;
105121

106-
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
107-
list_move(&inst->alg.cra_list, list);
108-
hlist_del(&inst->list);
109-
inst->alg.cra_destroy = crypto_destroy_instance;
122+
crypto_remove_spawn(spawn, list, &secondary_spawns);
123+
}
110124

111-
if (!list_empty(&inst->alg.cra_users)) {
112-
if (&n->list == spawns)
113-
n = list_entry(inst->alg.cra_users.next,
114-
typeof(*n), list);
115-
__list_splice(&inst->alg.cra_users, spawns->prev);
116-
}
125+
while (!list_empty(&secondary_spawns)) {
126+
list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
127+
crypto_remove_spawn(spawn, list, &secondary_spawns);
117128
}
118129
}
119130

@@ -164,7 +175,7 @@ static int __crypto_register_alg(struct crypto_alg *alg,
164175
q->cra_priority > alg->cra_priority)
165176
continue;
166177

167-
crypto_remove_spawns(&q->cra_users, list);
178+
crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
168179
}
169180

170181
list_add(&alg->cra_list, &crypto_alg_list);
@@ -214,7 +225,7 @@ static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
214225

215226
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
216227
list_del_init(&alg->cra_list);
217-
crypto_remove_spawns(&alg->cra_users, list);
228+
crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
218229

219230
return 0;
220231
}
@@ -351,11 +362,12 @@ int crypto_register_instance(struct crypto_template *tmpl,
351362
EXPORT_SYMBOL_GPL(crypto_register_instance);
352363

353364
int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
354-
struct crypto_instance *inst)
365+
struct crypto_instance *inst, u32 mask)
355366
{
356367
int err = -EAGAIN;
357368

358369
spawn->inst = inst;
370+
spawn->mask = mask;
359371

360372
down_write(&crypto_alg_sem);
361373
if (!crypto_is_moribund(alg)) {
@@ -494,7 +506,8 @@ struct crypto_instance *crypto_alloc_instance(const char *name,
494506
goto err_free_inst;
495507

496508
spawn = crypto_instance_ctx(inst);
497-
err = crypto_init_spawn(spawn, alg, inst);
509+
err = crypto_init_spawn(spawn, alg, inst,
510+
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
498511

499512
if (err)
500513
goto err_free_inst;

‎include/crypto/algapi.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct crypto_spawn {
5151
struct list_head list;
5252
struct crypto_alg *alg;
5353
struct crypto_instance *inst;
54+
u32 mask;
5455
};
5556

5657
struct crypto_queue {
@@ -103,7 +104,7 @@ void crypto_unregister_template(struct crypto_template *tmpl);
103104
struct crypto_template *crypto_lookup_template(const char *name);
104105

105106
int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
106-
struct crypto_instance *inst);
107+
struct crypto_instance *inst, u32 mask);
107108
void crypto_drop_spawn(struct crypto_spawn *spawn);
108109
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
109110
u32 mask);

0 commit comments

Comments
 (0)