mirror of
https://github.com/cuberite/polarssl.git
synced 2025-09-12 16:45:16 -04:00
Use thread-local flag to enable memory poisoning
Allow memory poisoning to be enabled and disabled at runtime using a thread-local flag. This allows poisoning to be disabled whenever a PSA function is called but not through the test wrappers, removing false positive use-after-poisons. Signed-off-by: David Horstmann <david.horstmann@arm.com>
This commit is contained in:
parent
8e72c8f154
commit
756b4dcfa4
@ -21,9 +21,12 @@
|
|||||||
* memory as poisoned, which can be used to enforce some memory access
|
* memory as poisoned, which can be used to enforce some memory access
|
||||||
* policies.
|
* policies.
|
||||||
*
|
*
|
||||||
|
* Support for the C11 thread_local keyword is also required.
|
||||||
|
*
|
||||||
* Currently, only Asan (Address Sanitizer) is supported.
|
* Currently, only Asan (Address Sanitizer) is supported.
|
||||||
*/
|
*/
|
||||||
#if defined(MBEDTLS_TEST_HAVE_ASAN)
|
#if defined(MBEDTLS_TEST_HAVE_ASAN) && \
|
||||||
|
(__STDC_VERSION__ >= 201112L)
|
||||||
# define MBEDTLS_TEST_MEMORY_CAN_POISON
|
# define MBEDTLS_TEST_MEMORY_CAN_POISON
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -61,6 +64,12 @@
|
|||||||
|
|
||||||
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
#if defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
|
||||||
|
|
||||||
|
/** Thread-local 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 _Thread_local int mbedtls_test_memory_poisoning_enabled;
|
||||||
|
|
||||||
/** 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 +77,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_enabled = 1; \
|
||||||
|
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 +91,10 @@ 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); \
|
||||||
|
mbedtls_test_memory_poisoning_enabled = 0; \
|
||||||
|
} 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))
|
||||||
|
@ -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)
|
||||||
|
|
||||||
|
_Thread_local int mbedtls_test_memory_poisoning_enabled = 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_enabled) {
|
||||||
|
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 */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user