From fdb53c9ff5cf62be75f1b912c7aeec8915e8a7d2 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 23 Oct 2024 13:19:54 +0200 Subject: [PATCH] crypto: Fix deadlock race in crypto:supports(curves) Commit 85edb09c595336388737b3beed24f82cb39aaa57 exposed this race by starting to call get_curve_cnt() lazily, potentially from different threads at the same time. --- lib/crypto/c_src/algorithms.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/crypto/c_src/algorithms.c b/lib/crypto/c_src/algorithms.c index 578f79e4f1f1..7afa8cb15902 100644 --- a/lib/crypto/c_src/algorithms.c +++ b/lib/crypto/c_src/algorithms.c @@ -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);