Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/applet/AppletSecurity.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.applet;2627import java.io.File;28import java.io.FilePermission;29import java.io.IOException;30import java.io.FileDescriptor;31import java.net.URL;32import java.net.URLClassLoader;33import java.net.InetAddress;34import java.net.UnknownHostException;35import java.net.SocketPermission;36import java.util.Enumeration;37import java.util.Iterator;38import java.util.HashSet;39import java.util.StringTokenizer;40import java.security.*;41import java.lang.reflect.*;42import sun.awt.AWTSecurityManager;43import sun.awt.AppContext;44import sun.security.provider.*;45import sun.security.util.SecurityConstants;464748/**49* This class defines an applet security policy50*51*/52public53class AppletSecurity extends AWTSecurityManager {5455//URLClassLoader.acc56private static Field facc = null;5758//AccessControlContext.context;59private static Field fcontext = null;6061static {62try {63facc = URLClassLoader.class.getDeclaredField("acc");64facc.setAccessible(true);65fcontext = AccessControlContext.class.getDeclaredField("context");66fcontext.setAccessible(true);67} catch (NoSuchFieldException e) {68throw new UnsupportedOperationException(e);69}70}717273/**74* Construct and initialize.75*/76public AppletSecurity() {77reset();78}7980// Cache to store known restricted packages81private HashSet restrictedPackages = new HashSet();8283/**84* Reset from Properties85*/86public void reset()87{88// Clear cache89restrictedPackages.clear();9091AccessController.doPrivileged(new PrivilegedAction() {92public Object run()93{94// Enumerate system properties95Enumeration e = System.getProperties().propertyNames();9697while (e.hasMoreElements())98{99String name = (String) e.nextElement();100101if (name != null && name.startsWith("package.restrict.access."))102{103String value = System.getProperty(name);104105if (value != null && value.equalsIgnoreCase("true"))106{107String pkg = name.substring(24);108109// Cache restricted packages110restrictedPackages.add(pkg);111}112}113}114return null;115}116});117}118119/**120* get the current (first) instance of an AppletClassLoader on the stack.121*/122private AppletClassLoader currentAppletClassLoader()123{124// try currentClassLoader first125ClassLoader loader = currentClassLoader();126127if ((loader == null) || (loader instanceof AppletClassLoader))128return (AppletClassLoader)loader;129130// if that fails, get all the classes on the stack and check them.131Class[] context = getClassContext();132for (int i = 0; i < context.length; i++) {133loader = context[i].getClassLoader();134if (loader instanceof AppletClassLoader)135return (AppletClassLoader)loader;136}137138/*139* fix bug # 6433620 the logic here is : try to find URLClassLoader from140* class context, check its AccessControlContext to see if141* AppletClassLoader is in stack when it's created. for this kind of142* URLClassLoader, return the AppContext associated with the143* AppletClassLoader.144*/145for (int i = 0; i < context.length; i++) {146final ClassLoader currentLoader = context[i].getClassLoader();147148if (currentLoader instanceof URLClassLoader) {149loader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {150public Object run() {151152AccessControlContext acc = null;153ProtectionDomain[] pds = null;154155try {156acc = (AccessControlContext) facc.get(currentLoader);157if (acc == null) {158return null;159}160161pds = (ProtectionDomain[]) fcontext.get(acc);162if (pds == null) {163return null;164}165} catch (Exception e) {166throw new UnsupportedOperationException(e);167}168169for (int i=0; i<pds.length; i++) {170ClassLoader cl = pds[i].getClassLoader();171172if (cl instanceof AppletClassLoader) {173return cl;174}175}176177return null;178}179});180181if (loader != null) {182return (AppletClassLoader) loader;183}184}185}186187// if that fails, try the context class loader188loader = Thread.currentThread().getContextClassLoader();189if (loader instanceof AppletClassLoader)190return (AppletClassLoader)loader;191192// no AppletClassLoaders on the stack193return (AppletClassLoader)null;194}195196/**197* Returns true if this threadgroup is in the applet's own thread198* group. This will return false if there is no current class199* loader.200*/201protected boolean inThreadGroup(ThreadGroup g) {202if (currentAppletClassLoader() == null)203return false;204else205return getThreadGroup().parentOf(g);206}207208/**209* Returns true of the threadgroup of thread is in the applet's210* own threadgroup.211*/212protected boolean inThreadGroup(Thread thread) {213return inThreadGroup(thread.getThreadGroup());214}215216/**217* Applets are not allowed to manipulate threads outside218* applet thread groups. However a terminated thread no longer belongs219* to any group.220*/221public void checkAccess(Thread t) {222/* When multiple applets is reloaded simultaneously, there will be223* multiple invocations to this method from plugin's SecurityManager.224* This method should not be synchronized to avoid deadlock when225* a page with multiple applets is reloaded226*/227if ((t.getState() != Thread.State.TERMINATED) && !inThreadGroup(t)) {228checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION);229}230}231232private boolean inThreadGroupCheck = false;233234/**235* Applets are not allowed to manipulate thread groups outside236* applet thread groups.237*/238public synchronized void checkAccess(ThreadGroup g) {239if (inThreadGroupCheck) {240// if we are in a recursive check, it is because241// inThreadGroup is calling appletLoader.getThreadGroup242// in that case, only do the super check, as appletLoader243// has a begin/endPrivileged244checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);245} else {246try {247inThreadGroupCheck = true;248if (!inThreadGroup(g)) {249checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);250}251} finally {252inThreadGroupCheck = false;253}254}255}256257258/**259* Throws a <code>SecurityException</code> if the260* calling thread is not allowed to access the package specified by261* the argument.262* <p>263* This method is used by the <code>loadClass</code> method of class264* loaders.265* <p>266* The <code>checkPackageAccess</code> method for class267* <code>SecurityManager</code> calls268* <code>checkPermission</code> with the269* <code>RuntimePermission("accessClassInPackage."+pkg)</code>270* permission.271*272* @param pkg the package name.273* @exception SecurityException if the caller does not have274* permission to access the specified package.275* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)276*/277public void checkPackageAccess(final String pkgname) {278279// first see if the VM-wide policy allows access to this package280super.checkPackageAccess(pkgname);281282// now check the list of restricted packages283for (Iterator iter = restrictedPackages.iterator(); iter.hasNext();)284{285String pkg = (String) iter.next();286287// Prevent matching "sun" and "sunir" even if they288// starts with similar beginning characters289//290if (pkgname.equals(pkg) || pkgname.startsWith(pkg + "."))291{292checkPermission(new java.lang.RuntimePermission293("accessClassInPackage." + pkgname));294}295}296}297298/**299* Tests if a client can get access to the AWT event queue.300* <p>301* This method calls <code>checkPermission</code> with the302* <code>AWTPermission("accessEventQueue")</code> permission.303*304* @since JDK1.1305* @exception SecurityException if the caller does not have306* permission to access the AWT event queue.307*/308public void checkAwtEventQueueAccess() {309AppContext appContext = AppContext.getAppContext();310AppletClassLoader appletClassLoader = currentAppletClassLoader();311312if (AppContext.isMainContext(appContext) && (appletClassLoader != null)) {313// If we're about to allow access to the main EventQueue,314// and anything untrusted is on the class context stack,315// disallow access.316super.checkPermission(SecurityConstants.AWT.CHECK_AWT_EVENTQUEUE_PERMISSION);317}318} // checkAwtEventQueueAccess()319320/**321* Returns the thread group of the applet. We consult the classloader322* if there is one.323*/324public ThreadGroup getThreadGroup() {325/* If any applet code is on the execution stack, we return326that applet's ThreadGroup. Otherwise, we use the default327behavior. */328AppletClassLoader appletLoader = currentAppletClassLoader();329ThreadGroup loaderGroup = (appletLoader == null) ? null330: appletLoader.getThreadGroup();331if (loaderGroup != null) {332return loaderGroup;333} else {334return super.getThreadGroup();335}336} // getThreadGroup()337338/**339* Get the AppContext corresponding to the current context.340* The default implementation returns null, but this method341* may be overridden by various SecurityManagers342* (e.g. AppletSecurity) to index AppContext objects by the343* calling context.344*345* @return the AppContext corresponding to the current context.346* @see sun.awt.AppContext347* @see java.lang.SecurityManager348* @since JDK1.2.1349*/350public AppContext getAppContext() {351AppletClassLoader appletLoader = currentAppletClassLoader();352353if (appletLoader == null) {354return null;355} else {356AppContext context = appletLoader.getAppContext();357358// context == null when some thread in applet thread group359// has not been destroyed in AppContext.dispose()360if (context == null) {361throw new SecurityException("Applet classloader has invalid AppContext");362}363364return context;365}366}367368} // class AppletSecurity369370371