From 92c98931f28b2523752ce0155b6a730f890b440e Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 1 May 2019 17:09:11 +0100 Subject: [PATCH 1/5] Fix mpi_bigendian_to_host() on bigendian systems The previous implementation of mpi_bigendian_to_host() did a byte-swapping regardless of the endianness of the system. Fixes #2622. --- library/bignum.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/bignum.c b/library/bignum.c index 41946183c..a1822fc6c 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -742,10 +742,15 @@ cleanup: static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x ) { uint8_t i; + unsigned char *x_ptr; mbedtls_mpi_uint tmp = 0; - /* This works regardless of the endianness. */ - for( i = 0; i < ciL; i++, x >>= 8 ) - tmp |= ( x & 0xFF ) << ( ( ciL - 1 - i ) << 3 ); + + for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ ) + { + tmp <<= CHAR_BIT; + tmp |= (mbedtls_mpi_uint) *x_ptr; + } + return( tmp ); } From b4e9950d026ab14d4f6d152f0ae473be3fb74fce Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Thu, 2 May 2019 09:33:56 +0100 Subject: [PATCH 2/5] Adapt ChangeLog --- ChangeLog | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ChangeLog b/ChangeLog index 798e84a00..07ba4c767 100644 --- a/ChangeLog +++ b/ChangeLog @@ -38,6 +38,9 @@ Bugfix for the parameter. * Add a check for MBEDTLS_X509_CRL_PARSE_C in ssl_server2, guarding the crl sni entry parameter. Reported by inestlerode in #560. + * Fix bug in endianness conversion in bignum module. This lead to + functionally incorrect code on bigendian systems which don't have + __BYTE_ORDER__ defined. Reported by Brendan Shanks. Fixes #2622. Changes * Return from various debugging routines immediately if the From 43dc0d6a8f1d62063b10025a41b2015088991df0 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 2 Sep 2019 10:42:57 -0400 Subject: [PATCH 3/5] ssl-opt.sh: wait for proxy to start before running the script further --- tests/ssl-opt.sh | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 238ed97d0..fa334c38b 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -420,9 +420,9 @@ has_mem_err() { fi } -# Wait for process $2 to be listening on port $1 +# Wait for process $2 named $3 to be listening on port $1. Print error to $4. if type lsof >/dev/null 2>/dev/null; then - wait_server_start() { + wait_app_start() { START_TIME=$(date +%s) if [ "$DTLS" -eq 1 ]; then proto=UDP @@ -432,8 +432,8 @@ if type lsof >/dev/null 2>/dev/null; then # Make a tight loop, server normally takes less than 1s to start. while ! lsof -a -n -b -i "$proto:$1" -p "$2" >/dev/null 2>/dev/null; do if [ $(( $(date +%s) - $START_TIME )) -gt $DOG_DELAY ]; then - echo "SERVERSTART TIMEOUT" - echo "SERVERSTART TIMEOUT" >> $SRV_OUT + echo "$3 START TIMEOUT" + echo "$3 START TIMEOUT" >> $4 break fi # Linux and *BSD support decimal arguments to sleep. On other @@ -442,12 +442,22 @@ if type lsof >/dev/null 2>/dev/null; then done } else - echo "Warning: lsof not available, wait_server_start = sleep" - wait_server_start() { + echo "Warning: lsof not available, wait_app_start = sleep" + wait_app_start() { sleep "$START_DELAY" } fi +# Wait for server process $2 to be listening on port $1. +wait_server_start() { + wait_app_start $1 $2 "SERVER" $SRV_OUT +} + +# Wait for proxy process $2 to be listening on port $1. +wait_proxy_start() { + wait_app_start $1 $2 "PROXY" $PXY_OUT +} + # Given the client or server debug output, parse the unix timestamp that is # included in the first 4 bytes of the random bytes and check that it's within # acceptable bounds @@ -600,7 +610,7 @@ run_test() { echo "$PXY_CMD" > $PXY_OUT $PXY_CMD >> $PXY_OUT 2>&1 & PXY_PID=$! - # assume proxy starts faster than server + wait_proxy_start "$PXY_PORT" "$PXY_PID" fi check_osrv_dtls From 0c34499805ecbeff45177727ae44418eb6c2dba5 Mon Sep 17 00:00:00 2001 From: Vikas Katariya Date: Thu, 15 Aug 2019 14:24:20 +0100 Subject: [PATCH 4/5] Check for zero length and NULL buffer pointer In reference to issue https://github.com/ARMmbed/mbed-crypto/issues/49 --- library/platform_util.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/platform_util.c b/library/platform_util.c index 756e22679..b1f745097 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -72,7 +72,10 @@ static void * (* const volatile memset_func)( void *, int, size_t ) = memset; void mbedtls_platform_zeroize( void *buf, size_t len ) { - memset_func( buf, 0, len ); + MBEDTLS_INTERNAL_VALIDATE( len == 0 || buf != NULL ); + + if( len > 0 ) + memset_func( buf, 0, len ); } #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */ From cc9135f42e597bfc75bce3b2531580471a230731 Mon Sep 17 00:00:00 2001 From: Vikas Katariya Date: Thu, 15 Aug 2019 16:32:21 +0100 Subject: [PATCH 5/5] Changelog entry --- ChangeLog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ChangeLog b/ChangeLog index a78257803..49e1d6ed5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,8 @@ Bugfix * Fix propagation of restart contexts in restartable EC operations. This could previously lead to segmentation faults in builds using an address-sanitizer and enabling but not using MBEDTLS_ECP_RESTARTABLE. + * Zero length buffer check for undefined behavior in + mbedtls_platform_zeroize(). Fixes ARMmbed/mbed-crypto#49. Changes * Make it easier to define MBEDTLS_PARAM_FAILED as assert (which config.h