Skip to content

Commit abacece

Browse files
author
Alan Bateman
committed
8344011: Remove usage of security manager from Class and reflective APIs
Reviewed-by: liach, yzheng, rriggs
1 parent c977ef7 commit abacece

26 files changed

+160
-1248
lines changed

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

Lines changed: 21 additions & 383 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import java.lang.reflect.AnnotatedElement;
4040
import java.net.URI;
4141
import java.net.URL;
42-
import java.security.AccessController;
43-
import java.security.PrivilegedAction;
4442
import java.util.HashMap;
4543
import java.util.HashSet;
4644
import java.util.List;
@@ -64,14 +62,12 @@
6462
import jdk.internal.misc.Unsafe;
6563
import jdk.internal.misc.VM;
6664
import jdk.internal.module.ModuleBootstrap;
67-
import jdk.internal.module.ModuleBootstrap.IllegalNativeAccess;
6865
import jdk.internal.module.ModuleLoaderMap;
6966
import jdk.internal.module.ServicesCatalog;
7067
import jdk.internal.module.Resources;
7168
import jdk.internal.reflect.CallerSensitive;
7269
import jdk.internal.reflect.Reflection;
7370
import jdk.internal.vm.annotation.Stable;
74-
import sun.security.util.SecurityConstants;
7571

7672
/**
7773
* Represents a run-time module, either {@link #isNamed() named} or unnamed.
@@ -198,11 +194,6 @@ public String getName() {
198194
* @return The class loader for this module
199195
*/
200196
public ClassLoader getClassLoader() {
201-
@SuppressWarnings("removal")
202-
SecurityManager sm = System.getSecurityManager();
203-
if (sm != null) {
204-
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
205-
}
206197
return loader;
207198
}
208199

@@ -1556,7 +1547,6 @@ public Annotation[] getDeclaredAnnotations() {
15561547
// cached class file with annotations
15571548
private volatile Class<?> moduleInfoClass;
15581549

1559-
@SuppressWarnings("removal")
15601550
private Class<?> moduleInfoClass() {
15611551
Class<?> clazz = this.moduleInfoClass;
15621552
if (clazz != null)
@@ -1566,8 +1556,7 @@ private Class<?> moduleInfoClass() {
15661556
clazz = this.moduleInfoClass;
15671557
if (clazz == null) {
15681558
if (isNamed()) {
1569-
PrivilegedAction<Class<?>> pa = this::loadModuleInfoClass;
1570-
clazz = AccessController.doPrivileged(pa);
1559+
clazz = loadModuleInfoClass();
15711560
}
15721561
if (clazz == null) {
15731562
class DummyModuleInfo { }

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.stream.Collectors;
4545
import java.util.stream.Stream;
4646

47-
import jdk.internal.javac.PreviewFeature;
4847
import jdk.internal.javac.Restricted;
4948
import jdk.internal.loader.ClassLoaderValue;
5049
import jdk.internal.loader.Loader;
@@ -54,7 +53,6 @@
5453
import jdk.internal.reflect.CallerSensitive;
5554
import jdk.internal.reflect.Reflection;
5655
import jdk.internal.vm.annotation.Stable;
57-
import sun.security.util.SecurityConstants;
5856

5957
/**
6058
* A layer of modules in the Java virtual machine.
@@ -505,9 +503,6 @@ public static Controller defineModulesWithOneLoader(Configuration cf,
505503
List<ModuleLayer> parents = List.copyOf(parentLayers);
506504
checkConfiguration(cf, parents);
507505

508-
checkCreateClassLoaderPermission();
509-
checkGetClassLoaderPermission();
510-
511506
try {
512507
Loader loader = new Loader(cf.modules(), parentLoader);
513508
loader.initRemotePackageMap(cf, parents);
@@ -572,9 +567,6 @@ public static Controller defineModulesWithManyLoaders(Configuration cf,
572567
List<ModuleLayer> parents = List.copyOf(parentLayers);
573568
checkConfiguration(cf, parents);
574569

575-
checkCreateClassLoaderPermission();
576-
checkGetClassLoaderPermission();
577-
578570
LoaderPool pool = new LoaderPool(cf, parents, parentLoader);
579571
try {
580572
ModuleLayer layer = new ModuleLayer(cf, parents, pool::loaderFor);
@@ -654,8 +646,6 @@ public static Controller defineModules(Configuration cf,
654646
checkConfiguration(cf, parents);
655647
Objects.requireNonNull(clf);
656648

657-
checkGetClassLoaderPermission();
658-
659649
// The boot layer is checked during module system initialization
660650
if (boot() != null) {
661651
checkForDuplicatePkgs(cf, clf);
@@ -693,20 +683,6 @@ private static void checkConfiguration(Configuration cf,
693683
}
694684
}
695685

696-
private static void checkCreateClassLoaderPermission() {
697-
@SuppressWarnings("removal")
698-
SecurityManager sm = System.getSecurityManager();
699-
if (sm != null)
700-
sm.checkPermission(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
701-
}
702-
703-
private static void checkGetClassLoaderPermission() {
704-
@SuppressWarnings("removal")
705-
SecurityManager sm = System.getSecurityManager();
706-
if (sm != null)
707-
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
708-
}
709-
710686
/**
711687
* Checks a configuration and the module-to-loader mapping to ensure that
712688
* no two modules mapped to the same class loader have the same package.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, 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
@@ -30,8 +30,6 @@
3030
import java.net.MalformedURLException;
3131
import java.net.URI;
3232
import java.net.URL;
33-
import java.security.AccessController;
34-
import java.security.PrivilegedAction;
3533
import java.util.Objects;
3634

3735
import jdk.internal.loader.BootLoader;
@@ -417,9 +415,7 @@ private Class<?> getPackageInfo() {
417415
// find package-info.class defined by loader
418416
String cn = packageName() + ".package-info";
419417
Module module = module();
420-
PrivilegedAction<ClassLoader> pa = module::getClassLoader;
421-
@SuppressWarnings("removal")
422-
ClassLoader loader = AccessController.doPrivileged(pa);
418+
ClassLoader loader = module.getClassLoader();
423419
Class<?> c;
424420
if (loader != null) {
425421
c = loader.loadClass(module, cn);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, 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
@@ -28,7 +28,6 @@
2828

2929
import java.lang.reflect.Method;
3030
import java.lang.reflect.Modifier;
31-
import java.security.AccessController;
3231
import java.util.Arrays;
3332
import java.util.LinkedHashMap;
3433
import java.util.Map;
@@ -88,10 +87,7 @@ Method[] toArray() {
8887
* Method (name, parameter types) tuple.
8988
*/
9089
private static final class Key {
91-
@SuppressWarnings("removal")
92-
private static final ReflectionFactory reflectionFactory =
93-
AccessController.doPrivileged(
94-
new ReflectionFactory.GetReflectionFactoryAction());
90+
private static final ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
9591

9692
private final String name; // must be interned (as from Method.getName())
9793
private final Class<?>[] ptypes;

src/java.base/share/classes/java/lang/invoke/AbstractValidatingLambdaMetafactory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@
107107
* implemented by invoking the implementation method
108108
* @throws LambdaConversionException If any of the meta-factory protocol
109109
* invariants are violated
110-
* @throws SecurityException If a security manager is present, and it
111-
* <a href="MethodHandles.Lookup.html#secmgr">denies access</a>
112-
* from {@code caller} to the package of {@code implementation}.
113110
*/
114111
AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller,
115112
MethodType factoryType,
@@ -138,7 +135,7 @@
138135
this.implementation = implementation;
139136
this.implMethodType = implementation.type();
140137
try {
141-
this.implInfo = caller.revealDirect(implementation); // may throw SecurityException
138+
this.implInfo = caller.revealDirect(implementation);
142139
} catch (IllegalArgumentException e) {
143140
throw new LambdaConversionException(implementation + " is not direct or cannot be cracked");
144141
}

src/java.base/share/classes/java/lang/invoke/InfoFromMemberName.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2024, 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
@@ -25,7 +25,6 @@
2525

2626
package java.lang.invoke;
2727

28-
import java.security.*;
2928
import java.lang.reflect.*;
3029
import java.lang.invoke.MethodHandles.Lookup;
3130

@@ -85,16 +84,13 @@ public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup) {
8584
// For more information see comments on {@link MethodHandleNatives#linkMethod}.
8685
throw new IllegalArgumentException("cannot reflect signature polymorphic method");
8786
}
88-
@SuppressWarnings("removal")
89-
Member mem = AccessController.doPrivileged(new PrivilegedAction<>() {
90-
public Member run() {
91-
try {
92-
return reflectUnchecked();
93-
} catch (ReflectiveOperationException ex) {
94-
throw new IllegalArgumentException(ex);
95-
}
96-
}
97-
});
87+
88+
Member mem;
89+
try {
90+
mem = reflectUnchecked();
91+
} catch (ReflectiveOperationException ex) {
92+
throw new IllegalArgumentException(ex);
93+
}
9894
try {
9995
Class<?> defc = getDeclaringClass();
10096
byte refKind = (byte) getReferenceKind();

src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import jdk.internal.misc.CDS;
3030
import jdk.internal.util.ClassFileDumper;
3131
import sun.invoke.util.VerifyAccess;
32-
import sun.security.action.GetBooleanAction;
3332

3433
import java.io.Serializable;
3534
import java.lang.classfile.ClassBuilder;
@@ -83,7 +82,7 @@
8382
lambdaProxyClassFileDumper = ClassFileDumper.getInstance(dumpProxyClassesKey, "DUMP_LAMBDA_PROXY_CLASS_FILES");
8483

8584
final String disableEagerInitializationKey = "jdk.internal.lambda.disableEagerInitialization";
86-
disableEagerInitialization = GetBooleanAction.privilegedGetProperty(disableEagerInitializationKey);
85+
disableEagerInitialization = Boolean.getBoolean(disableEagerInitializationKey);
8786
}
8887

8988
// See context values in AbstractValidatingLambdaMetafactory
@@ -134,9 +133,6 @@
134133
* implemented by invoking the implementation method
135134
* @throws LambdaConversionException If any of the meta-factory protocol
136135
* invariants are violated
137-
* @throws SecurityException If a security manager is present, and it
138-
* <a href="MethodHandles.Lookup.html#secmgr">denies access</a>
139-
* from {@code caller} to the package of {@code implementation}.
140136
*/
141137
public InnerClassLambdaMetafactory(MethodHandles.Lookup caller,
142138
MethodType factoryType,

src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,11 +1208,7 @@ private static MethodHandle restoreToType(MethodHandle vamh,
12081208

12091209
private static boolean checkInjectedInvoker(Class<?> hostClass, Class<?> invokerClass) {
12101210
assert (hostClass.getClassLoader() == invokerClass.getClassLoader()) : hostClass.getName()+" (CL)";
1211-
try {
1212-
assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
1213-
} catch (SecurityException ex) {
1214-
// Self-check was blocked by security manager. This is OK.
1215-
}
1211+
assert (hostClass.getProtectionDomain() == invokerClass.getProtectionDomain()) : hostClass.getName()+" (PD)";
12161212
try {
12171213
// Test the invoker to ensure that it really injects into the right place.
12181214
MethodHandle invoker = IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);

src/java.base/share/classes/java/lang/invoke/MethodHandleProxies.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import java.lang.reflect.Method;
3434
import java.lang.reflect.Modifier;
3535
import java.lang.reflect.UndeclaredThrowableException;
36-
import java.security.AccessController;
37-
import java.security.PrivilegedAction;
3836
import java.util.ArrayList;
3937
import java.util.Arrays;
4038
import java.util.Collections;
@@ -56,10 +54,7 @@
5654
import jdk.internal.constant.ConstantUtils;
5755
import jdk.internal.loader.ClassLoaders;
5856
import jdk.internal.module.Modules;
59-
import jdk.internal.reflect.CallerSensitive;
60-
import jdk.internal.reflect.Reflection;
6157
import jdk.internal.util.ClassFileDumper;
62-
import sun.reflect.misc.ReflectUtil;
6358

6459
import static java.lang.constant.ConstantDescs.*;
6560
import static java.lang.invoke.MethodHandleStatics.*;
@@ -159,7 +154,6 @@ private MethodHandleProxies() { } // do not instantiate
159154
* be converted to the type required by the requested interface
160155
*/
161156
@SuppressWarnings("doclint:reference") // cross-module links
162-
@CallerSensitive
163157
public static <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) {
164158
if (!intfc.isInterface() || !Modifier.isPublic(intfc.getModifiers()))
165159
throw newIllegalArgumentException("not a public interface", intfc.getName());
@@ -168,17 +162,7 @@ public static <T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle
168162
if (intfc.isHidden())
169163
throw newIllegalArgumentException("a hidden interface", intfc.getName());
170164
Objects.requireNonNull(target);
171-
final MethodHandle mh;
172-
@SuppressWarnings("removal")
173-
var sm = System.getSecurityManager();
174-
if (sm != null) {
175-
final Class<?> caller = Reflection.getCallerClass();
176-
final ClassLoader ccl = caller != null ? caller.getClassLoader() : null;
177-
ReflectUtil.checkProxyPackageAccess(ccl, intfc);
178-
mh = ccl != null ? bindCaller(target, caller) : target;
179-
} else {
180-
mh = target;
181-
}
165+
final MethodHandle mh = target;
182166

183167
// Define one hidden class for each interface. Create an instance of
184168
// the hidden class for a given target method handle which will be
@@ -283,17 +267,7 @@ private static Class<?> newProxyClass(Class<?> intfc) {
283267
// define the dynamic module to the class loader of the interface
284268
var definer = new Lookup(intfc).makeHiddenClassDefiner(className, template, DUMPER);
285269

286-
@SuppressWarnings("removal")
287-
var sm = System.getSecurityManager();
288-
Lookup lookup;
289-
if (sm != null) {
290-
@SuppressWarnings("removal")
291-
var l = AccessController.doPrivileged((PrivilegedAction<Lookup>) () ->
292-
definer.defineClassAsLookup(true));
293-
lookup = l;
294-
} else {
295-
lookup = definer.defineClassAsLookup(true);
296-
}
270+
Lookup lookup = definer.defineClassAsLookup(true);
297271
// cache the wrapper type
298272
var ret = lookup.lookupClass();
299273
WRAPPER_TYPES.add(ret);

src/java.base/share/classes/java/lang/invoke/MethodHandleStatics.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import jdk.internal.misc.CDS;
2929
import jdk.internal.misc.Unsafe;
3030
import jdk.internal.util.ClassFileDumper;
31-
import sun.security.action.GetPropertyAction;
3231

3332
import java.lang.reflect.ClassFileFormatVersion;
3433
import java.util.Properties;
@@ -66,7 +65,7 @@ private MethodHandleStatics() { } // do not instantiate
6665
static final ClassFileDumper DUMP_CLASS_FILES;
6766

6867
static {
69-
Properties props = GetPropertyAction.privilegedGetProperties();
68+
Properties props = System.getProperties();
7069
DEBUG_METHOD_HANDLE_NAMES = Boolean.parseBoolean(
7170
props.getProperty("java.lang.invoke.MethodHandle.DEBUG_NAMES"));
7271

0 commit comments

Comments
 (0)