wolfmath: check mpSz in wc_export_int.#10444
Open
philljj wants to merge 1 commit intowolfSSL:masterfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens wc_export_int()’s unsigned-binary export path by preventing an underflowed destination pointer when the mp_int requires more bytes than the requested/padded keySz.
Changes:
- Add an
mpSz > keySzguard inwc_export_int()before computing the output offset. - Reuse the computed
mpSzwhen calculating the write position formp_to_unsigned_bin(). - Add a unit test intended to exercise the “mp too large” error path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
wolfcrypt/src/wolfmath.c |
Adds a size check to prevent exporting an mp_int that won’t fit into the requested keySz padded output. |
tests/api/test_wolfmath.c |
Adds a test case intended to ensure wc_export_int() returns BUFFER_E when the mp_int is too large. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+256
to
+267
| word32 mpSz = 0; | ||
| if (*len < keySz) { | ||
| *len = keySz; | ||
| return BUFFER_E; | ||
| } | ||
| *len = keySz; | ||
| mpSz = (word32)mp_unsigned_bin_size(mp); | ||
| if (mpSz > keySz) { | ||
| return BUFFER_E; | ||
| } | ||
| XMEMSET(buf, 0, *len); | ||
| err = mp_to_unsigned_bin(mp, buf + | ||
| (keySz - (word32)mp_unsigned_bin_size(mp))); | ||
| err = mp_to_unsigned_bin(mp, buf + (keySz - mpSz)); |
| len = sizeof(buf); | ||
| ExpectIntEQ(mp_init(&mp), MP_OKAY); | ||
| ExpectIntEQ(mp_set_bit(&mp, 257), 0); | ||
| ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_UNSIGNED_BIN), |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
wc_export_int()was checking if the keySz was too large for the buffer, but not if the mpSz was too large.Fixes: zd21783.