Skip to content

Conversation

@valeriosetti
Copy link
Contributor

Description

Backport of Mbed-TLS/TF-PSA-Crypto#492

What has been backported?

PR checklist

…ent key

Do not try to load a persistent key whose key ID is in the volatile range.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti added needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests size-s Estimated task size: small (~2d) priority-high High priority - will be reviewed soon labels Oct 27, 2025
@valeriosetti
Copy link
Contributor Author

@bjwtaylor @gilles-peskine-arm I took the liberty to add you as reviewers of this PR since you are also checking the one in tf-psa-crypto. Let me know if this is Ok for you ;)

gilles-peskine-arm and others added 4 commits October 29, 2025 23:07
Add some assertions on the various ranges of key identifiers to ensure that
they're disjoint and they comply with documented guarantees.

Signed-off-by: Gilles Peskine <[email protected]>
Test what happens when the application tries to access a key and the storage
contains something invalid: either a corrupted file, or a key ID that's
outside the standard range for persistent keys.

Coverage of APIs in this commit:

* `psa_get_key_attributes()` (generally as a proxy for any key access);
* `psa_export_key()` (minor, but does provide some coverage of what happens
  if only the key material is corrupted);
* `psa_destroy_key()`, which hopefully should work even for a corrupted file.

Coverage of key IDs in this commit:

* Key IDs in various ranges: user (i.e. the normal range for persistent
  keys), builtin, volatile, reserved file ID, none of the above.
* Includes coverage for nonzero owner ID.

No coverage of corrupted files in this commit.

Assert the behavior that I think is the right thing. Subsequent commits will
reconcile the library behavior with the code as needed.

Signed-off-by: Gilles Peskine <[email protected]>
When testing what happens with when accessing a key ID in the built-in or
volatile range and a file exists in storage, we were skipping the test case
when the key existed. When the volatile or built-in key exists, the
expectations on the test case are wrong, but the test case is still useful:
we should ensure that the existence of the file doesn't somehow prevent
access to the built-in or volatile key. So, instead of skipping, change the
test assertions on the fly to ensure that we are accessing the existing key.

Signed-off-by: Gilles Peskine <[email protected]>
@valeriosetti valeriosetti changed the title [backport] psa_load_builtin_key_into_slot: prevent accessing the PSA storage if key ID is in volatile range [3.6] psa_load_builtin_key_into_slot: prevent accessing the PSA storage if key ID is in volatile range Oct 29, 2025
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Oct 30, 2025
Previous tests were backported from tf-psa-crypto and they work fine there.
However the library implementation is not the same between 3.6 and
tf-psa-crypto: in 3.6 we only prevent loading of persistent keys if their
ID is within the volatile range, but the built-in one is still allowed.
Therefore this commit fix expected return values for the 3.6 branch
when built-in keys are accessed.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti added needs-review Every commit must be reviewed by at least two team members, and removed needs-ci Needs to pass CI tests needs-work labels Nov 3, 2025
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except the reasoning for the case of reserved file IDs isn't right, even if the result happens to work.

Comment on lines 573 to 581
/* Only remove the key file if it was created during this test.
* This helps for the case where MBEDTLS_PSA_INJECT_ENTROPY is enabled and
* PSA_CRYPTO_ITS_RANDOM_SEED_UID is being tested. This would case the file
* to be cancelled and the following test case to fail at the PSA_INIT()
* call.
*/
if (remove_key_on_exit) {
psa_its_remove(uid);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is functionally correct, but the reason to skip the removal is not “if it was created during this test”, it's “if the key file is a reserved file”. This would be better expressed as

if (!KEY_ID_IN_RESERVED_FILE_ID_RANGE(key_id_arg) {
    /* The key ID corresponds to a reserved file (e.g. transaction
     * log or entropy seed). Don't corrupt that file. */
    psa_its_remove(uid);
}

Given that it's hard to understand and remember every subtlety about the semantics of file UIDs, please change the logic to this.

That wasn't a problem in Mbed-TLS/TF-PSA-Crypto#492 because we currently don't implement anything that uses a reserved file as of 1.0.0, but it would come to bite if we start doing so. So please fix this in the crypto PR too.

Do not remove keys that belong to the reserved range.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti requested a review from mpg November 3, 2025 10:09
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review Every commit must be reviewed by at least two team members, priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)

Projects

Development

Successfully merging this pull request may close these issues.

2 participants