-
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
Dynamically allocate requested CSR write buffer size #3464
Conversation
3192: Update upstream mbedTLS patches backported to v2.16 r=mingweishih a=CodeMonkeyLeet - Add 0001-Avoid-stack-allocation-of-large-memory-buffers.patch from upstream [mbedTLS PR #3464](Mbed-TLS/mbedtls#3464). - Replace 0001-Patch-x509_write_crt.c-for-attestedTLS.patch with patch from upstream mbedTLS 0001-Backport-2632-code-changes-to-2.16.patch. This contains commits 6ad3fd1, 4063ad2 and 67d4259 from [mbedTLS PR #2632](Mbed-TLS/mbedtls#2632). - Apply the upstream changes to 3rdparty/mbedtls. Fixes #3170 Signed-off-by: Simon Leet <[email protected]> 3197: Remove the import of logging from tls_client.edl r=radhikaj a=mingweishih The PR removes the import of logging.edl in the sample that was missed by #3151. Signed-off-by: Ming-Wei Shih <[email protected]> Co-authored-by: Simon Leet <[email protected]> Co-authored-by: Ming-Wei Shih <[email protected]>
3192: Update upstream mbedTLS patches backported to v2.16 r=mingweishih a=CodeMonkeyLeet - Add 0001-Avoid-stack-allocation-of-large-memory-buffers.patch from upstream [mbedTLS PR #3464](Mbed-TLS/mbedtls#3464). - Replace 0001-Patch-x509_write_crt.c-for-attestedTLS.patch with patch from upstream mbedTLS 0001-Backport-2632-code-changes-to-2.16.patch. This contains commits 6ad3fd1, 4063ad2 and 67d4259 from [mbedTLS PR #2632](Mbed-TLS/mbedtls#2632). - Apply the upstream changes to 3rdparty/mbedtls. Fixes #3170 Signed-off-by: Simon Leet <[email protected]> Co-authored-by: Simon Leet <[email protected]>
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.
Thanks for doing the rework! Looks good to me.
ChangeLog
Outdated
Changes | ||
* Reduce the stack consumption of mbedtls_x509write_csr_der() which | ||
previously could lead to stack overflow on constrained devices. | ||
Contributed by Doru Gucea. |
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.
Feel free to add “and Simon Leet”. Reworking a pull request is a contribution that warrants acknowledgement.
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.
Thanks for the quick turnaround on the review, I've updated the Changelog as suggested. Is there any additional follow-up needed from me? I don't have read permissions to the CI failures, but let me know if I need to be involved.
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.
Thanks, it's entirely on us now. The next step is for another team member to review. Mbed OS CI is currently failing for unrelated reasons, so we'll ignore it if it isn't repaired by the time this PR is ready to merge.
9a36649
to
2d11f82
Compare
Using a stack-buffer with a size > 2K could easily produce a stack overflow for an embedded device which has a limited stack size. This commit dynamically allocates the large CSR buffer. This commit avoids using a temporary buffer for storing the OIDs. A single buffer is used: a) OIDs are written backwards starting with the end of the buffer; b) OIDs are memmove'd to the beginning of the buffer; c) signature over this OIDs is computed and written backwards from the end of the buffer; d) the two memory regions are compacted. Signed-off-by: Doru Gucea <[email protected]>
Address remaining PR comments for Mbed-TLS#2118 - Add ChangeLog.d/x509write_csr_heap_alloc.txt. - Fix parameter alignment per Gille's recommendation. - Update comments to more explicitly describe the manipulation of buf. - Replace use of `MBEDTLS_MPI_MAX_SIZE` as `sig` buffer size for call to `x509write_csr_der_internal()` with more intuitive `MBEDTLS_PK_SIGNATURE_MAX_SIZE`. - Update `mbedtls_x509write_csr_der()` to return `MBEDTLS_ERR_X509_ALLOC_FAILED` on mbedtls_calloc error. Signed-off-by: Simon Leet <[email protected]>
2d11f82
to
40ca54a
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.
Looks good to me. Thanks for reviving and finalizing this old PR.
I'm merging this without waiting for potential backports as it's ready, and since it's marked as change in the ChangeLog we normally wouldn't backport it (we normally only backport bug fixes and some testing improvements). However you mention that a 2.16 backport would be helpful to you. The change doesn't seem very invasive and can reasonably be argued to be a bug fix (the same issues with cert writing were labelled "bug"), so while I can't speak for other gatekeepers, I'd be willing to accept a backport to 2.16 if you're willing to submit one. |
Signed-off-by: Simon Leet <[email protected]>
Backport 2.16: PR #3464 Dynamically allocate requested CSR write buffer size
3398: Update to mbedTLS 2.16.7 r=CodeMonkeyLeet a=CodeMonkeyLeet mbedTLS 2.16.7 no longer releases through the https://tls.mbed.org/download-archive we previously depended on, so this PR also takes the opportunity to convert the mbedTLS cloned sources into a git submodule dependency instead. The submodule dependency points at the new openenclave-mbedtls fork, which currently has the following patches applied: - [Backport ](openenclave/openenclave-mbedtls@b3500d5) of [mbedTLS PR #2632](Mbed-TLS/mbedtls#2632) - [Backport](openenclave/openenclave-mbedtls@9d4e4a7) of [mbedTLS PR #3464](Mbed-TLS/mbedtls#3464) - [Patch being upstreamed](openenclave/openenclave-mbedtls@98a7b77) as [mbedTLS PR #3564](Mbed-TLS/mbedtls#3564) Co-authored-by: Simon Leet <[email protected]>
Description
x509write_csr_der_internal()
uses a fixed 2KB stack buffer for writing the CSR. This is problematic because:This PR revives #2118, which appears to be abandoned.
x509write_csr_der_internal()
to dynamically allocate the requested CSR buffer size, which resolves both issues.Status
READY
Requires Backporting
NO
Migrations
NO
Additional comments
In reference to issue #2631, we were waiting on change #2118 in addition to the committed #2632. While nothing necessitates this change being backported, we would like to have both of these PRs backported to 2.16 LTS if possible, so that we don't have to carry the separate patching ourselves.
Todos
Steps to test or reproduce
Per the original PR, the changes are self-contained and exercised through existing
make
andmake check
.