| 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Ensure gmtime_r is available even with -std=c99; must be defined before |
| 10 | * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms. |
| 11 | */ |
| 12 | #if !defined(_POSIX_C_SOURCE) |
| 13 | #define _POSIX_C_SOURCE 200112L |
| 14 | #endif |
| 15 | |
| 16 | #include "common.h" |
| 17 | |
| 18 | #if defined(MBEDTLS_THREADING_C) |
| 19 | |
| 20 | #include "threading_internal.h" |
| 21 | |
| 22 | #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) |
| 23 | |
| 24 | #if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) |
| 25 | #include <unistd.h> |
| 26 | #endif /* !_WIN32 && (unix || __unix || __unix__ || |
| 27 | * (__APPLE__ && __MACH__)) */ |
| 28 | |
| 29 | #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \ |
| 30 | (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \ |
| 31 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L)) |
| 32 | /* |
| 33 | * This is a convenience shorthand macro to avoid checking the long |
| 34 | * preprocessor conditions above. Ideally, we could expose this macro in |
| 35 | * platform_util.h and simply use it in platform_util.c, threading.c and |
| 36 | * threading.h. However, this macro is not part of the Mbed TLS public API, so |
| 37 | * we keep it private by only defining it in this file |
| 38 | */ |
| 39 | |
| 40 | #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) |
| 41 | #define THREADING_USE_GMTIME |
| 42 | #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ |
| 43 | |
| 44 | #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ |
| 45 | ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ |
| 46 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */ |
| 47 | |
| 48 | #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ |
| 49 | |
| 50 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 51 | static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex) |
| 52 | { |
| 53 | if (mutex == NULL) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | /* One problem here is that calling lock on a pthread mutex without first |
| 58 | * having initialised it is undefined behaviour. Obviously we cannot check |
| 59 | * this here in a thread safe manner without a significant performance |
| 60 | * hit, so state transitions are checked in tests only via the state |
| 61 | * variable. Please make sure any new mutex that gets added is exercised in |
| 62 | * tests; see framework/tests/src/threading_helpers.c for more details. */ |
| 63 | (void) pthread_mutex_init(&mutex->mutex, NULL); |
| 64 | } |
| 65 | |
| 66 | static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex) |
| 67 | { |
| 68 | if (mutex == NULL) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | (void) pthread_mutex_destroy(&mutex->mutex); |
| 73 | } |
| 74 | |
| 75 | static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex) |
| 76 | { |
| 77 | if (mutex == NULL) { |
| 78 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 79 | } |
| 80 | |
| 81 | if (pthread_mutex_lock(&mutex->mutex) != 0) { |
| 82 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex) |
| 89 | { |
| 90 | if (mutex == NULL) { |
| 91 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 92 | } |
| 93 | |
| 94 | if (pthread_mutex_unlock(&mutex->mutex) != 0) { |
| 95 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread; |
| 102 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread; |
| 103 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread; |
| 104 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread; |
| 105 | |
| 106 | /* |
| 107 | * With pthreads we can statically initialize mutexes |
| 108 | */ |
| 109 | #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } |
| 110 | |
| 111 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 112 | |
| 113 | #if defined(MBEDTLS_THREADING_ALT) |
| 114 | static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex) |
| 115 | { |
| 116 | ((void) mutex); |
| 117 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 118 | } |
| 119 | static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex) |
| 120 | { |
| 121 | ((void) mutex); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 126 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 127 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
| 128 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
| 129 | |
| 130 | /* |
| 131 | * Set functions pointers and initialize global mutexes |
| 132 | */ |
| 133 | void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *), |
| 134 | void (*mutex_free)(mbedtls_threading_mutex_t *), |
| 135 | int (*mutex_lock)(mbedtls_threading_mutex_t *), |
| 136 | int (*mutex_unlock)(mbedtls_threading_mutex_t *)) |
| 137 | { |
| 138 | mbedtls_mutex_init = mutex_init; |
| 139 | mbedtls_mutex_free = mutex_free; |
| 140 | mbedtls_mutex_lock = mutex_lock; |
| 141 | mbedtls_mutex_unlock = mutex_unlock; |
| 142 | |
| 143 | #if defined(MBEDTLS_FS_IO) |
| 144 | mbedtls_mutex_init(&mbedtls_threading_readdir_mutex); |
| 145 | #endif |
| 146 | #if defined(THREADING_USE_GMTIME) |
| 147 | mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex); |
| 148 | #endif |
| 149 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 150 | mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex); |
| 151 | mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex); |
| 152 | mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Free global mutexes |
| 158 | */ |
| 159 | void mbedtls_threading_free_alt(void) |
| 160 | { |
| 161 | #if defined(MBEDTLS_FS_IO) |
| 162 | mbedtls_mutex_free(&mbedtls_threading_readdir_mutex); |
| 163 | #endif |
| 164 | #if defined(THREADING_USE_GMTIME) |
| 165 | mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex); |
| 166 | #endif |
| 167 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 168 | mbedtls_mutex_free(&mbedtls_threading_key_slot_mutex); |
| 169 | mbedtls_mutex_free(&mbedtls_threading_psa_globaldata_mutex); |
| 170 | mbedtls_mutex_free(&mbedtls_threading_psa_rngdata_mutex); |
| 171 | #endif |
| 172 | } |
| 173 | #endif /* MBEDTLS_THREADING_ALT */ |
| 174 | |
| 175 | /* |
| 176 | * Define global mutexes |
| 177 | */ |
| 178 | #ifndef MUTEX_INIT |
| 179 | #define MUTEX_INIT |
| 180 | #endif |
| 181 | #if defined(MBEDTLS_FS_IO) |
| 182 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; |
| 183 | #endif |
| 184 | #if defined(THREADING_USE_GMTIME) |
| 185 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; |
| 186 | #endif |
| 187 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 188 | mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex MUTEX_INIT; |
| 189 | mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex MUTEX_INIT; |
| 190 | mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex MUTEX_INIT; |
| 191 | #endif |
| 192 | |
| 193 | #endif /* MBEDTLS_THREADING_C */ |
| 194 | |