Skip to content

Commit acf4352

Browse files
committed
Harden DMA security: default-deny NULL allowlist and validate SHA context fields from untrusted client memory
1 parent bc29749 commit acf4352

3 files changed

Lines changed: 55 additions & 25 deletions

File tree

src/wh_dma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ static int _checkMemOperAgainstAllowList(const whDmaAddrAllowList* allowlist,
9999
return rc;
100100
}
101101

102-
/* If no allowlist is registered, anything goes */
102+
/* If no allowlist is registered, deny all operations (fail-closed) */
103103
if (allowlist == NULL) {
104-
return WH_ERROR_OK;
104+
return WH_ERROR_ACCESS;
105105
}
106106

107107
/* If a read/write operation is requested, check the transformed address

src/wh_server_crypto.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4784,11 +4784,17 @@ static int _HandleSha256Dma(whServerContext* ctx, uint16_t magic, int devId,
47844784
res.dmaAddrStatus.badAddr = req.state;
47854785
}
47864786
else {
4787-
/* Save the client devId to be restored later, when the context is
4788-
* copied back into client memory. */
4789-
clientDevId = sha256->devId;
4790-
/* overwrite the devId to that of the server for local crypto */
4791-
sha256->devId = devId;
4787+
/* Validate buffLen from untrusted context */
4788+
if (sha256->buffLen > WC_SHA256_BLOCK_SIZE) {
4789+
ret = WH_ERROR_BADARGS;
4790+
}
4791+
else {
4792+
/* Save the client devId to be restored later, when the context
4793+
* is copied back into client memory. */
4794+
clientDevId = sha256->devId;
4795+
/* overwrite the devId to that of the server for local crypto */
4796+
sha256->devId = devId;
4797+
}
47924798
}
47934799
}
47944800

@@ -4906,11 +4912,17 @@ static int _HandleSha224Dma(whServerContext* ctx, uint16_t magic, int devId,
49064912
res.dmaAddrStatus.badAddr = req.state;
49074913
}
49084914
else {
4909-
/* Save the client devId to be restored later, when the context is
4910-
* copied back into client memory. */
4911-
clientDevId = sha224->devId;
4912-
/* overwrite the devId to that of the server for local crypto */
4913-
sha224->devId = devId;
4915+
/* Validate buffLen from untrusted context */
4916+
if (sha224->buffLen > WC_SHA224_BLOCK_SIZE) {
4917+
ret = WH_ERROR_BADARGS;
4918+
}
4919+
else {
4920+
/* Save the client devId to be restored later, when the context
4921+
* is copied back into client memory. */
4922+
clientDevId = sha224->devId;
4923+
/* overwrite the devId to that of the server for local crypto */
4924+
sha224->devId = devId;
4925+
}
49144926
}
49154927
}
49164928

@@ -5028,11 +5040,17 @@ static int _HandleSha384Dma(whServerContext* ctx, uint16_t magic, int devId,
50285040
res.dmaAddrStatus.badAddr = req.state;
50295041
}
50305042
else {
5031-
/* Save the client devId to be restored later, when the context is
5032-
* copied back into client memory. */
5033-
clientDevId = sha384->devId;
5034-
/* overwrite the devId to that of the server for local crypto */
5035-
sha384->devId = devId;
5043+
/* Validate buffLen from untrusted context */
5044+
if (sha384->buffLen > WC_SHA384_BLOCK_SIZE) {
5045+
ret = WH_ERROR_BADARGS;
5046+
}
5047+
else {
5048+
/* Save the client devId to be restored later, when the context
5049+
* is copied back into client memory. */
5050+
clientDevId = sha384->devId;
5051+
/* overwrite the devId to that of the server for local crypto */
5052+
sha384->devId = devId;
5053+
}
50365054
}
50375055
}
50385056

@@ -5150,13 +5168,25 @@ static int _HandleSha512Dma(whServerContext* ctx, uint16_t magic, int devId,
51505168
res.dmaAddrStatus.badAddr = req.state;
51515169
}
51525170
else {
5153-
/* Save the client devId to be restored later, when the context is
5154-
* copied back into client memory. */
5155-
clientDevId = sha512->devId;
5156-
/* overwrite the devId to that of the server for local crypto */
5157-
sha512->devId = devId;
5158-
/* retrieve hash Type to handle 512, 512-224, or 512-256 */
5159-
hashType = sha512->hashType;
5171+
/* Validate buffLen from untrusted context */
5172+
if (sha512->buffLen > WC_SHA512_BLOCK_SIZE) {
5173+
ret = WH_ERROR_BADARGS;
5174+
}
5175+
else {
5176+
/* Save the client devId to be restored later, when the context
5177+
* is copied back into client memory. */
5178+
clientDevId = sha512->devId;
5179+
/* overwrite the devId to that of the server for local crypto */
5180+
sha512->devId = devId;
5181+
/* retrieve hash Type to handle 512, 512-224, or 512-256 */
5182+
hashType = sha512->hashType;
5183+
/* Validate hashType from untrusted context */
5184+
if (hashType != WC_HASH_TYPE_SHA512 &&
5185+
hashType != WC_HASH_TYPE_SHA512_224 &&
5186+
hashType != WC_HASH_TYPE_SHA512_256) {
5187+
ret = WH_ERROR_BADARGS;
5188+
}
5189+
}
51605190
}
51615191
}
51625192

wolfhsm/wh_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int wh_Server_DmaRegisterAllowList(struct whServerContext_t* server,
372372
* This function verifies whether a specified DMA memory operation is permitted
373373
* by checking the operation type and the address range against the server's
374374
* registered allowlist. If no allowlist is registered, the operation is
375-
* allowed.
375+
* denied (fail-closed).
376376
*
377377
* @param[in] server Pointer to the server context.
378378
* @param[in] oper The DMA operation type (e.g., read or write).

0 commit comments

Comments
 (0)