From f148312db4580e835996ebf8ad60dfd1092a67d5 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:21:30 +0100 Subject: [PATCH 01/14] Zeroize tmp buf on fail in load_file() dhm.c --- library/dhm.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/dhm.c b/library/dhm.c index 0a4f82028..6109e0a7a 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -532,7 +532,10 @@ static int load_file( const char *path, unsigned char **buf, size_t *n ) if( fread( *buf, 1, *n, f ) != *n ) { fclose( f ); + + polarssl_zeroize( *buf, *n + 1 ); polarssl_free( *buf ); + return( POLARSSL_ERR_DHM_FILE_IO_ERROR ); } From fa6fa6850e2d8c45a6780eeb866ba32f6b3f785b Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:32:27 +0100 Subject: [PATCH 02/14] Zeroize tmp bufs in entropy.c functions --- library/entropy.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/library/entropy.c b/library/entropy.c index 540a27c57..caff22f42 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -210,7 +210,7 @@ static int entropy_gather_internal( entropy_context *ctx ) if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, buf, ENTROPY_MAX_GATHER, &olen ) ) != 0 ) { - return( ret ); + goto cleanup; } /* @@ -223,7 +223,10 @@ static int entropy_gather_internal( entropy_context *ctx ) } } - return( 0 ); +cleanup: + polarssl_zeroize( buf, sizeof( buf ) ); + + return( ret ); } /* @@ -324,6 +327,8 @@ int entropy_func( void *data, unsigned char *output, size_t len ) ret = 0; exit: + polarssl_zeroize( buf, sizeof( buf ) ); + #if defined(POLARSSL_THREADING_C) if( polarssl_mutex_unlock( &ctx->mutex ) != 0 ) return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); @@ -354,12 +359,15 @@ int entropy_write_seed_file( entropy_context *ctx, const char *path ) ret = 0; exit: + polarssl_zeroize( buf, sizeof( buf ) ); + fclose( f ); return( ret ); } int entropy_update_seed_file( entropy_context *ctx, const char *path ) { + int ret = 0; FILE *f; size_t n; unsigned char buf[ ENTROPY_MAX_SEED_SIZE ]; @@ -375,14 +383,16 @@ int entropy_update_seed_file( entropy_context *ctx, const char *path ) n = ENTROPY_MAX_SEED_SIZE; if( fread( buf, 1, n, f ) != n ) - { - fclose( f ); - return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_ENTROPY_FILE_IO_ERROR; + else + ret = entropy_update_manual( ctx, buf, n ); fclose( f ); - entropy_update_manual( ctx, buf, n ); + polarssl_zeroize( buf, sizeof( buf ) ); + + if( ret != 0 ) + return( ret ); return( entropy_write_seed_file( ctx, path ) ); } From beb42837ac296459744780d5173be30d88f17316 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:36:30 +0100 Subject: [PATCH 03/14] Zeroize tmp bufs in hmac_drbg.c functions --- library/hmac_drbg.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/library/hmac_drbg.c b/library/hmac_drbg.c index c7904d069..eece38997 100644 --- a/library/hmac_drbg.c +++ b/library/hmac_drbg.c @@ -342,11 +342,14 @@ int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path ) exit: fclose( f ); + polarssl_zeroize( buf, sizeof( buf ) ); + return( ret ); } int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path ) { + int ret = 0; FILE *f; size_t n; unsigned char buf[ POLARSSL_HMAC_DRBG_MAX_INPUT ]; @@ -365,14 +368,16 @@ int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path ) } if( fread( buf, 1, n, f ) != n ) - { - fclose( f ); - return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR; + else + hmac_drbg_update( ctx, buf, n ); fclose( f ); - hmac_drbg_update( ctx, buf, n ); + polarssl_zeroize( buf, sizeof( buf ) ); + + if( ret != 0 ) + return( ret ); return( hmac_drbg_write_seed_file( ctx, path ) ); } From ff13995812ce1d62e4bd3404b6af821ef92bc05f Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:38:12 +0100 Subject: [PATCH 04/14] Zeroize return buf on failure in pkparse.c --- library/pkparse.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/pkparse.c b/library/pkparse.c index 39c51f648..83c93baa4 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -98,7 +98,10 @@ int pk_load_file( const char *path, unsigned char **buf, size_t *n ) if( fread( *buf, 1, *n, f ) != *n ) { fclose( f ); + + polarssl_zeroize( *buf, *n ); polarssl_free( *buf ); + return( POLARSSL_ERR_PK_FILE_IO_ERROR ); } From dd471788d8dcd17dfb5a4d7667658008f7e056f9 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:43:11 +0100 Subject: [PATCH 05/14] Zeroize tmp bufs in ctr_drbg.c functions --- library/ctr_drbg.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index 7b315e888..fe7fb27ae 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -402,12 +402,11 @@ int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path ) goto exit; if( fwrite( buf, 1, CTR_DRBG_MAX_INPUT, f ) != CTR_DRBG_MAX_INPUT ) - { ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR; - goto exit; - } + else + ret = 0; - ret = 0; + polarssl_zeroize( buf, sizeof( buf ) ); exit: fclose( f ); @@ -416,6 +415,7 @@ exit: int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ) { + int ret = 0; FILE *f; size_t n; unsigned char buf[ CTR_DRBG_MAX_INPUT ]; @@ -428,20 +428,18 @@ int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ) fseek( f, 0, SEEK_SET ); if( n > CTR_DRBG_MAX_INPUT ) - { - fclose( f ); - return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG ); - } - - if( fread( buf, 1, n, f ) != n ) - { - fclose( f ); - return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG; + else if( fread( buf, 1, n, f ) != n ) + ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR; + else + ctr_drbg_update( ctx, buf, n ); fclose( f ); - ctr_drbg_update( ctx, buf, n ); + polarssl_zeroize( buf, sizeof( buf ) ); + + if( ret != 0 ) + return( ret ); return( ctr_drbg_write_seed_file( ctx, path ) ); } From c381444c7f033cbc5968069ed28795ac6933c091 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:44:50 +0100 Subject: [PATCH 06/14] Zeroize tmp buf in mbedtls_mpi_fill_random() --- library/bignum.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/bignum.c b/library/bignum.c index 4829d916b..e4a8dece5 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1880,6 +1880,8 @@ int mpi_fill_random( mpi *X, size_t size, MPI_CHK( mpi_read_binary( X, buf, size ) ); cleanup: + polarssl_zeroize( buf, sizeof( buf ) ); + return( ret ); } From a0ae1db2f7635e8a92250c08d9b00338b7c953ff Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:51:22 +0100 Subject: [PATCH 07/14] Zeroize buffers in various modules --- library/ssl_tls.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index bae8433fe..645fa32c7 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4140,12 +4140,19 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len, return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); } - if( ssl->psk != NULL || ssl->psk_identity != NULL ) + if( ssl->psk != NULL ) { + polarssl_zeroize( ssl->psk, ssl->psk_len ); + polarssl_free( ssl->psk ); - polarssl_free( ssl->psk_identity ); ssl->psk = NULL; + ssl->psk_len = 0; + } + if( ssl->psk_identity != NULL ) + { + polarssl_free( ssl->psk_identity ); ssl->psk_identity = NULL; + ssl->psk_identity_len = 0; } if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL || From f4660aaf4c24b5779bb6a5304ab1dfacb729f4ed Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:54:06 +0100 Subject: [PATCH 08/14] Zeroize heap buf on failure in pem.c --- library/pem.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/pem.c b/library/pem.c index b2c16c292..611c788fd 100644 --- a/library/pem.c +++ b/library/pem.c @@ -343,6 +343,7 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, ( defined(POLARSSL_DES_C) || defined(POLARSSL_AES_C) ) if( pwd == NULL ) { + polarssl_zeroize( buf, len ); polarssl_free( buf ); return( POLARSSL_ERR_PEM_PASSWORD_REQUIRED ); } @@ -371,10 +372,12 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, */ if( len <= 2 || buf[0] != 0x30 || buf[1] > 0x83 ) { + polarssl_zeroize( buf, len ); polarssl_free( buf ); return( POLARSSL_ERR_PEM_PASSWORD_MISMATCH ); } #else + polarssl_zeroize( buf, len ); polarssl_free( buf ); return( POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE ); #endif /* POLARSSL_MD5_C && POLARSSL_CIPHER_MODE_CBC && From c0dc5b5d3b2d91d868a3ff7822e46192acc90ed8 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:56:39 +0100 Subject: [PATCH 09/14] Zeroize tmp buf in ctr_drbg_write_seed_file() --- library/ctr_drbg.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index fe7fb27ae..f66064ff4 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -406,9 +406,9 @@ int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path ) else ret = 0; +exit: polarssl_zeroize( buf, sizeof( buf ) ); -exit: fclose( f ); return( ret ); } @@ -428,8 +428,12 @@ int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ) fseek( f, 0, SEEK_SET ); if( n > CTR_DRBG_MAX_INPUT ) - ret = POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG; - else if( fread( buf, 1, n, f ) != n ) + { + fclose( f ); + return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG ); + } + + if( fread( buf, 1, n, f ) != n ) ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR; else ctr_drbg_update( ctx, buf, n ); From af134da17eac7d2b129288f271db63efb5d78f75 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 10:59:05 +0100 Subject: [PATCH 10/14] Add ChangeLog entry for buf zeroize --- ChangeLog | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ChangeLog b/ChangeLog index 1672bdf07..1029dc970 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ mbed TLS ChangeLog (Sorted per branch, date) += mbed TLS 1.3.x released xxxx-xx-xx + +Security + * Ensure that buffers are cleared after use if they contain sensitive data. + Changes were introduced in multiple places in the library. + = mbed TLS 1.3.20 released 2017-06-21 Security From 1bfa46a45617d7ddb1403900a0f8f53158aac66a Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 11:00:02 +0100 Subject: [PATCH 11/14] Zeroize tmp buffer in entropy_update() --- library/entropy.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/entropy.c b/library/entropy.c index caff22f42..5a8321b8d 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -166,6 +166,8 @@ static int entropy_update( entropy_context *ctx, unsigned char source_id, sha256_update( &ctx->accumulator, p, use_len ); #endif + polarssl_zeroize( tmp, sizeof( tmp ) ); + return( 0 ); } From 2d829fb4b367427749727aeabb1bbfbd0f244567 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 12 Jul 2017 11:01:32 +0100 Subject: [PATCH 12/14] Zeroize buf if mbedtls_base64_decode() fails --- library/pem.c | 1 + 1 file changed, 1 insertion(+) diff --git a/library/pem.c b/library/pem.c index 611c788fd..789a92d51 100644 --- a/library/pem.c +++ b/library/pem.c @@ -333,6 +333,7 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer, if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 ) { + polarssl_zeroize( buf, len ); polarssl_free( buf ); return( POLARSSL_ERR_PEM_INVALID_DATA + ret ); } From 3d98b9744272652609931b451bcde71ec0d1392a Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Wed, 20 Sep 2017 11:47:49 +0100 Subject: [PATCH 13/14] Modify zeroize internal buffers in md modules Modify all the following functions to zeroize an internal buffer before exiting the function. The buffer could potentially contain confidential data read from a file. * md2_file() * md4_file() * md5_file() * ripemd160_file() * sha1_file() * sha256_file() * sha512_file() --- library/md2.c | 16 ++++++++-------- library/md4.c | 16 ++++++++-------- library/md5.c | 16 ++++++++-------- library/ripemd160.c | 16 ++++++++-------- library/sha1.c | 16 ++++++++-------- library/sha256.c | 16 ++++++++-------- library/sha512.c | 16 ++++++++-------- 7 files changed, 56 insertions(+), 56 deletions(-) diff --git a/library/md2.c b/library/md2.c index 2ac7eba61..2d6123f12 100644 --- a/library/md2.c +++ b/library/md2.c @@ -217,6 +217,7 @@ void md2( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md2_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md2_context ctx; @@ -231,17 +232,16 @@ int md2_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md2_update( &ctx, buf, n ); - md2_finish( &ctx, output ); - md2_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD2_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD2_FILE_IO_ERROR; + else + md2_finish( &ctx, output ); + md2_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/md4.c b/library/md4.c index 8754d2f52..9c4a9b80a 100644 --- a/library/md4.c +++ b/library/md4.c @@ -313,6 +313,7 @@ void md4( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md4_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md4_context ctx; @@ -327,17 +328,16 @@ int md4_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md4_update( &ctx, buf, n ); - md4_finish( &ctx, output ); - md4_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD4_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD4_FILE_IO_ERROR; + else + md4_finish( &ctx, output ); + md4_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/md5.c b/library/md5.c index 8c7ed1fc3..4a0f25191 100644 --- a/library/md5.c +++ b/library/md5.c @@ -330,6 +330,7 @@ void md5( const unsigned char *input, size_t ilen, unsigned char output[16] ) */ int md5_file( const char *path, unsigned char output[16] ) { + int ret = 0; FILE *f; size_t n; md5_context ctx; @@ -344,17 +345,16 @@ int md5_file( const char *path, unsigned char output[16] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) md5_update( &ctx, buf, n ); - md5_finish( &ctx, output ); - md5_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_MD5_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_MD5_FILE_IO_ERROR; + else + md5_finish( &ctx, output ); + md5_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/ripemd160.c b/library/ripemd160.c index 2c196f42b..7b5d02e2e 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -388,6 +388,7 @@ void ripemd160( const unsigned char *input, size_t ilen, */ int ripemd160_file( const char *path, unsigned char output[20] ) { + int ret = 0; FILE *f; size_t n; ripemd160_context ctx; @@ -402,17 +403,16 @@ int ripemd160_file( const char *path, unsigned char output[20] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) ripemd160_update( &ctx, buf, n ); - ripemd160_finish( &ctx, output ); - ripemd160_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR; + else + ripemd160_finish( &ctx, output ); + ripemd160_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha1.c b/library/sha1.c index 44de8727e..a5a235b64 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -363,6 +363,7 @@ void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) */ int sha1_file( const char *path, unsigned char output[20] ) { + int ret = 0; FILE *f; size_t n; sha1_context ctx; @@ -377,17 +378,16 @@ int sha1_file( const char *path, unsigned char output[20] ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha1_update( &ctx, buf, n ); - sha1_finish( &ctx, output ); - sha1_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA1_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA1_FILE_IO_ERROR; + else + sha1_finish( &ctx, output ); + sha1_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha256.c b/library/sha256.c index 674fdf25e..caae79f9b 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -366,6 +366,7 @@ void sha256( const unsigned char *input, size_t ilen, */ int sha256_file( const char *path, unsigned char output[32], int is224 ) { + int ret = 0; FILE *f; size_t n; sha256_context ctx; @@ -380,17 +381,16 @@ int sha256_file( const char *path, unsigned char output[32], int is224 ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha256_update( &ctx, buf, n ); - sha256_finish( &ctx, output ); - sha256_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA256_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA256_FILE_IO_ERROR; + else + sha256_finish( &ctx, output ); + sha256_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ diff --git a/library/sha512.c b/library/sha512.c index bd607e0ca..5e51f7f0b 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -370,6 +370,7 @@ void sha512( const unsigned char *input, size_t ilen, */ int sha512_file( const char *path, unsigned char output[64], int is384 ) { + int ret = 0; FILE *f; size_t n; sha512_context ctx; @@ -384,17 +385,16 @@ int sha512_file( const char *path, unsigned char output[64], int is384 ) while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) sha512_update( &ctx, buf, n ); - sha512_finish( &ctx, output ); - sha512_free( &ctx ); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA512_FILE_IO_ERROR ); - } + ret = POLARSSL_ERR_SHA512_FILE_IO_ERROR; + else + sha512_finish( &ctx, output ); + sha512_free( &ctx ); + polarssl_zeroize( buf, sizeof( buf ) ); fclose( f ); - return( 0 ); + + return( ret ); } #endif /* POLARSSL_FS_IO */ From b662cc1f52893f9797060193add61cd6d0a8b8a7 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 24 Nov 2017 18:55:19 +0100 Subject: [PATCH 14/14] Avoid uninitialized variable warning in entropy_gather_internal The variable ret was always initialized in entropy_gather_internal, but `gcc -Werror=maybe-uninitialized` rightfully complained that it was unable to determine this statically. Therefore, tweak the problematic case (ctx->source_count == 0) to not use ret in that case. --- library/entropy.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/entropy.c b/library/entropy.c index 5a8321b8d..518d98215 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -196,13 +196,11 @@ int entropy_update_manual( entropy_context *ctx, */ static int entropy_gather_internal( entropy_context *ctx ) { - int ret, i; + int ret = POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED; + int i; unsigned char buf[ENTROPY_MAX_GATHER]; size_t olen; - if( ctx->source_count == 0 ) - return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED ); - /* * Run through our entropy sources */