2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2020-09-13 14:45:12 +00:00
|
|
|
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* 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
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2007-12-01 00:00:00 +00:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package java.security;
|
|
|
|
|
2015-07-20 09:03:03 -04:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
2014-11-20 17:52:18 +08:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.function.Function;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
import sun.security.util.Debug;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class extends ClassLoader with additional support for defining
|
|
|
|
* classes with an associated code source and permissions which are
|
|
|
|
* retrieved by the system policy by default.
|
|
|
|
*
|
|
|
|
* @author Li Gong
|
|
|
|
* @author Roland Schemers
|
2017-05-31 19:54:16 -07:00
|
|
|
* @since 1.2
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
public class SecureClassLoader extends ClassLoader {
|
|
|
|
|
2015-06-12 12:50:41 -04:00
|
|
|
/*
|
2015-07-20 09:03:03 -04:00
|
|
|
* Map that maps the CodeSource to a ProtectionDomain. The key is a
|
|
|
|
* CodeSourceKey class that uses a String instead of a URL to avoid
|
2015-06-12 12:50:41 -04:00
|
|
|
* potential expensive name service lookups. This does mean that URLs that
|
|
|
|
* are equivalent after nameservice lookup will be placed in separate
|
|
|
|
* ProtectionDomains; however during policy enforcement these URLs will be
|
|
|
|
* canonicalized and resolved resulting in a consistent set of granted
|
|
|
|
* permissions.
|
|
|
|
*/
|
2015-07-20 09:03:03 -04:00
|
|
|
private final Map<CodeSourceKey, ProtectionDomain> pdcache
|
2014-11-20 17:52:18 +08:00
|
|
|
= new ConcurrentHashMap<>(11);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2009-04-06 18:46:20 -07:00
|
|
|
static {
|
|
|
|
ClassLoader.registerAsParallelCapable();
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* Creates a new SecureClassLoader using the specified parent
|
|
|
|
* class loader for delegation.
|
|
|
|
*
|
|
|
|
* <p>If there is a security manager, this method first
|
2013-06-28 10:48:02 -07:00
|
|
|
* calls the security manager's {@code checkCreateClassLoader}
|
2007-12-01 00:00:00 +00:00
|
|
|
* method to ensure creation of a class loader is allowed.
|
2015-03-16 19:09:13 +04:00
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* @param parent the parent ClassLoader
|
2019-09-20 11:07:52 +01:00
|
|
|
* @throws SecurityException if a security manager exists and its
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code checkCreateClassLoader} method doesn't allow
|
2007-12-01 00:00:00 +00:00
|
|
|
* creation of a class loader.
|
|
|
|
* @see SecurityManager#checkCreateClassLoader
|
|
|
|
*/
|
|
|
|
protected SecureClassLoader(ClassLoader parent) {
|
|
|
|
super(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new SecureClassLoader using the default parent class
|
|
|
|
* loader for delegation.
|
|
|
|
*
|
|
|
|
* <p>If there is a security manager, this method first
|
2013-06-28 10:48:02 -07:00
|
|
|
* calls the security manager's {@code checkCreateClassLoader}
|
2007-12-01 00:00:00 +00:00
|
|
|
* method to ensure creation of a class loader is allowed.
|
|
|
|
*
|
2019-09-20 11:07:52 +01:00
|
|
|
* @throws SecurityException if a security manager exists and its
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code checkCreateClassLoader} method doesn't allow
|
2007-12-01 00:00:00 +00:00
|
|
|
* creation of a class loader.
|
|
|
|
* @see SecurityManager#checkCreateClassLoader
|
|
|
|
*/
|
|
|
|
protected SecureClassLoader() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:08:28 -07:00
|
|
|
/**
|
|
|
|
* Creates a new {@code SecureClassLoader} of the specified name and
|
|
|
|
* using the specified parent class loader for delegation.
|
|
|
|
*
|
|
|
|
* @param name class loader name; or {@code null} if not named
|
|
|
|
* @param parent the parent class loader
|
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException if the given name is empty.
|
|
|
|
*
|
|
|
|
* @throws SecurityException if a security manager exists and its
|
|
|
|
* {@link SecurityManager#checkCreateClassLoader()} method
|
|
|
|
* doesn't allow creation of a class loader.
|
|
|
|
*
|
|
|
|
* @since 9
|
|
|
|
*/
|
|
|
|
protected SecureClassLoader(String name, ClassLoader parent) {
|
|
|
|
super(name, parent);
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/**
|
|
|
|
* Converts an array of bytes into an instance of class Class,
|
|
|
|
* with an optional CodeSource. Before the
|
|
|
|
* class can be used it must be resolved.
|
|
|
|
* <p>
|
|
|
|
* If a non-null CodeSource is supplied a ProtectionDomain is
|
|
|
|
* constructed and associated with the class being defined.
|
2015-03-16 19:09:13 +04:00
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* @param name the expected name of the class, or {@code null}
|
2007-12-01 00:00:00 +00:00
|
|
|
* if not known, using '.' and not '/' as the separator
|
|
|
|
* and without a trailing ".class" suffix.
|
|
|
|
* @param b the bytes that make up the class data. The bytes in
|
2013-06-28 10:48:02 -07:00
|
|
|
* positions {@code off} through {@code off+len-1}
|
2011-04-13 12:16:13 -07:00
|
|
|
* should have the format of a valid class file as defined by
|
8249205: Remove unnecessary trademark symbols
Reviewed-by: darcy, lancea, bpb, prr, jjg, cjplummer, iris, dfuchs, weijun, joehw, wetmore, bchristi
2020-07-15 14:21:54 -07:00
|
|
|
* <cite>The Java Virtual Machine Specification</cite>.
|
2013-06-28 10:48:02 -07:00
|
|
|
* @param off the start offset in {@code b} of the class data
|
2007-12-01 00:00:00 +00:00
|
|
|
* @param len the length of the class data
|
2013-06-28 10:48:02 -07:00
|
|
|
* @param cs the associated CodeSource, or {@code null} if none
|
|
|
|
* @return the {@code Class} object created from the data,
|
2007-12-01 00:00:00 +00:00
|
|
|
* and optional CodeSource.
|
2019-09-20 11:07:52 +01:00
|
|
|
* @throws ClassFormatError if the data did not contain a valid class
|
|
|
|
* @throws IndexOutOfBoundsException if either {@code off} or
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code len} is negative, or if
|
|
|
|
* {@code off+len} is greater than {@code b.length}.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
2019-09-20 11:07:52 +01:00
|
|
|
* @throws SecurityException if an attempt is made to add this class
|
2007-12-01 00:00:00 +00:00
|
|
|
* to a package that contains classes that were signed by
|
|
|
|
* a different set of certificates than this class, or if
|
|
|
|
* the class name begins with "java.".
|
|
|
|
*/
|
|
|
|
protected final Class<?> defineClass(String name,
|
|
|
|
byte[] b, int off, int len,
|
|
|
|
CodeSource cs)
|
|
|
|
{
|
2009-04-06 18:46:20 -07:00
|
|
|
return defineClass(name, b, off, len, getProtectionDomain(cs));
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-28 10:48:02 -07:00
|
|
|
* Converts a {@link java.nio.ByteBuffer ByteBuffer}
|
|
|
|
* into an instance of class {@code Class}, with an optional CodeSource.
|
2007-12-01 00:00:00 +00:00
|
|
|
* Before the class can be used it must be resolved.
|
|
|
|
* <p>
|
|
|
|
* If a non-null CodeSource is supplied a ProtectionDomain is
|
|
|
|
* constructed and associated with the class being defined.
|
2015-03-16 19:09:13 +04:00
|
|
|
*
|
2013-06-28 10:48:02 -07:00
|
|
|
* @param name the expected name of the class, or {@code null}
|
2007-12-01 00:00:00 +00:00
|
|
|
* if not known, using '.' and not '/' as the separator
|
|
|
|
* and without a trailing ".class" suffix.
|
|
|
|
* @param b the bytes that make up the class data. The bytes from positions
|
2013-06-28 10:48:02 -07:00
|
|
|
* {@code b.position()} through {@code b.position() + b.limit() -1}
|
2011-04-13 12:16:13 -07:00
|
|
|
* should have the format of a valid class file as defined by
|
8249205: Remove unnecessary trademark symbols
Reviewed-by: darcy, lancea, bpb, prr, jjg, cjplummer, iris, dfuchs, weijun, joehw, wetmore, bchristi
2020-07-15 14:21:54 -07:00
|
|
|
* <cite>The Java Virtual Machine Specification</cite>.
|
2013-06-28 10:48:02 -07:00
|
|
|
* @param cs the associated CodeSource, or {@code null} if none
|
|
|
|
* @return the {@code Class} object created from the data,
|
2007-12-01 00:00:00 +00:00
|
|
|
* and optional CodeSource.
|
2019-09-20 11:07:52 +01:00
|
|
|
* @throws ClassFormatError if the data did not contain a valid class
|
|
|
|
* @throws SecurityException if an attempt is made to add this class
|
2007-12-01 00:00:00 +00:00
|
|
|
* to a package that contains classes that were signed by
|
|
|
|
* a different set of certificates than this class, or if
|
|
|
|
* the class name begins with "java.".
|
|
|
|
*
|
|
|
|
* @since 1.5
|
|
|
|
*/
|
|
|
|
protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
|
|
|
|
CodeSource cs)
|
|
|
|
{
|
2009-04-06 18:46:20 -07:00
|
|
|
return defineClass(name, b, getProtectionDomain(cs));
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the permissions for the given CodeSource object.
|
|
|
|
* <p>
|
|
|
|
* This method is invoked by the defineClass method which takes
|
|
|
|
* a CodeSource as an argument when it is constructing the
|
|
|
|
* ProtectionDomain for the class being defined.
|
2015-03-16 19:09:13 +04:00
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* @param codesource the codesource.
|
|
|
|
*
|
|
|
|
* @return the permissions granted to the codesource.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected PermissionCollection getPermissions(CodeSource codesource)
|
|
|
|
{
|
|
|
|
return new Permissions(); // ProtectionDomain defers the binding
|
|
|
|
}
|
|
|
|
|
2016-05-10 21:19:25 -07:00
|
|
|
/*
|
|
|
|
* holder class for the static field "debug" to delay its initialization
|
|
|
|
*/
|
|
|
|
private static class DebugHolder {
|
|
|
|
private static final Debug debug = Debug.getInstance("scl");
|
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* Returned cached ProtectionDomain for the specified CodeSource.
|
|
|
|
*/
|
|
|
|
private ProtectionDomain getProtectionDomain(CodeSource cs) {
|
2015-06-12 12:50:41 -04:00
|
|
|
if (cs == null) {
|
2007-12-01 00:00:00 +00:00
|
|
|
return null;
|
2015-06-12 12:50:41 -04:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2015-07-20 09:03:03 -04:00
|
|
|
// Use a CodeSourceKey object key. It should behave in the
|
|
|
|
// same manner as the CodeSource when compared for equality except
|
|
|
|
// that no nameservice lookup is done on the hostname (String comparison
|
2014-11-20 17:52:18 +08:00
|
|
|
// only), and the fragment is not considered.
|
2015-07-20 09:03:03 -04:00
|
|
|
CodeSourceKey key = new CodeSourceKey(cs);
|
2014-11-20 17:52:18 +08:00
|
|
|
return pdcache.computeIfAbsent(key, new Function<>() {
|
|
|
|
@Override
|
2015-07-20 09:03:03 -04:00
|
|
|
public ProtectionDomain apply(CodeSourceKey key /* not used */) {
|
2014-11-20 17:52:18 +08:00
|
|
|
PermissionCollection perms
|
|
|
|
= SecureClassLoader.this.getPermissions(cs);
|
|
|
|
ProtectionDomain pd = new ProtectionDomain(
|
|
|
|
cs, perms, SecureClassLoader.this, null);
|
2016-05-10 21:19:25 -07:00
|
|
|
if (DebugHolder.debug != null) {
|
|
|
|
DebugHolder.debug.println(" getPermissions " + pd);
|
|
|
|
DebugHolder.debug.println("");
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2014-11-20 17:52:18 +08:00
|
|
|
return pd;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2014-11-20 17:52:18 +08:00
|
|
|
});
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 09:03:03 -04:00
|
|
|
private static class CodeSourceKey {
|
|
|
|
private final CodeSource cs;
|
|
|
|
|
|
|
|
CodeSourceKey(CodeSource cs) {
|
|
|
|
this.cs = cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
String locationNoFrag = cs.getLocationNoFragString();
|
|
|
|
return locationNoFrag != null ? locationNoFrag.hashCode() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(obj instanceof CodeSourceKey)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeSourceKey csk = (CodeSourceKey) obj;
|
|
|
|
|
|
|
|
if (!Objects.equals(cs.getLocationNoFragString(),
|
|
|
|
csk.cs.getLocationNoFragString())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.matchCerts(csk.cs, true);
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 14:45:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by the VM, during -Xshare:dump
|
|
|
|
*/
|
|
|
|
private void resetArchivedStates() {
|
|
|
|
pdcache.clear();
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|