Fix 32-bit size_t overflow in AES needed_size checks and two SHE size guards#487
Open
yosuke-wolfssl wants to merge 1 commit into
Open
Fix 32-bit size_t overflow in AES needed_size checks and two SHE size guards#487yosuke-wolfssl wants to merge 1 commit into
yosuke-wolfssl wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens request-size validation on 32-bit targets to prevent size_t wraparound from bypassing AES and SHE packet length checks, avoiding pre-auth memory corruption paths in server request handlers.
Changes:
- Fixes 32-bit overflow in AES
needed_sizecomputations by promoting all operands touint64_tacross CTR/ECB/CBC/GCM and DMA variants. - Adds explicit magnitude guards in two SHE handlers to prevent
sizeof(req) + req.szfrom overflowing on 32-bit. - Introduces/refactors tests to exercise wrap-crafted request vectors for the affected handlers and registers the new test in the refactor test runner.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test-refactor/wh_test_list.c | Registers the new whTest_CryptoReqSize in the server test set. |
| test-refactor/server/wh_test_she_server.c | Adds overflow-focused SHE req_size tests and mirrors the UPDATE state value for setup. |
| test-refactor/server/wh_test_crypto_reqsize.c | New refactor test covering AES handler req_size validation against 32-bit wrap-crafted vectors (incl. DMA where enabled). |
| src/wh_server_she.c | Adds pre-checks to prevent 32-bit size_t overflow in two variable-length SHE request size validations. |
| src/wh_server_crypto.c | Fixes AES handler needed_size calculations to be 64-bit regardless of target word size. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #487
Scan targets checked: wolfhsm-core-bugs, wolfhsm-crypto-bugs, wolfhsm-src
No new issues found in the changed files. ✅
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.
Problem
Every AES request handler validates its packet with a
needed_sizesum:sizeofissize_tandlen/key_lenareuint32_t, so on a 32-bit targetevery operand is 32-bit and the sum wraps before the widening assignment — the
uint64_tdeclaration buys nothing.inSizeisuint16_t, so a client pickslensuch that the wrapped sum equals a small legitimate packet size, the guardpasses, and
lenreacheswc_AesEcbEncrypt(..., (word32)len)unmodified: a~4 GB read/write off a small buffer. Reachable pre-auth
(
wh_Server_HandleRequestMessage), and every supported port is 32-bit — the64-bit POSIX build where this cannot trigger is only CI. Pre-auth memory
corruption on every real target.
Closes findings F-4325 / F-4327 / F-4328 / F-4331.
Fix (
src/wh_server_crypto.c)Cast every operand to
uint64_tso the sum is 64-bit at any word size, at all8
needed_sizesites — the 4 reported handlers plus_HandleAesGcm(the worstsite, 5 attacker-controlled addends, unreported) and the 4 DMA variants. Also
guards the 2 unreported SHE sites (
_GenerateMac,_SecureBootUpdateinsrc/wh_server_she.c) with the same> WOLFHSM_CFG_COMM_DATA_LENoverflow checktheir 5 neighbours already carry. No behavior change on 64-bit; added lines only.
Tests
test-refactor/server/wh_test_crypto_reqsize.c(whTest_CryptoReqSize):wrap-crafted rejection for all 8 handlers + a width-independent proof each
vector aliases a header-only
inSizein 32-bit.whTest_SheReqSizeChecking.Verification
16/54/0; legacy
test/exits 0; clean under-std=c90 -Werror.whTest_CryptoReqSizepasses (50/20/0); reverting only the two source files makes the same binary
SIGSEGV inside
whTest_CryptoReqSizeon the ~4 GB read — the actualexploit path.
Not in this PR: a permanent 32-bit CI job (no 32-bit CI exists today) — filed as
follow-up.