blob: fde7cea1d63a2688a72a64c5400223b4f72005a1 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker2466d932013-09-28 14:40:38 +02006 */
7
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +01008/*
Hanno Becker48a816f2018-09-05 15:22:22 +01009 * Ensure gmtime_r is available even with -std=c99; must be defined before
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020010 * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms.
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010011 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010012#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010013#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010014#endif
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010015
Gilles Peskinedb09ef62020-06-03 01:43:33 +020016#include "common.h"
Paul Bakker2466d932013-09-28 14:40:38 +020017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020019
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020021
Hanno Becker6a739782018-09-05 15:06:19 +010022#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +010023
Hanno Beckercfeb70c2018-09-05 13:50:22 +010024#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +010025 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
26 defined(__MACH__)))
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010027#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +010028#endif /* !_WIN32 && (unix || __unix || __unix__ ||
29 * (__APPLE__ && __MACH__)) */
30
Gilles Peskine449bd832023-01-11 14:50:10 +010031#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
32 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
33 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010034/*
35 * This is a convenience shorthand macro to avoid checking the long
36 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +010037 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010038 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +010039 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010040 */
Hanno Beckerf5106d52018-09-06 12:09:56 +010041
Gilles Peskine449bd832023-01-11 14:50:10 +010042#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
Andres Amaya Garciad7177432018-08-08 09:41:17 +010043#define THREADING_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +010044#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
45
Gilles Peskine449bd832023-01-11 14:50:10 +010046#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
47 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jonesd4603232020-11-12 17:10:36 +000048 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Hanno Becker323d8012018-09-06 11:30:57 +010049
Hanno Becker6a739782018-09-05 15:06:19 +010050#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_THREADING_PTHREAD)
Gilles Peskine449bd832023-01-11 14:50:10 +010053static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020054{
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if (mutex == NULL) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020056 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 }
Paul Bakker2466d932013-09-28 14:40:38 +020058
Paul Elliott5fa986c2023-11-10 14:05:09 +000059 /* 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
Paul Elliott8c6d3322023-11-23 18:53:13 +000062 * 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
David Horstmanndcf42a02024-11-08 14:40:12 +000064 * tests; see framework/tests/src/threading_helpers.c for more details. */
Paul Elliott5fa986c2023-11-10 14:05:09 +000065 (void) pthread_mutex_init(&mutex->mutex, NULL);
Paul Bakker2466d932013-09-28 14:40:38 +020066}
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020069{
Paul Elliott5fa986c2023-11-10 14:05:09 +000070 if (mutex == NULL) {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020071 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 }
Paul Bakker2466d932013-09-28 14:40:38 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 (void) pthread_mutex_destroy(&mutex->mutex);
Paul Bakker2466d932013-09-28 14:40:38 +020075}
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020078{
Paul Elliott5fa986c2023-11-10 14:05:09 +000079 if (mutex == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
81 }
Paul Bakker2466d932013-09-28 14:40:38 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if (pthread_mutex_lock(&mutex->mutex) != 0) {
84 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
85 }
Paul Bakker2466d932013-09-28 14:40:38 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +020088}
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
Paul Bakker2466d932013-09-28 14:40:38 +020091{
Paul Elliott5fa986c2023-11-10 14:05:09 +000092 if (mutex == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
94 }
Paul Bakker2466d932013-09-28 14:40:38 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (pthread_mutex_unlock(&mutex->mutex) != 0) {
97 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
98 }
Paul Bakker2466d932013-09-28 14:40:38 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200101}
102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
104void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
105int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
106int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200107
108/*
Artur Allmann3f396152022-03-21 16:11:35 +0200109 * With pthreads we can statically initialize mutexes
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200110 */
111#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_THREADING_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
Paul Bakkerc78c8422013-12-31 11:55:27 +0100117{
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 ((void) mutex);
119 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Paul Bakkerc78c8422013-12-31 11:55:27 +0100120}
Gilles Peskine449bd832023-01-11 14:50:10 +0100121static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200122{
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 ((void) mutex);
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200124 return;
125}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
128void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
129int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
130int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200131
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200132/*
133 * Set functions pointers and initialize global mutexes
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135void 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 *))
Paul Bakker2466d932013-09-28 14:40:38 +0200139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_mutex_init = mutex_init;
141 mbedtls_mutex_free = mutex_free;
142 mbedtls_mutex_lock = mutex_lock;
143 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200144
Gergely Budai13f7fb32017-08-23 14:23:58 +0200145#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200147#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100148#if defined(THREADING_USE_GMTIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100150#endif
Ryan Everett558da2f2024-01-19 12:59:28 +0000151#if defined(MBEDTLS_PSA_CRYPTO_C)
Ryan Everett63952b72024-01-19 13:45:19 +0000152 mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
Paul Elliott077fd872024-02-22 16:55:03 +0000153 mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
Paul Elliottb8e38e02024-03-11 12:09:49 +0000154 mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
Ryan Everett558da2f2024-01-19 12:59:28 +0000155#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200156}
157
158/*
159 * Free global mutexes
160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161void mbedtls_threading_free_alt(void)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200162{
Gergely Budai13f7fb32017-08-23 14:23:58 +0200163#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
Gergely Budai13f7fb32017-08-23 14:23:58 +0200165#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100166#if defined(THREADING_USE_GMTIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100168#endif
Ryan Everett558da2f2024-01-19 12:59:28 +0000169#if defined(MBEDTLS_PSA_CRYPTO_C)
170 mbedtls_mutex_free(&mbedtls_threading_key_slot_mutex);
Paul Elliott077fd872024-02-22 16:55:03 +0000171 mbedtls_mutex_free(&mbedtls_threading_psa_globaldata_mutex);
Paul Elliottb8e38e02024-03-11 12:09:49 +0000172 mbedtls_mutex_free(&mbedtls_threading_psa_rngdata_mutex);
Ryan Everett558da2f2024-01-19 12:59:28 +0000173#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200176
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200177/*
178 * Define global mutexes
179 */
180#ifndef MUTEX_INIT
181#define MUTEX_INIT
182#endif
Gergely Budai13f7fb32017-08-23 14:23:58 +0200183#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200184mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200185#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100186#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100187mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
188#endif
Ryan Everett558da2f2024-01-19 12:59:28 +0000189#if defined(MBEDTLS_PSA_CRYPTO_C)
190mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex MUTEX_INIT;
Paul Elliott077fd872024-02-22 16:55:03 +0000191mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex MUTEX_INIT;
Paul Elliottb8e38e02024-03-11 12:09:49 +0000192mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex MUTEX_INIT;
Ryan Everett558da2f2024-01-19 12:59:28 +0000193#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_THREADING_C */