p11_load.c: fixed crash when loaded dll initializes with error #616
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.
Fixes #615
When we load module (util_uri.c, line 174), we call pkcs11_CTX_load(). After module successfully loaded (p11_load.c, line 99), both method and handle members of cpriv structure are initialized. Then we call pkcs11_initialize() (p11_load.c, line 105). However, if C_Initialize() method returns error (in my case it was 0x80000384 — "General error from secure messaging system – probably caused by HSM failure or network failure"), we unload module (p11_load.c, lines 106-107). Notice only handle member is nulled; method is unchanged, though it pointers are no more valid. Then we return back to util_ctx_init_libp11(), and it tries to free module once again, calling UTIL_CTX_free_libp11() (util_uri.c, line 180). Ultimately we end up in pkcs11_CTX_unload() (p11_load.c, line 143). There we check if module if loaded by checking method member (p11_load.c, line 147), but it's not null (only handle is cleared). And so we crash when calling cpriv->method->C_Finalize(NULL) (p11_load.c, line 152).