Skip to content

Commit a8a4b14

Browse files
Reject NULL metadata in checked NVM add path
1 parent 62eea4e commit a8a4b14

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/wh_nvm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ int wh_Nvm_AddObjectChecked(whNvmContext* context, const whNvmMetadata* meta,
275275
int ret;
276276
whNvmMetadata sanitized;
277277

278+
if (meta == NULL) {
279+
return WH_ERROR_BADARGS;
280+
}
281+
278282
ret = wh_Nvm_CheckPolicy(context, WH_NVM_OP_ADD, meta->id, NULL);
279283
if (ret != WH_ERROR_OK && ret != WH_ERROR_NOTFOUND) {
280284
return ret;

src/wh_server_nvm.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@ int wh_Server_HandleNvmRequest(whServerContext* server,
394394
}
395395
}
396396
if (resp.rc == 0) {
397+
/* A permit-all DMA config passes a zero metadata address through
398+
* untouched, so reject it before it reaches the NVM layer. */
399+
if (metadata == NULL) {
400+
resp.rc = WH_ERROR_BADARGS;
401+
}
402+
}
403+
if (resp.rc == 0) {
397404
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && \
398405
(defined(WOLFSSL_HAVE_LMS) || defined(WOLFSSL_HAVE_XMSS))
399406
/* Block direct NVM import of stateful (LMS/XMSS) private key state;

test-refactor/client-server/wh_test_nvm_ops.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,44 @@ static const WhNvmTestObjectOps g_dmaTestOps = {
457457
};
458458

459459

460+
/* A zero DMA address passes through the permit-all DMA config untouched,
461+
* so the server must reject it, not dereference it. */
462+
static int _whTest_NvmDmaNullAddrs(whClientContext* ctx)
463+
{
464+
whNvmMetadata meta = {0};
465+
int32_t server_rc = 0;
466+
uint32_t client_id = 0;
467+
uint32_t server_id = 0;
468+
469+
WH_TEST_RETURN_ON_FAIL(wh_Client_NvmInit(
470+
ctx, &server_rc, &client_id, &server_id));
471+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
472+
473+
/* Zero metadata address */
474+
WH_TEST_RETURN_ON_FAIL(wh_Client_NvmAddObjectDma(
475+
ctx, NULL, 0, NULL, &server_rc));
476+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADARGS);
477+
478+
/* Valid metadata, but a zero data address with a non-zero length.
479+
* The NVM backend rejects this rather than reading through it. */
480+
meta.id = NVM_TEST_DMA_ID_BASE;
481+
meta.access = WH_NVM_ACCESS_ANY;
482+
meta.flags = WH_NVM_FLAGS_NONE;
483+
WH_TEST_RETURN_ON_FAIL(wh_Client_NvmAddObjectDma(
484+
ctx, &meta, sizeof(meta), NULL, &server_rc));
485+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADARGS);
486+
487+
return WH_ERROR_OK;
488+
}
489+
490+
460491
int whTest_NvmDma(whClientContext* ctx)
461492
{
462-
return _runNvmObjectTest(ctx, &g_dmaTestOps, NVM_TEST_DMA_ID_BASE);
493+
WH_TEST_RETURN_ON_FAIL(
494+
_runNvmObjectTest(ctx, &g_dmaTestOps, NVM_TEST_DMA_ID_BASE));
495+
WH_TEST_RETURN_ON_FAIL(_whTest_NvmDmaNullAddrs(ctx));
496+
497+
return WH_ERROR_OK;
463498
}
464499

465500
#endif /* WOLFHSM_CFG_DMA */

0 commit comments

Comments
 (0)