From 9a05634558ccd2f8cbb8cf62a22555a7085f4d42 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 1 Aug 2018 15:46:54 +0200 Subject: [PATCH] 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. --- library/psa_crypto.c | 2 +- tests/suites/test_suite_psa_crypto.function | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index fe3072935..8b25dac1a 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -3453,7 +3453,7 @@ psa_status_t psa_generate_key( psa_key_slot_t key, void mbedtls_psa_crypto_free( void ) { 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 ); mbedtls_ctr_drbg_free( &global_data.ctr_drbg ); mbedtls_entropy_free( &global_data.entropy ); diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 88ef27fbb..43e479470 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -496,12 +496,10 @@ void fill_slots( int max_arg ) &exported_size ) == PSA_SUCCESS ); TEST_ASSERT( exported_size == sizeof( slot ) ); TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 ); - TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); } exit: - for( slot = 1; slot <= max; slot++ ) - psa_destroy_key( slot ); + /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */ mbedtls_psa_crypto_free( ); } /* END_CASE */