From 864e998decba799fd8e63111f553efd869f2a00a Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 11 Oct 2018 11:15:14 +0100 Subject: [PATCH 1/3] Add test for MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO to all.sh This commit adds a test to tests/scripts/all.sh exercising an ASan build of the default configuration with MBEDTLS_PLATFORM_MEMORY enabled, MBEDTLS_PLATFORM_CALLOC_MACRO set to std calloc MBEDTLS_PLATFORM_FREE_MACRO set to std free (This should functionally be indistinguishable from a default build) --- tests/scripts/all.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 53c5e37a2..4e44a82af 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -591,6 +591,18 @@ scripts/config.pl unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc. scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux make CC=gcc CFLAGS='-Werror -O0 -std=c99 -pedantic' lib +msg "build: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)" +cleanup +cp "$CONFIG_H" "$CONFIG_BAK" +scripts/config.pl set MBEDTLS_PLATFORM_MEMORY +scripts/config.pl set MBEDTLS_PLATFORM_CALLOC_MACRO calloc +scripts/config.pl set MBEDTLS_PLATFORM_FREE_MACRO free +CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . +make + +msg "test: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)" +make test + if uname -a | grep -F Linux >/dev/null; then msg "build/test: make shared" # ~ 40s cleanup From bbd5131c195e9556a20935279d0c822c430d18af Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 11 Oct 2018 10:26:55 +0100 Subject: [PATCH 2/3] Omit runtime configuration of calloc/free if macro config enabled This commit removes the definition of the API function `mbedtls_platform_set_calloc_free()` from `library/platform.c` in case the macros `MBEDTLS_PLATFORM_CALLOC_MACRO` `MBEDTLS_PLATFORM_FREE_MACRO` for compile time configuration of calloc/free are set. This is in line with the corresponding header `mbedtls/platform.h` which declares `mbedtls_platform_set_calloc_free()` only if `MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO` are not defined. Fixes #1642. --- library/platform.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/library/platform.c b/library/platform.c index e90dc3c93..2cdfe70df 100644 --- a/library/platform.c +++ b/library/platform.c @@ -29,7 +29,14 @@ #include "mbedtls/platform.h" -#if defined(MBEDTLS_PLATFORM_MEMORY) +/* The compile time configuration of memory allocation via the macros + * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime + * configuration via mbedtls_platform_set_calloc_free(). So, omit everything + * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */ +#if defined(MBEDTLS_PLATFORM_MEMORY) && \ + !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \ + defined(MBEDTLS_PLATFORM_FREE_MACRO) ) + #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) static void *platform_calloc_uninit( size_t n, size_t size ) { @@ -60,7 +67,9 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), mbedtls_free = free_func; return( 0 ); } -#endif /* MBEDTLS_PLATFORM_MEMORY */ +#endif /* MBEDTLS_PLATFORM_MEMORY && + !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && + defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ #if defined(_WIN32) #include From 7e95aa6bd03547f4d3ed8878e40b23f7fe3713a4 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 11 Oct 2018 10:54:45 +0100 Subject: [PATCH 3/3] Adapt ChangeLog --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8c82d08d1..b4f1c52c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,10 @@ Bugfix MBEDTLS_THREADING_C is defined. Found by TrinityTonic, #1095 * Fix a bug in the update function for SSL ticket keys which previously invalidated keys of a lifetime of less than a 1s. Fixes #1968. + * Fix compilation failure for configurations which use compile time + replacements of standard calloc/free functions through the macros + MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_FREE_MACRO. + Reported by ole-de and ddhome2006. Fixes #882, #1642 and #1706. Changes * Add tests for session resumption in DTLS.