Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSA Update return code for non-existing key in various key operations #4198

Merged

Conversation

maulik-arm
Copy link
Contributor

@maulik-arm maulik-arm commented Mar 4, 2021

Description

Update return code for non-existing key in various key operations
Fixes issue #4162

Status

READY

Requires Backporting

No, PSA only.

Migrations

If there is any API change, what's the incentive and logic for it.

YES | NO

Additional comments

Any additional information that could be of interest

@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch from 3ee50a2 to 70b4301 Compare March 4, 2021 13:53
library/psa_crypto_slot_management.c Outdated Show resolved Hide resolved
@ronald-cron-arm ronald-cron-arm self-requested a review March 5, 2021 09:30
@bensze01 bensze01 added bug needs-ci Needs to pass CI tests PSA compliance labels Mar 5, 2021
@bensze01 bensze01 self-assigned this Mar 5, 2021
@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch from bb12893 to aac7145 Compare March 5, 2021 10:40
@bensze01 bensze01 added the needs-review Every commit must be reviewed by at least two team members, label Mar 5, 2021
@bensze01 bensze01 self-requested a review March 5, 2021 10:43
@bensze01 bensze01 removed the needs-ci Needs to pass CI tests label Mar 5, 2021
Copy link
Contributor

@bensze01 bensze01 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Copy link
Contributor

@bensze01 bensze01 left a comment

Choose a reason for hiding this comment

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

Copied over review comments from #4189

library/psa_crypto.c Outdated Show resolved Hide resolved
*
* \retval #PSA_SUCCESS
* \retval #PSA_ERROR_INVALID_HANDLE
Copy link
Contributor

Choose a reason for hiding this comment

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

Originally posted by @Summer-ARM in #4189 (comment)

sorry, do I miss something? didn't see this return value in the function implementation.

Copy link
Contributor

@bensze01 bensze01 Mar 5, 2021

Choose a reason for hiding this comment

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

@Summer-ARM It looks like psa_validate_key_id() is returning PSA_ERROR_INVALID_HANDLE instead of PSA_ERROR_INVALID_ARGUMENT, as it's documentation would suggest.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is indeed an issue here but not related to the issue this PR is solving. Thus not addressed in this PR. It is now addressed in #4279.

@bensze01 bensze01 changed the title Fixes issue #4162, correct return type PSA Update return code for non-existing key in various key operations Mar 5, 2021
@bensze01 bensze01 added the size-s Estimated task size: small (~2d) label Mar 5, 2021
*
* \retval #PSA_SUCCESS
* \retval #PSA_ERROR_INVALID_HANDLE
* \p key_id is not a valid key identifier.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is psa_validate_key_id() to validate key identifiers thus why do you introduce such validation in this function that validates the persistence of a key ?

@bensze01 bensze01 added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Mar 8, 2021
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

In its current version, I think that there are still APIs that are returning PSA_ERROR_DOES_NOT_EXIST. The approach of this PR is to fix the return value just before they return. I think it would be better to fix the source of the PSA_ERROR_DOES_NOT_EXIST error code for APIs which seems to be only psa_get_and_lock_key_slot(). This implies some changes in PSA test suites and in the coding of psa_open/close_key() I think. I don't know how much time you can spend on this. Please let me know if you are ok to follow the suggested path of if you prefer me to do it.

@maulik-arm
Copy link
Contributor Author

maulik-arm commented Mar 9, 2021 via email

@ronald-cron-arm
Copy link
Contributor

I started to investigate what is need to change in the PSA crypto implementation, in order for PSA APIs not to return PSA_ERROR_DOES_NOT_EXIST error code but for the deprecated psa_open_key() and thus address completely #4162. Please have a look to this. I've started to update the unit tests accordingly and I think it seems to go in the right direction, not completed though.

Could you check that the changes in PSA crypto as done here fix the issues related to the PSA_ERROR_DOES_NOT_EXIST when running the compliance test suite ?

@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch 2 times, most recently from ace4792 to 817f07b Compare March 15, 2021 15:19
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. The final result seems correct to me but I think the commit history should be reworked:

  1. Remove the three first commits.
  2. When fixing the return codes in the tests please do it in three commits: one with the fixes for psa_open_key, one with the fixes for psa_close_key and the last one for the other APIs.
    Do that locally and then force push. In case you are not familiar with the force push: git push --force maulik-arm/fix-4162.

@gilles-peskine-arm
Copy link
Contributor

Please use git push --force-with-lease when you do a force-push, never git push --force. It seems that push --force sometimes causes a race condition with GitHub that causes it to close the PR and makes it difficult to reopen.

@ronald-cron-arm
Copy link
Contributor

I never had any issue with --force but if --force-with-lease is safer I will use that.

Update expected return values of psa_open_key() to
PSA_ERROR_DOES_NOT_EXIST for invalid key handle operations.

Signed-off-by: Maulik  Patel <[email protected]>
Update expected return values of psa_close_key() to
PSA_ERROR_INVALID_HANDLE for invalid key handle operations.

Signed-off-by: Maulik  Patel <[email protected]>
Update expected return values of psa_get_key_attributes(),
psa_export_key() and other key api(s) to PSA_ERROR_INVALID_HANDLE
for invalid key.

Signed-off-by: Maulik  Patel <[email protected]>
@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch 2 times, most recently from 53cfe6c to abfffd2 Compare March 17, 2021 17:01
@maulik-arm
Copy link
Contributor Author

Hello Ronald, Giles.
Thank you for comments. I have reworked the commit history and added the change log entry file. Regarding the documentation, I see that psa_open_key already mentions return value PSA_ERROR_DOES_NOT_EXIST for invalid ID. Is there any other place that needs update?
Thank you

@gilles-peskine-arm
Copy link
Contributor

psa_open_key was the only function that documented DOES_NOT_EXIST, so it doesn't need to be removed anywhere. Changing the invalid-handle case of psa_open_key is the only necessary documentation change I can see.

@ronald-cron-arm
Copy link
Contributor

Brilliant. Thanks for the rework. The flow of changes is easy to follow and understand that way. The documentation of psa_open_key already lists PSA_ERROR_DOES_NOT_EXIST and not PSA_ERROR_INVALID_HANDLE (as defined in v1 beta 3 of the spec). I think it is rather the case that the documentation and the code were not aligned before but are aligned now. Thus nothing to change here I think. It seems the only things left to take care of are:
. Update the change log
. Make sure tests/scripts/check_files.py is passing (not passing yet on the CI but seems related to the change log thus if you change it it may be ok with just modifying the change log).

@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch from abfffd2 to 29ca5d5 Compare March 18, 2021 16:52
Copy link
Contributor

@ronald-cron-arm ronald-cron-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

@maulik-arm
Copy link
Contributor Author

Thank you for review Ronald. There is Jenkins Test failure in checks: PR-4198-merging-TLS-Testing (https://jenkins-mbedtls.oss.arm.com/blue/organizations/jenkins/mbed-tls-pr-merge/detail/PR-4198-merge/18/pipeline) showing here.. I tried enabling config MBEDTLS_MEMORY_BUFFER_ALLOC_C, MBEDTLS_MEMORY_BACKTRACE. and MBEDTLS_PLATFORM_MEMORY in config.h and it is passing tests locally on my ubuntu machine (taking really long though). Is there any cause of concern here?

@ronald-cron-arm
Copy link
Contributor

I think it is just a CI timeout thus nothing to worry on your side.

@maulik-arm maulik-arm force-pushed the maulik-arm/fix-4162 branch from aa59823 to f41be14 Compare April 1, 2021 09:03
@ronald-cron-arm ronald-cron-arm dismissed bensze01’s stale review April 1, 2021 11:26

There are two comments associated to this "change requested". The first one is not longer relevant as related to code that is not changed anymore. The second one is still valid but not relevant to this PR and addressed in #4279.

@ronald-cron-arm ronald-cron-arm merged commit 2af9641 into Mbed-TLS:development Apr 1, 2021
daverodgman pushed a commit that referenced this pull request Apr 23, 2021
PSA Update return code for non-existing key in various key operations
theotherjimmy pushed a commit to theotherjimmy/trusted-firmware-m that referenced this pull request Jun 7, 2021
PSA Crypto API spec requests key operation to return error code
PSA_ERROR_INVALID_HANDLE when the key doesn't exist.
However, according to [1], PSA key operation implementation in Mbed TLS
returns PSA_ERROR_DOES_NOT_EXIST instead.

TF-M currently works normally since TF-M specific key handle check will
return PSA_ERROR_INVALID_HANDLE for a non-existing key, without calling
Mbed TLS PSA key operation.

Apply the merged Mbed TLS fix to TF-M to prepare for enhancement of TF-M
key handle check.

[1]: Mbed-TLS/mbedtls#4198

Change-Id: I79dda1c54dc8377afbfaefdf180bb81c7ff99f02
Signed-off-by: David Hu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-work size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not return PSA_ERROR_DOES_NOT_EXIST for a non-existing key
4 participants