Skip to content

wolfmath: check mpSz in wc_export_int.#10444

Open
philljj wants to merge 1 commit intowolfSSL:masterfrom
philljj:fix_wc_export_int
Open

wolfmath: check mpSz in wc_export_int.#10444
philljj wants to merge 1 commit intowolfSSL:masterfrom
philljj:fix_wc_export_int

Conversation

@philljj
Copy link
Copy Markdown
Contributor

@philljj philljj commented May 8, 2026

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.

@philljj philljj self-assigned this May 8, 2026
Copilot AI review requested due to automatic review settings May 8, 2026 20:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 > keySz guard in wc_export_int() before computing the output offset.
  • Reuse the computed mpSz when calculating the write position for mp_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 thread wolfcrypt/src/wolfmath.c
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));
Comment thread tests/api/test_wolfmath.c
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),
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

MemBrowse Memory Report

gcc-arm-cortex-m4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants