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