Skip to content

Commit b699cf0

Browse files
authored
wh_client: fix: cap labelSz and add defensive NULL checks (#234)
Cap label size before copying and store the capped length in the request structure. Initialize request label length to zero and add a defensive null pointer check before copying in the DMA path. Fix two configuration output assignments returned by comm info. Rationale: prevent sending mismatched length fields and avoid potential null-dereference or buffer over-read. Signed-off-by: Badr Bacem KAABIA <[email protected]>
1 parent c907f44 commit b699cf0

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/wh_client.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ int wh_Client_CommInfoResponse(whClientContext* c,
339339
if (out_cfg_keycache_bufsize != NULL) {
340340
*out_cfg_keycache_bufsize = msg.cfg_server_keycache_bufsize;
341341
}
342-
if (out_cfg_keycache_count != NULL) {
342+
if (out_cfg_keycache_bigcount != NULL) {
343343
*out_cfg_keycache_bigcount = msg.cfg_server_keycache_bigcount;
344344
}
345-
if (out_cfg_keycache_bufsize != NULL) {
345+
if (out_cfg_keycache_bigbufsize != NULL) {
346346
*out_cfg_keycache_bigbufsize = msg.cfg_server_keycache_bigbufsize;
347347
}
348348
if (out_cfg_customcb_count != NULL) {
@@ -731,6 +731,7 @@ int wh_Client_KeyCacheRequest_ex(whClientContext* c, uint32_t flags,
731731
{
732732
whMessageKeystore_CacheRequest* req = NULL;
733733
uint8_t* packIn;
734+
uint16_t capSz;
734735

735736
if (c == NULL || in == NULL || inSz == 0 ||
736737
sizeof(*req) + inSz > WOLFHSM_CFG_COMM_DATA_LEN) {
@@ -751,14 +752,10 @@ int wh_Client_KeyCacheRequest_ex(whClientContext* c, uint32_t flags,
751752
req->labelSz = 0;
752753
}
753754
else {
754-
req->labelSz = labelSz;
755755
/* write label */
756-
if (labelSz > WH_NVM_LABEL_LEN) {
757-
memcpy(req->label, label, WH_NVM_LABEL_LEN);
758-
}
759-
else {
760-
memcpy(req->label, label, labelSz);
761-
}
756+
capSz = (labelSz > WH_NVM_LABEL_LEN) ? WH_NVM_LABEL_LEN : labelSz;
757+
req->labelSz = capSz;
758+
memcpy(req->label, label, capSz);
762759
}
763760

764761
/* write in */
@@ -1339,6 +1336,7 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13391336
int ret;
13401337
whMessageKeystore_CacheDmaRequest* req = NULL;
13411338
uintptr_t keyAddrPtr = 0;
1339+
uint16_t capSz = 0;
13421340

13431341
if (c == NULL || (labelSz > 0 && label == NULL)) {
13441342
return WH_ERROR_BADARGS;
@@ -1351,7 +1349,7 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13511349
memset(req, 0, sizeof(*req));
13521350
req->id = keyId;
13531351
req->flags = flags;
1354-
req->labelSz = labelSz;
1352+
req->labelSz = 0;
13551353

13561354
/* Set up DMA buffer info */
13571355
req->key.sz = keySz;
@@ -1361,11 +1359,10 @@ int wh_Client_KeyCacheDmaRequest(whClientContext* c, uint32_t flags,
13611359
req->key.addr = keyAddrPtr;
13621360

13631361
/* Copy label if provided, truncate if necessary */
1364-
if (labelSz > 0) {
1365-
if (labelSz > WH_NVM_LABEL_LEN) {
1366-
labelSz = WH_NVM_LABEL_LEN;
1367-
}
1368-
memcpy(req->label, label, labelSz);
1362+
if (labelSz > 0 && label != NULL) {
1363+
capSz = (labelSz > WH_NVM_LABEL_LEN) ? WH_NVM_LABEL_LEN : labelSz;
1364+
req->labelSz = capSz;
1365+
memcpy(req->label, label, capSz);
13691366
}
13701367

13711368
if (ret == WH_ERROR_OK) {

0 commit comments

Comments
 (0)