Skip to content

Commit 5e01c40

Browse files
author
Alan Bateman
committed
8343981: Remove usage of security manager from Thread and related classes
Reviewed-by: rriggs, yzheng
1 parent dbf2346 commit 5e01c40

File tree

10 files changed

+54
-282
lines changed

10 files changed

+54
-282
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.nio.channels.spi.SelectorProvider;
4848
import java.nio.charset.CharacterCodingException;
4949
import java.nio.charset.Charset;
50-
import java.security.AccessControlContext;
5150
import java.security.AccessController;
5251
import java.security.CodeSource;
5352
import java.security.PrivilegedAction;
@@ -2134,9 +2133,6 @@ public void blockedOn(Interruptible b) {
21342133
public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
21352134
Shutdown.add(slot, registerShutdownInProgress, hook);
21362135
}
2137-
public Thread newThreadWithAcc(Runnable target, @SuppressWarnings("removal") AccessControlContext acc) {
2138-
return new Thread(target, acc);
2139-
}
21402136
@SuppressWarnings("removal")
21412137
public void invokeFinalize(Object o) throws Throwable {
21422138
o.finalize();

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

Lines changed: 14 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
import java.lang.ref.Reference;
2929
import java.lang.reflect.Field;
30-
import java.security.AccessController;
31-
import java.security.AccessControlContext;
32-
import java.security.PrivilegedAction;
3330
import java.time.Duration;
3431
import java.util.Map;
3532
import java.util.HashMap;
@@ -41,8 +38,6 @@
4138
import jdk.internal.misc.TerminatingThreadLocal;
4239
import jdk.internal.misc.Unsafe;
4340
import jdk.internal.misc.VM;
44-
import jdk.internal.reflect.CallerSensitive;
45-
import jdk.internal.reflect.Reflection;
4641
import jdk.internal.vm.Continuation;
4742
import jdk.internal.vm.ScopedValueContainer;
4843
import jdk.internal.vm.StackableScope;
@@ -52,7 +47,6 @@
5247
import jdk.internal.vm.annotation.IntrinsicCandidate;
5348
import jdk.internal.vm.annotation.Stable;
5449
import sun.nio.ch.Interruptible;
55-
import sun.security.util.SecurityConstants;
5650
import static java.util.concurrent.TimeUnit.MILLISECONDS;
5751
import static java.util.concurrent.TimeUnit.NANOSECONDS;
5852

@@ -652,21 +646,6 @@ static long next() {
652646
}
653647
}
654648

655-
/**
656-
* Returns the context class loader to inherit from the parent thread.
657-
* See Thread initialization.
658-
*/
659-
private static ClassLoader contextClassLoader(Thread parent) {
660-
@SuppressWarnings("removal")
661-
SecurityManager sm = System.getSecurityManager();
662-
if (sm == null || isCCLOverridden(parent.getClass())) {
663-
return parent.getContextClassLoader();
664-
} else {
665-
// skip call to getContextClassLoader
666-
return parent.contextClassLoader;
667-
}
668-
}
669-
670649
/**
671650
* Initializes a platform Thread.
672651
*
@@ -676,11 +655,8 @@ private static ClassLoader contextClassLoader(Thread parent) {
676655
* @param task the object whose run() method gets called
677656
* @param stackSize the desired stack size for the new thread, or
678657
* zero to indicate that this parameter is to be ignored.
679-
* @param acc ignored
680658
*/
681-
@SuppressWarnings("removal")
682-
Thread(ThreadGroup g, String name, int characteristics, Runnable task,
683-
long stackSize, AccessControlContext acc) {
659+
Thread(ThreadGroup g, String name, int characteristics, Runnable task, long stackSize) {
684660

685661
Thread parent = currentThread();
686662
boolean attached = (parent == this); // primordial or JNI attached
@@ -691,27 +667,10 @@ private static ClassLoader contextClassLoader(Thread parent) {
691667
}
692668
this.holder = new FieldHolder(g, task, stackSize, NORM_PRIORITY, false);
693669
} else {
694-
SecurityManager sm = System.getSecurityManager();
695670
if (g == null) {
696-
// the security manager can choose the thread group
697-
if (sm != null) {
698-
g = sm.getThreadGroup();
699-
}
700-
701671
// default to current thread's group
702-
if (g == null) {
703-
g = parent.getThreadGroup();
704-
}
672+
g = parent.getThreadGroup();
705673
}
706-
707-
// permission checks when creating a child Thread
708-
if (sm != null) {
709-
sm.checkAccess(g);
710-
if (isCCLOverridden(getClass())) {
711-
sm.checkPermission(SecurityConstants.SUBCLASS_IMPLEMENTATION_PERMISSION);
712-
}
713-
}
714-
715674
int priority = Math.min(parent.getPriority(), g.getMaxPriority());
716675
this.holder = new FieldHolder(g, task, stackSize, priority, parent.isDaemon());
717676
}
@@ -732,7 +691,7 @@ private static ClassLoader contextClassLoader(Thread parent) {
732691
this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parentMap);
733692
}
734693
if (VM.isBooted()) {
735-
this.contextClassLoader = contextClassLoader(parent);
694+
this.contextClassLoader = parent.getContextClassLoader();
736695
}
737696
} else if (VM.isBooted()) {
738697
// default CCL to the system class loader when not inheriting
@@ -763,7 +722,7 @@ private static ClassLoader contextClassLoader(Thread parent) {
763722
if (parentMap != null && parentMap.size() > 0) {
764723
this.inheritableThreadLocals = ThreadLocal.createInheritedMap(parentMap);
765724
}
766-
this.contextClassLoader = contextClassLoader(parent);
725+
this.contextClassLoader = parent.getContextClassLoader();
767726
} else {
768727
// default CCL to the system class loader when not inheriting
769728
this.contextClassLoader = ClassLoader.getSystemClassLoader();
@@ -1105,7 +1064,7 @@ private static String checkName(String name) {
11051064
* @see <a href="#inheritance">Inheritance when creating threads</a>
11061065
*/
11071066
public Thread() {
1108-
this(null, null, 0, null, 0, null);
1067+
this(null, null, 0, null, 0);
11091068
}
11101069

11111070
/**
@@ -1126,16 +1085,7 @@ public Thread() {
11261085
* @see <a href="#inheritance">Inheritance when creating threads</a>
11271086
*/
11281087
public Thread(Runnable task) {
1129-
this(null, null, 0, task, 0, null);
1130-
}
1131-
1132-
/**
1133-
* Creates a new Thread that inherits the given AccessControlContext
1134-
* but thread-local variables are not inherited.
1135-
* This is not a public constructor.
1136-
*/
1137-
Thread(Runnable task, @SuppressWarnings("removal") AccessControlContext acc) {
1138-
this(null, null, 0, task, 0, acc);
1088+
this(null, null, 0, task, 0);
11391089
}
11401090

11411091
/**
@@ -1160,7 +1110,7 @@ public Thread(Runnable task) {
11601110
* @see <a href="#inheritance">Inheritance when creating threads</a>
11611111
*/
11621112
public Thread(ThreadGroup group, Runnable task) {
1163-
this(group, null, 0, task, 0, null);
1113+
this(group, null, 0, task, 0);
11641114
}
11651115

11661116
/**
@@ -1177,7 +1127,7 @@ public Thread(ThreadGroup group, Runnable task) {
11771127
* @see <a href="#inheritance">Inheritance when creating threads</a>
11781128
*/
11791129
public Thread(String name) {
1180-
this(null, checkName(name), 0, null, 0, null);
1130+
this(null, checkName(name), 0, null, 0);
11811131
}
11821132

11831133
/**
@@ -1198,7 +1148,7 @@ public Thread(String name) {
11981148
* @see <a href="#inheritance">Inheritance when creating threads</a>
11991149
*/
12001150
public Thread(ThreadGroup group, String name) {
1201-
this(group, checkName(name), 0, null, 0, null);
1151+
this(group, checkName(name), 0, null, 0);
12021152
}
12031153

12041154
/**
@@ -1220,7 +1170,7 @@ public Thread(ThreadGroup group, String name) {
12201170
* @see <a href="#inheritance">Inheritance when creating threads</a>
12211171
*/
12221172
public Thread(Runnable task, String name) {
1223-
this(null, checkName(name), 0, task, 0, null);
1173+
this(null, checkName(name), 0, task, 0);
12241174
}
12251175

12261176
/**
@@ -1256,7 +1206,7 @@ public Thread(Runnable task, String name) {
12561206
* @see <a href="#inheritance">Inheritance when creating threads</a>
12571207
*/
12581208
public Thread(ThreadGroup group, Runnable task, String name) {
1259-
this(group, checkName(name), 0, task, 0, null);
1209+
this(group, checkName(name), 0, task, 0);
12601210
}
12611211

12621212
/**
@@ -1330,7 +1280,7 @@ public Thread(ThreadGroup group, Runnable task, String name) {
13301280
* @see <a href="#inheritance">Inheritance when creating threads</a>
13311281
*/
13321282
public Thread(ThreadGroup group, Runnable task, String name, long stackSize) {
1333-
this(group, checkName(name), 0, task, stackSize, null);
1283+
this(group, checkName(name), 0, task, stackSize);
13341284
}
13351285

13361286
/**
@@ -1390,7 +1340,7 @@ public Thread(ThreadGroup group, Runnable task, String name,
13901340
long stackSize, boolean inheritInheritableThreadLocals) {
13911341
this(group, checkName(name),
13921342
(inheritInheritableThreadLocals ? 0 : NO_INHERIT_THREAD_LOCALS),
1393-
task, stackSize, null);
1343+
task, stackSize);
13941344
}
13951345

13961346
/**
@@ -2140,18 +2090,8 @@ public String toString() {
21402090
*
21412091
* @since 1.2
21422092
*/
2143-
@CallerSensitive
21442093
public ClassLoader getContextClassLoader() {
2145-
ClassLoader cl = this.contextClassLoader;
2146-
if (cl == null)
2147-
return null;
2148-
@SuppressWarnings("removal")
2149-
SecurityManager sm = System.getSecurityManager();
2150-
if (sm != null) {
2151-
Class<?> caller = Reflection.getCallerClass();
2152-
ClassLoader.checkClassLoaderPermission(cl, caller);
2153-
}
2154-
return cl;
2094+
return contextClassLoader;
21552095
}
21562096

21572097
/**
@@ -2167,11 +2107,6 @@ public ClassLoader getContextClassLoader() {
21672107
* @since 1.2
21682108
*/
21692109
public void setContextClassLoader(ClassLoader cl) {
2170-
@SuppressWarnings("removal")
2171-
SecurityManager sm = System.getSecurityManager();
2172-
if (sm != null) {
2173-
sm.checkPermission(new RuntimePermission("setContextClassLoader"));
2174-
}
21752110
contextClassLoader = cl;
21762111
}
21772112

@@ -2220,12 +2155,6 @@ public void setContextClassLoader(ClassLoader cl) {
22202155
*/
22212156
public StackTraceElement[] getStackTrace() {
22222157
if (this != Thread.currentThread()) {
2223-
// check for getStackTrace permission
2224-
@SuppressWarnings("removal")
2225-
SecurityManager security = System.getSecurityManager();
2226-
if (security != null) {
2227-
security.checkPermission(SecurityConstants.GET_STACK_TRACE_PERMISSION);
2228-
}
22292158
// optimization so we do not call into the vm for threads that
22302159
// have not yet started or have terminated
22312160
if (!isAlive()) {
@@ -2285,14 +2214,6 @@ StackTraceElement[] asyncGetStackTrace() {
22852214
* @since 1.5
22862215
*/
22872216
public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
2288-
// check for getStackTrace permission
2289-
@SuppressWarnings("removal")
2290-
SecurityManager security = System.getSecurityManager();
2291-
if (security != null) {
2292-
security.checkPermission(SecurityConstants.GET_STACK_TRACE_PERMISSION);
2293-
security.checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
2294-
}
2295-
22962217
// Get a snapshot of the list of all threads
22972218
Thread[] threads = getThreads();
22982219
StackTraceElement[][] traces = dumpThreads(threads);
@@ -2307,64 +2228,6 @@ public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
23072228
return m;
23082229
}
23092230

2310-
/** cache of subclass security audit results */
2311-
private static class Caches {
2312-
/** cache of subclass security audit results */
2313-
static final ClassValue<Boolean> subclassAudits =
2314-
new ClassValue<>() {
2315-
@Override
2316-
protected Boolean computeValue(Class<?> type) {
2317-
return auditSubclass(type);
2318-
}
2319-
};
2320-
}
2321-
2322-
/**
2323-
* Verifies that this (possibly subclass) instance can be constructed
2324-
* without violating security constraints: the subclass must not override
2325-
* security-sensitive non-final methods, or else the
2326-
* "enableContextClassLoaderOverride" RuntimePermission is checked.
2327-
*/
2328-
private static boolean isCCLOverridden(Class<?> cl) {
2329-
if (cl == Thread.class)
2330-
return false;
2331-
2332-
return Caches.subclassAudits.get(cl);
2333-
}
2334-
2335-
/**
2336-
* Performs reflective checks on given subclass to verify that it doesn't
2337-
* override security-sensitive non-final methods. Returns true if the
2338-
* subclass overrides any of the methods, false otherwise.
2339-
*/
2340-
private static boolean auditSubclass(final Class<?> subcl) {
2341-
@SuppressWarnings("removal")
2342-
Boolean result = AccessController.doPrivileged(
2343-
new PrivilegedAction<>() {
2344-
public Boolean run() {
2345-
for (Class<?> cl = subcl;
2346-
cl != Thread.class;
2347-
cl = cl.getSuperclass())
2348-
{
2349-
try {
2350-
cl.getDeclaredMethod("getContextClassLoader", new Class<?>[0]);
2351-
return Boolean.TRUE;
2352-
} catch (NoSuchMethodException ex) {
2353-
}
2354-
try {
2355-
Class<?>[] params = {ClassLoader.class};
2356-
cl.getDeclaredMethod("setContextClassLoader", params);
2357-
return Boolean.TRUE;
2358-
} catch (NoSuchMethodException ex) {
2359-
}
2360-
}
2361-
return Boolean.FALSE;
2362-
}
2363-
}
2364-
);
2365-
return result.booleanValue();
2366-
}
2367-
23682231
/**
23692232
* Return an array of all live threads.
23702233
*/
@@ -2603,12 +2466,6 @@ public interface UncaughtExceptionHandler {
26032466
* @since 1.5
26042467
*/
26052468
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler ueh) {
2606-
@SuppressWarnings("removal")
2607-
SecurityManager sm = System.getSecurityManager();
2608-
if (sm != null) {
2609-
sm.checkPermission(
2610-
new RuntimePermission("setDefaultUncaughtExceptionHandler"));
2611-
}
26122469
defaultUncaughtExceptionHandler = ueh;
26132470
}
26142471

@@ -2675,7 +2532,6 @@ void dispatchUncaughtException(Throwable e) {
26752532
/**
26762533
* Holder class for constants.
26772534
*/
2678-
@SuppressWarnings("removal")
26792535
private static class Constants {
26802536
// Thread group for virtual threads.
26812537
static final ThreadGroup VTHREAD_GROUP;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public OfPlatform stackSize(long stackSize) {
179179
public Thread unstarted(Runnable task) {
180180
Objects.requireNonNull(task);
181181
String name = nextThreadName();
182-
var thread = new Thread(group, name, characteristics(), task, stackSize, null);
182+
var thread = new Thread(group, name, characteristics(), task, stackSize);
183183
if (daemonChanged)
184184
thread.daemon(daemon);
185185
if (priority != 0)
@@ -353,7 +353,7 @@ String nextThreadName() {
353353
public Thread newThread(Runnable task) {
354354
Objects.requireNonNull(task);
355355
String name = nextThreadName();
356-
Thread thread = new Thread(group, name, characteristics(), task, stackSize, null);
356+
Thread thread = new Thread(group, name, characteristics(), task, stackSize);
357357
if (daemonChanged)
358358
thread.daemon(daemon);
359359
if (priority != 0)

0 commit comments

Comments
 (0)