From f08aa3e023a0ca1d11324944d161133609a171d1 Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Thu, 17 Jan 2019 13:30:57 +0100 Subject: [PATCH 1/5] fix memory leak in mpi_miller_rabin() Fixes memory leak in mpi_miller_rabin() that occurs when the function has failed to obtain a usable random 'A' 30 turns in a row. Signed-off-by: Jens Wiklander --- library/bignum.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/bignum.c b/library/bignum.c index d3d02b1a0..606bca456 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -2329,7 +2329,8 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds, } if (count++ > 30) { - return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; + ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; + goto cleanup; } } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 || From 035eaea78325da5888c4720bc97aba551fb55f0d Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Thu, 17 Jan 2019 17:45:05 +0100 Subject: [PATCH 2/5] Add ChangeLog entry Signed-off-by: Jens Wiklander --- ChangeLog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ChangeLog b/ChangeLog index b39b95391..842843899 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,8 @@ Bugfix previously lead to a stack overflow on constrained targets. * Add `MBEDTLS_SELF_TEST` for the mbedtls_self_test functions in the header files, which missed the precompilation check. #971 + * Fix memory leak in in mpi_miller_rabin(). Contributed by + Jens Wiklander in #2363 = mbed TLS 2.16.0 branch released 2018-12-21 From df8e511381f457bb9b2e2c85142585ea86114ffc Mon Sep 17 00:00:00 2001 From: Robert Larsen Date: Fri, 23 Aug 2019 10:55:47 +0200 Subject: [PATCH 3/5] Added mbedtls_net_close and use it in ssl_fork_server to correctly disassociate the client socket from the parent process and the server socket from the child process. --- include/mbedtls/net_sockets.h | 7 +++++++ library/net_sockets.c | 13 +++++++++++++ programs/ssl/ssl_fork_server.c | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/mbedtls/net_sockets.h b/include/mbedtls/net_sockets.h index df42b450c..adb589ee9 100644 --- a/include/mbedtls/net_sockets.h +++ b/include/mbedtls/net_sockets.h @@ -257,6 +257,13 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ); int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout ); +/** + * \brief Closes down the connection and free associated data + * + * \param ctx The context to close + */ +void mbedtls_net_close( mbedtls_net_context *ctx ); + /** * \brief Gracefully shutdown the connection and free associated data * diff --git a/library/net_sockets.c b/library/net_sockets.c index 5d538bfd5..c7b358d05 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -651,6 +651,19 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) return( ret ); } +/* + * Close the connection + */ +void mbedtls_net_close( mbedtls_net_context *ctx ) +{ + if( ctx->fd == -1 ) + return; + + close( ctx->fd ); + + ctx->fd = -1; +} + /* * Gracefully close the connection */ diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c index 80407e49a..851bc0536 100644 --- a/programs/ssl/ssl_fork_server.c +++ b/programs/ssl/ssl_fork_server.c @@ -254,6 +254,7 @@ int main( void ) if( pid != 0 ) { mbedtls_printf( " ok\n" ); + mbedtls_net_close( &client_fd ); if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg, (const unsigned char *) "parent", @@ -266,7 +267,7 @@ int main( void ) continue; } - mbedtls_net_init( &listen_fd ); + mbedtls_net_close( &listen_fd ); pid = getpid(); From 52bc1947ffd76a4c937f597c63f5eb4b00e13e69 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Fri, 23 Aug 2019 10:39:07 +0100 Subject: [PATCH 4/5] Add a ChangeLog entry for mbedtls_net_close() --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index d84769208..5510c7de1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -31,6 +31,10 @@ Features changed its IP or port. The feature is enabled at compile-time by setting MBEDTLS_SSL_DTLS_CONNECTION_ID (disabled by default), and at run-time through the new APIs mbedtls_ssl_conf_cid() and mbedtls_ssl_set_cid(). + * Add mbedtls_net_close(), enabling the building of forking servers where + the parent process closes the client socket and continue accepting, and + the child process closes the listening socket and handles the client + socket. Contributed by Robert Larsen in #2803. API Changes * Extend the MBEDTLS_SSL_EXPORT_KEYS to export the handshake randbytes, From 1f62714db86e6afc1791b6b31947c223905eeb38 Mon Sep 17 00:00:00 2001 From: Andy Gross Date: Wed, 30 Jan 2019 10:25:53 -0600 Subject: [PATCH 5/5] Fix uninitialized variable in x509_crt This patch fixes an issue we encountered with more stringent compiler warnings. The signature_is_good variable has a possibility of being used uninitialized. This patch moves the use of the variable to a place where it cannot be used while uninitialized. Signed-off-by: Andy Gross --- ChangeLog | 3 +++ library/x509_crt.c | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 77d9d81cd..b9f46166f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -49,6 +49,9 @@ 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. + * Improve code clarity in x509_crt module, removing false-positive + uninitialized variable warnings on some recent toolchains (GCC8, etc). + Discovered and fixed by Andy Gross (Linaro), #2392. Changes * Replace multiple uses of MD2 by SHA-256 in X.509 test suite. Fixes #821. diff --git a/library/x509_crt.c b/library/x509_crt.c index b2c19db68..48f244e2e 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -2611,15 +2611,13 @@ check_signature: continue; } + *r_parent = parent; + *r_signature_is_good = signature_is_good; + break; } - if( parent != NULL ) - { - *r_parent = parent; - *r_signature_is_good = signature_is_good; - } - else + if( parent == NULL ) { *r_parent = fallback_parent; *r_signature_is_good = fallback_signature_is_good;