Skip to content

Commit 0ae5748

Browse files
author
Alan Bateman
committed
8343982: Remove usage of security manager from ClassLoader and related classes
Reviewed-by: jpai, yzheng, lancea
1 parent 9907065 commit 0ae5748

File tree

7 files changed

+78
-433
lines changed

7 files changed

+78
-433
lines changed

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

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import jdk.internal.reflect.CallerSensitiveAdapter;
6565
import jdk.internal.reflect.Reflection;
6666
import jdk.internal.util.StaticProperty;
67-
import sun.security.util.SecurityConstants;
6867

6968
/**
7069
* A class loader is an object that is responsible for loading classes. The
@@ -357,12 +356,6 @@ private static Void checkCreateClassLoader(String name) {
357356
if (name != null && name.isEmpty()) {
358357
throw new IllegalArgumentException("name must be non-empty or null");
359358
}
360-
361-
@SuppressWarnings("removal")
362-
SecurityManager security = System.getSecurityManager();
363-
if (security != null) {
364-
security.checkCreateClassLoader();
365-
}
366359
return null;
367360
}
368361

@@ -1735,18 +1728,7 @@ public static InputStream getSystemResourceAsStream(String name) {
17351728
*
17361729
* @since 1.2
17371730
*/
1738-
@CallerSensitive
17391731
public final ClassLoader getParent() {
1740-
if (parent == null)
1741-
return null;
1742-
@SuppressWarnings("removal")
1743-
SecurityManager sm = System.getSecurityManager();
1744-
if (sm != null) {
1745-
// Check access to the parent class loader
1746-
// If the caller's class loader is same as this class loader,
1747-
// permission check is performed.
1748-
checkClassLoaderPermission(parent, Reflection.getCallerClass());
1749-
}
17501732
return parent;
17511733
}
17521734

@@ -1774,15 +1756,8 @@ public final Module getUnnamedModule() {
17741756
*
17751757
* @since 9
17761758
*/
1777-
@CallerSensitive
17781759
public static ClassLoader getPlatformClassLoader() {
1779-
@SuppressWarnings("removal")
1780-
SecurityManager sm = System.getSecurityManager();
1781-
ClassLoader loader = getBuiltinPlatformClassLoader();
1782-
if (sm != null) {
1783-
checkClassLoaderPermission(loader, Reflection.getCallerClass());
1784-
}
1785-
return loader;
1760+
return getBuiltinPlatformClassLoader();
17861761
}
17871762

17881763
/**
@@ -1853,7 +1828,6 @@ public static ClassLoader getPlatformClassLoader() {
18531828
* underlying cause of the error can be retrieved via the
18541829
* {@link Throwable#getCause()} method.
18551830
*/
1856-
@CallerSensitive
18571831
public static ClassLoader getSystemClassLoader() {
18581832
switch (VM.initLevel()) {
18591833
case 0:
@@ -1867,11 +1841,6 @@ public static ClassLoader getSystemClassLoader() {
18671841
default:
18681842
// system fully initialized
18691843
assert VM.isBooted() && scl != null;
1870-
@SuppressWarnings("removal")
1871-
SecurityManager sm = System.getSecurityManager();
1872-
if (sm != null) {
1873-
checkClassLoaderPermission(scl, Reflection.getCallerClass());
1874-
}
18751844
return scl;
18761845
}
18771846
}
@@ -1902,8 +1871,6 @@ static synchronized ClassLoader initSystemClassLoader() {
19021871
}
19031872

19041873
ClassLoader builtinLoader = getBuiltinAppClassLoader();
1905-
1906-
// All are privileged frames. No need to call doPrivileged.
19071874
String cn = System.getProperty("java.system.class.loader");
19081875
if (cn != null) {
19091876
try {
@@ -1930,36 +1897,6 @@ static synchronized ClassLoader initSystemClassLoader() {
19301897
return scl;
19311898
}
19321899

1933-
// Returns true if the specified class loader can be found in this class
1934-
// loader's delegation chain.
1935-
boolean isAncestor(ClassLoader cl) {
1936-
ClassLoader acl = this;
1937-
do {
1938-
acl = acl.parent;
1939-
if (cl == acl) {
1940-
return true;
1941-
}
1942-
} while (acl != null);
1943-
return false;
1944-
}
1945-
1946-
// Tests if class loader access requires "getClassLoader" permission
1947-
// check. A class loader 'from' can access class loader 'to' if
1948-
// class loader 'from' is same as class loader 'to' or an ancestor
1949-
// of 'to'. The class loader in a system domain can access
1950-
// any class loader.
1951-
private static boolean needsClassLoaderPermissionCheck(ClassLoader from,
1952-
ClassLoader to)
1953-
{
1954-
if (from == to)
1955-
return false;
1956-
1957-
if (from == null)
1958-
return false;
1959-
1960-
return !to.isAncestor(from);
1961-
}
1962-
19631900
// Returns the class's class loader, or null if none.
19641901
static ClassLoader getClassLoader(Class<?> caller) {
19651902
// This can be null if the VM is requesting it
@@ -1970,23 +1907,6 @@ static ClassLoader getClassLoader(Class<?> caller) {
19701907
return caller.getClassLoader0();
19711908
}
19721909

1973-
/*
1974-
* Checks RuntimePermission("getClassLoader") permission
1975-
* if caller's class loader is not null and caller's class loader
1976-
* is not the same as or an ancestor of the given cl argument.
1977-
*/
1978-
static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) {
1979-
@SuppressWarnings("removal")
1980-
SecurityManager sm = System.getSecurityManager();
1981-
if (sm != null) {
1982-
// caller can be null if the VM is requesting it
1983-
ClassLoader ccl = getClassLoader(caller);
1984-
if (needsClassLoaderPermissionCheck(ccl, cl)) {
1985-
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
1986-
}
1987-
}
1988-
}
1989-
19901910
// The system class loader
19911911
// @GuardedBy("ClassLoader.class")
19921912
private static volatile ClassLoader scl;

src/java.base/share/classes/jdk/internal/loader/BootLoader.java

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 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
@@ -32,8 +32,6 @@
3232
import java.net.URL;
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
35-
import java.security.AccessController;
36-
import java.security.PrivilegedAction;
3735
import java.util.Arrays;
3836
import java.util.Enumeration;
3937
import java.util.concurrent.ConcurrentHashMap;
@@ -143,18 +141,8 @@ public static Class<?> loadClass(Module module, String name) {
143141
/**
144142
* Loads a native library from the system library path.
145143
*/
146-
@SuppressWarnings("removal")
147144
public static void loadLibrary(String name) {
148-
if (System.getSecurityManager() == null) {
149-
BootLoader.getNativeLibraries().loadLibrary(name);
150-
} else {
151-
AccessController.doPrivileged(new java.security.PrivilegedAction<>() {
152-
public Void run() {
153-
BootLoader.getNativeLibraries().loadLibrary(name);
154-
return null;
155-
}
156-
});
157-
}
145+
getNativeLibraries().loadLibrary(name);
158146
}
159147

160148
/**
@@ -294,38 +282,28 @@ private static Module findModule(String location) {
294282
/**
295283
* Returns URL if the given location is a regular file path.
296284
*/
297-
@SuppressWarnings("removal")
298285
private static URL toFileURL(String location) {
299-
return AccessController.doPrivileged(new PrivilegedAction<>() {
300-
public URL run() {
301-
Path path = Path.of(location);
302-
if (Files.isRegularFile(path)) {
303-
try {
304-
return path.toUri().toURL();
305-
} catch (MalformedURLException e) {}
306-
}
307-
return null;
308-
}
309-
});
286+
Path path = Path.of(location);
287+
if (Files.isRegularFile(path)) {
288+
try {
289+
return path.toUri().toURL();
290+
} catch (MalformedURLException e) {}
291+
}
292+
return null;
310293
}
311294

312295
/**
313296
* Returns the Manifest if the given location is a JAR file
314297
* containing a manifest.
315298
*/
316-
@SuppressWarnings("removal")
317299
private static Manifest getManifest(String location) {
318-
return AccessController.doPrivileged(new PrivilegedAction<>() {
319-
public Manifest run() {
320-
Path jar = Path.of(location);
321-
try (InputStream in = Files.newInputStream(jar);
322-
JarInputStream jis = new JarInputStream(in, false)) {
323-
return jis.getManifest();
324-
} catch (IOException e) {
325-
return null;
326-
}
327-
}
328-
});
300+
Path jar = Path.of(location);
301+
try (InputStream in = Files.newInputStream(jar);
302+
JarInputStream jis = new JarInputStream(in, false)) {
303+
return jis.getManifest();
304+
} catch (IOException e) {
305+
return null;
306+
}
329307
}
330308
}
331309

0 commit comments

Comments
 (0)