8345565: Remove remaining SecurityManager motivated APIs from sun.reflect.util

Reviewed-by: mullan, rriggs, liach
This commit is contained in:
Alan Bateman 2024-12-05 15:22:00 +00:00
parent 97b8a09bda
commit 691e692149
10 changed files with 7 additions and 258 deletions

View file

@ -42,7 +42,6 @@ import jdk.internal.access.SharedSecrets;
import jdk.internal.event.DeserializationEvent; import jdk.internal.event.DeserializationEvent;
import jdk.internal.misc.Unsafe; import jdk.internal.misc.Unsafe;
import jdk.internal.util.ByteArray; import jdk.internal.util.ByteArray;
import sun.reflect.misc.ReflectUtil;
/** /**
* An ObjectInputStream deserializes primitive data and objects previously * An ObjectInputStream deserializes primitive data and objects previously
@ -1828,12 +1827,6 @@ public class ObjectInputStream
}; };
} }
private boolean isCustomSubclass() {
// Return true if this class is a custom subclass of ObjectInputStream
return getClass().getClassLoader()
!= ObjectInputStream.class.getClassLoader();
}
/** /**
* Reads in and returns class descriptor for a dynamic proxy class. Sets * Reads in and returns class descriptor for a dynamic proxy class. Sets
* passHandle to proxy class descriptor's assigned handle. If proxy class * passHandle to proxy class descriptor's assigned handle. If proxy class
@ -1879,12 +1872,6 @@ public class ObjectInputStream
} else if (!Proxy.isProxyClass(cl)) { } else if (!Proxy.isProxyClass(cl)) {
throw new InvalidClassException("Not a proxy"); throw new InvalidClassException("Not a proxy");
} else { } else {
// ReflectUtil.checkProxyPackageAccess makes a test
// equivalent to isCustomSubclass so there's no need
// to condition this call to isCustomSubclass == true here.
ReflectUtil.checkProxyPackageAccess(
getClass().getClassLoader(),
cl.getInterfaces());
// Filter the interfaces // Filter the interfaces
for (Class<?> clazz : cl.getInterfaces()) { for (Class<?> clazz : cl.getInterfaces()) {
filterCheck(clazz, -1); filterCheck(clazz, -1);
@ -1954,12 +1941,9 @@ public class ObjectInputStream
Class<?> cl = null; Class<?> cl = null;
ClassNotFoundException resolveEx = null; ClassNotFoundException resolveEx = null;
bin.setBlockDataMode(true); bin.setBlockDataMode(true);
final boolean checksRequired = isCustomSubclass();
try { try {
if ((cl = resolveClass(readDesc)) == null) { if ((cl = resolveClass(readDesc)) == null) {
resolveEx = new ClassNotFoundException("null class"); resolveEx = new ClassNotFoundException("null class");
} else if (checksRequired) {
ReflectUtil.checkPackageAccess(cl);
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
resolveEx = ex; resolveEx = ex;

View file

@ -35,7 +35,6 @@ import java.util.StringJoiner;
import jdk.internal.util.ByteArray; import jdk.internal.util.ByteArray;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import sun.reflect.misc.ReflectUtil;
import static jdk.internal.util.ModifiedUtf.putChar; import static jdk.internal.util.ModifiedUtf.putChar;
import static jdk.internal.util.ModifiedUtf.utfLen; import static jdk.internal.util.ModifiedUtf.utfLen;
@ -1170,12 +1169,6 @@ public class ObjectOutputStream
} }
} }
private boolean isCustomSubclass() {
// Return true if this class is a custom subclass of ObjectOutputStream
return getClass().getClassLoader()
!= ObjectOutputStream.class.getClassLoader();
}
/** /**
* Writes class descriptor representing a dynamic proxy class to stream. * Writes class descriptor representing a dynamic proxy class to stream.
*/ */
@ -1193,9 +1186,6 @@ public class ObjectOutputStream
} }
bout.setBlockDataMode(true); bout.setBlockDataMode(true);
if (isCustomSubclass()) {
ReflectUtil.checkPackageAccess(cl);
}
annotateProxyClass(cl); annotateProxyClass(cl);
bout.setBlockDataMode(false); bout.setBlockDataMode(false);
bout.writeByte(TC_ENDBLOCKDATA); bout.writeByte(TC_ENDBLOCKDATA);
@ -1222,9 +1212,6 @@ public class ObjectOutputStream
Class<?> cl = desc.forClass(); Class<?> cl = desc.forClass();
bout.setBlockDataMode(true); bout.setBlockDataMode(true);
if (cl != null && isCustomSubclass()) {
ReflectUtil.checkPackageAccess(cl);
}
annotateClass(cl); annotateClass(cl);
bout.setBlockDataMode(false); bout.setBlockDataMode(false);
bout.writeByte(TC_ENDBLOCKDATA); bout.writeByte(TC_ENDBLOCKDATA);

View file

@ -60,7 +60,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -91,7 +90,6 @@ import sun.reflect.generics.repository.MethodRepository;
import sun.reflect.generics.repository.ConstructorRepository; import sun.reflect.generics.repository.ConstructorRepository;
import sun.reflect.generics.scope.ClassScope; import sun.reflect.generics.scope.ClassScope;
import sun.reflect.annotation.*; import sun.reflect.annotation.*;
import sun.reflect.misc.ReflectUtil;
/** /**
* Instances of the class {@code Class} represent classes and * Instances of the class {@code Class} represent classes and

View file

@ -52,7 +52,6 @@ import jdk.internal.module.Modules;
import jdk.internal.misc.VM; import jdk.internal.misc.VM;
import jdk.internal.loader.ClassLoaderValue; import jdk.internal.loader.ClassLoaderValue;
import jdk.internal.vm.annotation.Stable; import jdk.internal.vm.annotation.Stable;
import sun.reflect.misc.ReflectUtil;
import static java.lang.invoke.MethodType.methodType; import static java.lang.invoke.MethodType.methodType;
import static java.lang.module.ModuleDescriptor.Modifier.SYNTHETIC; import static java.lang.module.ModuleDescriptor.Modifier.SYNTHETIC;
@ -984,7 +983,7 @@ public class Proxy implements java.io.Serializable {
return ih; return ih;
} }
private static final String PROXY_PACKAGE_PREFIX = ReflectUtil.PROXY_PACKAGE; private static final String PROXY_PACKAGE_PREFIX = "com.sun.proxy";
/** /**
* A cache of Method -> MethodHandle for default methods. * A cache of Method -> MethodHandle for default methods.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -29,7 +29,6 @@ import java.lang.annotation.*;
import java.lang.reflect.AnnotatedType; import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.GenericDeclaration; import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable; import java.lang.reflect.TypeVariable;
@ -41,7 +40,6 @@ import sun.reflect.annotation.TypeAnnotationParser;
import sun.reflect.annotation.AnnotationType; import sun.reflect.annotation.AnnotationType;
import sun.reflect.generics.factory.GenericsFactory; import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.tree.FieldTypeSignature; import sun.reflect.generics.tree.FieldTypeSignature;
import sun.reflect.misc.ReflectUtil;
/** /**
* Implementation of {@code java.lang.reflect.TypeVariable} interface * Implementation of {@code java.lang.reflect.TypeVariable} interface
@ -135,13 +133,9 @@ public class TypeVariableImpl<D extends GenericDeclaration>
* @since 1.5 * @since 1.5
*/ */
public D getGenericDeclaration() { public D getGenericDeclaration() {
if (genericDeclaration instanceof Class<?> c) assert genericDeclaration instanceof Class<?> ||
ReflectUtil.checkPackageAccess(c); genericDeclaration instanceof Method ||
else if ((genericDeclaration instanceof Method) || genericDeclaration instanceof Constructor : "Unexpected kind of GenericDeclaration";
(genericDeclaration instanceof Constructor))
ReflectUtil.conservativeCheckMemberAccess((Member)genericDeclaration);
else
throw new AssertionError("Unexpected kind of GenericDeclaration");
return genericDeclaration; return genericDeclaration;
} }

View file

@ -1,45 +0,0 @@
/*
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.reflect.misc;
import java.lang.reflect.Constructor;
public final class ConstructorUtil {
private ConstructorUtil() {
}
public static Constructor<?> getConstructor(Class<?> cls, Class<?>[] params)
throws NoSuchMethodException {
ReflectUtil.checkPackageAccess(cls);
return cls.getConstructor(params);
}
public static Constructor<?>[] getConstructors(Class<?> cls) {
ReflectUtil.checkPackageAccess(cls);
return cls.getConstructors();
}
}

View file

@ -1,48 +0,0 @@
/*
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.reflect.misc;
import java.lang.reflect.Field;
/*
* Create a trampoline class.
*/
public final class FieldUtil {
private FieldUtil() {
}
public static Field getField(Class<?> cls, String name)
throws NoSuchFieldException {
ReflectUtil.checkPackageAccess(cls);
return cls.getField(name);
}
public static Field[] getFields(Class<?> cls) {
ReflectUtil.checkPackageAccess(cls);
return cls.getFields();
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -79,15 +79,9 @@ public final class MethodUtil extends SecureClassLoader {
public static Method getMethod(Class<?> cls, String name, Class<?>[] args) public static Method getMethod(Class<?> cls, String name, Class<?>[] args)
throws NoSuchMethodException { throws NoSuchMethodException {
ReflectUtil.checkPackageAccess(cls);
return cls.getMethod(name, args); return cls.getMethod(name, args);
} }
public static Method[] getMethods(Class<?> cls) {
ReflectUtil.checkPackageAccess(cls);
return cls.getMethods();
}
/* /*
* Bounce through the trampoline. * Bounce through the trampoline.
*/ */
@ -140,7 +134,6 @@ public final class MethodUtil extends SecureClassLoader {
throws ClassNotFoundException throws ClassNotFoundException
{ {
// First, check if the class has already been loaded // First, check if the class has already been loaded
ReflectUtil.checkPackageAccess(name);
Class<?> c = findLoadedClass(name); Class<?> c = findLoadedClass(name);
if (c == null) { if (c == null) {
try { try {

View file

@ -25,10 +25,6 @@
package sun.reflect.misc; package sun.reflect.misc;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import jdk.internal.reflect.Reflection; import jdk.internal.reflect.Reflection;
public final class ReflectUtil { public final class ReflectUtil {
@ -67,112 +63,4 @@ public final class ReflectUtil {
target == null ? null : target.getClass(), target == null ? null : target.getClass(),
modifiers); modifiers);
} }
/**
* Does nothing.
*/
public static void conservativeCheckMemberAccess(Member m) {
}
/**
* Does nothing.
*/
public static void checkPackageAccess(Class<?> clazz) {
}
/**
* Does nothing
*/
public static void checkPackageAccess(String name) {
}
/**
* Returns true.
*/
public static boolean isPackageAccessible(Class<?> clazz) {
return true;
}
/**
* Returns false.
*/
public static boolean needsPackageAccessCheck(ClassLoader from, ClassLoader to) {
return false;
}
/**
* Does nothing
*/
public static void checkProxyPackageAccess(Class<?> clazz) {
}
/**
* Does nothing.
*/
public static void checkProxyPackageAccess(ClassLoader ccl,
Class<?>... interfaces) {
}
// Note that bytecode instrumentation tools may exclude 'sun.*'
// classes but not generated proxy classes and so keep it in com.sun.*
public static final String PROXY_PACKAGE = "com.sun.proxy";
/**
* Test if the given class is a proxy class that implements
* non-public interface. Such proxy class may be in a non-restricted
* package that bypasses checkPackageAccess.
*/
public static boolean isNonPublicProxyClass(Class<?> cls) {
if (!Proxy.isProxyClass(cls)) {
return false;
}
return !Modifier.isPublic(cls.getModifiers());
}
/**
* Check if the given method is a method declared in the proxy interface
* implemented by the given proxy instance.
*
* @param proxy a proxy instance
* @param method an interface method dispatched to a InvocationHandler
*
* @throws IllegalArgumentException if the given proxy or method is invalid.
*/
public static void checkProxyMethod(Object proxy, Method method) {
// check if it is a valid proxy instance
if (proxy == null || !Proxy.isProxyClass(proxy.getClass())) {
throw new IllegalArgumentException("Not a Proxy instance");
}
if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException("Can't handle static method");
}
Class<?> c = method.getDeclaringClass();
if (c == Object.class) {
String name = method.getName();
if (name.equals("hashCode") || name.equals("equals") || name.equals("toString")) {
return;
}
}
if (isSuperInterface(proxy.getClass(), c)) {
return;
}
// disallow any method not declared in one of the proxy interfaces
throw new IllegalArgumentException("Can't handle: " + method);
}
private static boolean isSuperInterface(Class<?> c, Class<?> intf) {
for (Class<?> i : c.getInterfaces()) {
if (i == intf) {
return true;
}
if (isSuperInterface(i, intf)) {
return true;
}
}
return false;
}
} }

View file

@ -46,7 +46,6 @@ import javax.management.ReflectionException;
import javax.management.RuntimeErrorException; import javax.management.RuntimeErrorException;
import javax.management.RuntimeMBeanException; import javax.management.RuntimeMBeanException;
import javax.management.RuntimeOperationsException; import javax.management.RuntimeOperationsException;
import sun.reflect.misc.ConstructorUtil;
/** /**
* Implements the MBeanInstantiator interface. Provides methods for * Implements the MBeanInstantiator interface. Provides methods for
@ -681,7 +680,7 @@ public class MBeanInstantiator {
private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) { private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) {
try { try {
return ConstructorUtil.getConstructor(c, params); return c.getConstructor(params);
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }