Skip to content

Validate inSize before translating fixed crypto requests - #466

Merged
bigbrett merged 1 commit into
wolfSSL:mainfrom
yosuke-wolfssl:fix/f_5463
Aug 4, 2026
Merged

Validate inSize before translating fixed crypto requests#466
bigbrett merged 1 commit into
wolfSSL:mainfrom
yosuke-wolfssl:fix/f_5463

Conversation

@yosuke-wolfssl

@yosuke-wolfssl yosuke-wolfssl commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Four server crypto handlers translated their fixed request struct out of
cryptoDataIn without first proving the request was long enough to hold it.
Two more validated the length only after translating.

wh_Server_HandleCryptoRequest validates the generic header, then passes the
remaining length straight through:

uint16_t cryptoInSize =
    req_size - sizeof(whMessageCrypto_GenericResponseHeader);

A request carrying only the generic header yields cryptoInSize == 0, but the
handler still reads a full struct. Since crypto requests are processed in place
in a buffer that retains the previous message, it consumes bytes left over from
an earlier request as key generation parameters.

Addressed by f_5463.

Changes

_HandleEccKeyGen, _HandleCurve25519KeyGen, _HandleEd25519KeyGen and
_HandleMlDsaKeyGen gain the guard _HandleRsaKeyGen already used:

if (inSize < sizeof(whMessageCrypto_XxxKeyGenRequest)) {
    return WH_ERROR_BADARGS;
}

_HandleMlDsaSign and _HandleMlDsaVerify keep their existing check, moved
ahead of the translate call.

Every other fixed-struct handler already validated inSize first, including
ECDH and Curve25519 shared secret. Impact is stale-parameter use, not a read
past the comm buffer. No API or wire format changes. This PR is now
source-only.

Tests

The test added by the earlier revision has been removed. It was
test-refactor/server/wh_test_crypto_reqsize.c (449 lines,
whTest_CryptoReqSize), which called wh_Server_HandleCryptoRequest directly
with hand-built truncated buffers. Per review, a test that hand-builds a request
packet and calls a server handler is testing the harness, not the fix, so it and
its wh_test_list.c entry are dropped. This also removes the add/add conflict
with #487, which created the same file.

No replacement test is added: wh_Client_* always sends a full request struct,
so a truncated crypto request is not reachable through the normal client API.
The reproducer that motivated the fix (a header-only request replayed over a
retained buffer) is inherently a crafted frame.

Verification

  • test-refactor: default 37 passed / 25 skipped / 0 failed of 62 · DMA=1 41/21/0 · SHE=1 43/19/0
  • Legacy test/ suite clean.
  • Clean under -std=c90 -Werror -Wall -Wextra, including reduced algorithm
    configs.
  • The four keygen guards were previously reproducer-verified: against main the
    truncated requests were accepted (ECC keygen accepted truncated request of 12 bytes) and the fix rejects them.

@yosuke-wolfssl yosuke-wolfssl self-assigned this Jul 21, 2026
Copilot AI review requested due to automatic review settings July 21, 2026 06:31

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

This PR hardens wh_Server_HandleCryptoRequest’s fixed-struct crypto request handlers by ensuring inSize is validated before translating/reading request structs, preventing stale-parameter consumption from previously-used comm buffers. It also adds a focused regression test that reproduces the truncated-request scenario by reusing an unmodified request buffer.

Changes:

  • Add early inSize < sizeof(fixed_request_struct) guards for ECC, Curve25519, Ed25519, and ML-DSA keygen handlers, and move existing ML-DSA sign/verify size checks ahead of translation.
  • Add new server-side test whTest_CryptoReqSize to confirm truncated requests are rejected with WH_ERROR_BADARGS while a full-length control succeeds.
  • Register the new test in the server test list.

Reviewed changes

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

File Description
src/wh_server_crypto.c Adds/moves fixed-struct inSize validation ahead of translate calls in several crypto handlers.
test-refactor/server/wh_test_crypto_reqsize.c New regression test that exercises truncated fixed-struct crypto requests against wh_Server_HandleCryptoRequest.
test-refactor/wh_test_list.c Registers whTest_CryptoReqSize in the server test group.

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

Comment thread test-refactor/server/wh_test_crypto_reqsize.c Outdated
Comment thread test-refactor/server/wh_test_crypto_reqsize.c Outdated
Comment thread test-refactor/server/wh_test_crypto_reqsize.c Outdated

@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 #466

Scan targets checked: wolfhsm-crypto-bugs, wolfhsm-src

No new issues found in the changed files. ✅

Frauschi
Frauschi previously approved these changes Jul 23, 2026

@bigbrett bigbrett 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.

The actual code changes look good but please remove regression test for malformed message scenario, per my slack message. We don't need manual packet crafting for every argument validation test, otherwise the test harness will blow up in complexity.

bigbrett
bigbrett previously approved these changes Aug 3, 2026
@bigbrett bigbrett assigned yosuke-wolfssl and unassigned bigbrett Aug 3, 2026
@bigbrett

bigbrett commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@yosuke-wolfssl pls fix merge conflicts, then can merge

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

Hi @bigbrett ,
I solved the merge conflict. Please check this again

@bigbrett
bigbrett merged commit 7f454eb into wolfSSL:main Aug 4, 2026
108 of 112 checks passed
@yosuke-wolfssl
yosuke-wolfssl deleted the fix/f_5463 branch August 4, 2026 02:14
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.

6 participants