src/java.base/share/classes/java/security/AccessController.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55705
e0f5ad90737c 8216381: More limited privilege usage
mullan
parents: 53435
diff changeset
     2
 * Copyright (c) 1997, 2019, 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: 2589
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: 2589
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: 2589
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
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.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    28
import java.lang.annotation.ElementType;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    29
import java.lang.annotation.Retention;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    30
import java.lang.annotation.RetentionPolicy;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    31
import java.lang.annotation.Target;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    32
import java.lang.ref.Reference;
53435
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 53068
diff changeset
    33
ea254e9fc587 8213234: Move LambdaForm.Hidden to jdk.internal.vm.annotation
vlivanov
parents: 53068
diff changeset
    34
import jdk.internal.vm.annotation.Hidden;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.Debug;
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    36
import sun.security.util.SecurityConstants;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 30033
diff changeset
    37
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 30033
diff changeset
    38
import jdk.internal.reflect.Reflection;
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    39
import jdk.internal.vm.annotation.DontInline;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    40
import jdk.internal.vm.annotation.ForceInline;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
    41
import jdk.internal.vm.annotation.ReservedStackAccess;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> The AccessController class is used for access control operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and decisions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> More specifically, the AccessController class is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * three purposes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li> to decide whether an access to a critical system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * resource is to be allowed or denied, based on the security policy
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21321
diff changeset
    53
 * currently in effect,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li>to mark code as being "privileged", thus affecting subsequent
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 21321
diff changeset
    55
 * access determinations, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li>to obtain a "snapshot" of the current calling context so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * access-control decisions from a different context can be made with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * respect to the saved context. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p> The {@link #checkPermission(Permission) checkPermission} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * determines whether the access request indicated by a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * permission should be granted or denied. A sample call appears
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    63
 * below. In this example, {@code checkPermission} will determine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * whether or not to grant "read" access to the file named "testFile" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * the "/temp" directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * FilePermission perm = new FilePermission("/temp/testFile", "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * AccessController.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p> If a requested access is allowed,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    75
 * {@code checkPermission} returns quietly. If denied, an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * AccessControlException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * thrown. AccessControlException can also be thrown if the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * permission is of an incorrect type or contains an invalid value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Such information is given whenever possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Suppose the current thread traversed m callers, in the order of caller 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * to caller 2 to caller m. Then caller m invoked the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    83
 * {@code checkPermission} method.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
    84
 * The {@code checkPermission} method determines whether access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * is granted or denied based on the following algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * for (int i = m; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     if (caller i's domain does not have the permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         throw AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     else if (caller i is marked as privileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *         if (a context was specified in the call to doPrivileged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *             context.checkPermission(permission)
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
    96
 *         if (limited permissions were specified in the call to doPrivileged) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
    97
 *             for (each limited permission) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
    98
 *                 if (the limited permission implies the requested permission)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
    99
 *                     return;
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   100
 *             }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   101
 *         } else
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   102
 *             return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *     }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   104
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * // Next, check the context inherited when the thread was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * // Whenever a new thread is created, the AccessControlContext at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * // that time is stored and associated with the new thread, as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * // "inherited" context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * inheritedContext.checkPermission(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p> A caller can be marked as being "privileged"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * (see {@link #doPrivileged(PrivilegedAction) doPrivileged} and below).
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   116
 * When making access control decisions, the {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * method stops checking if it reaches a caller that
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   118
 * was marked as "privileged" via a {@code doPrivileged}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * call without a context argument (see below for information about a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * context argument). If that caller's domain has the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   121
 * specified permission and at least one limiting permission argument (if any)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   122
 * implies the requested permission, no further checking is done and
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   123
 * {@code checkPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * returns quietly, indicating that the requested access is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * If that domain does not have the specified permission, an exception
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   126
 * is thrown, as usual. If the caller's domain had the specified permission
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   127
 * but it was not implied by any limiting permission arguments given in the call
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   128
 * to {@code doPrivileged} then the permission checking continues
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   129
 * until there are no more callers or another {@code doPrivileged}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   130
 * call matches the requested permission and returns normally.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <p> The normal use of the "privileged" feature is as follows. If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * don't need to return a value from within the "privileged" block, do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *     AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *         public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *             // privileged code goes here, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *             System.loadLibrary("awt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *             return null; // nothing to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *     });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * PrivilegedAction is an interface with a single method, named
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   151
 * {@code run}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * The above example shows creation of an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * of that interface; a concrete implementation of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   154
 * {@code run} method is supplied.
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   155
 * When the call to {@code doPrivileged} is made, an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * instance of the PrivilegedAction implementation is passed
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   157
 * to it. The {@code doPrivileged} method calls the
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   158
 * {@code run} method from the PrivilegedAction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * implementation after enabling privileges, and returns the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   160
 * {@code run} method's return value as the
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   161
 * {@code doPrivileged} return value (which is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * ignored in this example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <p> If you need to return a value, you can do something like the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *     String user = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *         new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *         public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *             return System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *         });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   178
 * <p>If the action performed in your {@code run} method could
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   179
 * throw a "checked" exception (those listed in the {@code throws} clause
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * of a method), then you need to use the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   181
 * {@code PrivilegedExceptionAction} interface instead of the
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   182
 * {@code PrivilegedAction} interface:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * somemethod() throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *         FileInputStream fis = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *         new PrivilegedExceptionAction<FileInputStream>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *             public FileInputStream run() throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *                 return new FileInputStream("someFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *         });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *     } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *         // e.getException() should be an instance of FileNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *         // as only "checked" exceptions will be "wrapped" in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *         // PrivilegedActionException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *         throw (FileNotFoundException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *  }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <p> Be *very* careful in your use of the "privileged" construct, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * always remember to make the privileged code section as small as possible.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   205
 * You can pass {@code Permission} arguments to further limit the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   206
 * scope of the "privilege" (see below).
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   207
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   209
 * <p> Note that {@code checkPermission} always performs security checks
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * within the context of the currently executing thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Sometimes a security check that should be made within a given context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * will actually need to be done from within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <i>different</i> context (for example, from within a worker thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * The {@link #getContext() getContext} method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * AccessControlContext class are provided
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   216
 * for this situation. The {@code getContext} method takes a "snapshot"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * of the current calling context, and places
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * it in an AccessControlContext object, which it returns. A sample call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * AccessControlContext acc = AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * <p>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   228
 * AccessControlContext itself has a {@code checkPermission} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * that makes access decisions based on the context <i>it</i> encapsulates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * rather than that of the current execution thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * Code within a different context can thus call that method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * previously-saved AccessControlContext object. A sample call is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * acc.checkPermission(permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * <p> There are also times where you don't know a priori which permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * to check the context against. In these cases you can use the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   243
 * doPrivileged method that takes a context. You can also limit the scope
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   244
 * of the privileged code by passing additional {@code Permission}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   245
 * parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *     AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *         public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *             // Code goes here. Any permission checks within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *             // run method will require that the intersection of the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   253
 *             // caller's protection domain and the snapshot's
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   254
 *             // context have the desired permission. If a requested
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   255
 *             // permission is not implied by the limiting FilePermission
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   256
 *             // argument then checking of the thread continues beyond the
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   257
 *             // caller of doPrivileged.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *         }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   259
 *     }, acc, new FilePermission("/temp/*", read));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * }}</pre>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   262
 * <p> Passing a limiting {@code Permission} argument of an instance of
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   263
 * {@code AllPermission} is equivalent to calling the equivalent
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   264
 * {@code doPrivileged} method without limiting {@code Permission}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   265
 * arguments. Passing a zero length array of {@code Permission} disables
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   266
 * the code privileges so that checking always continues beyond the caller of
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   267
 * that {@code doPrivileged} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * @see AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * @author Roland Schemers
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 37363
diff changeset
   273
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
public final class AccessController {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Don't allow anyone to instantiate an AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private AccessController() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   284
     * Performs the specified {@code PrivilegedAction} with privileges
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * enabled. The action is performed with <i>all</i> of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   288
     * <p> If the action's {@code run} method throws an (unchecked)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> Note that any DomainCombiner associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * AccessControlContext will be ignored while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   294
     * @param <T> the type of the value returned by the PrivilegedAction's
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   295
     *                  {@code run} method.
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   296
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   299
     * @return the value returned by the action's {@code run} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   301
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @see #doPrivileged(PrivilegedExceptionAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @see #doPrivilegedWithCombiner(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   309
    @CallerSensitive
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   310
    public static <T> T doPrivileged(PrivilegedAction<T> action)
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   311
    {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   312
        return executePrivileged(action, null, Reflection.getCallerClass());
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   313
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   316
     * Performs the specified {@code PrivilegedAction} with privileges
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * enabled. The action is performed with <i>all</i> of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   320
     * <p> If the action's {@code run} method throws an (unchecked)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p> This method preserves the current AccessControlContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * DomainCombiner (which may be null) while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   326
     * @param <T> the type of the value returned by the PrivilegedAction's
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   327
     *                  {@code run} method.
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   328
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   331
     * @return the value returned by the action's {@code run} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   333
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   340
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        AccessControlContext acc = getStackAccessControlContext();
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   343
        if (acc == null) {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   344
            return AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   346
        DomainCombiner dc = acc.getAssignedCombiner();
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   347
        return AccessController.doPrivileged(action,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   348
                                             preserveCombiner(dc, Reflection.getCallerClass()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   353
     * Performs the specified {@code PrivilegedAction} with privileges
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   354
     * enabled and restricted by the specified {@code AccessControlContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The action is performed with the intersection of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * possessed by the caller's protection domain, and those possessed
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   357
     * by the domains represented by the specified {@code AccessControlContext}.
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   358
     * <p>
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   359
     * If the action's {@code run} method throws an (unchecked) exception,
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   360
     * it will propagate through this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p>
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   362
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   363
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   364
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   365
     * {@literal "createAccessControlContext"}
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   366
     * {@link java.security.SecurityPermission}, then the action is performed
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   367
     * with no permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   369
     * @param <T> the type of the value returned by the PrivilegedAction's
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   370
     *                  {@code run} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param context an <i>access control context</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *                representing the restriction to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *                caller's domain's privileges before performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *                the specified action.  If the context is
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   376
     *                {@code null}, then no additional restriction is applied.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   378
     * @return the value returned by the action's {@code run} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   380
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   385
    @CallerSensitive
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   386
    public static <T> T doPrivileged(PrivilegedAction<T> action,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   387
                                     AccessControlContext context)
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   388
    {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   389
        Class<?> caller = Reflection.getCallerClass();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   390
        context = checkContext(context, caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   391
        return executePrivileged(action, context, caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   392
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   394
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   395
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   396
     * Performs the specified {@code PrivilegedAction} with privileges
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   397
     * enabled and restricted by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   398
     * {@code AccessControlContext} and with a privilege scope limited
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   399
     * by specified {@code Permission} arguments.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   400
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   401
     * The action is performed with the intersection of the permissions
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   402
     * possessed by the caller's protection domain, and those possessed
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   403
     * by the domains represented by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   404
     * {@code AccessControlContext}.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   405
     * <p>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   406
     * If the action's {@code run} method throws an (unchecked) exception,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   407
     * it will propagate through this method.
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   408
     * <p>
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   409
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   410
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   411
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   412
     * {@literal "createAccessControlContext"}
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   413
     * {@link java.security.SecurityPermission}, then the action is performed
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   414
     * with no permissions.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   415
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   416
     * @param <T> the type of the value returned by the PrivilegedAction's
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   417
     *                  {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   418
     * @param action the action to be performed.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   419
     * @param context an <i>access control context</i>
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   420
     *                representing the restriction to be applied to the
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   421
     *                caller's domain's privileges before performing
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   422
     *                the specified action.  If the context is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   423
     *                {@code null},
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   424
     *                then no additional restriction is applied.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   425
     * @param perms the {@code Permission} arguments which limit the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   426
     *              scope of the caller's privileges. The number of arguments
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   427
     *              is variable.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   428
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   429
     * @return the value returned by the action's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   430
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   431
     * @throws NullPointerException if action or perms or any element of
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   432
     *         perms is {@code null}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   433
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   434
     * @see #doPrivileged(PrivilegedAction)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   435
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   436
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   437
     * @since 1.8
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   438
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   439
    @CallerSensitive
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   440
    public static <T> T doPrivileged(PrivilegedAction<T> action,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   441
        AccessControlContext context, Permission... perms) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   442
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   443
        AccessControlContext parent = getContext();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   444
        if (perms == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   445
            throw new NullPointerException("null permissions parameter");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   446
        }
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   447
        Class<?> caller = Reflection.getCallerClass();
55705
e0f5ad90737c 8216381: More limited privilege usage
mullan
parents: 53435
diff changeset
   448
        DomainCombiner dc = (context == null) ? null : context.getCombiner();
e0f5ad90737c 8216381: More limited privilege usage
mullan
parents: 53435
diff changeset
   449
        return AccessController.doPrivileged(action, createWrapper(dc,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   450
            caller, parent, context, perms));
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   451
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   452
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   453
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   454
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   455
     * Performs the specified {@code PrivilegedAction} with privileges
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   456
     * enabled and restricted by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   457
     * {@code AccessControlContext} and with a privilege scope limited
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   458
     * by specified {@code Permission} arguments.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   459
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   460
     * The action is performed with the intersection of the permissions
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   461
     * possessed by the caller's protection domain, and those possessed
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   462
     * by the domains represented by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   463
     * {@code AccessControlContext}.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   464
     * <p>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   465
     * If the action's {@code run} method throws an (unchecked) exception,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   466
     * it will propagate through this method.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   467
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   468
     * <p> This method preserves the current AccessControlContext's
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   469
     * DomainCombiner (which may be null) while the action is performed.
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   470
     * <p>
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   471
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   472
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   473
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   474
     * {@literal "createAccessControlContext"}
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   475
     * {@link java.security.SecurityPermission}, then the action is performed
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   476
     * with no permissions.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   477
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   478
     * @param <T> the type of the value returned by the PrivilegedAction's
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   479
     *                  {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   480
     * @param action the action to be performed.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   481
     * @param context an <i>access control context</i>
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   482
     *                representing the restriction to be applied to the
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   483
     *                caller's domain's privileges before performing
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   484
     *                the specified action.  If the context is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   485
     *                {@code null},
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   486
     *                then no additional restriction is applied.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   487
     * @param perms the {@code Permission} arguments which limit the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   488
     *              scope of the caller's privileges. The number of arguments
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   489
     *              is variable.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   490
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   491
     * @return the value returned by the action's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   492
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   493
     * @throws NullPointerException if action or perms or any element of
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   494
     *         perms is {@code null}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   495
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   496
     * @see #doPrivileged(PrivilegedAction)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   497
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   498
     * @see java.security.DomainCombiner
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   499
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   500
     * @since 1.8
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   501
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   502
    @CallerSensitive
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   503
    public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   504
        AccessControlContext context, Permission... perms) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   505
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   506
        AccessControlContext parent = getContext();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   507
        DomainCombiner dc = parent.getCombiner();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   508
        if (dc == null && context != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   509
            dc = context.getCombiner();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   510
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   511
        if (perms == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   512
            throw new NullPointerException("null permissions parameter");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   513
        }
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   514
        Class<?> caller = Reflection.getCallerClass();
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   515
        return AccessController.doPrivileged(action, createWrapper(dc, caller,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   516
            parent, context, perms));
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   517
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   518
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   520
     * Performs the specified {@code PrivilegedExceptionAction} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * privileges enabled.  The action is performed with <i>all</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * permissions possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   524
     * <p> If the action's {@code run} method throws an <i>unchecked</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <p> Note that any DomainCombiner associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * AccessControlContext will be ignored while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   530
     * @param <T> the type of the value returned by the
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   531
     *                  PrivilegedExceptionAction's {@code run} method.
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   532
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param action the action to be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   535
     * @return the value returned by the action's {@code run} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   537
     * @throws    PrivilegedActionException if the specified action's
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   538
     *         {@code run} method threw a <i>checked</i> exception
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   539
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @see #doPrivilegedWithCombiner(PrivilegedExceptionAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   546
    @CallerSensitive
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   547
    public static <T> T
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        doPrivileged(PrivilegedExceptionAction<T> action)
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   549
        throws PrivilegedActionException
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   550
    {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   551
        AccessControlContext context = null;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   552
        Class<?> caller = Reflection.getCallerClass();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   553
        try {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   554
            return executePrivileged(action, context, caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   555
        } catch (RuntimeException e) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   556
            throw e;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   557
        } catch (Exception e) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   558
            throw wrapException(e);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   559
        }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   560
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   563
     * Performs the specified {@code PrivilegedExceptionAction} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * privileges enabled.  The action is performed with <i>all</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * permissions possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   567
     * <p> If the action's {@code run} method throws an <i>unchecked</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p> This method preserves the current AccessControlContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * DomainCombiner (which may be null) while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   573
     * @param <T> the type of the value returned by the
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   574
     *                  PrivilegedExceptionAction's {@code run} method.
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   575
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   578
     * @return the value returned by the action's {@code run} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   580
     * @throws    PrivilegedActionException if the specified action's
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   581
     *         {@code run} method threw a <i>checked</i> exception
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   582
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   590
    @CallerSensitive
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   591
    public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   592
        throws PrivilegedActionException
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   593
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        AccessControlContext acc = getStackAccessControlContext();
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   595
        if (acc == null) {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   596
            return AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   598
        DomainCombiner dc = acc.getAssignedCombiner();
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   599
        return AccessController.doPrivileged(action,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   600
                                             preserveCombiner(dc, Reflection.getCallerClass()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * preserve the combiner across the doPrivileged call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   606
    private static AccessControlContext preserveCombiner(DomainCombiner combiner,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   607
                                                         Class<?> caller)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   608
    {
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   609
        return createWrapper(combiner, caller, null, null, null);
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   610
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   611
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   612
    /**
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   613
     * Create a wrapper to contain the limited privilege scope data.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   614
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   615
    private static AccessControlContext
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   616
        createWrapper(DomainCombiner combiner, Class<?> caller,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   617
                      AccessControlContext parent, AccessControlContext context,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   618
                      Permission[] perms)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   619
    {
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   620
        ProtectionDomain callerPD = getProtectionDomain(caller);
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   621
        // check if caller is authorized to create context
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   622
        if (System.getSecurityManager() != null &&
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   623
            context != null && !context.isAuthorized() &&
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   624
            !callerPD.implies(SecurityConstants.CREATE_ACC_PERMISSION))
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   625
        {
24697
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   626
            return getInnocuousAcc();
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   627
        } else {
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   628
            return new AccessControlContext(callerPD, combiner, parent,
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   629
                                            context, perms);
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   630
        }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   631
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   632
24697
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   633
    private static class AccHolder {
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   634
        // An AccessControlContext with no granted permissions.
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   635
        // Only initialized on demand when getInnocuousAcc() is called.
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   636
        static final AccessControlContext innocuousAcc =
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   637
            new AccessControlContext(new ProtectionDomain[] {
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   638
                                     new ProtectionDomain(null, null) });
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   639
    }
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   640
    private static AccessControlContext getInnocuousAcc() {
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   641
        return AccHolder.innocuousAcc;
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   642
    }
f2fbe6256751 8036841: Reuse no-perms AccessControlContext object when performing isAuthorized check
mullan
parents: 21334
diff changeset
   643
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   644
    private static native ProtectionDomain getProtectionDomain(final Class<?> caller);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   647
     * Performs the specified {@code PrivilegedExceptionAction} with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * privileges enabled and restricted by the specified
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   649
     * {@code AccessControlContext}.  The action is performed with the
2589
af4853bc7e87 6827153: Miscellaneous typos in javadoc
martin
parents: 2
diff changeset
   650
     * intersection of the permissions possessed by the caller's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * protection domain, and those possessed by the domains represented by the
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   652
     * specified {@code AccessControlContext}.
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   653
     * <p>
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   654
     * If the action's {@code run} method throws an <i>unchecked</i>
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   655
     * exception, it will propagate through this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p>
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   657
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   658
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   659
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   660
     * {@literal "createAccessControlContext"}
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   661
     * {@link java.security.SecurityPermission}, then the action is performed
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   662
     * with no permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   664
     * @param <T> the type of the value returned by the
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   665
     *                  PrivilegedExceptionAction's {@code run} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param action the action to be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param context an <i>access control context</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *                representing the restriction to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *                caller's domain's privileges before performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *                the specified action.  If the context is
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   671
     *                {@code null}, then no additional restriction is applied.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   673
     * @return the value returned by the action's {@code run} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   675
     * @throws    PrivilegedActionException if the specified action's
18222
2b50015e08db 8001330: Improve on checking order
mullan
parents: 16906
diff changeset
   676
     *         {@code run} method threw a <i>checked</i> exception
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   677
     * @throws    NullPointerException if the action is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see #doPrivileged(PrivilegedAction)
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   680
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   682
    @CallerSensitive
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   683
    public static <T> T
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        doPrivileged(PrivilegedExceptionAction<T> action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                     AccessControlContext context)
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   686
        throws PrivilegedActionException
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   687
    {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   688
        Class<?> caller = Reflection.getCallerClass();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   689
        context = checkContext(context, caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   690
        try {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   691
            return executePrivileged(action, context, caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   692
        } catch (RuntimeException e) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   693
            throw e;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   694
        } catch (Exception e) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   695
            throw wrapException(e);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   696
        }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   697
    }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   698
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   699
    private static AccessControlContext checkContext(AccessControlContext context,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   700
        Class<?> caller)
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   701
    {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   702
        // check if caller is authorized to create context
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   703
        if (System.getSecurityManager() != null &&
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   704
            context != null && !context.isAuthorized() &&
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   705
            context != getInnocuousAcc())
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   706
        {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   707
            ProtectionDomain callerPD = getProtectionDomain(caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   708
            if (callerPD != null && !callerPD.implies(SecurityConstants.CREATE_ACC_PERMISSION)) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   709
                return getInnocuousAcc();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   710
            }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   711
        }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   712
        return context;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   713
    }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   714
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   715
    /**
53068
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   716
     * The value needs to be physically located in the frame, so that it
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   717
     * can be found by a stack walk.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   718
     */
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   719
    @Hidden
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   720
    private static native void ensureMaterializedForStackWalk(Object o);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   721
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   722
    /**
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   723
     * Sanity check that the caller context is indeed privileged.
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   724
     *
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   725
     * Used by executePrivileged to make sure the frame is properly
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   726
     * recognized by the VM.
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   727
     */
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   728
    private static boolean isPrivileged() {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   729
        AccessControlContext ctx = getStackAccessControlContext();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   730
        return ctx == null || ctx.isPrivileged();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   731
    }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   732
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   733
    /**
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   734
     * Execute the action as privileged.
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   735
     *
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   736
     * The VM recognizes this method as special, so any changes to the
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   737
     * name or signature require corresponding changes in
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   738
     * getStackAccessControlContext().
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   739
     */
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   740
    @Hidden
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   741
    @ForceInline
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   742
    private static <T> T
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   743
        executePrivileged(PrivilegedAction<T> action,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   744
                          AccessControlContext context,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   745
                          Class<?> caller)
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   746
    {
53068
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   747
        // Ensure context has a physical value in the frame
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   748
        if (context != null) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   749
            ensureMaterializedForStackWalk(context);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   750
        }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   751
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   752
        assert isPrivileged(); // sanity check invariant
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   753
        T result = action.run();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   754
        assert isPrivileged(); // sanity check invariant
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   756
        // Keep these alive across the run() call so they can be
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   757
        // retrieved by getStackAccessControlContext().
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   758
        Reference.reachabilityFence(context);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   759
        Reference.reachabilityFence(caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   760
        return result;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   761
    }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   762
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   763
    /**
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   764
     * Execute the action as privileged.
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   765
     *
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   766
     * The VM recognizes this method as special, so any changes to the
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   767
     * name or signature require corresponding changes in
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   768
     * getStackAccessControlContext().
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   769
     */
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   770
    @Hidden
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   771
    @ForceInline
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   772
    private static <T> T
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   773
        executePrivileged(PrivilegedExceptionAction<T> action,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   774
                          AccessControlContext context,
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   775
                          Class<?> caller)
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   776
        throws Exception
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   777
    {
53068
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   778
        // Ensure context has a physical value in the frame
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   779
        if (context != null) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   780
            ensureMaterializedForStackWalk(context);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   781
        }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents: 52431
diff changeset
   782
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   783
        assert isPrivileged(); // sanity check invariant
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   784
        T result = action.run();
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   785
        assert isPrivileged(); // sanity check invariant
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   786
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   787
        // Keep these alive across the run() call so they can be
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   788
        // retrieved by getStackAccessControlContext().
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   789
        Reference.reachabilityFence(context);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   790
        Reference.reachabilityFence(caller);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   791
        return result;
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   792
    }
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   793
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   794
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   795
    /**
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   796
     * Wrap an exception.  The annotations are used in a best effort to
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   797
     * avoid StackOverflowError in the caller.  Inlining the callees as
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   798
     * well and tail-call elimination could also help here, but are not
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   799
     * needed for correctness, only quality of implementation.
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   800
     */
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   801
    @Hidden
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   802
    @ForceInline
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   803
    @ReservedStackAccess
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   804
    private static PrivilegedActionException wrapException(Exception e) {
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   805
        return new PrivilegedActionException(e);
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   806
    }
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   807
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   808
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   809
     * Performs the specified {@code PrivilegedExceptionAction} with
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   810
     * privileges enabled and restricted by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   811
     * {@code AccessControlContext} and with a privilege scope limited by
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   812
     * specified {@code Permission} arguments.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   813
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   814
     * The action is performed with the intersection of the permissions
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   815
     * possessed by the caller's protection domain, and those possessed
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   816
     * by the domains represented by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   817
     * {@code AccessControlContext}.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   818
     * <p>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   819
     * If the action's {@code run} method throws an (unchecked) exception,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   820
     * it will propagate through this method.
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   821
     * <p>
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   822
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   823
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   824
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   825
     * {@literal "createAccessControlContext"}
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   826
     * {@link java.security.SecurityPermission}, then the action is performed
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   827
     * with no permissions.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   828
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   829
     * @param <T> the type of the value returned by the
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   830
     *                  PrivilegedExceptionAction's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   831
     * @param action the action to be performed.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   832
     * @param context an <i>access control context</i>
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   833
     *                representing the restriction to be applied to the
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   834
     *                caller's domain's privileges before performing
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   835
     *                the specified action.  If the context is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   836
     *                {@code null},
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   837
     *                then no additional restriction is applied.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   838
     * @param perms the {@code Permission} arguments which limit the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   839
     *              scope of the caller's privileges. The number of arguments
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   840
     *              is variable.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   841
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   842
     * @return the value returned by the action's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   843
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   844
     * @throws PrivilegedActionException if the specified action's
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   845
     *         {@code run} method threw a <i>checked</i> exception
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   846
     * @throws NullPointerException if action or perms or any element of
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   847
     *         perms is {@code null}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   848
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   849
     * @see #doPrivileged(PrivilegedAction)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   850
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   851
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   852
     * @since 1.8
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   853
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   854
    @CallerSensitive
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   855
    public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   856
                                     AccessControlContext context, Permission... perms)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   857
        throws PrivilegedActionException
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   858
    {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   859
        AccessControlContext parent = getContext();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   860
        if (perms == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   861
            throw new NullPointerException("null permissions parameter");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   862
        }
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   863
        Class<?> caller = Reflection.getCallerClass();
55705
e0f5ad90737c 8216381: More limited privilege usage
mullan
parents: 53435
diff changeset
   864
        DomainCombiner dc = (context == null) ? null : context.getCombiner();
e0f5ad90737c 8216381: More limited privilege usage
mullan
parents: 53435
diff changeset
   865
        return AccessController.doPrivileged(action, createWrapper(dc, caller, parent, context, perms));
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   866
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   867
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   868
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   869
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   870
     * Performs the specified {@code PrivilegedExceptionAction} with
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   871
     * privileges enabled and restricted by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   872
     * {@code AccessControlContext} and with a privilege scope limited by
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   873
     * specified {@code Permission} arguments.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   874
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   875
     * The action is performed with the intersection of the permissions
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   876
     * possessed by the caller's protection domain, and those possessed
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   877
     * by the domains represented by the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   878
     * {@code AccessControlContext}.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   879
     * <p>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   880
     * If the action's {@code run} method throws an (unchecked) exception,
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   881
     * it will propagate through this method.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   882
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   883
     * <p> This method preserves the current AccessControlContext's
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   884
     * DomainCombiner (which may be null) while the action is performed.
21321
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   885
     * <p>
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   886
     * If a security manager is installed and the specified
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   887
     * {@code AccessControlContext} was not created by system code and the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   888
     * caller's {@code ProtectionDomain} has not been granted the
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   889
     * {@literal "createAccessControlContext"}
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   890
     * {@link java.security.SecurityPermission}, then the action is performed
923ca53e6572 8021191: Add isAuthorized check to limited doPrivileged methods
mullan
parents: 19828
diff changeset
   891
     * with no permissions.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   892
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   893
     * @param <T> the type of the value returned by the
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   894
     *                  PrivilegedExceptionAction's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   895
     * @param action the action to be performed.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   896
     * @param context an <i>access control context</i>
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   897
     *                representing the restriction to be applied to the
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   898
     *                caller's domain's privileges before performing
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   899
     *                the specified action.  If the context is
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   900
     *                {@code null},
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   901
     *                then no additional restriction is applied.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   902
     * @param perms the {@code Permission} arguments which limit the
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   903
     *              scope of the caller's privileges. The number of arguments
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   904
     *              is variable.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   905
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   906
     * @return the value returned by the action's {@code run} method.
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   907
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   908
     * @throws PrivilegedActionException if the specified action's
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   909
     *         {@code run} method threw a <i>checked</i> exception
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   910
     * @throws NullPointerException if action or perms or any element of
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   911
     *         perms is {@code null}
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   912
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   913
     * @see #doPrivileged(PrivilegedAction)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   914
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   915
     * @see java.security.DomainCombiner
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   916
     *
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   917
     * @since 1.8
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   918
     */
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   919
    @CallerSensitive
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   920
    public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   921
                                                 AccessControlContext context,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   922
                                                 Permission... perms)
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   923
        throws PrivilegedActionException
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   924
    {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   925
        AccessControlContext parent = getContext();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   926
        DomainCombiner dc = parent.getCombiner();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   927
        if (dc == null && context != null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   928
            dc = context.getCombiner();
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   929
        }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   930
        if (perms == null) {
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   931
            throw new NullPointerException("null permissions parameter");
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   932
        }
52431
b0af758a092c 8212605: Pure-Java implementation of AccessController.doPrivileged
dlong
parents: 47216
diff changeset
   933
        Class<?> caller = Reflection.getCallerClass();
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   934
        return AccessController.doPrivileged(action, createWrapper(dc, caller,
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   935
            parent, context, perms));
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   936
    }
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   937
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns the AccessControl context. i.e., it gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * the protection domains of all the callers on the stack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * starting at the first class with a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @return the access control context based on the current stack or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *         null if there was only privileged system code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    private static native AccessControlContext getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   950
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Returns the "inherited" AccessControl context. This is the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * that existed when the thread was created. Package private so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * AccessControlContext can use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    static native AccessControlContext getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * This method takes a "snapshot" of the current calling context, which
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   961
     * includes the current Thread's inherited AccessControlContext and any
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   962
     * limited privilege scope, and places it in an AccessControlContext object.
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
   963
     * This context may then be checked at a later point, possibly in another thread.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @see AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @return the AccessControlContext based on the current context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    public static AccessControlContext getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        AccessControlContext acc = getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        if (acc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            // all we had was privileged system code. We don't want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            // to return null though, so we construct a real ACC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            return new AccessControlContext(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            return acc.optimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Determines whether the access request indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * specified permission should be allowed or denied, based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * the current AccessControlContext and security policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * This method quietly returns if the access request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * is permitted, or throws an AccessControlException otherwise. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * getPermission method of the AccessControlException returns the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   989
     * {@code perm} Permission object instance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param perm the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   993
     * @throws    AccessControlException if the specified permission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *            is not permitted, based on the current security policy.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55705
diff changeset
   995
     * @throws    NullPointerException if the specified permission
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18253
diff changeset
   996
     *            is {@code null} and is checked based on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *            security policy currently in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    public static void checkPermission(Permission perm)
17946
7791613dcbfd 8014097: add doPrivileged methods with limited privilege scope
jdn
parents: 16906
diff changeset
  1001
        throws AccessControlException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        //System.err.println("checkPermission "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        //Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (perm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            throw new NullPointerException("permission can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        AccessControlContext stack = getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // if context is null, we had privileged system code on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if (stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            Debug debug = AccessControlContext.getDebug();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            boolean dumpDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                dumpDebug = !Debug.isOn("codebase=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                dumpDebug &= !Debug.isOn("permission=") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    Debug.isOn("permission=" + perm.getClass().getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (dumpDebug && Debug.isOn("stack")) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1022
                Thread.dumpStack();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (dumpDebug && Debug.isOn("domain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                debug.println("domain (context is null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                debug.println("access allowed "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        AccessControlContext acc = stack.optimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        acc.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
}