Skip to content

Commit 0d745ae

Browse files
committed
8269034: AccessControlException for SunPKCS11 daemon threads
Reviewed-by: valeriep
1 parent d042029 commit 0d745ae

5 files changed

Lines changed: 78 additions & 42 deletions

File tree

src/java.base/share/classes/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
jdk.attach,
200200
jdk.charsets,
201201
jdk.compiler,
202+
jdk.crypto.cryptoki,
202203
jdk.incubator.vector,
203204
jdk.jfr,
204205
jdk.jshell,

src/java.base/share/lib/security/default.policy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ grant codeBase "jrt:/jdk.crypto.ec" {
128128
grant codeBase "jrt:/jdk.crypto.cryptoki" {
129129
permission java.lang.RuntimePermission
130130
"accessClassInPackage.com.sun.crypto.provider";
131+
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.misc";
131132
permission java.lang.RuntimePermission
132133
"accessClassInPackage.sun.security.*";
133134
permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import com.sun.crypto.provider.ChaCha20Poly1305Parameters;
4444

45+
import jdk.internal.misc.InnocuousThread;
4546
import sun.security.util.Debug;
4647
import sun.security.util.ResourcesMgr;
4748
import static sun.security.util.SecurityConstants.PROVIDER_VER;
@@ -907,15 +908,11 @@ private static void register(Descriptor d) {
907908
// background thread that periodically checks for token insertion
908909
// if no token is present. We need to do that in a separate thread because
909910
// the insertion check may block for quite a long time on some tokens.
910-
private static class TokenPoller extends Thread {
911+
private static class TokenPoller implements Runnable {
911912
private final SunPKCS11 provider;
912913
private volatile boolean enabled;
913914

914915
private TokenPoller(SunPKCS11 provider) {
915-
super((ThreadGroup)null, "Poller-" + provider.getName());
916-
setContextClassLoader(null);
917-
setDaemon(true);
918-
setPriority(Thread.MIN_PRIORITY);
919916
this.provider = provider;
920917
enabled = true;
921918
}
@@ -944,12 +941,20 @@ void disable() {
944941
}
945942

946943
// create the poller thread, if not already active
944+
@SuppressWarnings("removal")
947945
private void createPoller() {
948946
if (poller != null) {
949947
return;
950948
}
951949
poller = new TokenPoller(this);
952-
poller.start();
950+
Thread t = InnocuousThread.newSystemThread(
951+
"Poller-" + getName(),
952+
poller,
953+
Thread.MIN_PRIORITY);
954+
assert t.getContextClassLoader() == null;
955+
t.setDaemon(true);
956+
t.start();
957+
953958
}
954959

955960
// destroy the poller thread, if active
@@ -972,18 +977,11 @@ private boolean hasValidToken() {
972977
return (token != null) && token.isValid();
973978
}
974979

975-
private class NativeResourceCleaner extends Thread {
980+
private class NativeResourceCleaner implements Runnable {
976981
private long sleepMillis = config.getResourceCleanerShortInterval();
977982
private int count = 0;
978983
boolean keyRefFound, sessRefFound;
979984

980-
private NativeResourceCleaner() {
981-
super((ThreadGroup)null, "Cleanup-SunPKCS11");
982-
setContextClassLoader(null);
983-
setDaemon(true);
984-
setPriority(Thread.MIN_PRIORITY);
985-
}
986-
987985
/*
988986
* The cleaner.shortInterval and cleaner.longInterval properties
989987
* may be defined in the pkcs11 config file and are specified in milliseconds
@@ -1001,7 +999,7 @@ private NativeResourceCleaner() {
1001999
public void run() {
10021000
while (true) {
10031001
try {
1004-
sleep(sleepMillis);
1002+
Thread.sleep(sleepMillis);
10051003
} catch (InterruptedException ie) {
10061004
break;
10071005
}
@@ -1022,6 +1020,19 @@ public void run() {
10221020
}
10231021
}
10241022

1023+
// create the cleaner thread, if not already active
1024+
@SuppressWarnings("removal")
1025+
private void createCleaner() {
1026+
cleaner = new NativeResourceCleaner();
1027+
Thread t = InnocuousThread.newSystemThread(
1028+
"Cleanup-SunPKCS11",
1029+
cleaner,
1030+
Thread.MIN_PRIORITY);
1031+
assert t.getContextClassLoader() == null;
1032+
t.setDaemon(true);
1033+
t.start();
1034+
}
1035+
10251036
// destroy the token. Called if we detect that it has been removed
10261037
@SuppressWarnings("removal")
10271038
synchronized void uninitToken(Token token) {
@@ -1190,8 +1201,7 @@ public Object run() {
11901201

11911202
this.token = token;
11921203
if (cleaner == null) {
1193-
cleaner = new NativeResourceCleaner();
1194-
cleaner.start();
1204+
createCleaner();
11951205
}
11961206
}
11971207

test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -31,10 +31,9 @@
3131
import javax.security.auth.login.LoginException;
3232
import java.io.IOException;
3333
import java.lang.ref.WeakReference;
34-
import java.security.KeyStore;
35-
import java.security.Provider;
36-
import java.security.Security;
34+
import java.security.*;
3735
import java.util.Iterator;
36+
import java.util.PropertyPermission;
3837
import java.util.ServiceConfigurationError;
3938
import java.util.ServiceLoader;
4039

@@ -44,19 +43,28 @@ public class MultipleLogins {
4443
private static final String KS_TYPE = "PKCS11";
4544
private static final int NUM_PROVIDERS = 20;
4645
private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];
47-
46+
static final Policy DEFAULT_POLICY = Policy.getPolicy();
4847

4948
public static void main(String[] args) throws Exception {
49+
String nssConfig = PKCS11Test.getNssConfig();
50+
if (nssConfig == null) {
51+
// No test framework support yet. Ignore
52+
System.out.println("No NSS config found. Skipping.");
53+
return;
54+
}
55+
5056
for (int i =0; i < NUM_PROVIDERS; i++) {
51-
String nssConfig = PKCS11Test.getNssConfig();
52-
if (nssConfig == null) {
53-
// No test framework support yet. Ignore
54-
System.out.println("No NSS config found. Skipping.");
55-
return;
56-
}
57-
providers[i] =
58-
(SunPKCS11)PKCS11Test.newPKCS11Provider()
59-
.configure(nssConfig);
57+
// loop to set up test without security manger
58+
providers[i] = (SunPKCS11)PKCS11Test.newPKCS11Provider();
59+
}
60+
61+
if (args.length > 0) {
62+
Policy.setPolicy(new SimplePolicy());
63+
System.setSecurityManager(new SecurityManager());
64+
}
65+
66+
for (int i =0; i < NUM_PROVIDERS; i++) {
67+
providers[i] = (SunPKCS11)providers[i].configure(nssConfig);
6068
Security.addProvider(providers[i]);
6169
test(providers[i]);
6270
}
@@ -92,7 +100,6 @@ public static void main(String[] args) throws Exception {
92100

93101
private static void test(SunPKCS11 p) throws Exception {
94102
KeyStore ks = KeyStore.getInstance(KS_TYPE, p);
95-
96103
p.setCallbackHandler(new PasswordCallbackHandler());
97104
try {
98105
ks.load(null, (char[]) null);
@@ -117,6 +124,23 @@ private static void test(SunPKCS11 p) throws Exception {
117124
}
118125
}
119126

127+
static final class SimplePolicy extends Policy {
128+
129+
final Permissions perms = new Permissions();
130+
SimplePolicy() {
131+
perms.add(new PropertyPermission("*", "read, write"));
132+
perms.add(new SecurityPermission("authProvider.*"));
133+
perms.add(new SecurityPermission("insertProvider.*"));
134+
perms.add(new SecurityPermission("removeProvider.*"));
135+
}
136+
137+
@Override
138+
public boolean implies(ProtectionDomain domain, Permission permission) {
139+
return perms.implies(permission) ||
140+
DEFAULT_POLICY.implies(domain, permission);
141+
}
142+
}
143+
120144
public static class PasswordCallbackHandler implements CallbackHandler {
121145
public void handle(Callback[] callbacks)
122146
throws IOException, UnsupportedCallbackException {

test/jdk/sun/security/pkcs11/Provider/MultipleLogins.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#
2323

2424
# @test
25-
# @bug 7777777
25+
# @bug 8240256 8269034
2626
# @summary
2727
# @library /test/lib/
2828
# @build jdk.test.lib.util.ForceGC
@@ -114,9 +114,7 @@ ${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} \
114114
${TESTSRC}${FS}MultipleLogins.java \
115115
${TESTSRC}${FS}..${FS}PKCS11Test.java
116116

117-
# run test
118-
${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} \
119-
-classpath ${TESTCLASSPATH} \
117+
TEST_ARGS="${TESTVMOPTS} -classpath ${TESTCLASSPATH} \
120118
--add-modules jdk.crypto.cryptoki \
121119
--add-exports jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED \
122120
-DCUSTOM_DB_DIR=${TESTCLASSES} \
@@ -125,11 +123,13 @@ ${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} \
125123
-DNO_DEIMOS=true \
126124
-Dtest.src=${TESTSRC} \
127125
-Dtest.classes=${TESTCLASSES} \
128-
-Djava.security.debug=${DEBUG} \
129-
MultipleLogins
126+
-Djava.security.debug=${DEBUG}"
127+
128+
# run test without security manager
129+
${TESTJAVA}${FS}bin${FS}java ${TEST_ARGS} MultipleLogins || exit 10
130130

131-
# save error status
132-
status=$?
131+
# run test with security manager
132+
${TESTJAVA}${FS}bin${FS}java ${TEST_ARGS} MultipleLogins useSimplePolicy || exit 11
133133

134-
# return
135-
exit $status
134+
echo Done
135+
exit 0

0 commit comments

Comments
 (0)