Skip to content

Commit

Permalink
crypto: api - Do not wait for tests during registration
Browse files Browse the repository at this point in the history
As registration is usually carried out during module init, this
is a context where as little work as possible should be carried
out.  Testing may trigger module loads of underlying components,
which could even lead back to the module that is registering at
the moment.  This may lead to dead-locks outside of the Crypto API.

Avoid this by not waiting for the tests to complete.  They will
be scheduled but completion will be asynchronous.  Any users will
still wait for completion.

Reported-by: Russell King <linux@armlinux.org.uk>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
herbertx committed Aug 24, 2024
1 parent 96ad595 commit 37da5d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
23 changes: 12 additions & 11 deletions crypto/algapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ void crypto_alg_tested(const char *name, int err)
}

pr_err("alg: Unexpected test result for %s: %d\n", name, err);
goto unlock;
up_write(&crypto_alg_sem);
return;

found:
q->cra_flags |= CRYPTO_ALG_DEAD;
Expand All @@ -387,11 +388,12 @@ void crypto_alg_tested(const char *name, int err)
crypto_alg_finish_registration(alg, &list);

complete:
list_del_init(&test->alg.cra_list);
complete_all(&test->completion);

unlock:
up_write(&crypto_alg_sem);

crypto_alg_put(&test->alg);
crypto_remove_final(&list);
}
EXPORT_SYMBOL_GPL(crypto_alg_tested);
Expand All @@ -412,7 +414,6 @@ int crypto_register_alg(struct crypto_alg *alg)
{
struct crypto_larval *larval;
LIST_HEAD(algs_to_put);
bool test_started = false;
int err;

alg->cra_flags &= ~CRYPTO_ALG_DEAD;
Expand All @@ -423,15 +424,16 @@ int crypto_register_alg(struct crypto_alg *alg)
down_write(&crypto_alg_sem);
larval = __crypto_register_alg(alg, &algs_to_put);
if (!IS_ERR_OR_NULL(larval)) {
test_started = crypto_boot_test_finished();
bool test_started = crypto_boot_test_finished();

larval->test_started = test_started;
if (test_started)
crypto_schedule_test(larval);
}
up_write(&crypto_alg_sem);

if (IS_ERR(larval))
return PTR_ERR(larval);
if (test_started)
crypto_wait_for_test(larval);
crypto_remove_final(&algs_to_put);
return 0;
}
Expand Down Expand Up @@ -646,8 +648,10 @@ int crypto_register_instance(struct crypto_template *tmpl,
larval = __crypto_register_alg(&inst->alg, &algs_to_put);
if (IS_ERR(larval))
goto unlock;
else if (larval)
else if (larval) {
larval->test_started = true;
crypto_schedule_test(larval);
}

hlist_add_head(&inst->list, &tmpl->instances);
inst->tmpl = tmpl;
Expand All @@ -657,8 +661,6 @@ int crypto_register_instance(struct crypto_template *tmpl,

if (IS_ERR(larval))
return PTR_ERR(larval);
if (larval)
crypto_wait_for_test(larval);
crypto_remove_final(&algs_to_put);
return 0;
}
Expand Down Expand Up @@ -1042,15 +1044,14 @@ static void __init crypto_start_tests(void)

l->test_started = true;
larval = l;
crypto_schedule_test(larval);
break;
}

up_write(&crypto_alg_sem);

if (!larval)
break;

crypto_wait_for_test(larval);
}

set_crypto_boot_test_finished();
Expand Down
37 changes: 19 additions & 18 deletions crypto/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,32 +154,31 @@ static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
return alg;
}

void crypto_larval_kill(struct crypto_alg *alg)
static void crypto_larval_kill(struct crypto_larval *larval)
{
struct crypto_larval *larval = (void *)alg;
bool unlinked;

down_write(&crypto_alg_sem);
list_del(&alg->cra_list);
unlinked = list_empty(&larval->alg.cra_list);
if (!unlinked)
list_del_init(&larval->alg.cra_list);
up_write(&crypto_alg_sem);

if (unlinked)
return;

complete_all(&larval->completion);
crypto_alg_put(alg);
crypto_alg_put(&larval->alg);
}
EXPORT_SYMBOL_GPL(crypto_larval_kill);

void crypto_wait_for_test(struct crypto_larval *larval)
void crypto_schedule_test(struct crypto_larval *larval)
{
int err;

err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
if (WARN_ON_ONCE(err != NOTIFY_STOP))
goto out;

err = wait_for_completion_killable(&larval->completion);
WARN_ON(err);
out:
crypto_larval_kill(&larval->alg);
WARN_ON_ONCE(err != NOTIFY_STOP);
}
EXPORT_SYMBOL_GPL(crypto_wait_for_test);
EXPORT_SYMBOL_GPL(crypto_schedule_test);

static void crypto_start_test(struct crypto_larval *larval)
{
Expand All @@ -198,7 +197,7 @@ static void crypto_start_test(struct crypto_larval *larval)
larval->test_started = true;
up_write(&crypto_alg_sem);

crypto_wait_for_test(larval);
crypto_schedule_test(larval);
}

static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
Expand All @@ -218,9 +217,11 @@ static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
alg = larval->adult;
if (time_left < 0)
alg = ERR_PTR(-EINTR);
else if (!time_left)
else if (!time_left) {
if (crypto_is_test_larval(larval))
crypto_larval_kill(larval);
alg = ERR_PTR(-ETIMEDOUT);
else if (!alg) {
} else if (!alg) {
u32 type;
u32 mask;

Expand Down Expand Up @@ -355,7 +356,7 @@ struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
crypto_mod_put(larval);
alg = ERR_PTR(-ENOENT);
}
crypto_larval_kill(larval);
crypto_larval_kill(container_of(larval, struct crypto_larval, alg));
return alg;
}
EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
Expand Down
3 changes: 1 addition & 2 deletions crypto/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);

struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
void crypto_larval_kill(struct crypto_alg *alg);
void crypto_wait_for_test(struct crypto_larval *larval);
void crypto_schedule_test(struct crypto_larval *larval);
void crypto_alg_tested(const char *name, int err);

void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
Expand Down

0 comments on commit 37da5d0

Please sign in to comment.