Skip to content

Defensive code additions for sanity checks on input arguments with Base64, PEM write, mp_read_unsigned_bin#10721

Open
JacobBarthelmeh wants to merge 3 commits into
wolfSSL:masterfrom
JacobBarthelmeh:dev_2
Open

Defensive code additions for sanity checks on input arguments with Base64, PEM write, mp_read_unsigned_bin#10721
JacobBarthelmeh wants to merge 3 commits into
wolfSSL:masterfrom
JacobBarthelmeh:dev_2

Conversation

@JacobBarthelmeh

Copy link
Copy Markdown
Contributor

ZD21992

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Hardens several low-level routines against integer wrap/overflow by tightening bounds checks when parsing lengths, encoding Base64/PEM, and reading big integers.

Changes:

  • Added size/argument validation to prevent overflow in Base64 encoding and PEM size calculations.
  • Strengthened ASN.1 buffer bounds checks to avoid word32 wrap.
  • Added defensive checks in mp_read_unsigned_bin to reject problematic input sizes.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
wolfcrypt/src/integer.c Adds input length validation intended to prevent overflow in bit/digit calculations.
wolfcrypt/src/coding.c Adds a guard intended to prevent Base64 encoded-size calculation from wrapping.
wolfcrypt/src/asn.c Reworks index/length comparisons to avoid word32 addition overflow.
src/pk.c Adds a guard against overflow in PEM buffer size calculations and rejects negative lengths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wolfcrypt/src/integer.c
Comment on lines +774 to 779
/* reject sizes where the bit count would overflow an int */
if (c > (WOLFSSL_MAX_32BIT - (DIGIT_BIT - 1)) / CHAR_BIT) {
return MP_VAL;
}

digits_needed = ((c * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT;
Comment thread wolfcrypt/src/coding.c
Comment on lines +486 to 491
/* Reject lengths that would wrap the encoded-size calculation below. */
if (inLen >= (WOLFSSL_MAX_32BIT / 4))
return BAD_FUNC_ARG;

outSz = (inLen + 3 - 1) / 3 * 4;
addSz = (outSz + BASE64_LINE_SZ - 1) / BASE64_LINE_SZ; /* new lines */
Comment thread src/pk.c
Comment on lines +6917 to +6921
/* Reject lengths that would wrap the PEM size calculation below. */
if ((len < 0) || ((word32)len >= (WOLFSSL_MAX_32BIT / 4))) {
return BAD_FUNC_ARG;
}
derLen = (word32)len;

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10721

Scan targets checked: wolfcrypt-bugs, wolfcrypt-src, wolfssl-bugs, wolfssl-src

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Findings are non-blocking.

Comment thread wolfcrypt/src/integer.c
}

/* reject sizes where the bit count would overflow an int */
if (c > (WOLFSSL_MAX_32BIT - (DIGIT_BIT - 1)) / CHAR_BIT) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 [Medium] mp_read_unsigned_bin overflow guard uses unsigned max for signed int math · Incorrect sizeof/type usage

The guard bounds c by WOLFSSL_MAX_32BIT (0xffffffff), but c, CHAR_BIT and digits_needed are signed int. Values of c in (INT_MAX/8, UINT_MAX/8] pass the check yet still overflow c * CHAR_BIT in digits_needed, the exact signed-int overflow the check claims to prevent.

Fix: Bound c against INT_MAX instead of WOLFSSL_MAX_32BIT: c > (INT_MAX - (DIGIT_BIT - 1)) / CHAR_BIT.

@JacobBarthelmeh JacobBarthelmeh changed the title Hardening defensive code additions for sanity checks on input arguments with Base64, PEM write, mp_read_unsigned_bin Defensive code additions for sanity checks on input arguments with Base64, PEM write, mp_read_unsigned_bin Jun 17, 2026
@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown

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.

3 participants