1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-30 22:43:08 +03:00

Merge pull request #1143 from davidhorstmann-arm/memory-poisoning-runtime-enable-2.28

[Backport 2.28] Enable and disable memory poisoning at runtime
This commit is contained in:
David Horstmann
2024-01-24 14:46:52 +00:00
committed by GitHub
4 changed files with 43 additions and 23 deletions

View File

@ -366,22 +366,22 @@ metatest_t metatests[] = {
{ "double_free", "asan", double_free }, { "double_free", "asan", double_free },
{ "read_uninitialized_stack", "msan", read_uninitialized_stack }, { "read_uninitialized_stack", "msan", read_uninitialized_stack },
{ "memory_leak", "asan", memory_leak }, { "memory_leak", "asan", memory_leak },
{ "test_memory_poison_0_0_8_r", "asan", test_memory_poison }, { "test_memory_poison_0_0_8_r", "poison", test_memory_poison },
{ "test_memory_poison_0_0_8_w", "asan", test_memory_poison }, { "test_memory_poison_0_0_8_w", "poison", test_memory_poison },
{ "test_memory_poison_0_7_8_r", "asan", test_memory_poison }, { "test_memory_poison_0_7_8_r", "poison", test_memory_poison },
{ "test_memory_poison_0_7_8_w", "asan", test_memory_poison }, { "test_memory_poison_0_7_8_w", "poison", test_memory_poison },
{ "test_memory_poison_0_0_1_r", "asan", test_memory_poison }, { "test_memory_poison_0_0_1_r", "poison", test_memory_poison },
{ "test_memory_poison_0_0_1_w", "asan", test_memory_poison }, { "test_memory_poison_0_0_1_w", "poison", test_memory_poison },
{ "test_memory_poison_0_1_2_r", "asan", test_memory_poison }, { "test_memory_poison_0_1_2_r", "poison", test_memory_poison },
{ "test_memory_poison_0_1_2_w", "asan", test_memory_poison }, { "test_memory_poison_0_1_2_w", "poison", test_memory_poison },
{ "test_memory_poison_7_0_8_r", "asan", test_memory_poison }, { "test_memory_poison_7_0_8_r", "poison", test_memory_poison },
{ "test_memory_poison_7_0_8_w", "asan", test_memory_poison }, { "test_memory_poison_7_0_8_w", "poison", test_memory_poison },
{ "test_memory_poison_7_7_8_r", "asan", test_memory_poison }, { "test_memory_poison_7_7_8_r", "poison", test_memory_poison },
{ "test_memory_poison_7_7_8_w", "asan", test_memory_poison }, { "test_memory_poison_7_7_8_w", "poison", test_memory_poison },
{ "test_memory_poison_7_0_1_r", "asan", test_memory_poison }, { "test_memory_poison_7_0_1_r", "poison", test_memory_poison },
{ "test_memory_poison_7_0_1_w", "asan", test_memory_poison }, { "test_memory_poison_7_0_1_w", "poison", test_memory_poison },
{ "test_memory_poison_7_1_2_r", "asan", test_memory_poison }, { "test_memory_poison_7_1_2_r", "poison", test_memory_poison },
{ "test_memory_poison_7_1_2_w", "asan", test_memory_poison }, { "test_memory_poison_7_1_2_w", "poison", test_memory_poison },
{ "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized }, { "mutex_lock_not_initialized", "pthread", mutex_lock_not_initialized },
{ "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized }, { "mutex_unlock_not_initialized", "pthread", mutex_unlock_not_initialized },
{ "mutex_free_not_initialized", "pthread", mutex_free_not_initialized }, { "mutex_free_not_initialized", "pthread", mutex_free_not_initialized },

View File

@ -61,6 +61,12 @@
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON) #if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
/** Variable used to enable memory poisoning. This is set and unset in the
* test wrappers so that calls to PSA functions from the library do not
* poison memory.
*/
extern unsigned int mbedtls_test_memory_poisoning_count;
/** Poison a memory area so that any attempt to read or write from it will /** Poison a memory area so that any attempt to read or write from it will
* cause a runtime failure. * cause a runtime failure.
* *
@ -68,7 +74,10 @@
*/ */
void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size); void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \ #define MBEDTLS_TEST_MEMORY_POISON(ptr, size) \
mbedtls_test_memory_poison(ptr, size) do { \
mbedtls_test_memory_poisoning_count++; \
mbedtls_test_memory_poison(ptr, size); \
} while (0)
/** Undo the effect of mbedtls_test_memory_poison(). /** Undo the effect of mbedtls_test_memory_poison().
* *
@ -79,7 +88,12 @@ void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size);
*/ */
void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size); void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size);
#define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \ #define MBEDTLS_TEST_MEMORY_UNPOISON(ptr, size) \
mbedtls_test_memory_unpoison(ptr, size) do { \
mbedtls_test_memory_unpoison(ptr, size); \
if (mbedtls_test_memory_poisoning_count != 0) { \
mbedtls_test_memory_poisoning_count--; \
} \
} while (0)
#else /* MBEDTLS_TEST_MEMORY_CAN_POISON */ #else /* MBEDTLS_TEST_MEMORY_CAN_POISON */
#define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) (ptr), (void) (size)) #define MBEDTLS_TEST_MEMORY_POISON(ptr, size) ((void) (ptr), (void) (size))

View File

@ -876,7 +876,7 @@ component_test_default_cmake_gcc_asan () {
programs/test/selftest programs/test/selftest
msg "test: metatests (GCC, ASan build)" msg "test: metatests (GCC, ASan build)"
tests/scripts/run-metatests.sh any asan tests/scripts/run-metatests.sh any asan poison
msg "test: ssl-opt.sh (ASan build)" # ~ 1 min msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
tests/ssl-opt.sh tests/ssl-opt.sh
@ -1497,7 +1497,7 @@ component_test_everest () {
make test make test
msg "test: metatests (clang, ASan)" msg "test: metatests (clang, ASan)"
tests/scripts/run-metatests.sh any asan tests/scripts/run-metatests.sh any asan poison
msg "test: Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s msg "test: Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s
tests/ssl-opt.sh -f ECDH tests/ssl-opt.sh -f ECDH

View File

@ -13,12 +13,15 @@
#include <test/macros.h> #include <test/macros.h>
#include <test/memory.h> #include <test/memory.h>
#if defined(MBEDTLS_TEST_HAVE_ASAN) #if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
#include <sanitizer/asan_interface.h> #include <sanitizer/asan_interface.h>
#include <stdint.h> #include <stdint.h>
#endif #endif
#if defined(MBEDTLS_TEST_HAVE_ASAN) #if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
unsigned int mbedtls_test_memory_poisoning_count = 0;
static void align_for_asan(const unsigned char **p_ptr, size_t *p_size) static void align_for_asan(const unsigned char **p_ptr, size_t *p_size)
{ {
uintptr_t start = (uintptr_t) *p_ptr; uintptr_t start = (uintptr_t) *p_ptr;
@ -36,6 +39,9 @@ static void align_for_asan(const unsigned char **p_ptr, size_t *p_size)
void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size) void mbedtls_test_memory_poison(const unsigned char *ptr, size_t size)
{ {
if (mbedtls_test_memory_poisoning_count == 0) {
return;
}
if (size == 0) { if (size == 0) {
return; return;
} }
@ -51,4 +57,4 @@ void mbedtls_test_memory_unpoison(const unsigned char *ptr, size_t size)
align_for_asan(&ptr, &size); align_for_asan(&ptr, &size);
__asan_unpoison_memory_region(ptr, size); __asan_unpoison_memory_region(ptr, size);
} }
#endif /* Asan */ #endif /* Memory poisoning */