Skip to content

Commit 3bbed48

Browse files
authored
Merge pull request #410 from dgarske/tpm_mutex
Improve mutex locking protection for concurrent thread usage
2 parents 91ea368 + f2e6be4 commit 3bbed48

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

src/tpm2.c

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@
3737
/* --- Local Variables -- */
3838
/******************************************************************************/
3939

40+
41+
#ifdef WOLFTPM_NO_ACTIVE_THREAD_LS
42+
/* if using gHwLock and want to use a shared active TPM2_CTX between threads */
43+
static TPM2_CTX* gActiveTPM;
44+
#else
4045
static THREAD_LS_T TPM2_CTX* gActiveTPM;
46+
#endif
47+
4148
#ifndef WOLFTPM2_NO_WOLFCRYPT
4249
static volatile int gWolfCryptRefCount = 0;
4350
#endif
4451

52+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(WOLFTPM_NO_LOCK) && \
53+
!defined(SINGLE_THREADED)
54+
static wolfSSL_Mutex gHwLock WOLFSSL_MUTEX_INITIALIZER_CLAUSE(gHwLock);
55+
#endif
56+
4557
#ifdef WOLFTPM_LINUX_DEV
4658
#define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand
4759
#define TPM2_INTERNAL_CLEANUP(ctx)
@@ -61,43 +73,24 @@ static volatile int gWolfCryptRefCount = 0;
6173
/******************************************************************************/
6274
static TPM_RC TPM2_AcquireLock(TPM2_CTX* ctx)
6375
{
64-
#if defined(WOLFTPM2_NO_WOLFCRYPT) || defined(WOLFTPM_NO_LOCK)
65-
(void)ctx;
66-
#else
67-
int ret;
68-
69-
if (!ctx->hwLockInit) {
70-
if (wc_InitMutex(&ctx->hwLock) != 0) {
71-
#ifdef DEBUG_WOLFTPM
72-
printf("TPM Mutex Init failed\n");
73-
#endif
74-
return TPM_RC_FAILURE;
75-
}
76-
ctx->hwLockInit = 1;
77-
ctx->lockCount = 0;
78-
}
79-
80-
if (ctx->lockCount == 0) {
81-
ret = wc_LockMutex(&ctx->hwLock);
82-
if (ret != 0)
83-
return TPM_RC_FAILURE;
76+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(WOLFTPM_NO_LOCK) && \
77+
!defined(SINGLE_THREADED)
78+
int ret = wc_LockMutex(&gHwLock);
79+
if (ret != 0) {
80+
return TPM_RC_FAILURE;
8481
}
85-
ctx->lockCount++;
8682
#endif
83+
(void)ctx;
8784
return TPM_RC_SUCCESS;
8885
}
8986

9087
static void TPM2_ReleaseLock(TPM2_CTX* ctx)
9188
{
92-
#if defined(WOLFTPM2_NO_WOLFCRYPT) || defined(WOLFTPM_NO_LOCK)
93-
(void)ctx;
94-
#else
95-
ctx->lockCount--;
96-
if (ctx->lockCount == 0) {
97-
wc_UnLockMutex(&ctx->hwLock);
98-
}
99-
89+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && !defined(WOLFTPM_NO_LOCK) && \
90+
!defined(SINGLE_THREADED)
91+
wc_UnLockMutex(&gHwLock);
10092
#endif
93+
(void)ctx;
10194
}
10295

10396
static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
@@ -507,6 +500,10 @@ static inline int TPM2_WolfCrypt_Init(void)
507500
if (rc == 0)
508501
rc = wc_SetSeed_Cb(wc_GenerateSeed);
509502
#endif
503+
#if !defined(WOLFTPM_NO_LOCK) && !defined(SINGLE_THREADED) && \
504+
!defined(WOLFSSL_MUTEX_INITIALIZER)
505+
wc_InitMutex(&gHwLock);
506+
#endif
510507
}
511508
gWolfCryptRefCount++;
512509

@@ -697,19 +694,17 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
697694
wc_FreeRng(&ctx->rng);
698695
}
699696
#endif
700-
#ifndef WOLFTPM_NO_LOCK
701-
if (ctx->hwLockInit) {
702-
ctx->hwLockInit = 0;
703-
wc_FreeMutex(&ctx->hwLock);
704-
}
705-
#endif
706697

707698
/* track wolf initialize reference count in wolfTPM. wolfCrypt does not
708-
properly track reference count in v4.1 or older releases */
699+
* properly track reference count in v4.1 or older releases */
709700
gWolfCryptRefCount--;
710701
if (gWolfCryptRefCount < 0)
711702
gWolfCryptRefCount = 0;
712703
if (gWolfCryptRefCount == 0) {
704+
#if !defined(WOLFTPM_NO_LOCK) && !defined(SINGLE_THREADED) && \
705+
!defined(WOLFSSL_MUTEX_INITIALIZER)
706+
wc_FreeMutex(&gHwLock);
707+
#endif
713708
wolfCrypt_Cleanup();
714709
}
715710
#endif /* !WOLFTPM2_NO_WOLFCRYPT */

wolftpm/tpm2.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,10 +1855,6 @@ typedef struct TPM2_CTX {
18551855
struct wolfTPM_winContext winCtx;
18561856
#endif
18571857
#ifndef WOLFTPM2_NO_WOLFCRYPT
1858-
#ifndef WOLFTPM_NO_LOCK
1859-
wolfSSL_Mutex hwLock;
1860-
int lockCount;
1861-
#endif
18621858
#ifdef WOLFTPM2_USE_WOLF_RNG
18631859
WC_RNG rng;
18641860
#endif
@@ -1878,9 +1874,6 @@ typedef struct TPM2_CTX {
18781874
byte rid;
18791875
/* Informational Bits - use unsigned int for best compiler compatibility */
18801876
#ifndef WOLFTPM2_NO_WOLFCRYPT
1881-
#ifndef WOLFTPM_NO_LOCK
1882-
unsigned int hwLockInit:1;
1883-
#endif
18841877
#ifndef WC_NO_RNG
18851878
unsigned int rngInit:1;
18861879
#endif

wolftpm/tpm2_types.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ typedef int64_t INT64;
234234
#endif
235235
#endif
236236

237+
/* if using older wolfSSL that does not have the pthread mutex initializer */
238+
#ifndef WOLFSSL_MUTEX_INITIALIZER
239+
#if defined(WOLFSSL_PTHREADS)
240+
#define WOLFSSL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
241+
#endif
242+
#endif
243+
#ifndef WOLFSSL_MUTEX_INITIALIZER_CLAUSE
244+
#ifdef WOLFSSL_MUTEX_INITIALIZER
245+
#define WOLFSSL_MUTEX_INITIALIZER_CLAUSE(lockname) = WOLFSSL_MUTEX_INITIALIZER
246+
#else
247+
#define WOLFSSL_MUTEX_INITIALIZER_CLAUSE(lockname) /* null expansion */
248+
#endif
249+
#endif
250+
237251
#ifndef WOLFTPM_CUSTOM_TYPES
238252
#include <stdlib.h>
239253

0 commit comments

Comments
 (0)