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