Skip to content

Commit 3532558

Browse files
committed
8334394: Race condition in Class::protectionDomain
Backport-of: c3226aaeb810521257e961be5763552c86ee5651
1 parent 49027ee commit 3532558

File tree

2 files changed

+99
-17
lines changed

2 files changed

+99
-17
lines changed

src/java.base/share/classes/java/lang/Class.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
import java.lang.constant.Constable;
5454
import java.net.URL;
5555
import java.security.AccessController;
56+
import java.security.Permissions;
5657
import java.security.PrivilegedAction;
58+
import java.security.ProtectionDomain;
5759
import java.util.ArrayList;
5860
import java.util.Arrays;
5961
import java.util.Collection;
@@ -2966,10 +2968,6 @@ private boolean isOpenToCaller(String name, Class<?> caller) {
29662968
return true;
29672969
}
29682970

2969-
2970-
/** protection domain returned when the internal domain is null */
2971-
private static java.security.ProtectionDomain allPermDomain;
2972-
29732971
/**
29742972
* Returns the {@code ProtectionDomain} of this class. If there is a
29752973
* security manager installed, this method first calls the security
@@ -2990,7 +2988,7 @@ private boolean isOpenToCaller(String name, Class<?> caller) {
29902988
* @see java.lang.RuntimePermission
29912989
* @since 1.2
29922990
*/
2993-
public java.security.ProtectionDomain getProtectionDomain() {
2991+
public ProtectionDomain getProtectionDomain() {
29942992
@SuppressWarnings("removal")
29952993
SecurityManager sm = System.getSecurityManager();
29962994
if (sm != null) {
@@ -2999,26 +2997,30 @@ public java.security.ProtectionDomain getProtectionDomain() {
29992997
return protectionDomain();
30002998
}
30012999

3000+
/** Holder for the protection domain returned when the internal domain is null */
3001+
private static class Holder {
3002+
private static final ProtectionDomain allPermDomain;
3003+
static {
3004+
Permissions perms = new Permissions();
3005+
perms.add(SecurityConstants.ALL_PERMISSION);
3006+
allPermDomain = new ProtectionDomain(null, perms);
3007+
}
3008+
}
3009+
30023010
// package-private
3003-
java.security.ProtectionDomain protectionDomain() {
3004-
java.security.ProtectionDomain pd = getProtectionDomain0();
3011+
ProtectionDomain protectionDomain() {
3012+
ProtectionDomain pd = getProtectionDomain0();
30053013
if (pd == null) {
3006-
if (allPermDomain == null) {
3007-
java.security.Permissions perms =
3008-
new java.security.Permissions();
3009-
perms.add(SecurityConstants.ALL_PERMISSION);
3010-
allPermDomain =
3011-
new java.security.ProtectionDomain(null, perms);
3012-
}
3013-
pd = allPermDomain;
3014+
return Holder.allPermDomain;
3015+
} else {
3016+
return pd;
30143017
}
3015-
return pd;
30163018
}
30173019

30183020
/**
30193021
* Returns the ProtectionDomain of this class.
30203022
*/
3021-
private native java.security.ProtectionDomain getProtectionDomain0();
3023+
private native ProtectionDomain getProtectionDomain0();
30223024

30233025
/*
30243026
* Return the Virtual Machine's Class object for the named
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
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+
/*
25+
* @test
26+
* @bug 8334394
27+
* @summary ensure there is no race condition in Class::protectionDomain
28+
* @run main/othervm ProtectionDomainRace
29+
*/
30+
import javax.security.auth.Subject;
31+
import java.security.PrivilegedAction;
32+
33+
/**
34+
* Without the code fix, this test would fail with
35+
* java.lang.AssertionError: sun.security.util.ResourcesMgr (PD)
36+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller.checkInjectedInvoker(MethodHandleImpl.java:1209)
37+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller.makeInjectedInvoker(MethodHandleImpl.java:1110)
38+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller$1.computeValue(MethodHandleImpl.java:1117)
39+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller$1.computeValue(MethodHandleImpl.java:1114)
40+
* at java.base/java.lang.ClassValue.getFromHashMap(ClassValue.java:229)
41+
* at java.base/java.lang.ClassValue.getFromBackup(ClassValue.java:211)
42+
* at java.base/java.lang.ClassValue.get(ClassValue.java:117)
43+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller.bindCallerWithInjectedInvoker(MethodHandleImpl.java:1089)
44+
* at java.base/java.lang.invoke.MethodHandleImpl$BindCaller.bindCaller(MethodHandleImpl.java:1077)
45+
* at java.base/java.lang.invoke.MethodHandleImpl.bindCaller(MethodHandleImpl.java:1032)
46+
* at java.base/java.lang.invoke.MethodHandles$Lookup.maybeBindCaller(MethodHandles.java:4149)
47+
* at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:4133)
48+
* at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodNoSecurityManager(MethodHandles.java:4077)
49+
* at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodForConstant(MethodHandles.java:4326)
50+
* at java.base/java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:4274)
51+
* at java.base/java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:628)
52+
* at java.base/sun.security.util.ResourcesMgr.getBundle(ResourcesMgr.java:54)
53+
* at java.base/sun.security.util.ResourcesMgr.getString(ResourcesMgr.java:40)
54+
* at java.base/javax.security.auth.Subject.doAs(Subject.java:517)
55+
* ...
56+
* as the Class::protectionDomain might assign different objects to the (original) allPermDomain field.
57+
*/
58+
public class ProtectionDomainRace {
59+
private static volatile Throwable failed = null;
60+
public static void main(String[] args) throws Throwable {
61+
PrivilegedAction<?> pa = () -> null;
62+
Thread[] threads = new Thread[100];
63+
for (int i = 0; i < 100; i++) {
64+
threads[i] = new Thread(() -> {
65+
try {
66+
Subject.doAs(null, pa);
67+
} catch (Throwable t) {
68+
failed = t;
69+
}
70+
});
71+
threads[i].start();
72+
}
73+
for (int i = 0; i < 100; i++) {
74+
threads[i].join();
75+
}
76+
if (failed != null) {
77+
throw failed;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)