Skip to content

Fix integer overflow in _wc_Hash_Grow and TI hashUpdate#9984

Closed
Scottcjn wants to merge 1 commit intowolfSSL:masterfrom
Scottcjn:fix/hash-integer-overflow
Closed

Fix integer overflow in _wc_Hash_Grow and TI hashUpdate#9984
Scottcjn wants to merge 1 commit intowolfSSL:masterfrom
Scottcjn:fix/hash-integer-overflow

Conversation

@Scottcjn
Copy link

Summary

Fix integer overflow in hash buffer size computation that could lead to heap buffer overflow.

Problem

Both _wc_Hash_Grow() (hash.c) and hashUpdate() (ti-hash.c) compute the new buffer size as used + inSz without overflow checking. If used is near UINT32_MAX, the addition wraps to a small value, causing:

  1. Small allocation (XMALLOC with wrapped size)
  2. Large write (XMEMCPY with original inSz) → heap overflow

Same pattern as the SE050 fix in #9954.

Fix

Use WC_SAFE_SUM_WORD32() to detect overflow before the addition:

word32 newSz;
if (!WC_SAFE_SUM_WORD32(*used, (word32)inSz, newSz)) {
    return BUFFER_E;  // overflow detected
}

Then use newSz for all allocation and length comparisons.

Files Changed

  • wolfcrypt/src/hash.c_wc_Hash_Grow() (requires WOLFSSL_HASH_KEEP)
  • wolfcrypt/src/port/ti/ti-hash.chashUpdate() (requires WOLFSSL_TI_HASH)

Severity

Low — requires ~4GB of hash input to trigger, which will OOM on most systems first. But defense-in-depth is the right call, and this keeps the codebase consistent with the SE050 fix.

Fixes #9955

CLA: Previously signed (on file from PR #9932).

Both _wc_Hash_Grow() and hashUpdate() compute buffer sizes via
unchecked addition of used + inSz/len. If used is near UINT32_MAX,
the sum wraps to a small value, causing a small allocation followed
by a large memcpy — a heap buffer overflow.

Fix: use WC_SAFE_SUM_WORD32() to check for overflow before the
addition, consistent with the fix applied in wolfSSL#9954 for SE050.

Affects:
- wolfcrypt/src/hash.c: _wc_Hash_Grow() (WOLFSSL_HASH_KEEP)
- wolfcrypt/src/port/ti/ti-hash.c: hashUpdate() (WOLFSSL_TI_HASH)

Fixes wolfSSL#9955

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@wolfSSL-Bot
Copy link

Can one of the admins verify this patch?

@dgarske dgarske self-requested a review March 16, 2026 18:06
@dgarske dgarske self-assigned this Mar 16, 2026
@dgarske
Copy link
Contributor

dgarske commented Mar 16, 2026

@dgarske dgarske closed this Mar 16, 2026
@Scottcjn
Copy link
Author

Thanks @dgarske — confirmed #9954 covers the same overflow in hash.c and ti-hash.c with WC_SAFE_SUM_WORD32. Same bug, your preferred fix style. Glad the pattern got caught across all three paths.

Our other PR #9932 (POWER8 AES vcipher pipeline) is independent — still hoping that makes a future release.

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.

[Bug]: Integer overflow in hash update could mess up the heap (hash.c & ti-hash.c)

3 participants