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