-
Notifications
You must be signed in to change notification settings - Fork 35
Dma fixes #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Dma fixes #329
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -556,9 +556,10 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
|
|
||||||||||||||||||||
| #ifdef WOLFHSM_CFG_DMA | ||||||||||||||||||||
| case WH_MESSAGE_CERT_ACTION_ADDTRUSTED_DMA: { | ||||||||||||||||||||
| whMessageCert_AddTrustedDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_SimpleResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| whMessageCert_AddTrustedDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_SimpleResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| int cert_dma_pre_ok = 0; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (req_size != sizeof(req)) { | ||||||||||||||||||||
| /* Request is malformed */ | ||||||||||||||||||||
|
|
@@ -574,6 +575,9 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0}); | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| cert_dma_pre_ok = 1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| /* Process the add trusted action */ | ||||||||||||||||||||
|
|
@@ -586,9 +590,10 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| (void)WH_SERVER_NVM_UNLOCK(server); | ||||||||||||||||||||
| } /* WH_SERVER_NVM_LOCK() */ | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| /* Post-process client address */ | ||||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| /* Always call POST for successful PRE, regardless of operation | ||||||||||||||||||||
| * result */ | ||||||||||||||||||||
| if (cert_dma_pre_ok) { | ||||||||||||||||||||
| (void)wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+593
to
599
|
||||||||||||||||||||
|
|
@@ -600,11 +605,12 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| }; break; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case WH_MESSAGE_CERT_ACTION_READTRUSTED_DMA: { | ||||||||||||||||||||
| whMessageCert_ReadTrustedDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_SimpleResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| whMessageCert_ReadTrustedDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_SimpleResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| uint32_t cert_len; | ||||||||||||||||||||
| whNvmMetadata meta; | ||||||||||||||||||||
| int cert_dma_pre_ok = 0; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (req_size != sizeof(req)) { | ||||||||||||||||||||
| /* Request is malformed */ | ||||||||||||||||||||
|
|
@@ -620,6 +626,9 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0}); | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| cert_dma_pre_ok = 1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| /* Check metadata to see if the certificate is non-exportable */ | ||||||||||||||||||||
|
|
@@ -641,10 +650,11 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| (void)WH_SERVER_NVM_UNLOCK(server); | ||||||||||||||||||||
| } /* WH_SERVER_NVM_LOCK() */ | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| /* Post-process client address */ | ||||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, cert_len, | ||||||||||||||||||||
| /* Always call POST for successful PRE, regardless of operation | ||||||||||||||||||||
| * result */ | ||||||||||||||||||||
| if (cert_dma_pre_ok) { | ||||||||||||||||||||
| (void)wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0}); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+653
to
659
|
||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -655,10 +665,11 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| }; break; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| case WH_MESSAGE_CERT_ACTION_VERIFY_DMA: { | ||||||||||||||||||||
| whMessageCert_VerifyDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_VerifyDmaResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| whKeyId keyId = WH_KEYID_ERASED; | ||||||||||||||||||||
| whMessageCert_VerifyDmaRequest req = {0}; | ||||||||||||||||||||
| whMessageCert_VerifyDmaResponse resp = {0}; | ||||||||||||||||||||
| void* cert_data = NULL; | ||||||||||||||||||||
| whKeyId keyId = WH_KEYID_ERASED; | ||||||||||||||||||||
| int cert_dma_pre_ok = 0; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (req_size != sizeof(req)) { | ||||||||||||||||||||
| /* Request is malformed */ | ||||||||||||||||||||
|
|
@@ -677,6 +688,9 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0}); | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| cert_dma_pre_ok = 1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| resp.rc = WH_SERVER_NVM_LOCK(server); | ||||||||||||||||||||
|
|
@@ -693,9 +707,10 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic, | |||||||||||||||||||
| (void)WH_SERVER_NVM_UNLOCK(server); | ||||||||||||||||||||
| } /* WH_SERVER_NVM_LOCK() */ | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (resp.rc == WH_ERROR_OK) { | ||||||||||||||||||||
| /* Post-process client address */ | ||||||||||||||||||||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| /* Always call POST for successful PRE, regardless of operation | ||||||||||||||||||||
| * result */ | ||||||||||||||||||||
| if (cert_dma_pre_ok) { | ||||||||||||||||||||
| (void)wh_Server_DmaProcessClientAddress( | ||||||||||||||||||||
| server, req.cert_addr, &cert_data, req.cert_len, | ||||||||||||||||||||
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | ||||||||||||||||||||
|
Comment on lines
+713
to
715
|
||||||||||||||||||||
| (void)wh_Server_DmaProcessClientAddress( | |
| server, req.cert_addr, &cert_data, req.cert_len, | |
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | |
| int post_rc = wh_Server_DmaProcessClientAddress( | |
| server, req.cert_addr, &cert_data, req.cert_len, | |
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | |
| if ((resp.rc == WH_ERROR_OK) && (post_rc != WH_ERROR_OK)) { | |
| resp.rc = post_rc; | |
| } |
Copilot
AI
Apr 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DMA READ_POST return code is discarded. If the verify action succeeds but POST fails, this path will still report success (resp.rc remains WH_ERROR_OK), potentially masking DMA/cache maintenance failures. Consider capturing the POST rc and surfacing it when rc/resp.rc is still WH_ERROR_OK (or logging it).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4784,11 +4784,17 @@ static int _HandleSha256Dma(whServerContext* ctx, uint16_t magic, int devId, | |
| res.dmaAddrStatus.badAddr = req.state; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context is | ||
| * copied back into client memory. */ | ||
| clientDevId = sha256->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha256->devId = devId; | ||
| /* Validate buffLen from untrusted context */ | ||
| if (sha256->buffLen >= WC_SHA256_BLOCK_SIZE) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context | ||
| * is copied back into client memory. */ | ||
| clientDevId = sha256->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha256->devId = devId; | ||
| } | ||
|
Comment on lines
+4787
to
+4797
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -4906,11 +4912,17 @@ static int _HandleSha224Dma(whServerContext* ctx, uint16_t magic, int devId, | |
| res.dmaAddrStatus.badAddr = req.state; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context is | ||
| * copied back into client memory. */ | ||
| clientDevId = sha224->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha224->devId = devId; | ||
| /* Validate buffLen from untrusted context */ | ||
| if (sha224->buffLen >= WC_SHA224_BLOCK_SIZE) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context | ||
| * is copied back into client memory. */ | ||
| clientDevId = sha224->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha224->devId = devId; | ||
| } | ||
|
Comment on lines
+4915
to
+4925
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -5028,11 +5040,17 @@ static int _HandleSha384Dma(whServerContext* ctx, uint16_t magic, int devId, | |
| res.dmaAddrStatus.badAddr = req.state; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context is | ||
| * copied back into client memory. */ | ||
| clientDevId = sha384->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha384->devId = devId; | ||
| /* Validate buffLen from untrusted context */ | ||
| if (sha384->buffLen >= WC_SHA384_BLOCK_SIZE) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context | ||
| * is copied back into client memory. */ | ||
| clientDevId = sha384->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha384->devId = devId; | ||
| } | ||
|
Comment on lines
+5043
to
+5053
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -5150,13 +5168,25 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, int devId, | |
| res.dmaAddrStatus.badAddr = req.state; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context is | ||
| * copied back into client memory. */ | ||
| clientDevId = sha512->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha512->devId = devId; | ||
| /* retrieve hash Type to handle 512, 512-224, or 512-256 */ | ||
| hashType = sha512->hashType; | ||
| /* Validate buffLen from untrusted context */ | ||
| if (sha512->buffLen >= WC_SHA512_BLOCK_SIZE) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| else { | ||
| /* Save the client devId to be restored later, when the context | ||
| * is copied back into client memory. */ | ||
| clientDevId = sha512->devId; | ||
| /* overwrite the devId to that of the server for local crypto */ | ||
| sha512->devId = devId; | ||
| /* retrieve hash Type to handle 512, 512-224, or 512-256 */ | ||
| hashType = sha512->hashType; | ||
| /* Validate hashType from untrusted context */ | ||
| if (hashType != WC_HASH_TYPE_SHA512 && | ||
| hashType != WC_HASH_TYPE_SHA512_224 && | ||
| hashType != WC_HASH_TYPE_SHA512_256) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
|
Comment on lines
+5176
to
+5188
|
||
| } | ||
|
Comment on lines
+5171
to
+5189
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,15 +131,7 @@ int whServerDma_CopyFromClient(struct whServerContext_t* server, | |
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| /* Check the server address against the allow list */ | ||
| rc = wh_Dma_CheckMemOperAgainstAllowList(server->dma.dmaAddrAllowList, | ||
| WH_DMA_OPER_CLIENT_READ_PRE, | ||
| serverPtr, len); | ||
| if (rc != WH_ERROR_OK) { | ||
| return rc; | ||
| } | ||
|
|
||
| /* Process the client address pre-read */ | ||
| /* Process the client address pre-read (includes allow list check) */ | ||
| rc = wh_Server_DmaProcessClientAddress( | ||
| server, clientAddr, &transformedAddr, len, WH_DMA_OPER_CLIENT_READ_PRE, | ||
| flags); | ||
|
Comment on lines
+134
to
137
|
||
|
|
@@ -185,15 +177,7 @@ int whServerDma_CopyToClient(struct whServerContext_t* server, | |
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| /* Check the server address against the allow list */ | ||
| rc = wh_Dma_CheckMemOperAgainstAllowList(server->dma.dmaAddrAllowList, | ||
| WH_DMA_OPER_CLIENT_WRITE_PRE, | ||
| serverPtr, len); | ||
| if (rc != WH_ERROR_OK) { | ||
| return rc; | ||
| } | ||
|
|
||
| /* Process the client address pre-write */ | ||
| /* Process the client address pre-write (includes allow list check) */ | ||
| rc = wh_Server_DmaProcessClientAddress(server, clientAddr, &transformedAddr, | ||
| len, WH_DMA_OPER_CLIENT_WRITE_PRE, | ||
| flags); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -349,6 +349,8 @@ int wh_Server_HandleNvmRequest(whServerContext* server, | |
| whMessageNvm_SimpleResponse resp = {0}; | ||
| void* metadata = NULL; | ||
| void* data = NULL; | ||
| int metadata_dma_pre_ok = 0; | ||
| int data_dma_pre_ok = 0; | ||
|
|
||
| if (req_size != sizeof(req)) { | ||
| /* Request is malformed */ | ||
|
|
@@ -363,11 +365,17 @@ int wh_Server_HandleNvmRequest(whServerContext* server, | |
| resp.rc = wh_Server_DmaProcessClientAddress( | ||
| server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata), | ||
| WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0}); | ||
| if (resp.rc == 0) { | ||
| metadata_dma_pre_ok = 1; | ||
| } | ||
|
Comment on lines
365
to
+370
|
||
| } | ||
| if (resp.rc == 0) { | ||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||
| server, req.data_hostaddr, &data, req.data_len, | ||
| WH_DMA_OPER_CLIENT_READ_PRE, (whServerDmaFlags){0}); | ||
| if (resp.rc == 0) { | ||
| data_dma_pre_ok = 1; | ||
| } | ||
| } | ||
| if (resp.rc == 0) { | ||
| rc = WH_SERVER_NVM_LOCK(server); | ||
|
|
@@ -381,14 +389,15 @@ int wh_Server_HandleNvmRequest(whServerContext* server, | |
| } /* WH_SERVER_NVM_LOCK() */ | ||
| resp.rc = rc; | ||
| } | ||
| if (resp.rc == 0) { | ||
| /* perform platform-specific host address processing */ | ||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||
| /* Always call POST for successful PREs, regardless of operation | ||
| * result */ | ||
| if (metadata_dma_pre_ok) { | ||
| (void)wh_Server_DmaProcessClientAddress( | ||
| server, req.metadata_hostaddr, &metadata, sizeof(whNvmMetadata), | ||
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | ||
| } | ||
| if (resp.rc == 0) { | ||
| resp.rc = wh_Server_DmaProcessClientAddress( | ||
| if (data_dma_pre_ok) { | ||
| (void)wh_Server_DmaProcessClientAddress( | ||
| server, req.data_hostaddr, &data, req.data_len, | ||
| WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0}); | ||
| } | ||
|
Comment on lines
+392
to
403
|
||
|
|
@@ -405,6 +414,7 @@ int wh_Server_HandleNvmRequest(whServerContext* server, | |
| whNvmMetadata meta = {0}; | ||
| whNvmSize read_len = 0; | ||
| void* data = NULL; | ||
| int data_dma_pre_ok = 0; | ||
|
|
||
| if (req_size != sizeof(req)) { | ||
| /* Request is malformed */ | ||
|
|
@@ -441,15 +451,19 @@ int wh_Server_HandleNvmRequest(whServerContext* server, | |
| rc = wh_Server_DmaProcessClientAddress( | ||
| server, req.data_hostaddr, &data, req.data_len, | ||
| WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0}); | ||
| if (rc == 0) { | ||
| data_dma_pre_ok = 1; | ||
| } | ||
| } | ||
| if (rc == 0) { | ||
| /* Process the Read action */ | ||
| rc = wh_Nvm_ReadChecked(server->nvm, req.id, req.offset, | ||
| read_len, (uint8_t*)data); | ||
| } | ||
| if (rc == 0) { | ||
| /* perform platform-specific host address processing */ | ||
| rc = wh_Server_DmaProcessClientAddress( | ||
| /* Always call POST for successful PRE, regardless of read | ||
| * result */ | ||
| if (data_dma_pre_ok) { | ||
| (void)wh_Server_DmaProcessClientAddress( | ||
| server, req.data_hostaddr, &data, req.data_len, | ||
| WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0}); | ||
| } | ||
|
Comment on lines
+463
to
469
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR also changes the DMA allowlist contract from 'NULL allowlist allows all' to 'NULL allowlist denies all' (fail-closed). That’s a significant behavioral/API change beyond the described PRE/POST cleanup adjustments; consider explicitly calling this out in the PR description (and any release notes/changelog if applicable) so downstream users aren’t surprised by the new default-deny behavior.