aboutsummaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
authorGilles Peskine <gilles.peskine@arm.com>2024-05-02 15:48:21 +0000
committerGitHub <noreply@github.com>2024-05-02 15:48:21 +0000
commitfa8fc2705a47f21006d73addcf2922cb153e8c0b (patch)
treedba19fadc7592181f181894f90ec2a5dbfba1b2e /library
parentcedb011c50dd9d25a07dd413b7a5c237fa7e4896 (diff)
parentc51e94837032ea71b13c5b5d225b5b18880d6455 (diff)
downloadmbedtls-fa8fc2705a47f21006d73addcf2922cb153e8c0b.zip
mbedtls-fa8fc2705a47f21006d73addcf2922cb153e8c0b.tar.gz
mbedtls-fa8fc2705a47f21006d73addcf2922cb153e8c0b.tar.bz2
Merge pull request #9069 from Ryan-Everett-arm/fix-get-and-lock-key-slot-threading-bug
Wipe the returned slot pointer upon failure in `psa_get_and_lock_key_slot`
Diffstat (limited to 'library')
-rw-r--r--library/psa_crypto_slot_management.c5
-rw-r--r--library/psa_crypto_slot_management.h3
2 files changed, 8 insertions, 0 deletions
diff --git a/library/psa_crypto_slot_management.c b/library/psa_crypto_slot_management.c
index b184ed0..9986a44 100644
--- a/library/psa_crypto_slot_management.c
+++ b/library/psa_crypto_slot_management.c
@@ -424,6 +424,8 @@ psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
if (status != PSA_SUCCESS) {
psa_wipe_key_slot(*p_slot);
+ /* If the key does not exist, we need to return
+ * PSA_ERROR_INVALID_HANDLE. */
if (status == PSA_ERROR_DOES_NOT_EXIST) {
status = PSA_ERROR_INVALID_HANDLE;
}
@@ -440,6 +442,9 @@ psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
status = PSA_ERROR_INVALID_HANDLE;
#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
+ if (status != PSA_SUCCESS) {
+ *p_slot = NULL;
+ }
#if defined(MBEDTLS_THREADING_C)
PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
&mbedtls_threading_key_slot_mutex));
diff --git a/library/psa_crypto_slot_management.h b/library/psa_crypto_slot_management.h
index bcfc9d8..a84be7d 100644
--- a/library/psa_crypto_slot_management.h
+++ b/library/psa_crypto_slot_management.h
@@ -58,6 +58,9 @@ static inline int psa_key_id_is_volatile(psa_key_id_t key_id)
* It is the responsibility of the caller to call psa_unregister_read(slot)
* when they have finished reading the contents of the slot.
*
+ * On failure, `*p_slot` is set to NULL. This ensures that it is always valid
+ * to call psa_unregister_read on the returned slot.
+ *
* \param key Key identifier to query.
* \param[out] p_slot On success, `*p_slot` contains a pointer to the
* key slot containing the description of the key