psa_crypto_free: destroy the last slot

The last slot in the array was not freed due to an off-by-one error.

Amend the fill_slots test to serve as a non-regression test for this
issue: without this bug fix, it would cause a memory leak.
This commit is contained in:
Gilles Peskine 2018-08-01 15:46:54 +02:00 committed by Jaeden Amero
parent 996deb18cc
commit 9a05634558
2 changed files with 2 additions and 4 deletions

View file

@ -3453,7 +3453,7 @@ psa_status_t psa_generate_key( psa_key_slot_t key,
void mbedtls_psa_crypto_free( void ) void mbedtls_psa_crypto_free( void )
{ {
psa_key_slot_t key; psa_key_slot_t key;
for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ ) for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
psa_destroy_key( key ); psa_destroy_key( key );
mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
mbedtls_entropy_free( &global_data.entropy ); mbedtls_entropy_free( &global_data.entropy );

View file

@ -496,12 +496,10 @@ void fill_slots( int max_arg )
&exported_size ) == PSA_SUCCESS ); &exported_size ) == PSA_SUCCESS );
TEST_ASSERT( exported_size == sizeof( slot ) ); TEST_ASSERT( exported_size == sizeof( slot ) );
TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 ); TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
} }
exit: exit:
for( slot = 1; slot <= max; slot++ ) /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
psa_destroy_key( slot );
mbedtls_psa_crypto_free( ); mbedtls_psa_crypto_free( );
} }
/* END_CASE */ /* END_CASE */