Skip to content

Commit 4f87ee1

Browse files
committed
crypto: api - Do not zap spawn->alg
Currently when a spawn is removed we will zap its alg field. This is racy because the spawn could belong to an unregistered instance which may dereference the spawn->alg field. This patch fixes this by keeping spawn->alg constant and instead adding a new spawn->dead field to indicate that a spawn is going away. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 73669cc commit 4f87ee1

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,17 @@ static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
9393
if (!spawn)
9494
return NULL;
9595

96-
n = list_next_entry(spawn, list);
96+
n = list_prev_entry(spawn, list);
97+
list_move(&spawn->list, secondary_spawns);
9798

98-
if (spawn->alg && &n->list != stack && !n->alg)
99-
n->alg = (n->list.next == stack) ? alg :
100-
&list_next_entry(n, list)->inst->alg;
99+
if (list_is_last(&n->list, stack))
100+
return top;
101101

102-
list_move(&spawn->list, secondary_spawns);
102+
n = list_next_entry(n, list);
103+
if (!spawn->dead)
104+
n->dead = false;
103105

104-
return &n->list == stack ? top : &n->inst->alg.cra_users;
106+
return &n->inst->alg.cra_users;
105107
}
106108

107109
static void crypto_remove_instance(struct crypto_instance *inst,
@@ -160,7 +162,7 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
160162
if (&inst->alg == nalg)
161163
break;
162164

163-
spawn->alg = NULL;
165+
spawn->dead = true;
164166
spawns = &inst->alg.cra_users;
165167

166168
/*
@@ -179,7 +181,7 @@ void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
179181
&secondary_spawns)));
180182

181183
list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
182-
if (spawn->alg)
184+
if (!spawn->dead)
183185
list_move(&spawn->list, &spawn->alg->cra_users);
184186
else
185187
crypto_remove_instance(spawn->inst, list);
@@ -670,7 +672,7 @@ EXPORT_SYMBOL_GPL(crypto_grab_spawn);
670672
void crypto_drop_spawn(struct crypto_spawn *spawn)
671673
{
672674
down_write(&crypto_alg_sem);
673-
if (spawn->alg)
675+
if (!spawn->dead)
674676
list_del(&spawn->list);
675677
up_write(&crypto_alg_sem);
676678
}
@@ -682,7 +684,7 @@ static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
682684

683685
down_read(&crypto_alg_sem);
684686
alg = spawn->alg;
685-
if (alg && !crypto_mod_get(alg)) {
687+
if (!spawn->dead && !crypto_mod_get(alg)) {
686688
alg->cra_flags |= CRYPTO_ALG_DYING;
687689
alg = NULL;
688690
}

‎include/crypto/algapi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct crypto_spawn {
7070
struct crypto_instance *inst;
7171
const struct crypto_type *frontend;
7272
u32 mask;
73+
bool dead;
7374
};
7475

7576
struct crypto_queue {

0 commit comments

Comments
 (0)