jdk/src/share/classes/sun/applet/AppletSecurity.java
author leonidr
Thu, 21 Mar 2013 02:13:49 +0400
changeset 18202 e9a6d59491ab
parent 5506 202f599c92aa
child 19807 9f7860fad128
permissions -rw-r--r--
8004584: Augment applet contextualization Summary: Do not create the main AppContext for applets Reviewed-by: art, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.awt.AWTSecurityManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.provider.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class defines an applet security policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class AppletSecurity extends AWTSecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    //URLClassLoader.acc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static Field facc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    //AccessControlContext.context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static Field fcontext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            facc = URLClassLoader.class.getDeclaredField("acc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            facc.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            fcontext = AccessControlContext.class.getDeclaredField("context");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            fcontext.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } catch (NoSuchFieldException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new UnsupportedOperationException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Construct and initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public AppletSecurity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Cache to store known restricted packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private HashSet restrictedPackages = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Reset from Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Clear cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        restrictedPackages.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            public Object run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                // Enumerate system properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                Enumeration e = System.getProperties().propertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                while (e.hasMoreElements())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    String name = (String) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    if (name != null && name.startsWith("package.restrict.access."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        String value = System.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        if (value != null && value.equalsIgnoreCase("true"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            String pkg = name.substring(24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            // Cache restricted packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            restrictedPackages.add(pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * get the current (first) instance of an AppletClassLoader on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private AppletClassLoader currentAppletClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // try currentClassLoader first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ClassLoader loader = currentClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if ((loader == null) || (loader instanceof AppletClassLoader))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return (AppletClassLoader)loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // if that fails, get all the classes on the stack and check them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Class[] context = getClassContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            loader = context[i].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (loader instanceof AppletClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return (AppletClassLoader)loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * fix bug # 6433620 the logic here is : try to find URLClassLoader from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * class context, check its AccessControlContext to see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * AppletClassLoader is in stack when it's created. for this kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * URLClassLoader, return the AppContext assocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * AppletClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (int i = 0; i < context.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            final ClassLoader currentLoader = context[i].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (currentLoader instanceof URLClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                loader = (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        AccessControlContext acc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        ProtectionDomain[] pds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            acc = (AccessControlContext) facc.get(currentLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            if (acc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            pds = (ProtectionDomain[]) fcontext.get(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            if (pds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            throw new UnsupportedOperationException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        for (int i=0; i<pds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            ClassLoader cl = pds[i].getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            if (cl instanceof AppletClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                    return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    return (AppletClassLoader) loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // if that fails, try the context class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        loader = Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (loader instanceof AppletClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return (AppletClassLoader)loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // no AppletClassLoaders on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return (AppletClassLoader)null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns true if this threadgroup is in the applet's own thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * group. This will return false if there is no current class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected boolean inThreadGroup(ThreadGroup g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (currentAppletClassLoader() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return getThreadGroup().parentOf(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns true of the threadgroup of thread is in the applet's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * own threadgroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected boolean inThreadGroup(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return inThreadGroup(thread.getThreadGroup());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Applets are not allowed to manipulate threads outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * applet thread groups. However a terminated thread no longer belongs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * to any group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void checkAccess(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /* When multiple applets is reloaded simultaneously, there will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * multiple invocations to this method from plugin's SecurityManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * This method should not be synchronized to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * a page with multiple applets is reloaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if ((t.getState() != Thread.State.TERMINATED) && !inThreadGroup(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private boolean inThreadGroupCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Applets are not allowed to manipulate thread groups outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * applet thread groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public synchronized void checkAccess(ThreadGroup g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (inThreadGroupCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // if we are in a recursive check, it is because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // inThreadGroup is calling appletLoader.getThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // in that case, only do the super check, as appletLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // has a begin/endPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                inThreadGroupCheck = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (!inThreadGroup(g)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                inThreadGroupCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * calling thread is not allowed to access the package specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * This method is used by the <code>loadClass</code> method of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * The <code>checkPackageAccess</code> method for class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>SecurityManager</code>  calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>RuntimePermission("accessClassInPackage."+pkg)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param      pkg   the package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception  SecurityException  if the caller does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *             permission to access the specified package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @see        java.lang.ClassLoader#loadClass(java.lang.String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void checkPackageAccess(final String pkgname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // first see if the VM-wide policy allows access to this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        super.checkPackageAccess(pkgname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // now check the list of restricted packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (Iterator iter = restrictedPackages.iterator(); iter.hasNext();)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            String pkg = (String) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // Prevent matching "sun" and "sunir" even if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // starts with similar beginning characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (pkgname.equals(pkg) || pkgname.startsWith(pkg + "."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                checkPermission(new java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            ("accessClassInPackage." + pkgname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Tests if a client can get access to the AWT event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>AWTPermission("accessEventQueue")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @exception  SecurityException  if the caller does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *             permission to accesss the AWT event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void checkAwtEventQueueAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        AppletClassLoader appletClassLoader = currentAppletClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 5506
diff changeset
   313
        if (AppContext.isMainContext(appContext) && (appletClassLoader != null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // If we're about to allow access to the main EventQueue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // and anything untrusted is on the class context stack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // disallow access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            super.checkAwtEventQueueAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    } // checkAwtEventQueueAccess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the thread group of the applet. We consult the classloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public ThreadGroup getThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /* If any applet code is on the execution stack, we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
           that applet's ThreadGroup.  Otherwise, we use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
           behavior. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        AppletClassLoader appletLoader = currentAppletClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        ThreadGroup loaderGroup = (appletLoader == null) ? null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                          : appletLoader.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (loaderGroup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return loaderGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return super.getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    } // getThreadGroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
      * Get the AppContext corresponding to the current context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
      * The default implementation returns null, but this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
      * may be overridden by various SecurityManagers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
      * (e.g. AppletSecurity) to index AppContext objects by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      * calling context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
      * @return  the AppContext corresponding to the current context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
      * @see     sun.awt.AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
      * @see     java.lang.SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
      * @since   JDK1.2.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public AppContext getAppContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        AppletClassLoader appletLoader = currentAppletClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (appletLoader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            AppContext context =  appletLoader.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // context == null when some thread in applet thread group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // has not been destroyed in AppContext.dispose()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (context == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw new SecurityException("Applet classloader has invalid AppContext");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
} // class AppletSecurity