Skip to content

Commit 56f80c3

Browse files
committed
Fix module safety after fork
fix #500
1 parent 2e14cdb commit 56f80c3

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/p11_load.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,32 @@ void pkcs11_CTX_init_args(PKCS11_CTX *ctx, const char *init_args)
6969
cpriv->init_args = init_args ? OPENSSL_strdup(init_args) : NULL;
7070
}
7171

72+
/*
73+
* Tell the PKCS11 to initialize itself
74+
*/
75+
static int pkcs11_initialize(PKCS11_CTX_private *cpriv)
76+
{
77+
CK_C_INITIALIZE_ARGS args;
78+
int rv;
79+
80+
memset(&args, 0, sizeof(args));
81+
/* Unconditionally say using OS locking primitives is OK */
82+
args.flags |= CKF_OS_LOCKING_OK;
83+
args.pReserved = cpriv->init_args;
84+
rv = cpriv->method->C_Initialize(&args);
85+
if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
86+
CKRerr(P11_F_PKCS11_CTX_LOAD, rv);
87+
return -1;
88+
}
89+
return 0;
90+
}
91+
7292
/*
7393
* Load the shared library, and initialize it.
7494
*/
7595
int pkcs11_CTX_load(PKCS11_CTX *ctx, const char *name)
7696
{
7797
PKCS11_CTX_private *cpriv = PRIVCTX(ctx);
78-
CK_C_INITIALIZE_ARGS args;
7998
CK_INFO ck_info;
8099
int rv;
81100

@@ -85,16 +104,9 @@ int pkcs11_CTX_load(PKCS11_CTX *ctx, const char *name)
85104
return -1;
86105
}
87106

88-
/* Tell the PKCS11 to initialize itself */
89-
memset(&args, 0, sizeof(args));
90-
/* Unconditionally say using OS locking primitives is OK */
91-
args.flags |= CKF_OS_LOCKING_OK;
92-
args.pReserved = cpriv->init_args;
93-
rv = cpriv->method->C_Initialize(&args);
94-
if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
107+
if (pkcs11_initialize(cpriv)) {
95108
C_UnloadModule(cpriv->handle);
96109
cpriv->handle = NULL;
97-
CKRerr(P11_F_PKCS11_CTX_LOAD, rv);
98110
return -1;
99111
}
100112

@@ -119,28 +131,12 @@ int pkcs11_CTX_load(PKCS11_CTX *ctx, const char *name)
119131
/*
120132
* Reinitialize (e.g., after a fork).
121133
*/
122-
int pkcs11_CTX_reload(PKCS11_CTX_private *ctx)
134+
int pkcs11_CTX_reload(PKCS11_CTX_private *cpriv)
123135
{
124-
CK_C_INITIALIZE_ARGS _args;
125-
CK_C_INITIALIZE_ARGS *args = NULL;
126-
int rv;
127-
128-
if (!ctx->method) /* Module not loaded */
136+
if (!cpriv->method) /* Module not loaded */
129137
return 0;
130138

131-
/* Tell the PKCS11 to initialize itself */
132-
if (ctx->init_args) {
133-
memset(&_args, 0, sizeof(_args));
134-
args = &_args;
135-
args->pReserved = ctx->init_args;
136-
}
137-
rv = ctx->method->C_Initialize(args);
138-
if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
139-
CKRerr(P11_F_PKCS11_CTX_RELOAD, rv);
140-
return -1;
141-
}
142-
143-
return 0;
139+
return pkcs11_initialize(cpriv);
144140
}
145141

146142
/*

0 commit comments

Comments
 (0)