Skip to content

Commit 4be2173

Browse files
committed
8259319: Illegal package access when SunPKCS11 requires SunJCE's classes
Reviewed-by: valeriep, mullan
1 parent e4df209 commit 4be2173

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
// additional qualified exports may be inserted at build time
134134
// see make/gensrc/GenModuleInfo.gmk
135135

136+
exports com.sun.crypto.provider to
137+
jdk.crypto.cryptoki;
136138
exports sun.invoke.util to
137139
jdk.compiler,
138140
jdk.incubator.foreign;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ grant codeBase "jrt:/jdk.crypto.ec" {
124124
};
125125

126126
grant codeBase "jrt:/jdk.crypto.cryptoki" {
127+
permission java.lang.RuntimePermission
128+
"accessClassInPackage.com.sun.crypto.provider";
127129
permission java.lang.RuntimePermission
128130
"accessClassInPackage.sun.security.*";
129131
permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 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
@@ -88,12 +88,27 @@ private static Provider getProvider(Provider p, String providerName,
8888
p = Security.getProvider(providerName);
8989
if (p == null) {
9090
try {
91-
@SuppressWarnings("deprecation")
92-
Object o = Class.forName(className).newInstance();
93-
p = (Provider)o;
94-
} catch (Exception e) {
95-
throw new ProviderException
96-
("Could not find provider " + providerName, e);
91+
final Class<?> c = Class.forName(className);
92+
p = AccessController.doPrivileged(
93+
new PrivilegedAction<Provider>() {
94+
public Provider run() {
95+
try {
96+
@SuppressWarnings("deprecation")
97+
Object o = c.newInstance();
98+
return (Provider) o;
99+
} catch (Exception e) {
100+
throw new ProviderException(
101+
"Could not find provider " +
102+
providerName, e);
103+
}
104+
}
105+
}, null, new RuntimePermission(
106+
"accessClassInPackage." + c.getPackageName()));
107+
} catch (ClassNotFoundException e) {
108+
// Unexpected, as className is not a user but a
109+
// P11Util-internal value.
110+
throw new ProviderException("Could not find provider " +
111+
providerName, e);
97112
}
98113
}
99114
return p;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2021, Red Hat, Inc.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.security.AllPermission;
25+
import java.security.KeyFactory;
26+
import java.security.KeyPair;
27+
import java.security.KeyPairGenerator;
28+
import java.security.Permission;
29+
import java.security.PermissionCollection;
30+
import java.security.Permissions;
31+
import java.security.Policy;
32+
import java.security.ProtectionDomain;
33+
import java.security.Provider;
34+
import java.security.Security;
35+
import java.security.spec.X509EncodedKeySpec;
36+
37+
/*
38+
* @test
39+
* @bug 8259319
40+
* @library /test/lib ..
41+
* @run main/othervm IllegalPackageAccess
42+
*/
43+
44+
public class IllegalPackageAccess extends PKCS11Test {
45+
46+
private static Policy policy = Policy.getPolicy();
47+
private static RuntimePermission accessPerm =
48+
new RuntimePermission("accessClassInPackage.com.sun.crypto.provider");
49+
50+
private static class MyPolicy extends Policy {
51+
@Override
52+
public PermissionCollection getPermissions(ProtectionDomain domain) {
53+
PermissionCollection perms = new Permissions();
54+
perms.add(new AllPermission());
55+
return perms;
56+
}
57+
58+
@Override
59+
public boolean implies(ProtectionDomain domain, Permission permission) {
60+
if (permission.equals(accessPerm)) {
61+
return policy.implies(domain, permission);
62+
}
63+
return super.implies(domain, permission);
64+
}
65+
}
66+
67+
public static void main(String[] args) throws Exception {
68+
main(new IllegalPackageAccess(), args);
69+
System.out.println("TEST PASS - OK");
70+
}
71+
72+
@Override
73+
public void main(Provider p) throws Exception {
74+
Policy.setPolicy(new MyPolicy());
75+
System.setSecurityManager(new SecurityManager());
76+
77+
// Remove all security providers so a fallback scheme
78+
// that creates class instances is forced.
79+
for (Provider provider : Security.getProviders()) {
80+
Security.removeProvider(provider.getName());
81+
}
82+
83+
KeyPair kp = KeyPairGenerator.getInstance("DH", p)
84+
.generateKeyPair();
85+
byte[] encPubKey = kp.getPublic().getEncoded();
86+
KeyFactory kf = KeyFactory.getInstance("DH", p);
87+
88+
// Requires access to a SunJCE class that parses
89+
// the encoded key.
90+
kf.generatePublic(new X509EncodedKeySpec(encPubKey));
91+
92+
System.setSecurityManager(null);
93+
Policy.setPolicy(policy);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)