-
Notifications
You must be signed in to change notification settings - Fork 3k
PSA Crypto Service - multipart operation memory fixes #10232
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 Crypto Service - multipart operation memory fixes #10232
Conversation
Also refactor cipher setup function to one common function.
@itayzafrir, thank you for your changes. |
components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c
Outdated
Show resolved
Hide resolved
components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c
Outdated
Show resolved
Hide resolved
components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c
Outdated
Show resolved
Hide resolved
@@ -133,6 +137,9 @@ static psa_status_t psa_mac_setup(psa_mac_operation_t *operation, | |||
return (status); | |||
} | |||
status = ipc_call(&operation->handle, &in_vec, 1, NULL, 0, false); | |||
if (status != PSA_SUCCESS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ipc_call is a wrapper by itself.
i suggest for the same idea of code being clear and refactoring.
to put the
if (status != PSA_SUCCESS) {
ipc_close(&operation->handle);
inside ipc_call in case of failure.
with only one important note, if and only if we always want to close connections on call failure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not always true, e.g. generators functions like psa_get_generator_capacity
etc. don't close in case of error, so leaving it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why they don't close?
maybe we want to consider having a flag passed whether to close or not.
and still hide the close inside? there are too many of the same "close"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think having another flag makes much of a difference regarding code size or readability, if you think it's important I'll make another change.
psa_crypto.handle, | ||
psa_crypto.alg); | ||
if (status != PSA_SUCCESS) { | ||
mbedtls_free(msg.rhandle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok to free handle (mbedtls_free) for both scenarios:
sign setup fails
handle is not permitted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it is, the operation failed for whatever reason and the context is discarded.
@@ -328,22 +330,27 @@ static void psa_mac_operation(void) | |||
|
|||
uint8_t *mac = mbedtls_calloc(1, mac_length); | |||
if (mac == NULL) { | |||
psa_mac_abort(msg.rhandle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't it cause recursion?
calling psa_mac_abort will call psa_mac_abort which will call this function with case of abort, which calls psa_mac_abort in case of abort
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is service code calling library code, the library never calls the service.
mbedtls_free(salt); | ||
mbedtls_free(label); | ||
if (status != PSA_SUCCESS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this block returns dozens of times.
please wrap in in static cleanup named function.
i would also suggest considering instead of repeating it multiple times through various cases. considering some bailout flag and logic and doing cleanup related to this flag once before return or so.
code becomes now a bit spaghetti like and is being a bit blown in size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll provide a static cleanup function. I don't like the error flag approach in this case because it will add even more complexity to these nested switch
cases. We'll refactor the partition also once we get a chance.
@NirSonnenschein @avolinski I've added some more commits addressing the issues you raised, please re-review. |
components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes look good to me
@avolinski Thoughts? |
CI started |
Test run: FAILEDSummary: 6 of 7 test jobs failed Failed test jobs:
|
Restarted jenkins-ci |
Test run: FAILEDSummary: 1 of 11 test jobs failed Failed test jobs:
|
License server error, restarting exporters |
Description
Prevent potential memory leaks on the SPM side and also free dynamically allocated memory as soon as possible.
Summary of changes in this PR:
psa_hash_update
fails then the operation is aborted and the caller doesn't have to callpsa_hash_abort
(as abort is called by the library). The service behavior was not consistent with the library and could lead to memory leaks on the server side.psa_close
to free dynamically allocated contexts but free them ASAP.psa_hash_setup
) if the context is active (part of an active session), this could lead to memory leaks on the SPM side.Pull request type
Reviewers
@avolinski @NirSonnenschein
Release Notes