Skip to content

Commit aca7408

Browse files
committed
Fix ML-DSA context buffer: replace heap pointer with fixed-size array
Replace the dynamically-allocated `byte* ctx` pointer in WP11_MldsaParams with an inline `byte ctx[256]` array. PKCS#11 v3.2 (§2.3.12) caps the ML-DSA context length at 255 bytes, so heap allocation is unnecessary and introduced several memory-management hazards: - ctx was freed at the end of WP11_Mldsa_Sign/Verify before session teardown, leaving a dangling pointer if the session was reused - the cleanup in wp11_Session_Final checked the wrong mechanism set, meaning it could free ctx a second time - WP11_Session_SetMldsaParams freed ctx before re-initialising, which was safe only if the pointer was always valid (it wasn't on first call) Embedding the buffer in the struct eliminates all manual lifetime tracking.
1 parent 13dc13b commit aca7408

1 file changed

Lines changed: 1 addition & 29 deletions

File tree

src/internal.c

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ typedef struct WP11_PssParams {
356356
typedef struct WP11_MldsaParams {
357357
enum wc_HashType preHashType;
358358
word32 hedgeType;
359-
byte* ctx;
359+
byte ctx[256];
360360
byte ctxSz;
361361
} WP11_MldsaParams;
362362
#endif
@@ -911,14 +911,6 @@ static void wp11_Session_Final(WP11_Session* session)
911911
session->params.oaep.label = NULL;
912912
}
913913
#endif
914-
#ifdef WOLFPKCS11_MLDSA
915-
if ((session->mechanism == CKM_ML_DSA ||
916-
session->mechanism == CKM_HASH_ML_DSA) &&
917-
session->params.mldsa.ctx != NULL) {
918-
XFREE(session->params.mldsa.ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
919-
session->params.mldsa.ctx = NULL;
920-
}
921-
#endif
922914
#ifndef NO_AES
923915
#ifdef HAVE_AES_CBC
924916
if ((session->mechanism == CKM_AES_CBC ||
@@ -7849,7 +7841,6 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78497841
int ret = 0;
78507842
WP11_MldsaParams* mldsa = &session->params.mldsa;
78517843

7852-
XFREE(mldsa->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
78537844
XMEMSET(mldsa, 0, sizeof(*mldsa));
78547845

78557846
if (params != NULL) {
@@ -7863,19 +7854,12 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78637854
if (ctx->ulContextLen > 255) {
78647855
ret = BAD_FUNC_ARG;
78657856
}
7866-
if (ret == 0) {
7867-
mldsa->ctx = (byte*)XMALLOC(ctx->ulContextLen, NULL,
7868-
DYNAMIC_TYPE_TMP_BUFFER);
7869-
if (mldsa->ctx == NULL)
7870-
ret = MEMORY_E;
7871-
}
78727857
if (ret == 0) {
78737858
XMEMCPY(mldsa->ctx, ctx->pContext, ctx->ulContextLen);
78747859
mldsa->ctxSz = ctx->ulContextLen;
78757860
}
78767861
}
78777862
else {
7878-
mldsa->ctx = NULL;
78797863
mldsa->ctxSz = 0;
78807864
}
78817865

@@ -7892,19 +7876,12 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78927876
if (ctx->ulContextLen > 255) {
78937877
ret = BAD_FUNC_ARG;
78947878
}
7895-
if (ret == 0) {
7896-
mldsa->ctx = (byte*)XMALLOC(ctx->ulContextLen, NULL,
7897-
DYNAMIC_TYPE_TMP_BUFFER);
7898-
if (mldsa->ctx == NULL)
7899-
ret = MEMORY_E;
7900-
}
79017879
if (ret == 0) {
79027880
XMEMCPY(mldsa->ctx, ctx->pContext, ctx->ulContextLen);
79037881
mldsa->ctxSz = ctx->ulContextLen;
79047882
}
79057883
}
79067884
else {
7907-
mldsa->ctx = NULL;
79087885
mldsa->ctxSz = 0;
79097886
}
79107887

@@ -7921,7 +7898,6 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
79217898
else {
79227899
mldsa->preHashType = WC_HASH_TYPE_NONE;
79237900
mldsa->hedgeType = CKH_HEDGE_PREFERRED;
7924-
mldsa->ctx = NULL;
79257901
mldsa->ctxSz = 0;
79267902
}
79277903

@@ -12779,8 +12755,6 @@ int WP11_Mldsa_Sign(unsigned char* data, word32 dataLen, unsigned char* sig,
1277912755
Rng_Free(&rng);
1278012756
}
1278112757

12782-
XFREE(params->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
12783-
params->ctx = NULL;
1278412758
params->ctxSz = 0;
1278512759

1278612760
if (priv->onToken)
@@ -12829,8 +12803,6 @@ int WP11_Mldsa_Verify(unsigned char* sig, word32 sigLen, unsigned char* data,
1282912803
}
1283012804
}
1283112805

12832-
XFREE(params->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
12833-
params->ctx = NULL;
1283412806
params->ctxSz = 0;
1283512807

1283612808
if (pub->onToken)

0 commit comments

Comments
 (0)