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