Validate SHE client response payload size before use#457
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the SHE client response parsing by validating that the received payload is large enough before overlaying/reading response structs, preventing stale-buffer data from being interpreted as valid output on truncated replies.
Changes:
- Added a shared
_CheckRespSz()guard and applied it at all SHE client receive sites to reject undersized responses before reading fixed fields. - For variable-length ECB/CBC crypt responses, additionally verify the declared
resp->szfits within the bytes actually received before copying. - Added a new
resp_sizeregression test that drives the client against a raw comm server and injects malformed/truncated responses.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/wh_client_she.c | Adds response-size gates before accessing fixed/variable-length response fields and bounds copies to received data. |
| test/wh_test_she.c | Adds a new test that validates client-side rejection of truncated/mis-sized SHE responses. |
💡 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 #457
Scan targets checked: wolfhsm-core-bugs, wolfhsm-src
No new issues found in the changed files. ✅
93c01f8 to
862ff9c
Compare
|
@yosuke-wolfssl, please resolve conflicts |
|
wh_client_crypto and wh_server_crypto have similar checking, but they are in this form: Can we preserve that style? Or is there a reason to change? |
862ff9c to
4c8155d
Compare
|
Hi @padelsbach , One SHE-specific note: I kept the _CheckRespSz call before the resp->rc read. Unlike the crypto path, these handlers must read rc first — error replies carry only the fixed struct, so folding everything into a single pre-rc check would let an error response's indeterminate sz mask the real return code. That leaves the dataSz < sizeof(*resp) disjunct as belt-and-suspenders here, which is consistent with how the crypto helpers carry it too. |
|
@yosuke-wolfssl, couple requests: First, can we remove Second, for the SheEncEcb/EncCbc/DecEcb/DecCbc checks, let's do something like this: Also, can you do a follow-up PR to apply this change to other callers of |
4c8155d to
648d1f9
Compare
|
Hi @padelsbach ,
Regarding to other callers, The real gap is src/wh_client.c (~14 sites, no open PR touches it); KeyExport / KeyExportPublic / KeyExportDma are the same variable-length shape as this bug. wh_client_crypto.c is partly covered by #463/#464/#465/#467, with same convention you suggested. |
|
LGTM, let's give @bigbrett a chance to look it over. |
Summary
The SHE client response helpers overlaid a response struct on the comm buffer and
read its fields without checking the received payload was large enough to contain
them. The comm layer validates magic, kind and sequence and bounds the payload to
the MTU, but nothing checked a payload against the size of the message being
parsed.
The comm client reuses one buffer for the outgoing request and the incoming
response, so a short reply leaves the tail holding the client's own request
bytes. A truncated Generate MAC response therefore returned success with a
MAC assembled partly from those stale bytes. Confirmed against the unfixed code:
the call returned
0with a final MAC byte of0xA5, the filler from theoutbound buffer.
The ECB and CBC helpers also validated the declared
szagainst the caller'soutput buffer rather than the data actually received, so a reply could claim more
payload than arrived. The wrappers cap this, but the response helpers are public
API, so calling one directly with a large buffer reads past the comm buffer.
Addressed by f_5466.
Changes
src/wh_client_she.creceived before copying.
dataSz;wh_Client_RecvResponseonly writes it on successand the new checks read it by value on failure paths.
test-refactor/misc/wh_test_she_client.c,test-refactor/wh_test_list.cwhTest_SheCliententry point covering client-side response sizevalidation: a fixed-size reply one byte short of its struct, each of the four
variable-length helpers at the exact bound, and a reply too short for its fixed
fields.
server, so a reply can carry the expected kind and sequence but a malformed
size. The client-server group supplies a live server, which gives no way to
inject a malformed reply.
Notes
The bound sums in a wider type instead of subtracting the fixed size from the
received size. The subtracting form is only safe given a precondition established
lines earlier and repeated four times; an edit that dropped it would wrap the
subtraction and silently make the bound a no-op. The minimum size check is kept
alongside it: it guards the
rcread, the only check that runs when the serverreports an error, while the bound guards the copy. The payload check stays after
the
rcbranch, since error replies carry only the fixed struct and checkingfirst would mask real SHE error codes.
Testing
Runs on stock POSIX hosts with the in-memory transport; no SHE hardware needed.
Both CI workflows already build
SHE=1 ASAN=1, so the new test runs there.-Werror -Wall -Wextra -std=c90; both suites pass.the checks do not reject valid traffic.
caught, as is removing a minimum size check.