Skip to content

Commit e2e7b47

Browse files
committed
Update tests to register DMA allow lists for fail-closed default-deny behavior
1 parent acf4352 commit e2e7b47

3 files changed

Lines changed: 58 additions & 15 deletions

File tree

test/wh_test_dma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ static int whTest_DmaAllowListBasic(void)
6565
(void*)((uintptr_t)0x10000), 0);
6666
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_BADARGS);
6767

68-
WH_TEST_PRINT(" Testing NULL allowlist passthrough...\n");
68+
WH_TEST_PRINT(" Testing NULL allowlist denial (fail-closed)...\n");
6969
rc = wh_Dma_CheckMemOperAgainstAllowList(
7070
NULL, WH_DMA_OPER_CLIENT_READ_PRE,
7171
(void*)((uintptr_t)0x10000), 0x1000);
72-
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_OK);
72+
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_ACCESS);
7373

7474
return WH_ERROR_OK;
7575
}

test/wh_test_multiclient.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,32 @@ static int _testGlobalKeyDma(whClientContext* client1, whServerContext* server1,
442442
int ret;
443443
whKeyId keyId1 = WH_CLIENT_KEYID_MAKE_GLOBAL(DUMMY_KEYID_1);
444444
whKeyId keyId2 = WH_CLIENT_KEYID_MAKE_GLOBAL(DUMMY_KEYID_2);
445-
uint8_t keyData1[32] = "GlobalDmaCacheTestKey123456!";
446-
uint8_t keyData2[32] = "GlobalDmaExportTestKey12345!";
447-
uint8_t outBuf[32] = {0};
445+
/* Use static buffers so addresses are stable for DMA allow list */
446+
static uint8_t keyData1[32];
447+
static uint8_t keyData2[32];
448+
static uint8_t outBuf[32];
448449
uint8_t label[WH_NVM_LABEL_LEN];
449450
uint16_t labelSz = sizeof(label);
450451
uint16_t outSz;
451452

453+
static whServerDmaAddrAllowList dmaAllowList = {0};
454+
455+
memcpy(keyData1, "GlobalDmaCacheTestKey123456!", 28);
456+
memcpy(keyData2, "GlobalDmaExportTestKey12345!", 28);
457+
memset(outBuf, 0, sizeof(outBuf));
458+
459+
/* Register DMA allow list for both servers */
460+
dmaAllowList.readList[0].addr = keyData1;
461+
dmaAllowList.readList[0].size = sizeof(keyData1);
462+
dmaAllowList.readList[1].addr = keyData2;
463+
dmaAllowList.readList[1].size = sizeof(keyData2);
464+
dmaAllowList.writeList[0].addr = outBuf;
465+
dmaAllowList.writeList[0].size = sizeof(outBuf);
466+
WH_TEST_RETURN_ON_FAIL(
467+
wh_Server_DmaRegisterAllowList(server1, &dmaAllowList));
468+
WH_TEST_RETURN_ON_FAIL(
469+
wh_Server_DmaRegisterAllowList(server2, &dmaAllowList));
470+
452471
WH_TEST_PRINT("Test: DMA operations with global keys\n");
453472

454473
/* Part 1: Cache via DMA, export via regular */

test/wh_test_posix_threadsafe_stress.c

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ typedef struct {
501501
/* Per-client DMA buffers */
502502
uint8_t dmaKeyBuffer[KEY_DATA_SIZE];
503503
uint8_t dmaNvmBuffer[NVM_OBJECT_DATA_SIZE];
504+
whNvmMetadata dmaNvmMeta;
505+
/* DMA configuration */
506+
whServerDmaConfig dmaConfig;
507+
whServerDmaAddrAllowList dmaAllowList;
504508
#endif
505509
} ClientServerPair;
506510

@@ -654,6 +658,26 @@ static int initClientServerPair(StressTestContext* ctx, int pairIndex)
654658
pair->serverConfig.crypto = &pair->cryptoCtx;
655659
pair->serverConfig.devId = INVALID_DEVID;
656660

661+
#ifdef WOLFHSM_CFG_DMA
662+
/* Configure DMA allow list for this pair's buffers */
663+
memset(&pair->dmaAllowList, 0, sizeof(pair->dmaAllowList));
664+
pair->dmaAllowList.readList[0].addr = pair->dmaKeyBuffer;
665+
pair->dmaAllowList.readList[0].size = sizeof(pair->dmaKeyBuffer);
666+
pair->dmaAllowList.readList[1].addr = pair->dmaNvmBuffer;
667+
pair->dmaAllowList.readList[1].size = sizeof(pair->dmaNvmBuffer);
668+
pair->dmaAllowList.readList[2].addr = &pair->dmaNvmMeta;
669+
pair->dmaAllowList.readList[2].size = sizeof(pair->dmaNvmMeta);
670+
671+
pair->dmaAllowList.writeList[0].addr = pair->dmaKeyBuffer;
672+
pair->dmaAllowList.writeList[0].size = sizeof(pair->dmaKeyBuffer);
673+
pair->dmaAllowList.writeList[1].addr = pair->dmaNvmBuffer;
674+
pair->dmaAllowList.writeList[1].size = sizeof(pair->dmaNvmBuffer);
675+
676+
memset(&pair->dmaConfig, 0, sizeof(pair->dmaConfig));
677+
pair->dmaConfig.dmaAddrAllowList = &pair->dmaAllowList;
678+
pair->serverConfig.dmaConfig = &pair->dmaConfig;
679+
#endif
680+
657681
/* Initialize client in the main thread to avoid concurrent calls to
658682
* wolfCrypt_Init() and wc_CryptoCb_RegisterDevice() */
659683
rc = wh_Client_Init(&pair->client, &pair->clientConfig);
@@ -1191,24 +1215,24 @@ static int doKeyExportDma(ClientServerPair* pair, whKeyId keyId)
11911215

11921216
static int doNvmAddObjectDma(ClientServerPair* pair, whNvmId id, int iteration)
11931217
{
1194-
whNvmMetadata meta;
1195-
int32_t out_rc;
1196-
int rc;
1218+
int32_t out_rc;
1219+
int rc;
11971220

11981221
/* Fill DMA buffer with pattern */
11991222
memset(pair->dmaNvmBuffer, (uint8_t)(iteration & 0xFF),
12001223
sizeof(pair->dmaNvmBuffer));
12011224

1202-
/* Set up metadata */
1203-
memset(&meta, 0, sizeof(meta));
1204-
meta.id = id;
1205-
meta.access = WH_NVM_ACCESS_ANY;
1206-
meta.flags = WH_NVM_FLAGS_USAGE_ANY;
1207-
meta.len = sizeof(pair->dmaNvmBuffer);
1225+
/* Set up metadata in pair struct so address is in the DMA allow list */
1226+
memset(&pair->dmaNvmMeta, 0, sizeof(pair->dmaNvmMeta));
1227+
pair->dmaNvmMeta.id = id;
1228+
pair->dmaNvmMeta.access = WH_NVM_ACCESS_ANY;
1229+
pair->dmaNvmMeta.flags = WH_NVM_FLAGS_USAGE_ANY;
1230+
pair->dmaNvmMeta.len = sizeof(pair->dmaNvmBuffer);
12081231

12091232
/* Send DMA request */
12101233
rc = wh_Client_NvmAddObjectDmaRequest(
1211-
&pair->client, &meta, sizeof(pair->dmaNvmBuffer), pair->dmaNvmBuffer);
1234+
&pair->client, &pair->dmaNvmMeta, sizeof(pair->dmaNvmBuffer),
1235+
pair->dmaNvmBuffer);
12121236
if (rc != WH_ERROR_OK) {
12131237
return rc;
12141238
}

0 commit comments

Comments
 (0)