From bedd856cad44122f03bba76a90f43dc1acb08f7c Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 4 Sep 2025 19:33:05 +0200 Subject: [PATCH 1/2] Support new threading interface where mutex_init returns int Signed-off-by: Gilles Peskine --- tests/src/threading_helpers.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/src/threading_helpers.c b/tests/src/threading_helpers.c index bbac43a75..182f389ab 100644 --- a/tests/src/threading_helpers.c +++ b/tests/src/threading_helpers.c @@ -172,8 +172,14 @@ static mbedtls_threading_mutex_t *mutex_container( #endif /* MBEDTLS_THREADING_INTERNAL_VERSION */ +#if MBEDTLS_THREADING_INTERNAL_VERSION >= 0x04000001 +typedef int mutex_init_return_t; +#else +typedef void mutex_init_return_t; +#endif + typedef struct { - void (*init)(mbedtls_platform_mutex_t *); + mutex_init_return_t (*init)(mbedtls_platform_mutex_t *); void (*free)(mbedtls_platform_mutex_t *); int (*lock)(mbedtls_platform_mutex_t *); int (*unlock)(mbedtls_platform_mutex_t *); @@ -235,9 +241,16 @@ static int mbedtls_test_mutex_can_test(mbedtls_platform_mutex_t *mutex) return 1; } -static void mbedtls_test_wrap_mutex_init(mbedtls_platform_mutex_t *mutex) +static mutex_init_return_t mbedtls_test_wrap_mutex_init(mbedtls_platform_mutex_t *mutex) { +#if MBEDTLS_THREADING_INTERNAL_VERSION >= 0x04000001 + int ret = mutex_functions.init(mutex); + if (ret != 0) { + return ret; + } +#else mutex_functions.init(mutex); +#endif if (mbedtls_test_mutex_can_test(mutex)) { if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) { @@ -247,6 +260,10 @@ static void mbedtls_test_wrap_mutex_init(mbedtls_platform_mutex_t *mutex) mutex_functions.unlock(&mbedtls_test_mutex_mutex); } } + +#if MBEDTLS_THREADING_INTERNAL_VERSION >= 0x04000001 + return 0; +#endif } static void mbedtls_test_wrap_mutex_free(mbedtls_platform_mutex_t *mutex) From 5b46871e5a59fc4399ab445c6e0d9acec6540bb6 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 6 Sep 2025 14:09:35 +0200 Subject: [PATCH 2/2] Double free on a mutex is now a no-op Since TF-PSA-Crypto 1.0 (threading internal interface version 4.0.0.1), `mbedtls_mutex_free()` on an all-bits-zero mutex is defined to be a no-op. Signed-off-by: Gilles Peskine --- tests/programs/metatest.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/programs/metatest.c b/tests/programs/metatest.c index 750ef20aa..b862889a1 100644 --- a/tests/programs/metatest.c +++ b/tests/programs/metatest.c @@ -39,7 +39,7 @@ #include #if defined(MBEDTLS_THREADING_C) -#include +#include "threading_internal.h" #endif @@ -285,6 +285,10 @@ exit: #endif } +/* Since TF-PSA-Crypto 1.0, mbedtls_mutex_free() on an all-bits-zero + * mutex is defined to be a no-op. In earlier library versions, + * it had undefined behavior. */ +#if MBEDTLS_THREADING_INTERNAL_VERSION <= 0x04000000 static void mutex_free_not_initialized(const char *name) { (void) name; @@ -298,6 +302,7 @@ static void mutex_free_not_initialized(const char *name) mbedtls_mutex_free(&mutex); #endif } +#endif static void mutex_double_init(const char *name) { @@ -314,6 +319,10 @@ static void mutex_double_init(const char *name) #endif } +/* Since TF-PSA-Crypto 1.0, mbedtls_mutex_free() on an all-bits-zero + * mutex is defined to be a no-op. In earlier library versions, + * it had undefined behavior. */ +#if MBEDTLS_THREADING_INTERNAL_VERSION <= 0x04000000 static void mutex_double_free(const char *name) { (void) name; @@ -328,6 +337,7 @@ static void mutex_double_free(const char *name) mbedtls_mutex_free(&mutex); #endif } +#endif static void mutex_leak(const char *name) { @@ -417,9 +427,13 @@ metatest_t metatests[] = { { "test_memory_poison_7_1_2_w", "poison", test_memory_poison }, { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, +#if MBEDTLS_THREADING_INTERNAL_VERSION <= 0x04000000 { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, +#endif { "mutex_double_init", "pthread", mutex_double_init }, +#if MBEDTLS_THREADING_INTERNAL_VERSION <= 0x04000000 { "mutex_double_free", "pthread", mutex_double_free }, +#endif { "mutex_leak", "pthread", mutex_leak }, { NULL, NULL, NULL } };