Skip to content

Commit 354c660

Browse files
committedJul 20, 2023
8307185: pkcs11 native libraries make JNI calls into java code while holding GC lock
Reviewed-by: alanb, valeriep
1 parent bae2247 commit 354c660

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed
 

‎src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_crypt.c

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
*/
44

55
/* Copyright (c) 2002 Graz University of Technology. All rights reserved.
@@ -155,7 +155,10 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Encrypt
155155
} else {
156156
outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);
157157
if (outBufP == NULL) {
158-
goto cleanup;
158+
if (directIn == 0) {
159+
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
160+
}
161+
return 0;
159162
}
160163
}
161164

@@ -166,15 +169,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Encrypt
166169
(CK_BYTE_PTR)(outBufP + jOutOfs),
167170
&ckEncryptedLen);
168171

169-
ckAssertReturnValueOK(env, rv);
170-
171-
cleanup:
172-
if (directIn == 0 && inBufP != NULL) {
172+
if (directIn == 0) {
173173
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
174174
}
175-
if (directOut == 0 && outBufP != NULL) {
175+
if (directOut == 0) {
176176
(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);
177177
}
178+
ckAssertReturnValueOK(env, rv);
178179
return ckEncryptedLen;
179180
}
180181
#endif
@@ -221,7 +222,10 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate
221222
} else {
222223
outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);
223224
if (outBufP == NULL) {
224-
goto cleanup;
225+
if (directIn == 0) {
226+
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
227+
}
228+
return 0;
225229
}
226230
}
227231

@@ -232,15 +236,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1EncryptUpdate
232236
(CK_BYTE_PTR)(outBufP + jOutOfs),
233237
&ckEncryptedPartLen);
234238

235-
ckAssertReturnValueOK(env, rv);
236-
237-
cleanup:
238-
if (directIn == 0 && inBufP != NULL) {
239+
if (directIn == 0) {
239240
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
240241
}
241-
if (directOut == 0 && outBufP != NULL) {
242+
if (directOut == 0) {
242243
(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);
243244
}
245+
ckAssertReturnValueOK(env, rv);
244246
return ckEncryptedPartLen;
245247
}
246248
#endif
@@ -391,7 +393,10 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Decrypt
391393
} else {
392394
outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);
393395
if (outBufP == NULL) {
394-
goto cleanup;
396+
if (directIn == 0) {
397+
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
398+
}
399+
return 0;
395400
}
396401
}
397402
ckOutLen = jOutLen;
@@ -401,15 +406,13 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1Decrypt
401406
(CK_BYTE_PTR)(outBufP + jOutOfs),
402407
&ckOutLen);
403408

404-
ckAssertReturnValueOK(env, rv);
405-
406-
cleanup:
407-
if (directIn == 0 && inBufP != NULL) {
409+
if (directIn == 0) {
408410
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
409411
}
410-
if (directOut == 0 && outBufP != NULL) {
412+
if (directOut == 0) {
411413
(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);
412414
}
415+
ckAssertReturnValueOK(env, rv);
413416
return ckOutLen;
414417
}
415418
#endif
@@ -456,7 +459,10 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate
456459
} else {
457460
outBufP = (*env)->GetPrimitiveArrayCritical(env, jOut, NULL);
458461
if (outBufP == NULL) {
459-
goto cleanup;
462+
if (directIn == 0) {
463+
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
464+
}
465+
return 0;
460466
}
461467
}
462468

@@ -465,15 +471,14 @@ Java_sun_security_pkcs11_wrapper_PKCS11_C_1DecryptUpdate
465471
(CK_BYTE_PTR)(inBufP + jInOfs), jInLen,
466472
(CK_BYTE_PTR)(outBufP + jOutOfs),
467473
&ckDecryptedPartLen);
468-
ckAssertReturnValueOK(env, rv);
469474

470-
cleanup:
471-
if (directIn == 0 && inBufP != NULL) {
475+
if (directIn == 0) {
472476
(*env)->ReleasePrimitiveArrayCritical(env, jIn, inBufP, JNI_ABORT);
473477
}
474-
if (directOut == 0 && outBufP != NULL) {
478+
if (directOut == 0) {
475479
(*env)->ReleasePrimitiveArrayCritical(env, jOut, outBufP, 0);
476480
}
481+
ckAssertReturnValueOK(env, rv);
477482
return ckDecryptedPartLen;
478483
}
479484

‎test/jdk/sun/security/pkcs11/Cipher/TestGCMKeyAndIvCheck.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8080462 8229243
26+
* @bug 8080462 8229243 8307185
2727
* @library /test/lib ..
2828
* @modules jdk.crypto.cryptoki
2929
* @run main TestGCMKeyAndIvCheck

0 commit comments

Comments
 (0)
Please sign in to comment.