Skip to content

Commit 3952112

Browse files
committed
Move shutdown mode workarounds move util_uri.c
1 parent 69a5133 commit 3952112

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

src/eng_front.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#define PKCS11_ENGINE_NAME "pkcs11 engine"
3030

3131
static int pkcs11_idx = -1;
32-
static int shutdown_mode = 0;
3332

3433
/* The definitions for control commands specific to this engine */
3534

@@ -152,14 +151,7 @@ static int engine_finish(ENGINE *engine)
152151
if (!ctx)
153152
return 0;
154153

155-
/* PKCS#11 modules that register their own atexit() callbacks may
156-
* already have been cleaned up by the time OpenSSL's atexit() callback
157-
* is executed. As a result, a crash occurs with certain versions of
158-
* OpenSSL and SoftHSM2. The workaround skips the execution of
159-
* ENGINE_CTX_finish() during OpenSSL's cleanup, converting the crash into
160-
* a harmless memory leak at exit. */
161-
if (!shutdown_mode)
162-
rv &= ENGINE_CTX_finish(ctx);
154+
rv &= ENGINE_CTX_finish(ctx);
163155

164156
return rv;
165157
}
@@ -289,11 +281,6 @@ static int bind_helper_methods(ENGINE *e)
289281
}
290282
}
291283

292-
static void exit_callback(void)
293-
{
294-
shutdown_mode = 1;
295-
}
296-
297284
static int bind_fn(ENGINE *e, const char *id)
298285
{
299286
if (id && (strcmp(id, PKCS11_ENGINE_ID) != 0)) {
@@ -304,7 +291,6 @@ static int bind_fn(ENGINE *e, const char *id)
304291
ENGINE_CTX_log(NULL, LOG_ERR, "bind failed\n");
305292
return 0;
306293
}
307-
atexit(exit_callback);
308294
return 1;
309295
}
310296

src/provider.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ typedef struct {
143143
void *ui_method_data;
144144
} PASSPHRASE_DATA;
145145

146-
static int g_shutdown_mode = 0;
147-
148146
#if defined(_WIN32) || defined(_WIN64)
149147
#define strcasecmp _stricmp
150148
#endif
@@ -174,19 +172,6 @@ static void PROVIDER_CTX_log(PROVIDER_CTX *prov_ctx, int level, int reason, int
174172
va_end(args);
175173
}
176174

177-
/*
178-
* PKCS#11 modules that register their own atexit() callbacks may
179-
* already have been cleaned up by the time OpenSSL's atexit() callback
180-
* is executed. As a result, a crash occurs with certain versions of
181-
* OpenSSL and SoftHSM2. The workaround skips the execution of
182-
* ENGINE_CTX_finish() during OpenSSL's cleanup, converting the crash into
183-
* a harmless memory leak at exit.
184-
*/
185-
static void exit_callback(void)
186-
{
187-
g_shutdown_mode = 1;
188-
}
189-
190175
/*
191176
* Updates the provider context with environment variable values.
192177
*/
@@ -341,8 +326,7 @@ static PROVIDER_CTX *PROVIDER_CTX_new(void)
341326
*/
342327
static void PROVIDER_CTX_destroy(PROVIDER_CTX *prov_ctx)
343328
{
344-
if (!g_shutdown_mode)
345-
UTIL_CTX_free_libp11(prov_ctx->util_ctx);
329+
UTIL_CTX_free_libp11(prov_ctx->util_ctx);
346330
UTIL_CTX_free(prov_ctx->util_ctx);
347331
OPENSSL_free(prov_ctx->provider_name);
348332
OPENSSL_free(prov_ctx->pkcs11_module);
@@ -479,7 +463,6 @@ static int provider_init(const OSSL_CORE_HANDLE *handle, const OSSL_DISPATCH *in
479463
*out = provider_functions;
480464
*ctx = prov_ctx;
481465

482-
atexit(exit_callback);
483466
return 1;
484467

485468
err:

src/util_uri.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ struct util_ctx_st {
7575

7676
};
7777

78+
static int g_shutdown_mode = 0;
79+
7880
/******************************************************************************/
7981
/* Initialization */
8082
/******************************************************************************/
@@ -139,8 +141,19 @@ static int util_ctx_enumerate_slots_unlocked(UTIL_CTX *ctx)
139141
return 1;
140142
}
141143

142-
/* Initialize libp11 data: ctx->pkcs11_ctx and ctx->slot_list */
144+
/*
145+
* PKCS#11 modules that register their own atexit() callbacks may already have
146+
* been cleaned up by the time OpenSSL's atexit() callback is executed.
147+
* As a result, a crash occurs with certain versions of OpenSSL and SoftHSM2.
148+
* The workaround skips libp11 cleanup during OpenSSL's cleanup, converting
149+
* the crash into a harmless memory leak at exit.
150+
*/
151+
static void exit_callback(void)
152+
{
153+
g_shutdown_mode = 1;
154+
}
143155

156+
/* Initialize libp11 data: ctx->pkcs11_ctx and ctx->slot_list */
144157
static int util_ctx_init_libp11(UTIL_CTX *ctx)
145158
{
146159
if (ctx->pkcs11_ctx && ctx->slot_list && ctx->slot_count > 0)
@@ -163,6 +176,7 @@ static int util_ctx_init_libp11(UTIL_CTX *ctx)
163176
UTIL_CTX_free_libp11(ctx);
164177
return -1;
165178
}
179+
atexit(exit_callback);
166180
return 0;
167181
}
168182

@@ -182,14 +196,17 @@ int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx)
182196
void UTIL_CTX_free_libp11(UTIL_CTX *ctx)
183197
{
184198
if (ctx->slot_list) {
185-
PKCS11_release_all_slots(ctx->pkcs11_ctx,
186-
ctx->slot_list, ctx->slot_count);
199+
if (!g_shutdown_mode)
200+
PKCS11_release_all_slots(ctx->pkcs11_ctx,
201+
ctx->slot_list, ctx->slot_count);
187202
ctx->slot_list = NULL;
188203
ctx->slot_count = 0;
189204
}
190205
if (ctx->pkcs11_ctx) {
191-
PKCS11_CTX_unload(ctx->pkcs11_ctx);
192-
PKCS11_CTX_free(ctx->pkcs11_ctx);
206+
if (!g_shutdown_mode) {
207+
PKCS11_CTX_unload(ctx->pkcs11_ctx);
208+
PKCS11_CTX_free(ctx->pkcs11_ctx);
209+
}
193210
ctx->pkcs11_ctx = NULL;
194211
}
195212
}

0 commit comments

Comments
 (0)