PK write: avoid large stack buffer #684
No reviewers
Labels
No labels
api-break
approved
bug
component-docs
component-platform
component-test
dependencies
deprecation
DO-NOT-MERGE
enhancement
good first issue
help wanted
needs-adoption
needs-backports
needs-ci
needs-design-approval
needs-info
needs-preceding-pr
needs-review
needs-reviewer
needs-work
priority-high
priority-low
priority-medium
priority-scheduled
priority-very-high
resolved-after-1.1
single-reviewer
size-l
size-m
size-optimisation
size-s
size-xs
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-Mbed-TLS/TF-PSA-Crypto!684
Loading…
Reference in a new issue
No description provided.
Delete branch "pkwrite-large-stack-buffer"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
Avoid uselessly allocating a large buffer on the stack when writing out RSA keypairs.
A step towards addressing https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/476
PR checklist
All good. I only left a comment for improving test code.
As I was recently remembered by @gilles-peskine-arm, using macros like
TEST_EQUAL()does not cause the test function to terminate immediately. It just marked as failed but then it continues until the next real failure (if any) or to the normal termination.As a consequence I think that
pk_write_check_contextshould return an error on failure and then consuming functions likepk_write_check_commonshould just do:Ah, thanks for the reminder! I don't think really a problem here: if the caller just continues to execute after a failure, it just possibly test again with a wrapping context, which is admittedly a waste of time, but not causing any real issues.
Anyway, I'm changing as you suggested, for the sake of robustness.
Yeah, I agree that in this case this wouldn't have been problematic. My intent is mostly to keep some good practices and common patterns so that if in the future someone else would like to implement something and take a look at what's already there, they will find a good reference
Thanks for addressing this!
Just one small question.
@ -56,0 +50,4 @@* we were supposed to write to its end. */*p -= key_len;memmove(*p, buf, key_len);mbedtls_platform_zeroize(buf, *p - buf);I know this is pre-existing. However does the psa_export_key guarantee not to leak during a failure? If not is worth zeroising the buffer? Maybe even worth doing it anyway just as good practice?
@ -56,0 +50,4 @@* we were supposed to write to its end. */*p -= key_len;memmove(*p, buf, key_len);mbedtls_platform_zeroize(buf, *p - buf);All PSA API functions are supposed to leave “safe” outputs on failure. That in particular means no confidential data if the function fails.
@ -56,0 +50,4 @@* we were supposed to write to its end. */*p -= key_len;memmove(*p, buf, key_len);mbedtls_platform_zeroize(buf, *p - buf);Aha, I asked myself the same question while editing the code, then got focused on something else and forgot about this, so thank you Ben for raising it :)
Looking at the API reference, 5.3.2 says: "the content of output parameters is not defined", but a bit below: "When a function returns an error status, it is recommended that implementations set output parameters to safe defaults to avoid leaking confidential data and limit risk, in case an application does not properly handle all errors."
So I guess users should not rely on the content being safe, but as implementers we are imposing this on ourselves as a matter of implementation quality.
But here in PK we're users of the PSA Crypto API, so perhaps we should not make any assumption beyond what the API reference guarantees? Also, if drivers are in use, I don't think we can assume they're going to follow the recommendation as we do.
@ -56,0 +50,4 @@* we were supposed to write to its end. */*p -= key_len;memmove(*p, buf, key_len);mbedtls_platform_zeroize(buf, *p - buf);I think the conclusion of yesterday's discussion is that no change is needed here. It is still under discussion, as part of the effort on standardising the driver API, whether the responsibility of ensuring the
psa_export_key()doesn't leave confidential data around on failure is on the driver or the core, but in any case it is not PK's responsibility.As a result, I think no change is needed here. @bjwtaylor if that's also your understanding of the discussion, I think this discussion can be marked as resolved.
@ -56,0 +50,4 @@* we were supposed to write to its end. */*p -= key_len;memmove(*p, buf, key_len);mbedtls_platform_zeroize(buf, *p - buf);Agreed, I think we can resolve this as per the discussion yesterday.