Skip to content

Fix timing bug in ensure_engine_loaded#1

Open
sverker wants to merge 1 commit into
maint-23from
lars/crypto/ensure-load-problem/OTP-17858
Open

Fix timing bug in ensure_engine_loaded#1
sverker wants to merge 1 commit into
maint-23from
lars/crypto/ensure-load-problem/OTP-17858

Conversation

@sverker

@sverker sverker commented Jan 13, 2022

Copy link
Copy Markdown
Owner

When two ensure_engine_loaded() calls were done in parallell there
was a possibility that a crypto lib function was called by both instead
of just one of them.

This is solved by moving the implementation from erlang down into a nif
function that uses a mutex to protect the sensitive part.

When two ensure_engine_loaded() calls were done in parallell there
was a possibility that a crypto lib function was called by both instead
of just one of them.

This is solved by moving the implementation from erlang down into a nif
function that uses a mutex to protect the sensitive part.
Comment thread lib/crypto/c_src/engine.c
Comment on lines +129 to +133
/* Destroy old mutex if exists (if upgrade function called) */
if(ensure_engine_loaded_mtx) {
enif_mutex_destroy(ensure_engine_loaded_mtx);
ensure_engine_loaded_mtx = NULL;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det här är onödigt. Om mutexen redan finns så fortsätt använd den.

Comment thread lib/crypto/c_src/crypto.c
Comment on lines 307 to 311
static void unload(ErlNifEnv* env, void* priv_data)
{
destroy_engine_mutex(env);
--library_refc;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Här borde du istället göra

if (--library_refc == 0) {
    destroy_engine_mutex(env);
}

Dvs, mutexen förstörs när ingen Erlang crypto module instans längre använder crypto.so.
Om man upgraderar crypto.beam så kanske både den nya modul instansen och den gamla använda samma crypto.so och då är library_refc 2. Och unload anropas av erlang:purge_module oavsett om .so filen verkligen kommer att laddas ur eller om det finns fler användare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants