Validate inSize before translating fixed crypto requests - #466
Conversation
There was a problem hiding this comment.
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_CryptoReqSizeto confirm truncated requests are rejected withWH_ERROR_BADARGSwhile 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.
7525e49 to
a74641d
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #466
Scan targets checked: wolfhsm-crypto-bugs, wolfhsm-src
No new issues found in the changed files. ✅
There was a problem hiding this comment.
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.
a74641d to
bc51b03
Compare
|
@yosuke-wolfssl pls fix merge conflicts, then can merge |
bc51b03 to
a734bbb
Compare
|
Hi @bigbrett , |
Summary
Four server crypto handlers translated their fixed request struct out of
cryptoDataInwithout first proving the request was long enough to hold it.Two more validated the length only after translating.
wh_Server_HandleCryptoRequestvalidates the generic header, then passes theremaining length straight through:
A request carrying only the generic header yields
cryptoInSize == 0, but thehandler 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,_HandleEd25519KeyGenand_HandleMlDsaKeyGengain the guard_HandleRsaKeyGenalready used:_HandleMlDsaSignand_HandleMlDsaVerifykeep their existing check, movedahead of the translate call.
Every other fixed-struct handler already validated
inSizefirst, includingECDH 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 calledwh_Server_HandleCryptoRequestdirectlywith 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.centry are dropped. This also removes the add/add conflictwith #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=141/21/0 ·SHE=143/19/0test/suite clean.-std=c90 -Werror -Wall -Wextra, including reduced algorithmconfigs.
mainthetruncated requests were accepted (
ECC keygen accepted truncated request of 12 bytes) and the fix rejects them.