Check RNG response length before copying inline payload - #463
Check RNG response length before copying inline payload#463yosuke-wolfssl wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Hardens the client-side RNG response handler against truncated/malformed server frames by validating that the received response length actually contains the claimed inline payload, preventing out-of-bounds reads of stale comm-buffer data.
Changes:
- Add a response-frame-length bound check in
wh_Client_RngGenerateResponsebefore copying inline RNG payload. - Add a new misc test that crafts malformed RNG responses over the mem transport to verify the abort behavior and that caller output remains untouched.
- Register the new test in the refactored test list.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/wh_client_crypto.c | Adds res_len vs header/payload size validation before inline RNG payload memcpy. |
| test-refactor/misc/wh_test_client_malformed_resp.c | New regression test that sends truncated RNG responses and asserts WH_ERROR_ABORTED + no buffer clobber. |
| test-refactor/wh_test_list.c | Registers whTest_ClientMalformedResp in misc tests. |
💡 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 #463
Scan targets checked: wolfhsm-crypto-bugs, wolfhsm-src
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Findings are non-blocking.
870931f to
264e472
Compare
There was a problem hiding this comment.
We don't need regression tests for every single argument validation scenario, especially when it requires setting up and tearing down an entire harness just to check something like this. We are going to start bogging down our test framework. Please remove this test for now.
264e472 to
117e8de
Compare
Problem
wh_Client_RngGenerateResponsevalidated the server-reported size against the inline cap (WH_MESSAGE_CRYPTO_RNG_MAX_INLINE_SZ) and the caller's buffer, but never againstres_len— the length of the frame actually received. A truncated or malformed OK response could setszpast what the frame delivered, making the clientmemcpystale comm-buffer bytes and hand them back as random data. Every other inline-copy handler inwh_client_crypto.c(AES-CTR/ECB/CBC/GCM, ECC, Curve25519, Ed25519, RSA, CMAC) already performed this check; RNG was the last one missing it.The DMA variant copies nothing inline, so it had no read exposure, but it equally never confirmed that a success reply was backed by a full response frame.
Fix (
src/wh_client_crypto.c)wh_Client_RngGenerateResponsederiveshdr_szfrom the generic response header pluswhMessageCrypto_RngResponseand rejects whenres_len < hdr_sz || res->sz > (res_len - hdr_sz). The first term also prevents the unsigned underflow in the second.wh_Client_RngGenerateDmaResponserejects a success reply whose frame is shorter than that same header pair. This is defense in depth, not an out-of-bounds fix: no field of the DMA response is read inline today, so the check pins the frame to the protocol contract for future readers. Error replies legitimately carry only the generic header and are unaffected, since the check sits behind the rc gate.Addressed by f_5460. This PR is now source-only.
Tests
The test added by the earlier revision has been removed. It was
test-refactor/misc/wh_test_client_malformed_resp.c(319 lines), which paired areal client with a raw
whCommServerso it could emit frames a real server neveremits. Per review, a test reachable only through a fake transport or a hand-built
packet is testing the harness, not the fix, so it and its
wh_test_list.centryare dropped. This also removes the add/add conflict with #473/#474/#475/#476,
which each created the same file.
No replacement test is added here: the guard rejects a frame shape the normal
wh_Client_*API cannot produce against a conforming server, so there is noclient-driven case that reaches it.
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.res_len < hdr_sz ||lets the short-frame case through, since the subtractionunderflows and the copy proceeds.