Skip to content

Commit

Permalink
crypto: Fix deadlock race in crypto:supports(curves)
Browse files Browse the repository at this point in the history
Commit 85edb09 exposed this race
by starting to call get_curve_cnt() lazily, potentially from different
threads at the same time.
  • Loading branch information
sverker committed Oct 23, 2024
1 parent 85edb09 commit fdb53c9
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/crypto/c_src/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,14 @@ int get_curve_cnt(ErlNifEnv* env, int fips) {

enif_mutex_lock(mtx_init_curve_types);
if (1 == fips) {
if (algo_curve_fips_cnt >= 0) {
return algo_curve_fips_cnt;
if (algo_curve_fips_cnt < 0) {
algo_curve_fips_cnt = init_curves(env, 1);
}
algo_curve_fips_cnt = init_curves(env, 1);
cnt = algo_curve_fips_cnt;
} else {
if (algo_curve_cnt >= 0) {
return algo_curve_cnt;
if (algo_curve_cnt < 0) {
algo_curve_cnt = init_curves(env, 0);
}
algo_curve_cnt = init_curves(env, 0);
cnt = algo_curve_cnt;
}
enif_mutex_unlock(mtx_init_curve_types);
Expand Down

0 comments on commit fdb53c9

Please sign in to comment.