Check RNG response length before copying inline payload#463
Open
yosuke-wolfssl wants to merge 1 commit into
Open
Check RNG response length before copying inline payload#463yosuke-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
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.
yosuke-wolfssl
force-pushed
the
fix/f_5460
branch
from
July 22, 2026 01:31
870931f to
264e472
Compare
yosuke-wolfssl
marked this pull request as ready for review
July 22, 2026 01:31
Frauschi
approved these changes
Jul 23, 2026
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
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.
Tests (
test-refactor/misc/wh_test_client_malformed_resp.c, new)Misc-group test pairing a real client with a raw
whCommServerover the mem transport, so it can emit frames a real server never would.WH_ERROR_ABORTED, output untouchedWH_ERROR_ABORTED, output untouchedWH_ERROR_ABORTEDWH_ERROR_ACCESSWH_ERROR_ACCESSreaches the callerDMA cases are gated on
WOLFHSM_CFG_DMA. Registered intest-refactor/wh_test_list.c.Verification
make check: 38 passed, 25 skipped, 0 failed of 63.make check DMA=1: 42 passed, 21 skipped, 0 failed.cd test && make && make run) clean.res_len < hdr_sz ||makes the short-frame case — and only that case — fail, since the subtraction underflows and the copy proceeds.