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