Skip to content

Commit df6c7c3

Browse files
Fix PQC Memory leak issues.
Make fixes found by zOS. That could or will cause a memory leak. Signed-off-by: johnpeck-us-ibm <johnpeck@us.ibm.com>
1 parent 9759b2a commit df6c7c3

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

src/main/native/KEM.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2025
2+
* Copyright IBM Corp. 2025, 2026
33
*
44
* This code is free software; you can redistribute it and/or modify it
55
* under the terms provided by IBM in the LICENSE file that accompanied
@@ -67,7 +67,7 @@ Java_com_ibm_crypto_plus_provider_ock_NativeInterface_KEM_1encapsulate(
6767
free(wrappedKeyLocal);
6868
}
6969
if (genkeylocal != NULL) {
70-
free(wrappedKeyLocal);
70+
free(genkeylocal);
7171
}
7272
ICC_EVP_PKEY_CTX_free(ockCtx, evp_pk);
7373
throwOCKException(env, 0, "malloc failed");

src/main/native/MLKey.c

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2025
2+
* Copyright IBM Corp. 2025, 2026
33
*
44
* This code is free software; you can redistribute it and/or modify it
55
* under the terms provided by IBM in the LICENSE file that accompanied
@@ -373,49 +373,45 @@ Java_com_ibm_crypto_plus_provider_ock_NativeInterface_MLKEY_1createPrivateKey(
373373
return mlkeyId;
374374
}
375375

376-
keyBytesNative = (unsigned char *)((*env)->GetPrimitiveArrayCritical(
377-
env, privateKeyBytes, &isCopy));
378-
if (NULL == keyBytesNative) {
379-
throwOCKException(env, 0, "NULL from GetPrimitiveArrayCritical!");
380-
} else {
381-
pBytes = keyBytesNative;
382-
size = (*env)->GetArrayLength(env, privateKeyBytes);
383-
if (cipherName == NULL) {
376+
if (!(algoChars = (*env)->GetStringUTFChars(env, cipherName, NULL))) {
384377
#ifdef DEBUG_PQC_KEY_DETAIL
385-
if (debug) {
386-
gslogMessage("cipherName = NULL");
387-
}
388-
#endif
389-
(*env)->ReleasePrimitiveArrayCritical(env, privateKeyBytes,
390-
keyBytesNative, JNI_ABORT);
391-
return 0;
378+
if (debug) {
379+
gslogMessage("GetStringUTFChars failed %s", cipherName);
392380
}
393-
394-
if (!(algoChars = (*env)->GetStringUTFChars(env, cipherName, NULL))) {
395-
#ifdef DEBUG_PQC_KEY_DETAIL
396-
if (debug) {
397-
gslogMessage("GetStringUTFChars failed %s", cipherName);
398-
}
399381
#endif
400-
(*env)->ReleasePrimitiveArrayCritical(env, privateKeyBytes,
401-
keyBytesNative, JNI_ABORT);
402-
return 0;
403-
}
404-
nid = ICC_OBJ_txt2nid(ockCtx, algoChars);
382+
return 0;
383+
}
405384

406-
if (!nid) {
407-
throwOCKException(
408-
env, 0, "Algorithm not found."); /* Unsupported algorithm */
409-
} else {
410-
ockPKey =
411-
ICC_d2i_PrivateKey(ockCtx, nid, &ockPKey, &pBytes, (long)size);
385+
nid = ICC_OBJ_txt2nid(ockCtx, algoChars);
412386

413-
if (ockPKey == NULL) {
414-
ockCheckStatus(ockCtx);
415-
throwOCKException(env, 0, "ICC_d2i_PrivateKey failed");
387+
if (!nid) {
388+
throwOCKException(
389+
env, 0, "Algorithm not found."); /* Unsupported algorithm */
390+
} else {
391+
keyBytesNative = (unsigned char *)((*env)->GetPrimitiveArrayCritical(
392+
env, privateKeyBytes, &isCopy));
393+
394+
if (NULL != keyBytesNative) {
395+
pBytes = keyBytesNative;
396+
size = (*env)->GetArrayLength(env, privateKeyBytes);
397+
398+
if (cipherName != NULL) {
399+
ockPKey =
400+
ICC_d2i_PrivateKey(ockCtx, nid, &ockPKey, &pBytes, (long)size);
401+
402+
if (ockPKey == NULL) {
403+
ockCheckStatus(ockCtx);
404+
throwOCKException(env, 0, "ICC_d2i_PrivateKey failed");
405+
} else {
406+
mlkeyId = (jlong)((intptr_t)ockPKey);
407+
}
416408
} else {
417-
mlkeyId = (jlong)((intptr_t)ockPKey);
418-
}
409+
#ifdef DEBUG_PQC_KEY_DETAIL
410+
if (debug) {
411+
gslogMessage("cipherName = NULL");
412+
}
413+
#endif
414+
}
419415
}
420416
}
421417

@@ -653,7 +649,7 @@ Java_com_ibm_crypto_plus_provider_ock_NativeInterface_MLKEY_1getPublicKeyBytes(
653649
(*env)->DeleteLocalRef(env, keyBytes);
654650
}
655651

656-
return keyBytes;
652+
return retKeyBytes;
657653
}
658654

659655
//============================================================================

src/main/native/SignaturePQC.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Java_com_ibm_crypto_plus_provider_ock_NativeInterface_PQC_1SIGNATURE_1verify(
177177
env, data, &isCopy));
178178

179179
if (dataNative == NULL) {
180-
(*env)->ReleasePrimitiveArrayCritical(env, data, dataNative,
180+
(*env)->ReleasePrimitiveArrayCritical(env, sigBytes, sigBytesNative,
181181
JNI_ABORT);
182182
throwOCKException(env, 0, "GetPrimitiveArrayCritical failed");
183183
return verified;

0 commit comments

Comments
 (0)