Skip to content

Bound SHE LoadKey auth key read to WH_SHE_KEY_SZ - #495

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

Bound SHE LoadKey auth key read to WH_SHE_KEY_SZ#495
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:mainfrom
yosuke-wolfssl:fix/f_7155

Conversation

@yosuke-wolfssl

@yosuke-wolfssl yosuke-wolfssl commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Problem

_LoadKey in the SHE extension read the key-update authorization key with the capacity set to the whole kdf input buffer (keySz = sizeof(kdfInput), 32) rather than the SHE key size (16). wh_Server_KeystoreReadKey reports the stored object's actual length, and the kdf then appends a 16-byte constant at that offset, so a slot holding more than WH_SHE_KEY_SZ bytes overruns the 32-byte stack buffer by up to 16 bytes on two memcpy calls and the subsequent _AesMp16 read.

The SHE and NVM id spaces overlap, so an authenticated client can plant an oversized object directly in a SHE slot with WH_MESSAGE_NVM_ACTION_ADDOBJECT and then reach the overflow through a normal CMD_LOAD_KEY. Requires WOLFHSM_CFG_SHE_EXTENSION.

The target-key read had a second, independent flaw: wh_Server_KeystoreReadKey returns WH_ERROR_NOSPACE before populating the caller's metadata, so an oversized target slot left meta zeroed. wh_She_Label2Meta then read all-zero flags, the WH_SHE_FLAG_WRITE_PROTECT bit appeared clear, and a write-protected slot was silently overwritten.

Fix (src/wh_server_she.c)

Both reads are pinned to WH_SHE_KEY_SZ and a slot of any other length is rejected, matching every other SHE handler.

Read Before After
Auth key capacity of the full kdf buffer WH_SHE_KEY_SZ; WH_ERROR_NOSPACE or size mismatch → WH_SHE_ERC_KEY_INVALID
Target key inherited capacity; meta unset on NOSPACE WH_SHE_KEY_SZ; rejected before the label is consulted, closing the write-protect bypass

wh_Client_ShePreProgramKey also rejects a keySz other than WH_SHE_KEY_SZ with WH_ERROR_BADARGS, so an ordinary client cannot create such a slot in the first place; the header documents the new precondition.

Closes 7155.

Tests (test-refactor/client-server/wh_test_she.c)

Three cases appended to whTest_She, driven only through wh_Client_*:

Case Asserts
Pre-program key size non-16-byte keys rejected with WH_ERROR_BADARGS, leaving no NVM object behind
Oversized auth key 32-byte object planted via wh_Client_NvmAddObject, used as the LoadKey AUTH id → WH_SHE_ERC_KEY_INVALID
Oversized target key same object planted at a write-protected TARGET slot → WH_SHE_ERC_KEY_INVALID

Verification

  • Build clean under -Werror -Wall -Wextra, C90.
  • Functional: 52 passed, 22 skipped, 0 failed of 74 (make SHE=1). ASan clean.
  • Negative controls: each hunk reverted individually fails only its own case. Without the auth-key bound, ASan reports a 16-byte stack-buffer-overflow past kdfInput in _LoadKey; without the target-key bound, wh_Client_SheLoadKey returns success against a write-protected slot.

@yosuke-wolfssl yosuke-wolfssl self-assigned this Jul 27, 2026
Copilot AI review requested due to automatic review settings July 27, 2026 04:39

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 the server-side SHE LOAD_KEY handler against oversized/undersized auth-key objects by bounding keystore reads to WH_SHE_KEY_SZ, ensuring consistent error mapping, and adding targeted regression tests in the refactored test suite.

Changes:

  • Fix _LoadKey to read the auth key with capacity WH_SHE_KEY_SZ and reject short reads / WH_ERROR_NOSPACE as WH_SHE_ERC_KEY_INVALID.
  • Pin the target-slot keystore read size to WH_SHE_KEY_SZ (instead of inheriting keySz from the auth-key read).
  • Add a new refactored server test that plants 32/8/16-byte auth-key objects and validates the handler’s return codes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/wh_server_she.c Bounds auth-key read size, normalizes error returns for invalid auth-key storage sizes, and pins the subsequent target-slot read size.
test-refactor/server/wh_test_she_loadkey.c Adds a regression test for LOAD_KEY auth-key length handling (oversized/undersized/valid).
test-refactor/wh_test_list.c Registers the new server test in the refactored test suite.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wh_server_she.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 #495

Scan targets checked: wolfhsm-core-bugs, wolfhsm-src

No new issues found in the changed files. ✅

@yosuke-wolfssl
yosuke-wolfssl force-pushed the fix/f_7155 branch 2 times, most recently from 6c75ef6 to 8b61798 Compare July 29, 2026 22:52
@bigbrett

bigbrett commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

@yosuke-wolfssl merge conflicts

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

Hi @bigbrett ,
I solved the merge conflict. Please check this as well.

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