Skip to content

Commit 73669cc

Browse files
committed
crypto: api - Fix race condition in crypto_spawn_alg
The function crypto_spawn_alg is racy because it drops the lock before shooting the dying algorithm. The algorithm could disappear altogether before we shoot it. This patch fixes it by moving the shooting into the locked section. Fixes: 6bfd480 ("[CRYPTO] api: Added spawns") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 4a2abbc commit 73669cc

3 files changed

Lines changed: 6 additions & 14 deletions

File tree

‎crypto/algapi.c‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -679,22 +679,16 @@ EXPORT_SYMBOL_GPL(crypto_drop_spawn);
679679
static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
680680
{
681681
struct crypto_alg *alg;
682-
struct crypto_alg *alg2;
683682

684683
down_read(&crypto_alg_sem);
685684
alg = spawn->alg;
686-
alg2 = alg;
687-
if (alg2)
688-
alg2 = crypto_mod_get(alg2);
689-
up_read(&crypto_alg_sem);
690-
691-
if (!alg2) {
692-
if (alg)
693-
crypto_shoot_alg(alg);
694-
return ERR_PTR(-EAGAIN);
685+
if (alg && !crypto_mod_get(alg)) {
686+
alg->cra_flags |= CRYPTO_ALG_DYING;
687+
alg = NULL;
695688
}
689+
up_read(&crypto_alg_sem);
696690

697-
return alg;
691+
return alg ?: ERR_PTR(-EAGAIN);
698692
}
699693

700694
struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,

‎crypto/api.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,12 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
331331
return len;
332332
}
333333

334-
void crypto_shoot_alg(struct crypto_alg *alg)
334+
static void crypto_shoot_alg(struct crypto_alg *alg)
335335
{
336336
down_write(&crypto_alg_sem);
337337
alg->cra_flags |= CRYPTO_ALG_DYING;
338338
up_write(&crypto_alg_sem);
339339
}
340-
EXPORT_SYMBOL_GPL(crypto_shoot_alg);
341340

342341
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
343342
u32 mask)

‎crypto/internal.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ void crypto_alg_tested(const char *name, int err);
6565
void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
6666
struct crypto_alg *nalg);
6767
void crypto_remove_final(struct list_head *list);
68-
void crypto_shoot_alg(struct crypto_alg *alg);
6968
struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
7069
u32 mask);
7170
void *crypto_create_tfm(struct crypto_alg *alg,

0 commit comments

Comments
 (0)