jdk/src/java.base/share/classes/java/lang/SecurityManager.java
author alanb
Thu, 04 May 2017 07:26:55 +0000
changeset 45004 ea3137042a61
parent 44545 83b611b88ac8
child 45658 0c39b586b8fa
permissions -rw-r--r--
8178380: Module system implementation refresh (5/2017) Reviewed-by: mchung, alanb, sspitsyn Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, chris.hegarty@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
     2
 * Copyright (c) 1995, 2017, 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: 3956
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: 3956
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: 3956
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
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 java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    28
import java.lang.module.ModuleDescriptor;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    29
import java.lang.module.ModuleDescriptor.Exports;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    30
import java.lang.module.ModuleDescriptor.Opens;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    31
import java.lang.module.ModuleReference;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    32
import java.lang.reflect.Member;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FilePermission;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    36
import java.net.InetAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.SocketPermission;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    38
import java.security.AccessControlContext;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    39
import java.security.AccessController;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    40
import java.security.Permission;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    41
import java.security.PrivilegedAction;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    42
import java.security.Security;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    43
import java.security.SecurityPermission;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    44
import java.util.HashSet;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    45
import java.util.Map;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    46
import java.util.Objects;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    47
import java.util.PropertyPermission;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    48
import java.util.Set;
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    49
import java.util.concurrent.ConcurrentHashMap;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
    50
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    52
import jdk.internal.module.ModuleBootstrap;
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
    53
import jdk.internal.module.ModuleLoaderMap;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 31180
diff changeset
    54
import jdk.internal.reflect.CallerSensitive;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * The security manager is a class that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * applications to implement a security policy. It allows an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * application to determine, before performing a possibly unsafe or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * sensitive operation, what the operation is and whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * it is being attempted in a security context that allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * operation to be performed. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * application can allow or disallow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The <code>SecurityManager</code> class contains many methods with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * names that begin with the word <code>check</code>. These methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * are called by various methods in the Java libraries before those
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * methods perform certain potentially sensitive operations. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * invocation of such a <code>check</code> method typically looks like this:
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 19807
diff changeset
    71
 * <blockquote><pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         security.check<i>XXX</i>(argument, &nbsp;.&nbsp;.&nbsp;.&nbsp;);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * The security manager is thereby given an opportunity to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * completion of the operation by throwing an exception. A security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * manager routine simply returns if the operation is permitted, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * throws a <code>SecurityException</code> if the operation is not
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
    82
 * permitted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * The current security manager is set by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>setSecurityManager</code> method in class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <code>System</code>. The current security manager is obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * by the <code>getSecurityManager</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * The special method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * {@link SecurityManager#checkPermission(java.security.Permission)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * determines whether an access request indicated by a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * permission should be granted or denied. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * default implementation calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   AccessController.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * If a requested access is allowed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <code>checkPermission</code> returns quietly. If denied, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <code>SecurityException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * As of Java 2 SDK v1.2, the default implementation of each of the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <code>check</code> methods in <code>SecurityManager</code> is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * call the <code>SecurityManager checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * to determine if the calling thread has permission to perform the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Note that the <code>checkPermission</code> method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * just a single permission argument always performs security checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * within the context of the currently executing thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Sometimes a security check that should be made within a given context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * will actually need to be done from within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <i>different</i> context (for example, from within a worker thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * The {@link SecurityManager#getSecurityContext getSecurityContext} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * and the {@link SecurityManager#checkPermission(java.security.Permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * java.lang.Object) checkPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * method that includes a context argument are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * for this situation. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <code>getSecurityContext</code> method returns a "snapshot"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * of the current calling context. (The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * returns an AccessControlContext object.) A sample call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *   Object context = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *   SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *   if (sm != null) context = sm.getSecurityContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * The <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * that takes a context object in addition to a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * makes access decisions based on that context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * rather than on that of the current execution thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Code within a different context can thus call that method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * passing the permission and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * previously-saved context object. A sample call, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * SecurityManager <code>sm</code> obtained as in the previous example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * is the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *   if (sm != null) sm.checkPermission(permission, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p>Permissions fall into these categories: File, Socket, Net,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * Security, Runtime, Property, AWT, Reflect, and Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * The classes managing these various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * permission categories are <code>java.io.FilePermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <code>java.net.SocketPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <code>java.net.NetPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <code>java.security.SecurityPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <code>java.lang.RuntimePermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <code>java.util.PropertyPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <code>java.awt.AWTPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <code>java.lang.reflect.ReflectPermission</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <code>java.io.SerializablePermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p>All but the first two (FilePermission and SocketPermission) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * subclasses of <code>java.security.BasicPermission</code>, which itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * is an abstract subclass of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * top-level class for permissions, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <code>java.security.Permission</code>. BasicPermission defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * functionality needed for all permissions that contain a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * that follows the hierarchical property naming convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * (for example, "exitVM", "setFactory", "queuePrintJob", etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * An asterisk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * may appear at the end of the name, following a ".", or by itself, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * signify a wildcard match. For example: "a.*" or "*" is valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * "*a" or "a*b" is not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <p>FilePermission and SocketPermission are subclasses of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * top-level class for permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * (<code>java.security.Permission</code>). Classes like these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * that have a more complicated name syntax than that used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * BasicPermission subclass directly from Permission rather than from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * BasicPermission. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * for a <code>java.io.FilePermission</code> object, the permission name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * the path name of a file (or directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * <p>Some of the permission classes have an "actions" list that tells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * the actions that are permitted for the object.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * for a <code>java.io.FilePermission</code> object, the actions list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * (such as "read, write") specifies which actions are granted for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * specified file (or for files in the specified directory).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * <p>Other permission classes are for "named" permissions -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * ones that contain a name but no actions list; you either have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * named permission or you don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * <p>Note: There is also a <code>java.security.AllPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * permission that implies all permissions. It exists to simplify the work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * of system administrators who might need to perform multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * tasks that require all (or numerous) permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * See <a href ="../../../technotes/guides/security/permissions.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * Permissions in the JDK</a> for permission-related information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * This document includes, for example, a table listing the various SecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * <code>check</code> methods and the permission(s) the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * implementation of each such method requires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * It also contains a table of all the version 1.2 methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * that require permissions, and for each such method tells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * which permission it requires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * For more information about <code>SecurityManager</code> changes made in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * the JDK and advice regarding porting of 1.1-style security managers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * see the <a href="../../../technotes/guides/security/index.html">security documentation</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * @author  Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * @see     java.lang.ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * @see     java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * @see     java.lang.System#getSecurityManager() getSecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * @see     java.lang.System#setSecurityManager(java.lang.SecurityManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *  setSecurityManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * @see     java.security.AccessController AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * @see     java.security.AccessControlContext AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * @see     java.security.AccessControlException AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * @see     java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * @see     java.security.BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * @see     java.io.FilePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * @see     java.net.SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * @see     java.util.PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * @see     java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * @see     java.awt.AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * @see     java.security.Policy Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * @see     java.security.SecurityPermission SecurityPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * @see     java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
   232
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
class SecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * This field is <code>true</code> if there is a security check in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * progress; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   243
     *  call be used instead. This field is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   244
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   246
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected boolean inCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Have we been initialized. Effective against finalizer attacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * returns true if the current context has been granted AllPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
   258
    private boolean hasAllPermission() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            checkPermission(SecurityConstants.ALL_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Tests if there is a security check in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return the value of the <code>inCheck</code> field. This field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          should contain <code>true</code> if a security check is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @see     java.lang.SecurityManager#inCheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   277
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   278
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   280
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public boolean getInCheck() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return inCheck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Constructs a new <code>SecurityManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p> If there is a security manager already installed, this method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * calls the security manager's <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * with the <code>RuntimePermission("createSecurityManager")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * permission to ensure the calling thread has permission to create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * security manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * This may result in throwing a <code>SecurityException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception  java.lang.SecurityException if a security manager already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *             exists and its <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *             doesn't allow creation of a new security manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see        java.lang.System#getSecurityManager()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public SecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        synchronized(SecurityManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // ask the currently installed security manager if we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // can create a new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                sm.checkPermission(new RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                   ("createSecurityManager"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns the current execution stack as an array of classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * The length of the array is the number of methods on the execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * stack. The element at index <code>0</code> is the class of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * currently executing method, the element at index <code>1</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the class of that method's caller, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return  the execution stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
22116
49bb2cb8cb51 8027063: SecurityManger.getClassContext returns a raw type
darcy
parents: 22060
diff changeset
   325
    protected native Class<?>[] getClassContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the class loader of the most recently executing method from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * a class defined using a non-system class loader. A non-system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * class loader is defined as being a class loader that is not equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the system class loader (as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * This method will return
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 19807
diff changeset
   335
     * <code>null</code> in the following three cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *   <li>All methods on the execution stack are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *   <li>All methods on the execution stack up to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *   "privileged" caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *   (see {@link java.security.AccessController#doPrivileged})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *   are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *   <li> A call to <code>checkPermission</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *   <code>java.security.AllPermission</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *   result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return  the class loader of the most recent occurrence on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *          of a method from a class defined using a non-system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *          loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   358
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   359
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @see  java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @see  #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   364
    @Deprecated(since="1.2", forRemoval=true)
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
   365
    protected ClassLoader currentClassLoader() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ClassLoader cl = currentClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if ((cl != null) && hasAllPermission())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private native ClassLoader currentClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns the class of the most recently executing method from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * a class defined using a non-system class loader. A non-system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * class loader is defined as being a class loader that is not equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * the system class loader (as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * This method will return
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 19807
diff changeset
   382
     * <code>null</code> in the following three cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *   <li>All methods on the execution stack are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *   <li>All methods on the execution stack up to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *   "privileged" caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *   (see {@link java.security.AccessController#doPrivileged})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *   are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *   <li> A call to <code>checkPermission</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *   <code>java.security.AllPermission</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *   result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return  the class  of the most recent occurrence on the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *          of a method from a class defined using a non-system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *          loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   405
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   406
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see  java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see  #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   411
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    protected Class<?> currentLoadedClass() {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
   413
        Class<?> c = currentLoadedClass0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if ((c != null) && hasAllPermission())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Returns the stack depth of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param   name   the fully qualified name of the class to search for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return  the depth on the stack frame of the first occurrence of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *          method from a class with the specified name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *          <code>-1</code> if such a frame cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   428
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   429
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   431
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    protected native int classDepth(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Returns the stack depth of the most recently executing method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * from a class defined using a non-system class loader.  A non-system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * class loader is defined as being a class loader that is not equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * the system class loader (as returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * This method will return
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 19807
diff changeset
   442
     * -1 in the following three cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *   <li>All methods on the execution stack are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *   <li>All methods on the execution stack up to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *   "privileged" caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *   (see {@link java.security.AccessController#doPrivileged})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *   are from classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *   defined using the system class loader or one of its ancestors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *   <li> A call to <code>checkPermission</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *   <code>java.security.AllPermission</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *   result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @return the depth on the stack frame of the most recent occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *          a method from a class defined using a non-system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   464
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   465
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @see   java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @see   #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   470
    @Deprecated(since="1.2", forRemoval=true)
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
   471
    protected int classLoaderDepth() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int depth = classLoaderDepth0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (depth != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (hasAllPermission())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                depth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                depth--; // make sure we don't include ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private native int classLoaderDepth0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Tests if a method from a class with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *         name is on the execution stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param  name   the fully qualified name of the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @return <code>true</code> if a method from a class with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *         name is on the execution stack; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   493
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   494
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   496
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    protected boolean inClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return classDepth(name) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Basically, tests if a method from a class defined using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *          class loader is on the execution stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return  <code>true</code> if a call to <code>currentClassLoader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *          has a non-null return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @deprecated This type of security checking is not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *  It is recommended that the <code>checkPermission</code>
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   510
     *  call be used instead. This method is subject to removal in a
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   511
     *  future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @see        #currentClassLoader() currentClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
39888
bec759b9b909 8161506: Deprecate pre-1.2 SecurityManager methods and fields with forRemoval=true
mullan
parents: 37894
diff changeset
   514
    @Deprecated(since="1.2", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    protected boolean inClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return currentClassLoader() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Creates an object that encapsulates the current execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * environment. The result of this method is used, for example, by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * three-argument <code>checkConnect</code> method and by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * two-argument <code>checkRead</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * These methods are needed because a trusted method may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * on to read a file or open a socket on behalf of another method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * The trusted method needs to determine if the other (possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * untrusted) method would be allowed to perform the operation on its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <p> The default implementation of this method is to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * an <code>AccessControlContext</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return  an implementation-dependent object that encapsulates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *          sufficient information about the current execution environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *          to perform some security checks later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @see     java.lang.SecurityManager#checkConnect(java.lang.String, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *   java.lang.Object) checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @see     java.lang.SecurityManager#checkRead(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *   java.lang.Object) checkRead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @see     java.security.AccessControlContext AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    public Object getSecurityContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Throws a <code>SecurityException</code> if the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * access, specified by the given permission, is not permitted based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * on the security policy currently in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * This method calls <code>AccessController.checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * with the given permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param     perm   the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @exception SecurityException if access is not permitted based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *            the current security policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @exception NullPointerException if the permission argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *            <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void checkPermission(Permission perm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        java.security.AccessController.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * specified security context is denied access to the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * specified by the given permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * The context must be a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * context returned by a previous call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <code>getSecurityContext</code> and the access control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * decision is based upon the configured security policy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * that security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * If <code>context</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <code>AccessControlContext</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <code>AccessControlContext.checkPermission</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * invoked with the specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * If <code>context</code> is not an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <code>AccessControlContext</code> then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <code>SecurityException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param      perm      the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param      context   a system-dependent security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @exception  SecurityException  if the specified security context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *             is not an instance of <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *             (e.g., is <code>null</code>), or is denied access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *             resource specified by the given permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @exception  NullPointerException if the permission argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @see        java.lang.SecurityManager#getSecurityContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public void checkPermission(Permission perm, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (context instanceof AccessControlContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            ((AccessControlContext)context).checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw new SecurityException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * calling thread is not allowed to create a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <code>RuntimePermission("createClassLoader")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <code>super.checkCreateClassLoader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @exception SecurityException if the calling thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *             have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *             to create a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @see        java.lang.ClassLoader#ClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public void checkCreateClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        checkPermission(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * reference to the root thread group, used for the checkAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    private static ThreadGroup rootGroup = getRootGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    private static ThreadGroup getRootGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        ThreadGroup root =  Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        while (root.getParent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            root = root.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * calling thread is not allowed to modify the thread argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * This method is invoked for the current security manager by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <code>stop</code>, <code>suspend</code>, <code>resume</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <code>setPriority</code>, <code>setName</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <code>setDaemon</code> methods of class <code>Thread</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * If the thread argument is a system thread (belongs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * the thread group with a <code>null</code> parent) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * this method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <code>RuntimePermission("modifyThread")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * If the thread argument is <i>not</i> a system thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * this method just returns silently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Applications that want a stricter policy should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * method. If this method is overridden, the method that overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * it should additionally check to see if the calling thread has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <code>RuntimePermission("modifyThread")</code> permission, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * if so, return silently. This is to ensure that code granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * that permission (such as the JDK itself) is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * manipulate any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * If this method is overridden, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <code>super.checkAccess</code> should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * be called by the first statement in the overridden method, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * equivalent security check should be placed in the overridden method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @param      t   the thread to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *             permission to modify the thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @exception  NullPointerException if the thread argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @see        java.lang.Thread#resume() resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @see        java.lang.Thread#setDaemon(boolean) setDaemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @see        java.lang.Thread#setName(java.lang.String) setName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see        java.lang.Thread#setPriority(int) setPriority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see        java.lang.Thread#stop() stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @see        java.lang.Thread#suspend() suspend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public void checkAccess(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            throw new NullPointerException("thread can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (t.getThreadGroup() == rootGroup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            // just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * calling thread is not allowed to modify the thread group argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * This method is invoked for the current security manager when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * new child thread or child thread group is created, and by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <code>setDaemon</code>, <code>setMaxPriority</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * <code>stop</code>, <code>suspend</code>, <code>resume</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <code>destroy</code> methods of class <code>ThreadGroup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * If the thread group argument is the system thread group (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * has a <code>null</code> parent) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * this method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <code>RuntimePermission("modifyThreadGroup")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * If the thread group argument is <i>not</i> the system thread group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * this method just returns silently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Applications that want a stricter policy should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * method. If this method is overridden, the method that overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * it should additionally check to see if the calling thread has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <code>RuntimePermission("modifyThreadGroup")</code> permission, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * if so, return silently. This is to ensure that code granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * that permission (such as the JDK itself) is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * manipulate any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * If this method is overridden, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <code>super.checkAccess</code> should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * be called by the first statement in the overridden method, or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * equivalent security check should be placed in the overridden method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @param      g   the thread group to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *             permission to modify the thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @exception  NullPointerException if the thread group argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @see        java.lang.ThreadGroup#destroy() destroy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @see        java.lang.ThreadGroup#resume() resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @see        java.lang.ThreadGroup#setDaemon(boolean) setDaemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @see        java.lang.ThreadGroup#setMaxPriority(int) setMaxPriority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @see        java.lang.ThreadGroup#stop() stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @see        java.lang.ThreadGroup#suspend() suspend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public void checkAccess(ThreadGroup g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (g == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            throw new NullPointerException("thread group can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (g == rootGroup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            // just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * calling thread is not allowed to cause the Java Virtual Machine to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * halt with the specified status code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * This method is invoked for the current security manager by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * <code>exit</code> method of class <code>Runtime</code>. A status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * of <code>0</code> indicates success; other values indicate various
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <code>RuntimePermission("exitVM."+status)</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <code>super.checkExit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param      status   the exit status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @exception SecurityException if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *              permission to halt the Java Virtual Machine with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *              the specified status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @see        java.lang.Runtime#exit(int) exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    public void checkExit(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        checkPermission(new RuntimePermission("exitVM."+status));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * calling thread is not allowed to create a subprocess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * This method is invoked for the current security manager by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <code>exec</code> methods of class <code>Runtime</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <code>FilePermission(cmd,"execute")</code> permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * if cmd is an absolute path, otherwise it calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <code>checkPermission</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <code>FilePermission("&lt;&lt;ALL FILES&gt;&gt;","execute")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * <code>super.checkExec</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @param      cmd   the specified system command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @exception  SecurityException if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *             permission to create a subprocess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @exception  NullPointerException if the <code>cmd</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @see     java.lang.Runtime#exec(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @see     java.lang.Runtime#exec(java.lang.String, java.lang.String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @see     java.lang.Runtime#exec(java.lang.String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @see     java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @see     #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    public void checkExec(String cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        File f = new File(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (f.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            checkPermission(new FilePermission(cmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                SecurityConstants.FILE_EXECUTE_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            checkPermission(new FilePermission("<<ALL FILES>>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                SecurityConstants.FILE_EXECUTE_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * calling thread is not allowed to dynamic link the library code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * specified by the string argument file. The argument is either a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * simple library name or a complete filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * This method is invoked for the current security manager by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * methods <code>load</code> and <code>loadLibrary</code> of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <code>Runtime</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * <code>RuntimePermission("loadLibrary."+lib)</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * <code>super.checkLink</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @param      lib   the name of the library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @exception  SecurityException if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *             permission to dynamically link the library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @exception  NullPointerException if the <code>lib</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @see        java.lang.Runtime#load(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @see        java.lang.Runtime#loadLibrary(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    public void checkLink(String lib) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (lib == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            throw new NullPointerException("library can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        checkPermission(new RuntimePermission("loadLibrary."+lib));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * calling thread is not allowed to read from the specified file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <code>RuntimePermission("readFileDescriptor")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <code>super.checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @param      fd   the system-dependent file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *             permission to access the specified file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @exception  NullPointerException if the file descriptor argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    public void checkRead(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (fd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            throw new NullPointerException("file descriptor can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        checkPermission(new RuntimePermission("readFileDescriptor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * calling thread is not allowed to read the file specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <code>FilePermission(file,"read")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <code>super.checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param      file   the system-dependent file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @exception  SecurityException if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *             permission to access the specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @exception  NullPointerException if the <code>file</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public void checkRead(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        checkPermission(new FilePermission(file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * specified security context is not allowed to read the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * specified by the string argument. The context must be a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * context returned by a previous call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * <code>getSecurityContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <p> If <code>context</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * <code>AccessControlContext</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * <code>AccessControlContext.checkPermission</code> method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * be invoked with the <code>FilePermission(file,"read")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * <p> If <code>context</code> is not an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * <code>AccessControlContext</code> then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * <code>SecurityException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * <code>super.checkRead</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param      file      the system-dependent filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param      context   a system-dependent security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @exception  SecurityException  if the specified security context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *             is not an instance of <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *             (e.g., is <code>null</code>), or does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *             to read the specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @exception  NullPointerException if the <code>file</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @see        java.lang.SecurityManager#getSecurityContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * @see        java.security.AccessControlContext#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    public void checkRead(String file, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            new FilePermission(file, SecurityConstants.FILE_READ_ACTION),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * calling thread is not allowed to write to the specified file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <code>RuntimePermission("writeFileDescriptor")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <code>super.checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @param      fd   the system-dependent file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @exception SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *             permission to access the specified file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @exception  NullPointerException if the file descriptor argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public void checkWrite(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        if (fd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            throw new NullPointerException("file descriptor can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        checkPermission(new RuntimePermission("writeFileDescriptor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * calling thread is not allowed to write to the file specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * the string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <code>FilePermission(file,"write")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * <code>super.checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param      file   the system-dependent filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @exception  SecurityException  if the calling thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *             have permission to access the specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @exception  NullPointerException if the <code>file</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    public void checkWrite(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        checkPermission(new FilePermission(file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            SecurityConstants.FILE_WRITE_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * calling thread is not allowed to delete the specified file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * This method is invoked for the current security manager by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * <code>delete</code> method of class <code>File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * <code>FilePermission(file,"delete")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <code>super.checkDelete</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * @param      file   the system-dependent filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @exception  SecurityException if the calling thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *             have permission to delete the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @exception  NullPointerException if the <code>file</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @see        java.io.File#delete()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public void checkDelete(String file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        checkPermission(new FilePermission(file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            SecurityConstants.FILE_DELETE_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * calling thread is not allowed to open a socket connection to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * specified host and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * A port number of <code>-1</code> indicates that the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * method is attempting to determine the IP address of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * <code>SocketPermission(host+":"+port,"connect")</code> permission if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * the port is not equal to -1. If the port is equal to -1, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * it calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * <code>SocketPermission(host,"resolve")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * <code>super.checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @param      host   the host name port to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @param      port   the protocol port to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *             permission to open a socket connection to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *               <code>host</code> and <code>port</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @exception  NullPointerException if the <code>host</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    public void checkConnect(String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            throw new NullPointerException("host can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (!host.startsWith("[") && host.indexOf(':') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        if (port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            checkPermission(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                SecurityConstants.SOCKET_RESOLVE_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            checkPermission(new SocketPermission(host+":"+port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                SecurityConstants.SOCKET_CONNECT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * specified security context is not allowed to open a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * connection to the specified host and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * A port number of <code>-1</code> indicates that the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * method is attempting to determine the IP address of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <p> If <code>context</code> is not an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <code>AccessControlContext</code> then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <code>SecurityException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Otherwise, the port number is checked. If it is not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * to -1, the <code>context</code>'s <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <code>SocketPermission(host+":"+port,"connect")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * If the port is equal to -1, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * the <code>context</code>'s <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <code>SocketPermission(host,"resolve")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * <code>super.checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @param      host      the host name port to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @param      port      the protocol port to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @param      context   a system-dependent security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @exception  SecurityException if the specified security context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *             is not an instance of <code>AccessControlContext</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *             (e.g., is <code>null</code>), or does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *             to open a socket connection to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *             <code>host</code> and <code>port</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * @exception  NullPointerException if the <code>host</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @see        java.lang.SecurityManager#getSecurityContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * @see        java.security.AccessControlContext#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public void checkConnect(String host, int port, Object context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            throw new NullPointerException("host can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if (!host.startsWith("[") && host.indexOf(':') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (port == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            checkPermission(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                SecurityConstants.SOCKET_RESOLVE_ACTION),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            checkPermission(new SocketPermission(host+":"+port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                SecurityConstants.SOCKET_CONNECT_ACTION),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * calling thread is not allowed to wait for a connection request on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * the specified local port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * <p>
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21330
diff changeset
  1129
     * This method calls <code>checkPermission</code> with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * <code>SocketPermission("localhost:"+port,"listen")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <code>super.checkListen</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @param      port   the local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *             permission to listen on the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public void checkListen(int port) {
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21330
diff changeset
  1143
        checkPermission(new SocketPermission("localhost:"+port,
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21330
diff changeset
  1144
            SecurityConstants.SOCKET_LISTEN_ACTION));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * calling thread is not permitted to accept a socket connection from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * the specified host and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * This method is invoked for the current security manager by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <code>accept</code> method of class <code>ServerSocket</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <code>SocketPermission(host+":"+port,"accept")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <code>super.checkAccept</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @param      host   the host name of the socket connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @param      port   the port number of the socket connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *             permission to accept the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @exception  NullPointerException if the <code>host</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @see        java.net.ServerSocket#accept()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    public void checkAccept(String host, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            throw new NullPointerException("host can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (!host.startsWith("[") && host.indexOf(':') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        checkPermission(new SocketPermission(host+":"+port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            SecurityConstants.SOCKET_ACCEPT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * calling thread is not allowed to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * (join/leave/send/receive) IP multicast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * <code>java.net.SocketPermission(maddr.getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * "accept,connect")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * <code>super.checkMulticast</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * @param      maddr  Internet group address to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @exception  SecurityException  if the calling thread is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *  use (join/leave/send/receive) IP multicast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @exception  NullPointerException if the address argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *             <code>null</code>.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1202
     * @since      1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    public void checkMulticast(InetAddress maddr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        String host = maddr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        if (!host.startsWith("[") && host.indexOf(':') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        checkPermission(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * calling thread is not allowed to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * (join/leave/send/receive) IP multicast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * <code>java.net.SocketPermission(maddr.getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * "accept,connect")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * <code>super.checkMulticast</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @param      maddr  Internet group address to be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @param      ttl        value in use, if it is multicast send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * Note: this particular implementation does not use the ttl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * @exception  SecurityException  if the calling thread is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *  use (join/leave/send/receive) IP multicast.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @exception  NullPointerException if the address argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *             <code>null</code>.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1236
     * @since      1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @deprecated Use #checkPermission(java.security.Permission) instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1240
    @Deprecated(since="1.4")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public void checkMulticast(InetAddress maddr, byte ttl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        String host = maddr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        if (!host.startsWith("[") && host.indexOf(':') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        checkPermission(new SocketPermission(host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * calling thread is not allowed to access or modify the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * This method is used by the <code>getProperties</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * <code>setProperties</code> methods of class <code>System</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <code>PropertyPermission("*", "read,write")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <code>super.checkPropertiesAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *             permission to access or modify the system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @see        java.lang.System#getProperties()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @see        java.lang.System#setProperties(java.util.Properties)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    public void checkPropertiesAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        checkPermission(new PropertyPermission("*",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            SecurityConstants.PROPERTY_RW_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * calling thread is not allowed to access the system property with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * the specified <code>key</code> name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * This method is used by the <code>getProperty</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * class <code>System</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * <code>PropertyPermission(key, "read")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * <code>super.checkPropertyAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @param      key   a system property key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *             permission to access the specified system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @exception  NullPointerException if the <code>key</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *             <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @exception  IllegalArgumentException if <code>key</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see        java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    public void checkPropertyAccess(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        checkPermission(new PropertyPermission(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    /**
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1310
     * Returns {@code true} if the calling thread has {@code AllPermission}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1312
     * @param      window   not used except to check if it is {@code null}.
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1313
     * @return     {@code true} if the calling thread has {@code AllPermission}.
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1314
     * @exception  NullPointerException if the {@code window} argument is
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1315
     *             {@code null}.
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1316
     * @deprecated This method was originally used to check if the calling thread
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1317
     *             was trusted to bring up a top-level window. The method has been
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1318
     *             obsoleted and code should instead use {@link #checkPermission}
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1319
     *             to check {@code AWTPermission("showWindowWithoutWarningBanner")}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1320
     *             This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1323
    @Deprecated(since="1.8", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    public boolean checkTopLevelWindow(Object window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        if (window == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            throw new NullPointerException("window can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1328
        return hasAllPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * calling thread is not allowed to initiate a print job request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * This method calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * <code>RuntimePermission("queuePrintJob")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * <code>super.checkPrintJobAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *             permission to initiate a print job request.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1346
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    public void checkPrintJobAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        checkPermission(new RuntimePermission("queuePrintJob"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    /**
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1354
     * Throws {@code SecurityException} if the calling thread does
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1355
     * not have {@code AllPermission}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1357
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @exception  SecurityException  if the calling thread does not have
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1359
     *             {@code AllPermission}
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1360
     * @deprecated This method was originally used to check if the calling
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1361
     *             thread could access the system clipboard. The method has been
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1362
     *             obsoleted and code should instead use {@link #checkPermission}
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1363
     *             to check {@code AWTPermission("accessClipboard")}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1364
     *             This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1367
    @Deprecated(since="1.8", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    public void checkSystemClipboardAccess() {
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1369
        checkPermission(SecurityConstants.ALL_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    /**
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1373
     * Throws {@code SecurityException} if the calling thread does
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1374
     * not have {@code AllPermission}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1376
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * @exception  SecurityException  if the calling thread does not have
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1378
     *             {@code AllPermission}
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1379
     * @deprecated This method was originally used to check if the calling
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1380
     *             thread could access the AWT event queue. The method has been
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1381
     *             obsoleted and code should instead use {@link #checkPermission}
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1382
     *             to check {@code AWTPermission("accessEventQueue")}.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1383
     *             This method is subject to removal in a future version of Java SE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1386
    @Deprecated(since="1.8", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    public void checkAwtEventQueueAccess() {
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 21330
diff changeset
  1388
        checkPermission(SecurityConstants.ALL_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * We have an initial invalid bit (initially false) for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * variables which tell if the cache is valid.  If the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * java.security.Security property changes via setProperty(), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Security class uses reflection to change the variable and thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * invalidate the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * Locking is handled by synchronization to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * packageAccessLock/packageDefinitionLock objects.  They are only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * used in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * Note that cache invalidation as a result of the property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * happens without using these locks, so there may be a delay between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * when a thread updates the property and when other threads updates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    private static boolean packageAccessValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    private static String[] packageAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    private static final Object packageAccessLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    private static boolean packageDefinitionValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    private static String[] packageDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    private static final Object packageDefinitionLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    private static String[] getPackages(String p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        String packages[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (p != null && !p.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            java.util.StringTokenizer tok =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                new java.util.StringTokenizer(p, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            int n = tok.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                packages = new String[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                while (tok.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                    String s = tok.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    packages[i++] = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1431
        if (packages == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            packages = new String[0];
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1433
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        return packages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1437
    // The non-exported packages in modules defined to the boot or platform
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1438
    // class loaders. A non-exported package is a package that is not exported
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1439
    // or is only exported to specific modules.
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1440
    private static final Map<String, Boolean> nonExportedPkgs = new ConcurrentHashMap<>();
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1441
    static {
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1442
        addNonExportedPackages(ModuleLayer.boot());
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1443
    }
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1444
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1445
    /**
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1446
     * Record the non-exported packages of the modules in the given layer
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1447
     */
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1448
    static void addNonExportedPackages(ModuleLayer layer) {
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1449
        Set<String> bootModules = ModuleLoaderMap.bootModules();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1450
        Set<String> platformModules = ModuleLoaderMap.platformModules();
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1451
        layer.modules().stream()
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1452
                .map(Module::getDescriptor)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1453
                .filter(md -> bootModules.contains(md.name())
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1454
                        || platformModules.contains(md.name()))
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1455
                .map(SecurityManager::nonExportedPkgs)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1456
                .flatMap(Set::stream)
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1457
                .forEach(pn -> nonExportedPkgs.put(pn, Boolean.TRUE));
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1458
    }
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1459
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1460
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1461
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1462
     * Called by java.security.Security
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1463
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1464
    static void invalidatePackageAccessCache() {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1465
        synchronized (packageAccessLock) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1466
            packageAccessValid = false;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1467
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1468
        synchronized (packageDefinitionLock) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1469
            packageDefinitionValid = false;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1470
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1471
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1472
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43221
diff changeset
  1473
    /**
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1474
     * Returns the non-exported packages of the specified module.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1475
     */
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1476
    private static Set<String> nonExportedPkgs(ModuleDescriptor md) {
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1477
        // start with all packages in the module
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1478
        Set<String> pkgs = new HashSet<>(md.packages());
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1479
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1480
        // remove the non-qualified exported packages
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1481
        md.exports().stream()
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1482
                    .filter(p -> !p.isQualified())
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1483
                    .map(Exports::source)
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1484
                    .forEach(pkgs::remove);
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1485
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1486
        // remove the non-qualified open packages
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1487
        md.opens().stream()
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1488
                  .filter(p -> !p.isQualified())
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1489
                  .map(Opens::source)
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1490
                  .forEach(pkgs::remove);
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1491
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1492
        return pkgs;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1493
    }
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1494
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1495
    /**
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1496
     * Throws a {@code SecurityException} if the calling thread is not allowed
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1497
     * to access the specified package.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * <p>
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1499
     * This method is called by the {@code loadClass} method of class loaders.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1500
     * <p>
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1501
     * This method checks if the specified package starts with or equals
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1502
     * any of the packages in the {@code package.access} Security Property.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1503
     * An implementation may also check the package against an additional
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1504
     * list of restricted packages as noted below. If the package is restricted,
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1505
     * {@link #checkPermission(Permission)} is called with a
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1506
     * {@code RuntimePermission("accessClassInPackage."+pkg)} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * <p>
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1508
     * If this method is overridden, then {@code super.checkPackageAccess}
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1509
     * should be called as the first line in the overridden method.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1510
     *
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1511
     * @implNote
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1512
     * This implementation also restricts all non-exported packages of modules
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1513
     * loaded by {@linkplain ClassLoader#getPlatformClassLoader
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1514
     * the platform class loader} or its ancestors. A "non-exported package"
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1515
     * refers to a package that is not exported to all modules. Specifically,
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1516
     * it refers to a package that either is not exported at all by its
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1517
     * containing module or is exported in a qualified fashion by its
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1518
     * containing module.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @param      pkg   the package name.
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1521
     * @throws     SecurityException  if the calling thread does not have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *             permission to access the specified package.
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1523
     * @throws     NullPointerException if the package name argument is
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1524
     *             {@code null}.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1525
     * @see        java.lang.ClassLoader#loadClass(String, boolean) loadClass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @see        java.security.Security#getProperty getProperty
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1527
     * @see        #checkPermission(Permission) checkPermission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    public void checkPackageAccess(String pkg) {
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1530
        Objects.requireNonNull(pkg, "package name can't be null");
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1531
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1532
        // check if pkg is not exported to all modules
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1533
        if (nonExportedPkgs.containsKey(pkg)) {
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1534
            checkPermission(
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1535
                new RuntimePermission("accessClassInPackage." + pkg));
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1536
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1539
        String[] restrictedPkgs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        synchronized (packageAccessLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
             * Do we need to update our property array?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            if (!packageAccessValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                String tmpPropertyStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  1547
                        new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                            public String run() {
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1549
                                return Security.getProperty("package.access");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                packageAccess = getPackages(tmpPropertyStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                packageAccessValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            // Using a snapshot of packageAccess -- don't care if static field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
            // changes afterwards; array contents won't change.
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1559
            restrictedPkgs = packageAccess;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * Traverse the list of packages, check for any matches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         */
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1565
        final int plen = pkg.length();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1566
        for (String restrictedPkg : restrictedPkgs) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1567
            final int rlast = restrictedPkg.length() - 1;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1568
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1569
            // Optimizations:
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1570
            //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1571
            // If rlast >= plen then restrictedPkg is longer than pkg by at
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1572
            // least one char. This means pkg cannot start with restrictedPkg,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1573
            // since restrictedPkg will be longer than pkg.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1574
            //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1575
            // Similarly if rlast != plen, then pkg + "." cannot be the same
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1576
            // as restrictedPkg, since pkg + "." will have a different length
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1577
            // than restrictedPkg.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1578
            //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1579
            if (rlast < plen && pkg.startsWith(restrictedPkg) ||
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1580
                // The following test is equivalent to
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1581
                // restrictedPkg.equals(pkg + ".") but is noticeably more
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1582
                // efficient:
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1583
                rlast == plen && restrictedPkg.startsWith(pkg) &&
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1584
                restrictedPkg.charAt(rlast) == '.')
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents: 29986
diff changeset
  1585
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                checkPermission(
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22342
diff changeset
  1587
                    new RuntimePermission("accessClassInPackage." + pkg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                break;  // No need to continue; only need to check this once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    /**
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1594
     * Throws a {@code SecurityException} if the calling thread is not
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1595
     * allowed to define classes in the specified package.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * <p>
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1597
     * This method is called by the {@code loadClass} method of some
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * class loaders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * <p>
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1600
     * This method checks if the specified package starts with or equals
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1601
     * any of the packages in the {@code package.definition} Security
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1602
     * Property. An implementation may also check the package against an
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1603
     * additional list of restricted packages as noted below. If the package
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1604
     * is restricted, {@link #checkPermission(Permission)} is called with a
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1605
     * {@code RuntimePermission("defineClassInPackage."+pkg)} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * <p>
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1607
     * If this method is overridden, then {@code super.checkPackageDefinition}
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1608
     * should be called as the first line in the overridden method.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1609
     *
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1610
     * @implNote
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1611
     * This implementation also restricts all non-exported packages of modules
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1612
     * loaded by {@linkplain ClassLoader#getPlatformClassLoader
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1613
     * the platform class loader} or its ancestors. A "non-exported package"
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1614
     * refers to a package that is not exported to all modules. Specifically,
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1615
     * it refers to a package that either is not exported at all by its
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1616
     * containing module or is exported in a qualified fashion by its
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1617
     * containing module.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @param      pkg   the package name.
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1620
     * @throws     SecurityException  if the calling thread does not have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *             permission to define classes in the specified package.
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1622
     * @throws     NullPointerException if the package name argument is
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1623
     *             {@code null}.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1624
     * @see        java.lang.ClassLoader#loadClass(String, boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * @see        java.security.Security#getProperty getProperty
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1626
     * @see        #checkPermission(Permission) checkPermission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    public void checkPackageDefinition(String pkg) {
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1629
        Objects.requireNonNull(pkg, "package name can't be null");
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1630
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1631
        // check if pkg is not exported to all modules
45004
ea3137042a61 8178380: Module system implementation refresh (5/2017)
alanb
parents: 44545
diff changeset
  1632
        if (nonExportedPkgs.containsKey(pkg)) {
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1633
            checkPermission(
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1634
                new RuntimePermission("defineClassInPackage." + pkg));
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 39888
diff changeset
  1635
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        String[] pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        synchronized (packageDefinitionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
             * Do we need to update our property array?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            if (!packageDefinitionValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                String tmpPropertyStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  1646
                        new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                                return java.security.Security.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                                    "package.definition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                packageDefinition = getPackages(tmpPropertyStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                packageDefinitionValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            // Using a snapshot of packageDefinition -- don't care if static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            // field changes afterwards; array contents won't change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            pkgs = packageDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * Traverse the list of packages, check for any matches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22342
diff changeset
  1664
        for (String restrictedPkg : pkgs) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22342
diff changeset
  1665
            if (pkg.startsWith(restrictedPkg) || restrictedPkg.equals(pkg + ".")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                checkPermission(
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 22342
diff changeset
  1667
                    new RuntimePermission("defineClassInPackage." + pkg));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                break; // No need to continue; only need to check this once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * calling thread is not allowed to set the socket factory used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * <code>ServerSocket</code> or <code>Socket</code>, or the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * handler factory used by <code>URL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * This method calls <code>checkPermission</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * <code>RuntimePermission("setFactory")</code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * <code>super.checkSetFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @exception  SecurityException  if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     *             permission to specify a socket factory or a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     *             handler factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) setSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * @see        java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) setSocketImplFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * @see        java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory) setURLStreamHandlerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    public void checkSetFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        checkPermission(new RuntimePermission("setFactory"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * Throws a <code>SecurityException</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * calling thread is not allowed to access members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * The default policy is to allow access to PUBLIC members, as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * as access to classes that have the same class loader as the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * In all other cases, this method calls <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * with the <code>RuntimePermission("accessDeclaredMembers")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * </code> permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * If this method is overridden, then a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * <code>super.checkMemberAccess</code> cannot be made,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * as the default implementation of <code>checkMemberAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * relies on the code being checked being at a stack depth of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * 4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * @param clazz the class that reflection is to be performed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @param which type of access, PUBLIC or DECLARED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * @exception  SecurityException if the caller does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     *             permission to access members.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * @exception  NullPointerException if the <code>clazz</code> argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     *             <code>null</code>.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 16906
diff changeset
  1724
     *
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 16906
diff changeset
  1725
     * @deprecated This method relies on the caller being at a stack depth
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 16906
diff changeset
  1726
     *             of 4 which is error-prone and cannot be enforced by the runtime.
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 16906
diff changeset
  1727
     *             Users of this method should instead invoke {@link #checkPermission}
37894
3acc9608c162 8156810: remove redundant sentence in SecurityManager.checkMemberAccess doc
smarks
parents: 37521
diff changeset
  1728
     *             directly.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1729
     *             This method is subject to removal in a future version of Java SE.
18766
28c62f5e9a47 8007035: deprecate public void SecurityManager.checkMemberAccess(Class<?> clazz, int which)
mchung
parents: 16906
diff changeset
  1730
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * @see java.lang.reflect.Member
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1732
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 37363
diff changeset
  1735
    @Deprecated(since="1.8", forRemoval=true)
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 16039
diff changeset
  1736
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    public void checkMemberAccess(Class<?> clazz, int which) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            throw new NullPointerException("class can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        if (which != Member.PUBLIC) {
21330
7b073d91ba9e 8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents: 19807
diff changeset
  1742
            Class<?> stack[] = getClassContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
             * stack depth of 4 should be the caller of one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
             * methods in java.lang.Class that invoke checkMember
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
             * access. The stack should look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
             * someCaller                        [3]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
             * java.lang.Class.someReflectionAPI [2]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
             * java.lang.Class.checkMemberAccess [1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
             * SecurityManager.checkMemberAccess [0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            if ((stack.length<4) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                (stack[3].getClassLoader() != clazz.getClassLoader())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Determines whether the permission with the specified permission target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * name should be granted or denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * <p> If the requested permission is allowed, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * quietly. If denied, a SecurityException is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * <p> This method creates a <code>SecurityPermission</code> object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * the given permission target name and calls <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * <p> See the documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * <code>{@link java.security.SecurityPermission}</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * a list of possible permission target names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * <p> If you override this method, then you should make a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * <code>super.checkSecurityAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * at the point the overridden method would normally throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * @param target the target name of the <code>SecurityPermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * @exception SecurityException if the calling thread does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * permission for the requested access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * @exception NullPointerException if <code>target</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * @exception IllegalArgumentException if <code>target</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1788
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * @see        #checkPermission(java.security.Permission) checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    public void checkSecurityAccess(String target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        checkPermission(new SecurityPermission(target));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
  1795
    private native Class<?> currentLoadedClass0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * Returns the thread group into which to instantiate any new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * thread being created at the time this is being called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * By default, it returns the thread group of the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * thread. This should be overridden by a specific security
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * manager to return the appropriate thread group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * @return  ThreadGroup that new threads are instantiated into
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
  1805
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * @see     java.lang.ThreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    public ThreadGroup getThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        return Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
}