Skip to content

Commit a61c216

Browse files
committed
Fix use-after-free at exit cleanup
1 parent 6d77aca commit a61c216

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

src/libp11-int.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ struct pkcs11_object_ops {
121121
extern PKCS11_OBJECT_ops pkcs11_rsa_ops;
122122
extern PKCS11_OBJECT_ops pkcs11_ec_ops;
123123

124+
extern int pkcs11_global_data_refs;
125+
124126
/*
125127
* Internal functions
126128
*/

src/p11_ec.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ EC_KEY_METHOD *PKCS11_get_ec_key_method(void)
811811

812812
void pkcs11_ec_key_method_free(void)
813813
{
814-
if (pkcs11_ec_key_method) {
814+
if (pkcs11_global_data_refs == 0 && pkcs11_ec_key_method) {
815815
free_ec_ex_index();
816816
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
817817
if (meth->pkcs11_ecdh_method & EC_KEY_METHOD_DYNAMIC)
@@ -855,7 +855,7 @@ ECDSA_METHOD *PKCS11_get_ecdsa_method(void)
855855

856856
void pkcs11_ecdsa_method_free(void)
857857
{
858-
if (pkcs11_ecdsa_method) {
858+
if (pkcs11_global_data_refs == 0 && pkcs11_ecdsa_method) {
859859
free_ec_ex_index();
860860
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
861861
if (pkcs11_ecdsa_method->flags & EC_KEY_METHOD_DYNAMIC)
@@ -881,7 +881,7 @@ ECDH_METHOD *PKCS11_get_ecdh_method(void)
881881

882882
void pkcs11_ecdh_method_free(void)
883883
{
884-
if (pkcs11_ecdh_method) {
884+
if (pkcs11_global_data_refs == 0 && pkcs11_ecdh_method) {
885885
free_ec_ex_index();
886886
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
887887
if (pkcs11_ecdh_method->flags & EC_KEY_METHOD_DYNAMIC)

src/p11_load.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
#include <string.h>
2121

2222
/* Global number of active PKCS11_CTX objects */
23-
static int pkcs11_global_data_refs = 0;
24-
25-
static void pkcs11_global_data_free(void);
23+
int pkcs11_global_data_refs = 0;
2624

2725
/*
2826
* Create a new context
@@ -177,12 +175,7 @@ void pkcs11_CTX_free(PKCS11_CTX *ctx)
177175
OPENSSL_free(ctx->_private);
178176
OPENSSL_free(ctx);
179177

180-
if (--pkcs11_global_data_refs == 0)
181-
pkcs11_global_data_free();
182-
}
183-
184-
static void pkcs11_global_data_free(void)
185-
{
178+
pkcs11_global_data_refs--;
186179
#ifndef OPENSSL_NO_RSA
187180
pkcs11_rsa_method_free();
188181
#endif

src/p11_rsa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ RSA_METHOD *PKCS11_get_rsa_method(void)
545545

546546
void pkcs11_rsa_method_free(void)
547547
{
548-
if (pkcs11_rsa_method) {
548+
if (pkcs11_global_data_refs == 0 && pkcs11_rsa_method) {
549549
free_rsa_ex_index();
550550
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
551551
RSA_meth_free(pkcs11_rsa_method);

0 commit comments

Comments
 (0)