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