Fix timing bug in ensure_engine_loaded#1
Open
sverker wants to merge 1 commit into
Open
Conversation
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.
sverker
commented
Jan 13, 2022
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; | ||
| } |
Owner
Author
There was a problem hiding this comment.
Det här är onödigt. Om mutexen redan finns så fortsätt använd den.
Comment on lines
307
to
311
| static void unload(ErlNifEnv* env, void* priv_data) | ||
| { | ||
| destroy_engine_mutex(env); | ||
| --library_refc; | ||
| } |
Owner
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.