Skip to content

Add bounds checks for MP integer size in SizeASN_Items#10051

Open
anhu wants to merge 3 commits intowolfSSL:masterfrom
anhu:mp_int_bounds
Open

Add bounds checks for MP integer size in SizeASN_Items#10051
anhu wants to merge 3 commits intowolfSSL:masterfrom
anhu:mp_int_bounds

Conversation

@anhu
Copy link
Copy Markdown
Member

@anhu anhu commented Mar 23, 2026

Fixes ZD 21401

@anhu anhu requested a review from wolfSSL-Bot March 23, 2026 20:18
@anhu anhu self-assigned this Mar 23, 2026
@dgarske
Copy link
Copy Markdown
Contributor

dgarske commented Mar 24, 2026

Jenkins retest this please -history lost

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 25, 2026

Jenkins retest this please.

Unable to get pull request trigger.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 25, 2026

Jenkins retest this please

Build was aborted

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 26, 2026

jenkins retest this please

remote hung up.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 26, 2026

jenkins retest this please

ABORTED

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 27, 2026

jenkins retest this please.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 27, 2026

Jenkins retest this please.

1 similar comment
@anhu
Copy link
Copy Markdown
Member Author

anhu commented Mar 30, 2026

Jenkins retest this please.

@anhu anhu added the Not For This Release Not for release 5.9.1 label Apr 1, 2026
@anhu
Copy link
Copy Markdown
Member Author

anhu commented Apr 1, 2026

jenkins retest this please

Not found.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Apr 1, 2026

Jenkins retest this please.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Apr 8, 2026

jenkins retest this please.

@anhu anhu removed the Not For This Release Not for release 5.9.1 label Apr 8, 2026
@anhu
Copy link
Copy Markdown
Member Author

anhu commented Apr 8, 2026

jenkins retest this please.


/* Sparse mmap — only the page at dp[used-1] is faulted in. */
if (EXPECT_SUCCESS()) {
crafted_dp = (mp_digit*)mmap(NULL, dp_bytes,
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.

This seems excessive.
Can't you test it another way that works on platforms not Windows?

Copy link
Copy Markdown
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

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

🐺 Skoll Code Review

Overall recommendation: APPROVE
Findings: 3 total — 3 posted, 0 skipped

Posted findings

  • [Medium] SetASN_Items MP path lacks matching bounds checkswolfcrypt/src/asn.c:1097-1099
  • [Medium] Test only covers USE_INTEGER_HEAP_MATH; tfm.c has same overflow patterntests/api/test_rsa.c:1161-1163
  • [Low] Unused variable derRet could use (void) cast or direct use in Expecttests/api/test_rsa.c:1254-1255

Review generated by Skoll via openclaw

}
break;

case ASN_DATA_TYPE_REPLACE_BUFFER:
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.

🟡 [Medium] SetASN_Items MP path lacks matching bounds checks
💡 SUGGEST bug

The ASN_DATA_TYPE_MP case in SetASN_Items (the write path) calls mp_unsigned_bin_size() and mp_leading_bit() on lines 1097-1099 with the same pattern that was just hardened in SizeASN_Items, but without the new bounds checks. In normal usage SizeASN_Items is called first and would reject a malformed mp_int before SetASN_Items runs, so this is not directly exploitable. However, as defense-in-depth—and to prevent future callers from hitting the same issue—the same length < 0 checks should be added here.

Suggestion:

Suggested change
case ASN_DATA_TYPE_REPLACE_BUFFER:
case ASN_DATA_TYPE_MP:
/* Get length in bytes. */
length = mp_unsigned_bin_size(data[i].data.mp);
if (length < 0) {
return ASN_PARSE_E;
}
/* Add one for leading zero to make encoding a positive num. */
length += mp_leading_bit(data[i].data.mp) ? 1 : 0;
if (length < 0) {
return ASN_PARSE_E;
}
/* Write out length. */
idx += SetASNLength((word32)length, out + idx);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The suggestion removes the handling of ASN_DATA_TYPE_REPLACE_BUFFER . This feels wrong. Not aking the suggestion.

int test_wc_RsaKeyToDer_SizeOverflow(void)
{
EXPECT_DECLS;
#if !defined(NO_RSA) && defined(USE_INTEGER_HEAP_MATH) && \
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.

🟡 [Medium] Test only covers USE_INTEGER_HEAP_MATH; tfm.c has same overflow pattern
💡 SUGGEST test

The test is gated on USE_INTEGER_HEAP_MATH because the pointer-biasing trick relies on heap-allocated dp. However, fp_count_bits in tfm.c (line 4116) has the same (a->used - 1) * DIGIT_BIT overflow pattern as mp_count_bits in integer.c (line 262). With TFM (fastmath), dp is a fixed-size stack array, so the pointer-biasing trick won't work, but the underlying SizeASN_Items fix is still relevant for TFM builds. Consider adding a separate test path for TFM (e.g., using mp_set_int or another approach) or at minimum adding a comment explaining why TFM is excluded.

Recommendation: Consider adding a comment or separate test variant for TFM builds. Even if the pointer trick doesn't work with stack-allocated digits, verifying that SizeASN_Items rejects a negative mp_unsigned_bin_size result is valuable across all math backends.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will not leave revealing comment.

}

/* Should return an error, not a bogus small size. */
derRet = wc_RsaKeyToDer(&key, NULL, 0);
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.

🔵 [Low] Unused variable derRet could use (void) cast or direct use in Expect
🔧 NIT style

The variable derRet is assigned from wc_RsaKeyToDer and immediately passed to ExpectIntLT. Some compilers may warn about the extra variable being unnecessary. The pattern could be simplified to avoid the intermediate variable, or the derRet value could be used in the ExpectIntLT call directly (which it already is). This is a very minor style point—the existing code is clear and readable.

Recommendation: No action required. The code is clear as-is. If the project convention is to avoid intermediate variables for single-use returns, consider ExpectIntLT(wc_RsaKeyToDer(&key, NULL, 0), 0) instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not going to bother with this.

@anhu
Copy link
Copy Markdown
Member Author

anhu commented Apr 10, 2026

jenkins retest this please

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