author | jbachorik |
Fri, 16 Jan 2015 18:19:23 +0100 | |
changeset 29027 | 2df5737b2c8a |
parent 27181 | 29f9c4f56e80 |
child 29030 | 725688906a2b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29027
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, 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 |
||
27181 | 28 |
import java.lang.ref.WeakReference; |
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
29 |
import java.util.ArrayList; |
2 | 30 |
import java.util.Enumeration; |
31 |
import java.util.List; |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
32 |
import java.util.Map; |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
33 |
import java.util.WeakHashMap; |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
34 |
import sun.misc.JavaSecurityProtectionDomainAccess; |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
35 |
import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache; |
2 | 36 |
import sun.security.util.Debug; |
37 |
import sun.security.util.SecurityConstants; |
|
8816
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
38 |
import sun.misc.JavaSecurityAccess; |
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
39 |
import sun.misc.SharedSecrets; |
2 | 40 |
|
41 |
/** |
|
42 |
* |
|
43 |
*<p> |
|
44 |
* This ProtectionDomain class encapsulates the characteristics of a domain, |
|
45 |
* which encloses a set of classes whose instances are granted a set |
|
46 |
* of permissions when being executed on behalf of a given set of Principals. |
|
47 |
* <p> |
|
48 |
* A static set of permissions can be bound to a ProtectionDomain when it is |
|
49 |
* constructed; such permissions are granted to the domain regardless of the |
|
50 |
* Policy in force. However, to support dynamic security policies, a |
|
51 |
* ProtectionDomain can also be constructed such that it is dynamically |
|
52 |
* mapped to a set of permissions by the current Policy whenever a permission |
|
53 |
* is checked. |
|
54 |
* <p> |
|
55 |
* |
|
56 |
* @author Li Gong |
|
57 |
* @author Roland Schemers |
|
58 |
* @author Gary Ellison |
|
59 |
*/ |
|
60 |
||
61 |
public class ProtectionDomain { |
|
29027
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
62 |
private static class JavaSecurityAccessImpl implements JavaSecurityAccess { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
63 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
64 |
private JavaSecurityAccessImpl() { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
65 |
} |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
66 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
67 |
@Override |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
68 |
public <T> T doIntersectionPrivilege( |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
69 |
PrivilegedAction<T> action, |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
70 |
final AccessControlContext stack, |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
71 |
final AccessControlContext context) { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
72 |
if (action == null) { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
73 |
throw new NullPointerException(); |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
74 |
} |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
75 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
76 |
return AccessController.doPrivileged( |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
77 |
action, |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
78 |
getCombinedACC(context, stack) |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
79 |
); |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
80 |
} |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
81 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
82 |
@Override |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
83 |
public <T> T doIntersectionPrivilege( |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
84 |
PrivilegedAction<T> action, |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
85 |
AccessControlContext context) { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
86 |
return doIntersectionPrivilege(action, |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
87 |
AccessController.getContext(), context); |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
88 |
} |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
89 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
90 |
private static AccessControlContext getCombinedACC(AccessControlContext context, AccessControlContext stack) { |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
91 |
AccessControlContext acc = new AccessControlContext(context, stack.getDomainCombiner(), true); |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
92 |
|
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
93 |
return new AccessControlContext(stack.getContext(), acc).optimize(); |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
94 |
} |
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
95 |
} |
2 | 96 |
|
8816
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
97 |
static { |
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
98 |
// Set up JavaSecurityAccess in SharedSecrets |
29027
2df5737b2c8a
8064331: JavaSecurityAccess.doIntersectionPrivilege() drops the information about the domain combiner of the stack ACC
jbachorik
parents:
27181
diff
changeset
|
99 |
SharedSecrets.setJavaSecurityAccess(new JavaSecurityAccessImpl()); |
8816
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
100 |
} |
29f983feda95
6907662: System clipboard should ensure access restrictions
denis
parents:
7970
diff
changeset
|
101 |
|
2 | 102 |
/* CodeSource */ |
103 |
private CodeSource codesource ; |
|
104 |
||
105 |
/* ClassLoader the protection domain was consed from */ |
|
106 |
private ClassLoader classloader; |
|
107 |
||
108 |
/* Principals running-as within this protection domain */ |
|
109 |
private Principal[] principals; |
|
110 |
||
111 |
/* the rights this protection domain is granted */ |
|
112 |
private PermissionCollection permissions; |
|
113 |
||
114 |
/* if the permissions object has AllPermission */ |
|
115 |
private boolean hasAllPerm = false; |
|
116 |
||
117 |
/* the PermissionCollection is static (pre 1.4 constructor) |
|
118 |
or dynamic (via a policy refresh) */ |
|
119 |
private boolean staticPermissions; |
|
120 |
||
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
121 |
/* |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
122 |
* An object used as a key when the ProtectionDomain is stored in a Map. |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
123 |
*/ |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
124 |
final Key key = new Key(); |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
125 |
|
2 | 126 |
private static final Debug debug = Debug.getInstance("domain"); |
127 |
||
128 |
/** |
|
129 |
* Creates a new ProtectionDomain with the given CodeSource and |
|
130 |
* Permissions. If the permissions object is not null, then |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18222
diff
changeset
|
131 |
* {@code setReadOnly())} will be called on the passed in |
2 | 132 |
* Permissions object. The only permissions granted to this domain |
133 |
* are the ones specified; the current Policy will not be consulted. |
|
134 |
* |
|
135 |
* @param codesource the codesource associated with this domain |
|
136 |
* @param permissions the permissions granted to this domain |
|
137 |
*/ |
|
138 |
public ProtectionDomain(CodeSource codesource, |
|
139 |
PermissionCollection permissions) { |
|
140 |
this.codesource = codesource; |
|
141 |
if (permissions != null) { |
|
142 |
this.permissions = permissions; |
|
143 |
this.permissions.setReadOnly(); |
|
144 |
if (permissions instanceof Permissions && |
|
145 |
((Permissions)permissions).allPermission != null) { |
|
146 |
hasAllPerm = true; |
|
147 |
} |
|
148 |
} |
|
149 |
this.classloader = null; |
|
150 |
this.principals = new Principal[0]; |
|
151 |
staticPermissions = true; |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Creates a new ProtectionDomain qualified by the given CodeSource, |
|
156 |
* Permissions, ClassLoader and array of Principals. If the |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18222
diff
changeset
|
157 |
* permissions object is not null, then {@code setReadOnly()} |
2 | 158 |
* will be called on the passed in Permissions object. |
159 |
* The permissions granted to this domain are dynamic; they include |
|
160 |
* both the static permissions passed to this constructor, and any |
|
161 |
* permissions granted to this domain by the current Policy at the |
|
162 |
* time a permission is checked. |
|
163 |
* <p> |
|
164 |
* This constructor is typically used by |
|
165 |
* {@link SecureClassLoader ClassLoaders} |
|
166 |
* and {@link DomainCombiner DomainCombiners} which delegate to |
|
18579
b678846778ad
8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents:
18222
diff
changeset
|
167 |
* {@code Policy} to actively associate the permissions granted to |
2 | 168 |
* this domain. This constructor affords the |
169 |
* Policy provider the opportunity to augment the supplied |
|
170 |
* PermissionCollection to reflect policy changes. |
|
171 |
* <p> |
|
172 |
* |
|
173 |
* @param codesource the CodeSource associated with this domain |
|
174 |
* @param permissions the permissions granted to this domain |
|
175 |
* @param classloader the ClassLoader associated with this domain |
|
176 |
* @param principals the array of Principals associated with this |
|
177 |
* domain. The contents of the array are copied to protect against |
|
178 |
* subsequent modification. |
|
179 |
* @see Policy#refresh |
|
180 |
* @see Policy#getPermissions(ProtectionDomain) |
|
181 |
* @since 1.4 |
|
182 |
*/ |
|
183 |
public ProtectionDomain(CodeSource codesource, |
|
184 |
PermissionCollection permissions, |
|
185 |
ClassLoader classloader, |
|
186 |
Principal[] principals) { |
|
187 |
this.codesource = codesource; |
|
188 |
if (permissions != null) { |
|
189 |
this.permissions = permissions; |
|
190 |
this.permissions.setReadOnly(); |
|
191 |
if (permissions instanceof Permissions && |
|
192 |
((Permissions)permissions).allPermission != null) { |
|
193 |
hasAllPerm = true; |
|
194 |
} |
|
195 |
} |
|
196 |
this.classloader = classloader; |
|
197 |
this.principals = (principals != null ? principals.clone(): |
|
198 |
new Principal[0]); |
|
199 |
staticPermissions = false; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns the CodeSource of this domain. |
|
204 |
* @return the CodeSource of this domain which may be null. |
|
205 |
* @since 1.2 |
|
206 |
*/ |
|
207 |
public final CodeSource getCodeSource() { |
|
208 |
return this.codesource; |
|
209 |
} |
|
210 |
||
211 |
||
212 |
/** |
|
213 |
* Returns the ClassLoader of this domain. |
|
214 |
* @return the ClassLoader of this domain which may be null. |
|
215 |
* |
|
216 |
* @since 1.4 |
|
217 |
*/ |
|
218 |
public final ClassLoader getClassLoader() { |
|
219 |
return this.classloader; |
|
220 |
} |
|
221 |
||
222 |
||
223 |
/** |
|
224 |
* Returns an array of principals for this domain. |
|
225 |
* @return a non-null array of principals for this domain. |
|
226 |
* Returns a new array each time this method is called. |
|
227 |
* |
|
228 |
* @since 1.4 |
|
229 |
*/ |
|
230 |
public final Principal[] getPrincipals() { |
|
231 |
return this.principals.clone(); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Returns the static permissions granted to this domain. |
|
236 |
* |
|
237 |
* @return the static set of permissions for this domain which may be null. |
|
238 |
* @see Policy#refresh |
|
239 |
* @see Policy#getPermissions(ProtectionDomain) |
|
240 |
*/ |
|
241 |
public final PermissionCollection getPermissions() { |
|
242 |
return permissions; |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Check and see if this ProtectionDomain implies the permissions |
|
247 |
* expressed in the Permission object. |
|
248 |
* <p> |
|
249 |
* The set of permissions evaluated is a function of whether the |
|
250 |
* ProtectionDomain was constructed with a static set of permissions |
|
251 |
* or it was bound to a dynamically mapped set of permissions. |
|
252 |
* <p> |
|
253 |
* If the ProtectionDomain was constructed to a |
|
254 |
* {@link #ProtectionDomain(CodeSource, PermissionCollection) |
|
255 |
* statically bound} PermissionCollection then the permission will |
|
256 |
* only be checked against the PermissionCollection supplied at |
|
257 |
* construction. |
|
258 |
* <p> |
|
259 |
* However, if the ProtectionDomain was constructed with |
|
260 |
* the constructor variant which supports |
|
261 |
* {@link #ProtectionDomain(CodeSource, PermissionCollection, |
|
262 |
* ClassLoader, java.security.Principal[]) dynamically binding} |
|
263 |
* permissions, then the permission will be checked against the |
|
264 |
* combination of the PermissionCollection supplied at construction and |
|
265 |
* the current Policy binding. |
|
266 |
* <p> |
|
267 |
* |
|
268 |
* @param permission the Permission object to check. |
|
269 |
* |
|
270 |
* @return true if "permission" is implicit to this ProtectionDomain. |
|
271 |
*/ |
|
272 |
public boolean implies(Permission permission) { |
|
273 |
||
274 |
if (hasAllPerm) { |
|
275 |
// internal permission collection already has AllPermission - |
|
276 |
// no need to go to policy |
|
277 |
return true; |
|
278 |
} |
|
279 |
||
280 |
if (!staticPermissions && |
|
281 |
Policy.getPolicyNoCheck().implies(this, permission)) |
|
282 |
return true; |
|
283 |
if (permissions != null) |
|
284 |
return permissions.implies(permission); |
|
285 |
||
286 |
return false; |
|
287 |
} |
|
288 |
||
18222 | 289 |
// called by the VM -- do not remove |
290 |
boolean impliesCreateAccessControlContext() { |
|
291 |
return implies(SecurityConstants.CREATE_ACC_PERMISSION); |
|
292 |
} |
|
293 |
||
2 | 294 |
/** |
295 |
* Convert a ProtectionDomain to a String. |
|
296 |
*/ |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
297 |
@Override public String toString() { |
2 | 298 |
String pals = "<no principals>"; |
299 |
if (principals != null && principals.length > 0) { |
|
300 |
StringBuilder palBuf = new StringBuilder("(principals "); |
|
301 |
||
302 |
for (int i = 0; i < principals.length; i++) { |
|
303 |
palBuf.append(principals[i].getClass().getName() + |
|
304 |
" \"" + principals[i].getName() + |
|
305 |
"\""); |
|
306 |
if (i < principals.length-1) |
|
307 |
palBuf.append(",\n"); |
|
308 |
else |
|
309 |
palBuf.append(")\n"); |
|
310 |
} |
|
311 |
pals = palBuf.toString(); |
|
312 |
} |
|
313 |
||
314 |
// Check if policy is set; we don't want to load |
|
315 |
// the policy prematurely here |
|
316 |
PermissionCollection pc = Policy.isSet() && seeAllp() ? |
|
317 |
mergePermissions(): |
|
318 |
getPermissions(); |
|
319 |
||
320 |
return "ProtectionDomain "+ |
|
321 |
" "+codesource+"\n"+ |
|
322 |
" "+classloader+"\n"+ |
|
323 |
" "+pals+"\n"+ |
|
324 |
" "+pc+"\n"; |
|
325 |
} |
|
326 |
||
327 |
/** |
|
328 |
* Return true (merge policy permissions) in the following cases: |
|
329 |
* |
|
330 |
* . SecurityManager is null |
|
331 |
* |
|
332 |
* . SecurityManager is not null, |
|
333 |
* debug is not null, |
|
334 |
* SecurityManager impelmentation is in bootclasspath, |
|
335 |
* Policy implementation is in bootclasspath |
|
336 |
* (the bootclasspath restrictions avoid recursion) |
|
337 |
* |
|
338 |
* . SecurityManager is not null, |
|
339 |
* debug is null, |
|
340 |
* caller has Policy.getPolicy permission |
|
341 |
*/ |
|
342 |
private static boolean seeAllp() { |
|
343 |
SecurityManager sm = System.getSecurityManager(); |
|
344 |
||
345 |
if (sm == null) { |
|
346 |
return true; |
|
347 |
} else { |
|
348 |
if (debug != null) { |
|
349 |
if (sm.getClass().getClassLoader() == null && |
|
350 |
Policy.getPolicyNoCheck().getClass().getClassLoader() |
|
351 |
== null) { |
|
352 |
return true; |
|
353 |
} |
|
354 |
} else { |
|
355 |
try { |
|
356 |
sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION); |
|
357 |
return true; |
|
358 |
} catch (SecurityException se) { |
|
359 |
// fall thru and return false |
|
360 |
} |
|
361 |
} |
|
362 |
} |
|
363 |
||
364 |
return false; |
|
365 |
} |
|
366 |
||
367 |
private PermissionCollection mergePermissions() { |
|
368 |
if (staticPermissions) |
|
369 |
return permissions; |
|
370 |
||
371 |
PermissionCollection perms = |
|
372 |
java.security.AccessController.doPrivileged |
|
373 |
(new java.security.PrivilegedAction<PermissionCollection>() { |
|
374 |
public PermissionCollection run() { |
|
375 |
Policy p = Policy.getPolicyNoCheck(); |
|
376 |
return p.getPermissions(ProtectionDomain.this); |
|
377 |
} |
|
378 |
}); |
|
379 |
||
380 |
Permissions mergedPerms = new Permissions(); |
|
381 |
int swag = 32; |
|
382 |
int vcap = 8; |
|
383 |
Enumeration<Permission> e; |
|
7970
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
5506
diff
changeset
|
384 |
List<Permission> pdVector = new ArrayList<>(vcap); |
af1579474d16
7008728: diamond conversion of basic security, permissions, authentication
smarks
parents:
5506
diff
changeset
|
385 |
List<Permission> plVector = new ArrayList<>(swag); |
2 | 386 |
|
387 |
// |
|
388 |
// Build a vector of domain permissions for subsequent merge |
|
389 |
if (permissions != null) { |
|
390 |
synchronized (permissions) { |
|
391 |
e = permissions.elements(); |
|
392 |
while (e.hasMoreElements()) { |
|
393 |
pdVector.add(e.nextElement()); |
|
394 |
} |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
// |
|
399 |
// Build a vector of Policy permissions for subsequent merge |
|
400 |
if (perms != null) { |
|
401 |
synchronized (perms) { |
|
402 |
e = perms.elements(); |
|
403 |
while (e.hasMoreElements()) { |
|
404 |
plVector.add(e.nextElement()); |
|
405 |
vcap++; |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
if (perms != null && permissions != null) { |
|
411 |
// |
|
412 |
// Weed out the duplicates from the policy. Unless a refresh |
|
21278 | 413 |
// has occurred since the pd was consed this should result in |
2 | 414 |
// an empty vector. |
415 |
synchronized (permissions) { |
|
416 |
e = permissions.elements(); // domain vs policy |
|
417 |
while (e.hasMoreElements()) { |
|
418 |
Permission pdp = e.nextElement(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
419 |
Class<?> pdpClass = pdp.getClass(); |
2 | 420 |
String pdpActions = pdp.getActions(); |
421 |
String pdpName = pdp.getName(); |
|
422 |
for (int i = 0; i < plVector.size(); i++) { |
|
423 |
Permission pp = plVector.get(i); |
|
424 |
if (pdpClass.isInstance(pp)) { |
|
425 |
// The equals() method on some permissions |
|
426 |
// have some side effects so this manual |
|
427 |
// comparison is sufficient. |
|
428 |
if (pdpName.equals(pp.getName()) && |
|
429 |
pdpActions.equals(pp.getActions())) { |
|
430 |
plVector.remove(i); |
|
431 |
break; |
|
432 |
} |
|
433 |
} |
|
434 |
} |
|
435 |
} |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
if (perms !=null) { |
|
440 |
// the order of adding to merged perms and permissions |
|
441 |
// needs to preserve the bugfix 4301064 |
|
442 |
||
443 |
for (int i = plVector.size()-1; i >= 0; i--) { |
|
444 |
mergedPerms.add(plVector.get(i)); |
|
445 |
} |
|
446 |
} |
|
447 |
if (permissions != null) { |
|
448 |
for (int i = pdVector.size()-1; i >= 0; i--) { |
|
449 |
mergedPerms.add(pdVector.get(i)); |
|
450 |
} |
|
451 |
} |
|
452 |
||
453 |
return mergedPerms; |
|
454 |
} |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
455 |
|
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
456 |
/** |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
457 |
* Used for storing ProtectionDomains as keys in a Map. |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
458 |
*/ |
27181 | 459 |
final static class Key {} |
460 |
||
461 |
// A cache of ProtectionDomains and their Permissions |
|
462 |
private static class PDCache implements ProtectionDomainCache { |
|
463 |
// We must wrap the PermissionCollection in a WeakReference as there |
|
464 |
// are some PermissionCollections which contain strong references |
|
465 |
// back to a ProtectionDomain and otherwise would never be removed |
|
466 |
// from the WeakHashMap |
|
467 |
private final Map<Key, WeakReference<PermissionCollection>> |
|
468 |
map = new WeakHashMap<>(); |
|
469 |
||
470 |
@Override |
|
471 |
public synchronized void put(ProtectionDomain pd, |
|
472 |
PermissionCollection pc) { |
|
473 |
map.put(pd == null ? null : pd.key, new WeakReference<>(pc)); |
|
474 |
} |
|
475 |
||
476 |
@Override |
|
477 |
public synchronized PermissionCollection get(ProtectionDomain pd) { |
|
478 |
WeakReference<PermissionCollection> ref = |
|
479 |
map.get(pd == null ? null : pd.key); |
|
480 |
return ref == null ? null : ref.get(); |
|
481 |
} |
|
482 |
} |
|
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
483 |
|
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
484 |
static { |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
485 |
SharedSecrets.setJavaSecurityProtectionDomainAccess( |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
486 |
new JavaSecurityProtectionDomainAccess() { |
27181 | 487 |
@Override |
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
488 |
public ProtectionDomainCache getProtectionDomainCache() { |
27181 | 489 |
return new PDCache(); |
5183
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
490 |
} |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
491 |
}); |
7825d97c4cf2
6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents:
2
diff
changeset
|
492 |
} |
2 | 493 |
} |