Skip to content

Commit f09e80d

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 89c66b0 commit f09e80d

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 ||
@@ -7851,7 +7843,6 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78517843
int ret = 0;
78527844
WP11_MldsaParams* mldsa = &session->params.mldsa;
78537845

7854-
XFREE(mldsa->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
78557846
XMEMSET(mldsa, 0, sizeof(*mldsa));
78567847

78577848
if (params != NULL) {
@@ -7865,19 +7856,12 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78657856
if (ctx->ulContextLen > 255) {
78667857
ret = BAD_FUNC_ARG;
78677858
}
7868-
if (ret == 0) {
7869-
mldsa->ctx = (byte*)XMALLOC(ctx->ulContextLen, NULL,
7870-
DYNAMIC_TYPE_TMP_BUFFER);
7871-
if (mldsa->ctx == NULL)
7872-
ret = MEMORY_E;
7873-
}
78747859
if (ret == 0) {
78757860
XMEMCPY(mldsa->ctx, ctx->pContext, ctx->ulContextLen);
78767861
mldsa->ctxSz = ctx->ulContextLen;
78777862
}
78787863
}
78797864
else {
7880-
mldsa->ctx = NULL;
78817865
mldsa->ctxSz = 0;
78827866
}
78837867

@@ -7894,19 +7878,12 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
78947878
if (ctx->ulContextLen > 255) {
78957879
ret = BAD_FUNC_ARG;
78967880
}
7897-
if (ret == 0) {
7898-
mldsa->ctx = (byte*)XMALLOC(ctx->ulContextLen, NULL,
7899-
DYNAMIC_TYPE_TMP_BUFFER);
7900-
if (mldsa->ctx == NULL)
7901-
ret = MEMORY_E;
7902-
}
79037881
if (ret == 0) {
79047882
XMEMCPY(mldsa->ctx, ctx->pContext, ctx->ulContextLen);
79057883
mldsa->ctxSz = ctx->ulContextLen;
79067884
}
79077885
}
79087886
else {
7909-
mldsa->ctx = NULL;
79107887
mldsa->ctxSz = 0;
79117888
}
79127889

@@ -7923,7 +7900,6 @@ int WP11_Session_SetMldsaParams(WP11_Session* session, CK_VOID_PTR params,
79237900
else {
79247901
mldsa->preHashType = WC_HASH_TYPE_NONE;
79257902
mldsa->hedgeType = CKH_HEDGE_PREFERRED;
7926-
mldsa->ctx = NULL;
79277903
mldsa->ctxSz = 0;
79287904
}
79297905

@@ -12798,8 +12774,6 @@ int WP11_Mldsa_Sign(unsigned char* data, word32 dataLen, unsigned char* sig,
1279812774
Rng_Free(&rng);
1279912775
}
1280012776

12801-
XFREE(params->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
12802-
params->ctx = NULL;
1280312777
params->ctxSz = 0;
1280412778

1280512779
if (priv->onToken)
@@ -12848,8 +12822,6 @@ int WP11_Mldsa_Verify(unsigned char* sig, word32 sigLen, unsigned char* data,
1284812822
}
1284912823
}
1285012824

12851-
XFREE(params->ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
12852-
params->ctx = NULL;
1285312825
params->ctxSz = 0;
1285412826

1285512827
if (pub->onToken)

0 commit comments

Comments
 (0)