author | mullan |
Wed, 04 Apr 2018 11:26:10 -0400 | |
changeset 49520 | 7a64b48586d8 |
parent 48027 | ddbcfca4d51d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49520
7a64b48586d8
8193032: Remove terminally deprecated SecurityManager APIs
mullan
parents:
48027
diff
changeset
|
2 |
* Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.applet; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.FilePermission; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.FileDescriptor; |
|
32 |
import java.net.URL; |
|
33 |
import java.net.URLClassLoader; |
|
34 |
import java.net.InetAddress; |
|
35 |
import java.net.UnknownHostException; |
|
36 |
import java.net.SocketPermission; |
|
37 |
import java.util.Enumeration; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.HashSet; |
|
40 |
import java.util.StringTokenizer; |
|
41 |
import java.security.*; |
|
42 |
import java.lang.reflect.*; |
|
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
43 |
import jdk.internal.misc.JavaNetURLClassLoaderAccess; |
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
44 |
import jdk.internal.misc.JavaSecurityAccess; |
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
45 |
import jdk.internal.misc.SharedSecrets; |
2 | 46 |
import sun.awt.AWTSecurityManager; |
47 |
import sun.awt.AppContext; |
|
22060
cd4f9d7dbeda
8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents:
21278
diff
changeset
|
48 |
import sun.awt.AWTPermissions; |
2 | 49 |
import sun.security.util.SecurityConstants; |
50 |
||
48027
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
51 |
import static java.lang.StackWalker.*; |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
52 |
import static java.lang.StackWalker.Option.*; |
2 | 53 |
|
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
54 |
|
2 | 55 |
/** |
56 |
* This class defines an applet security policy |
|
57 |
* |
|
58 |
*/ |
|
59 |
public |
|
60 |
class AppletSecurity extends AWTSecurityManager { |
|
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
61 |
private static final JavaNetURLClassLoaderAccess JNUCLA |
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
62 |
= SharedSecrets.getJavaNetURLClassLoaderAccess(); |
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
63 |
private static final JavaSecurityAccess JSA = SharedSecrets.getJavaSecurityAccess(); |
2 | 64 |
|
65 |
/** |
|
66 |
* Construct and initialize. |
|
67 |
*/ |
|
68 |
public AppletSecurity() { |
|
69 |
reset(); |
|
70 |
} |
|
71 |
||
72 |
// Cache to store known restricted packages |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
73 |
private HashSet<String> restrictedPackages = new HashSet<>(); |
2 | 74 |
|
75 |
/** |
|
76 |
* Reset from Properties |
|
77 |
*/ |
|
78 |
public void reset() |
|
79 |
{ |
|
80 |
// Clear cache |
|
81 |
restrictedPackages.clear(); |
|
82 |
||
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
83 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
2 | 84 |
public Object run() |
85 |
{ |
|
86 |
// Enumerate system properties |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
87 |
Enumeration<?> e = System.getProperties().propertyNames(); |
2 | 88 |
|
89 |
while (e.hasMoreElements()) |
|
90 |
{ |
|
91 |
String name = (String) e.nextElement(); |
|
92 |
||
93 |
if (name != null && name.startsWith("package.restrict.access.")) |
|
94 |
{ |
|
95 |
String value = System.getProperty(name); |
|
96 |
||
97 |
if (value != null && value.equalsIgnoreCase("true")) |
|
98 |
{ |
|
99 |
String pkg = name.substring(24); |
|
100 |
||
101 |
// Cache restricted packages |
|
102 |
restrictedPackages.add(pkg); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
return null; |
|
107 |
} |
|
108 |
}); |
|
109 |
} |
|
110 |
||
48027
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
111 |
private static final StackWalker walker = |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
112 |
AccessController.doPrivileged( |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
113 |
(PrivilegedAction<StackWalker>) () -> |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
114 |
StackWalker.getInstance(RETAIN_CLASS_REFERENCE)); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
115 |
/** |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
116 |
* Returns the class loader of the most recently executing method from |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
117 |
* a class defined using a non-system class loader. A non-system |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
118 |
* class loader is defined as being a class loader that is not equal to |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
119 |
* the system class loader (as returned |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
120 |
* by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
121 |
* <p> |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
122 |
* This method will return |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
123 |
* <code>null</code> in the following three cases: |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
124 |
* <ol> |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
125 |
* <li>All methods on the execution stack are from classes |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
126 |
* defined using the system class loader or one of its ancestors. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
127 |
* |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
128 |
* <li>All methods on the execution stack up to the first |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
129 |
* "privileged" caller |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
130 |
* (see {@link java.security.AccessController#doPrivileged}) |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
131 |
* are from classes |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
132 |
* defined using the system class loader or one of its ancestors. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
133 |
* |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
134 |
* <li> A call to <code>checkPermission</code> with |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
135 |
* <code>java.security.AllPermission</code> does not |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
136 |
* result in a SecurityException. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
137 |
* </ol> |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
138 |
* |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
139 |
* NOTE: This is an implementation of the SecurityManager.currentClassLoader |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
140 |
* method that uses StackWalker. SecurityManager.currentClassLoader |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
141 |
* has been removed from SE. This is a temporary workaround which is |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
142 |
* only needed while applets are still supported. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
143 |
* |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
144 |
* @return the class loader of the most recent occurrence on the stack |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
145 |
* of a method from a class defined using a non-system class |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
146 |
* loader. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
147 |
*/ |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
148 |
private static ClassLoader currentClassLoader() { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
149 |
StackFrame f = |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
150 |
walker.walk(s -> s.takeWhile(AppletSecurity::isNonPrivileged) |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
151 |
.filter(AppletSecurity::isNonSystemFrame) |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
152 |
.findFirst()) |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
153 |
.orElse(null); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
154 |
|
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
155 |
SecurityManager sm = System.getSecurityManager(); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
156 |
if (f != null && sm != null) { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
157 |
try { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
158 |
sm.checkPermission(new AllPermission()); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
159 |
} catch (SecurityException se) { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
160 |
return f.getDeclaringClass().getClassLoader(); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
161 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
162 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
163 |
return null; |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
164 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
165 |
|
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
166 |
/** |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
167 |
* Returns true if the StackFrame is not AccessController.doPrivileged. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
168 |
*/ |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
169 |
private static boolean isNonPrivileged(StackFrame f) { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
170 |
// possibly other doPrivileged variants |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
171 |
Class<?> c = f.getDeclaringClass(); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
172 |
return c == AccessController.class && |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
173 |
f.getMethodName().equals("doPrivileged"); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
174 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
175 |
|
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
176 |
/** |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
177 |
* Returns true if the StackFrame is not from a class defined by the |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
178 |
* system class loader or one of its ancestors. |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
179 |
*/ |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
180 |
private static boolean isNonSystemFrame(StackFrame f) { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
181 |
ClassLoader loader = ClassLoader.getSystemClassLoader(); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
182 |
ClassLoader ld = f.getDeclaringClass().getClassLoader(); |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
183 |
if (ld == null || ld == loader) return false; |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
184 |
|
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
185 |
while ((loader = loader.getParent()) != null) { |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
186 |
if (ld == loader) |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
187 |
return false; |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
188 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
189 |
return true; |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
190 |
} |
ddbcfca4d51d
8186535: Remove deprecated pre-1.2 SecurityManager methods and fields
mullan
parents:
47216
diff
changeset
|
191 |
|
2 | 192 |
/** |
193 |
* get the current (first) instance of an AppletClassLoader on the stack. |
|
194 |
*/ |
|
195 |
private AppletClassLoader currentAppletClassLoader() |
|
196 |
{ |
|
197 |
// try currentClassLoader first |
|
198 |
ClassLoader loader = currentClassLoader(); |
|
199 |
||
200 |
if ((loader == null) || (loader instanceof AppletClassLoader)) |
|
201 |
return (AppletClassLoader)loader; |
|
202 |
||
203 |
// if that fails, get all the classes on the stack and check them. |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
204 |
Class<?>[] context = getClassContext(); |
2 | 205 |
for (int i = 0; i < context.length; i++) { |
206 |
loader = context[i].getClassLoader(); |
|
207 |
if (loader instanceof AppletClassLoader) |
|
208 |
return (AppletClassLoader)loader; |
|
209 |
} |
|
210 |
||
211 |
/* |
|
212 |
* fix bug # 6433620 the logic here is : try to find URLClassLoader from |
|
213 |
* class context, check its AccessControlContext to see if |
|
214 |
* AppletClassLoader is in stack when it's created. for this kind of |
|
21278 | 215 |
* URLClassLoader, return the AppContext associated with the |
2 | 216 |
* AppletClassLoader. |
217 |
*/ |
|
218 |
for (int i = 0; i < context.length; i++) { |
|
219 |
final ClassLoader currentLoader = context[i].getClassLoader(); |
|
220 |
||
221 |
if (currentLoader instanceof URLClassLoader) { |
|
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
222 |
URLClassLoader ld = (URLClassLoader)currentLoader; |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
223 |
loader = AccessController.doPrivileged( |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
224 |
new PrivilegedAction<ClassLoader>() { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
225 |
public ClassLoader run() { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
226 |
|
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
227 |
AccessControlContext acc = null; |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
228 |
ProtectionDomain[] pds = null; |
2 | 229 |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
230 |
try { |
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
231 |
acc = JNUCLA.getAccessControlContext(ld); |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
232 |
if (acc == null) { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
233 |
return null; |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
234 |
} |
2 | 235 |
|
41596
a6382b6ba29d
8165271: Fix use of reflection to gain access to private fields
ddehaven
parents:
36511
diff
changeset
|
236 |
pds = JSA.getProtectDomains(acc); |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
237 |
if (pds == null) { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
238 |
return null; |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
239 |
} |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
240 |
} catch (Exception e) { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
241 |
throw new UnsupportedOperationException(e); |
2 | 242 |
} |
243 |
||
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
244 |
for (int i=0; i<pds.length; i++) { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
245 |
ClassLoader cl = pds[i].getClassLoader(); |
2 | 246 |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
247 |
if (cl instanceof AppletClassLoader) { |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
248 |
return cl; |
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
249 |
} |
2 | 250 |
} |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
251 |
|
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
252 |
return null; |
2 | 253 |
} |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
254 |
}); |
2 | 255 |
|
256 |
if (loader != null) { |
|
257 |
return (AppletClassLoader) loader; |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
// if that fails, try the context class loader |
|
263 |
loader = Thread.currentThread().getContextClassLoader(); |
|
264 |
if (loader instanceof AppletClassLoader) |
|
265 |
return (AppletClassLoader)loader; |
|
266 |
||
267 |
// no AppletClassLoaders on the stack |
|
268 |
return (AppletClassLoader)null; |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Returns true if this threadgroup is in the applet's own thread |
|
273 |
* group. This will return false if there is no current class |
|
274 |
* loader. |
|
275 |
*/ |
|
276 |
protected boolean inThreadGroup(ThreadGroup g) { |
|
277 |
if (currentAppletClassLoader() == null) |
|
278 |
return false; |
|
279 |
else |
|
280 |
return getThreadGroup().parentOf(g); |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Returns true of the threadgroup of thread is in the applet's |
|
285 |
* own threadgroup. |
|
286 |
*/ |
|
287 |
protected boolean inThreadGroup(Thread thread) { |
|
288 |
return inThreadGroup(thread.getThreadGroup()); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* Applets are not allowed to manipulate threads outside |
|
293 |
* applet thread groups. However a terminated thread no longer belongs |
|
294 |
* to any group. |
|
295 |
*/ |
|
296 |
public void checkAccess(Thread t) { |
|
297 |
/* When multiple applets is reloaded simultaneously, there will be |
|
298 |
* multiple invocations to this method from plugin's SecurityManager. |
|
299 |
* This method should not be synchronized to avoid deadlock when |
|
300 |
* a page with multiple applets is reloaded |
|
301 |
*/ |
|
302 |
if ((t.getState() != Thread.State.TERMINATED) && !inThreadGroup(t)) { |
|
303 |
checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
private boolean inThreadGroupCheck = false; |
|
308 |
||
309 |
/** |
|
310 |
* Applets are not allowed to manipulate thread groups outside |
|
311 |
* applet thread groups. |
|
312 |
*/ |
|
313 |
public synchronized void checkAccess(ThreadGroup g) { |
|
314 |
if (inThreadGroupCheck) { |
|
315 |
// if we are in a recursive check, it is because |
|
316 |
// inThreadGroup is calling appletLoader.getThreadGroup |
|
317 |
// in that case, only do the super check, as appletLoader |
|
318 |
// has a begin/endPrivileged |
|
319 |
checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION); |
|
320 |
} else { |
|
321 |
try { |
|
322 |
inThreadGroupCheck = true; |
|
323 |
if (!inThreadGroup(g)) { |
|
324 |
checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION); |
|
325 |
} |
|
326 |
} finally { |
|
327 |
inThreadGroupCheck = false; |
|
328 |
} |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
||
333 |
/** |
|
35667 | 334 |
* Throws a {@code SecurityException} if the |
2 | 335 |
* calling thread is not allowed to access the package specified by |
336 |
* the argument. |
|
337 |
* <p> |
|
35667 | 338 |
* This method is used by the {@code loadClass} method of class |
2 | 339 |
* loaders. |
340 |
* <p> |
|
35667 | 341 |
* The {@code checkPackageAccess} method for class |
342 |
* {@code SecurityManager} calls |
|
343 |
* {@code checkPermission} with the |
|
344 |
* {@code RuntimePermission("accessClassInPackage."+ pkgname)} |
|
2 | 345 |
* permission. |
346 |
* |
|
30948
0a0972d3b58d
6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents:
28231
diff
changeset
|
347 |
* @param pkgname the package name. |
2 | 348 |
* @exception SecurityException if the caller does not have |
349 |
* permission to access the specified package. |
|
350 |
* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) |
|
351 |
*/ |
|
352 |
public void checkPackageAccess(final String pkgname) { |
|
353 |
||
354 |
// first see if the VM-wide policy allows access to this package |
|
355 |
super.checkPackageAccess(pkgname); |
|
356 |
||
357 |
// now check the list of restricted packages |
|
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
358 |
for (Iterator<String> iter = restrictedPackages.iterator(); iter.hasNext();) |
2 | 359 |
{ |
25768
b10ec9701a7e
8042872: Fix raw and unchecked warnings in sun.applet
henryjen
parents:
24865
diff
changeset
|
360 |
String pkg = iter.next(); |
2 | 361 |
|
362 |
// Prevent matching "sun" and "sunir" even if they |
|
363 |
// starts with similar beginning characters |
|
364 |
// |
|
365 |
if (pkgname.equals(pkg) || pkgname.startsWith(pkg + ".")) |
|
366 |
{ |
|
367 |
checkPermission(new java.lang.RuntimePermission |
|
368 |
("accessClassInPackage." + pkgname)); |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
/** |
|
374 |
* Returns the thread group of the applet. We consult the classloader |
|
375 |
* if there is one. |
|
376 |
*/ |
|
377 |
public ThreadGroup getThreadGroup() { |
|
378 |
/* If any applet code is on the execution stack, we return |
|
379 |
that applet's ThreadGroup. Otherwise, we use the default |
|
380 |
behavior. */ |
|
381 |
AppletClassLoader appletLoader = currentAppletClassLoader(); |
|
382 |
ThreadGroup loaderGroup = (appletLoader == null) ? null |
|
383 |
: appletLoader.getThreadGroup(); |
|
384 |
if (loaderGroup != null) { |
|
385 |
return loaderGroup; |
|
386 |
} else { |
|
387 |
return super.getThreadGroup(); |
|
388 |
} |
|
389 |
} // getThreadGroup() |
|
390 |
||
391 |
/** |
|
392 |
* Get the AppContext corresponding to the current context. |
|
393 |
* The default implementation returns null, but this method |
|
394 |
* may be overridden by various SecurityManagers |
|
395 |
* (e.g. AppletSecurity) to index AppContext objects by the |
|
396 |
* calling context. |
|
397 |
* |
|
398 |
* @return the AppContext corresponding to the current context. |
|
399 |
* @see sun.awt.AppContext |
|
400 |
* @see java.lang.SecurityManager |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
401 |
* @since 1.2.1 |
2 | 402 |
*/ |
403 |
public AppContext getAppContext() { |
|
404 |
AppletClassLoader appletLoader = currentAppletClassLoader(); |
|
405 |
||
406 |
if (appletLoader == null) { |
|
407 |
return null; |
|
408 |
} else { |
|
409 |
AppContext context = appletLoader.getAppContext(); |
|
410 |
||
411 |
// context == null when some thread in applet thread group |
|
412 |
// has not been destroyed in AppContext.dispose() |
|
413 |
if (context == null) { |
|
414 |
throw new SecurityException("Applet classloader has invalid AppContext"); |
|
415 |
} |
|
416 |
||
417 |
return context; |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
} // class AppletSecurity |