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