-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Remove Extraneous bytes from buffer post pem write #3898
Remove Extraneous bytes from buffer post pem write #3898
Conversation
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 agree with this approach. It needs:
- Same thing with CSR.
- In unit tests, after calling
mbedtls_x509write_crt_pem
, check that the trail of the buffer is zeroed out. Make sure that there is at least one unit test where the output buffer is larger than the output data. - Unit tests for the other functions with the same pattern (
mbedtls_pk_write_pubkey_pem
,mbedtls_pk_write_key_pem
,mbedtls_x509write_csr_pem
). - Changelog entry.
16b0c32
to
c018547
Compare
c018547
to
6b6376d
Compare
ChangeLog.d/clean_pem_buffers.txt
Outdated
@@ -0,0 +1,8 @@ | |||
Bugfix | |||
* As an optimisation to stop large buffers being written to the stack the | |||
raw der data was instead written to the same buffer that the PEM data |
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.
DER also needs to be in all caps.
6b6376d
to
7385f10
Compare
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.
LGTM but not to CI
ChangeLog.d/clean_pem_buffers.txt
Outdated
bytes. This guarantees that the corresponding parsing function can read | ||
the buffer back, which was the case for mbedtls_x509write_{crt,csr}_pem | ||
until this property was inadvertently broken in Mbed TLS 2.19.0. | ||
Fixes #3682. |
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.
Please ensure that tests/scripts/all.sh -k check_files check_changelog
passes
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.
Missing newline at end. Fixed.
7385f10
to
b3e9d2d
Compare
b3e9d2d
to
2451aba
Compare
2451aba
to
6b654c7
Compare
In order to remove large buffers from the stack, the der data is written into the same buffer that the pem is eventually written into, however although the pem data is zero terminated, there is now data left in the buffer after the zero termination, which can cause mbedtls_x509_crt_parse to fail to parse the same buffer if passed back in. Patches also applied to mbedtls_pk_write_pubkey_pem, and mbedtls_pk_write_key_pem, which use similar methods of writing der data to the same buffer, and tests modified to hopefully catch any future regression on this. Signed-off-by: Paul Elliott <[email protected]>
6b654c7
to
557b8d6
Compare
The CI is being unstable, but between https://jenkins-mbedtls.oss.arm.com/blue/organizations/jenkins/mbed-tls-pr-merge/detail/PR-3898-merge/10 and https://jenkins-mbedtls.oss.arm.com/blue/organizations/jenkins/mbed-tls-pr-merge/detail/PR-3898-merge/11 all the jobs passed (for Windows-2013, which failed in both, all the sub-jobs passed in at least one of the runs). |
Description
In order to remove some big (4k) buffers from being created on the stack, the output buffer was re-used - in this case the raw der data is written to the buffer prior to being base64 encoded into an allocated buffer, and then overwritten with the pem data. However, even though the pem data is zero terminated, usually der data will remain in the buffer after the terminator.
Should this buffer get passed into mbedtls_x509_crt_parse() then it will likely fail to parse, as it decides whether or not a buffer is pem by checking the last byte of the buffer for being zero - if it isn't zero, it will attempt to parse as der, which will obviously fail.
This just ensures the buffer is cleaned to avoid the regression, there may be future work around this subject to make the area safer and tests to ensure this doesn't happen again.
This closes #3682
Status
READY
Requires Backporting
YES
2.16
2.7
(Bug did not affect the 2.7 branch, but wanted to add the tests)
Migrations
NO
Todos
Steps to test or reproduce
See #3682