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     /* A nonzero value of is_valid indicates a successfully initialized
60      * mutex. This is a workaround for not being able to return an error
61      * code for this function. The lock/unlock functions return an error
62      * if is_valid is nonzero. The Mbed TLS unit test code uses this field
63      * to distinguish more states of the mutex; see
64      * tests/src/threading_helpers for details. */
65     mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
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 || !mutex->is_valid) {
71         return;
72     }
73 
74     (void) pthread_mutex_destroy(&mutex->mutex);
75     mutex->is_valid = 0;
76 }
77 
threading_mutex_lock_pthread(mbedtls_threading_mutex_t * mutex)78 static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
79 {
80     if (mutex == NULL || !mutex->is_valid) {
81         return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
82     }
83 
84     if (pthread_mutex_lock(&mutex->mutex) != 0) {
85         return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
86     }
87 
88     return 0;
89 }
90 
threading_mutex_unlock_pthread(mbedtls_threading_mutex_t * mutex)91 static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
92 {
93     if (mutex == NULL || !mutex->is_valid) {
94         return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
95     }
96 
97     if (pthread_mutex_unlock(&mutex->mutex) != 0) {
98         return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
99     }
100 
101     return 0;
102 }
103 
104 void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
105 void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
106 int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
107 int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
108 
109 /*
110  * With pthreads we can statically initialize mutexes
111  */
112 #define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
113 
114 #endif /* MBEDTLS_THREADING_PTHREAD */
115 
116 #if defined(MBEDTLS_THREADING_ALT)
threading_mutex_fail(mbedtls_threading_mutex_t * mutex)117 static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
118 {
119     ((void) mutex);
120     return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
121 }
threading_mutex_dummy(mbedtls_threading_mutex_t * mutex)122 static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
123 {
124     ((void) mutex);
125     return;
126 }
127 
128 void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
129 void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
130 int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
131 int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
132 
133 /*
134  * Set functions pointers and initialize global mutexes
135  */
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 *))136 void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
137                                void (*mutex_free)(mbedtls_threading_mutex_t *),
138                                int (*mutex_lock)(mbedtls_threading_mutex_t *),
139                                int (*mutex_unlock)(mbedtls_threading_mutex_t *))
140 {
141     mbedtls_mutex_init = mutex_init;
142     mbedtls_mutex_free = mutex_free;
143     mbedtls_mutex_lock = mutex_lock;
144     mbedtls_mutex_unlock = mutex_unlock;
145 
146 #if defined(MBEDTLS_FS_IO)
147     mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
148 #endif
149 #if defined(THREADING_USE_GMTIME)
150     mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
151 #endif
152 }
153 
154 /*
155  * Free global mutexes
156  */
mbedtls_threading_free_alt(void)157 void mbedtls_threading_free_alt(void)
158 {
159 #if defined(MBEDTLS_FS_IO)
160     mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
161 #endif
162 #if defined(THREADING_USE_GMTIME)
163     mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
164 #endif
165 }
166 #endif /* MBEDTLS_THREADING_ALT */
167 
168 /*
169  * Define global mutexes
170  */
171 #ifndef MUTEX_INIT
172 #define MUTEX_INIT
173 #endif
174 #if defined(MBEDTLS_FS_IO)
175 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
176 #endif
177 #if defined(THREADING_USE_GMTIME)
178 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
179 #endif
180 
181 #endif /* MBEDTLS_THREADING_C */
182