Skip to content

Validate inSize before translating fixed crypto requests#466

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:mainfrom
yosuke-wolfssl:fix/f_5463
Open

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

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown

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.

Test

New test-refactor/server/wh_test_crypto_reqsize.c (whTest_CryptoReqSize,
Server group) drives wh_Server_HandleCryptoRequest directly. Per algorithm it
sends a valid request as a positive control, then reissues the unmodified
buffer
truncated to the header and to one byte short of the struct — leaving
the control request in place is what reproduces the stale read. Truncated calls
must return WH_ERROR_BADARGS.

Keygen cases use ephemeral keys so nothing touches the keystore. ML-DSA sign
and verify need real state, so that subtest caches a key, signs, and verifies
before its truncated cases, then evicts on every path and confirms the eviction.

Against main the four keygen cases fail:

ERROR [_CheckFixedRequest]: ECC keygen accepted truncated request of 12 bytes

Sign and verify are regression guards, not reproducers — the old ordering also
rejected, just after reading past the payload. Deleting their guard outright
does break them, which is the point.

Verification

  • cd test-refactor && make check — 38 passed, 25 skipped, 0 failed of 63
  • Legacy cd test && make run — clean
  • Clean under -std=c90 -Werror -Wall -Wextra, including reduced algorithm
    configs; with no covered algorithm the test is skipped rather than built

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

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.

5 participants