1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# HG changeset patch
# User ewendeli
# Date 1353844618 -3600
# Node ID 21415f01c66add2891500f10e41c7e99b2b10447
# Parent 787e9230b414f346f9c318918aaf58b872b9912e
8000537: Contextualize RequiredModelMBean class
Reviewed-by: jbachorik
Contributed-by: Andreas Eriksson <andreas.eriksson@oracle.com>
diff --git a/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java b/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java
--- jdk/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java
+++ jdk/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,9 @@ import java.lang.reflect.InvocationTarge
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.Date;
import java.util.HashMap;
@@ -77,6 +80,8 @@ import javax.management.RuntimeOperation
import javax.management.RuntimeOperationsException;
import javax.management.ServiceNotFoundException;
import javax.management.loading.ClassLoaderRepository;
+import sun.misc.JavaSecurityAccess;
+import sun.misc.SharedSecrets;
import sun.reflect.misc.MethodUtil;
import sun.reflect.misc.ReflectUtil;
@@ -138,6 +143,9 @@ public class RequiredModelMBean
private boolean registered = false;
private transient MBeanServer server = null;
+ private final static JavaSecurityAccess javaSecurityAccess = SharedSecrets.getJavaSecurityAccess();
+ final private AccessControlContext acc = AccessController.getContext();
+
/*************************************/
/* constructors */
/*************************************/
@@ -1025,10 +1033,30 @@ public class RequiredModelMBean
if (opClassName != null) {
try {
- final ClassLoader targetClassLoader =
- targetObject.getClass().getClassLoader();
- targetClass = Class.forName(opClassName, false,
- targetClassLoader);
+ AccessControlContext stack = AccessController.getContext();
+ final Object obj = targetObject;
+ final String className = opClassName;
+ final ClassNotFoundException[] caughtException = new ClassNotFoundException[1];
+
+ targetClass = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Class<?>>() {
+
+ public Class<?> run() {
+ try {
+ ReflectUtil.checkPackageAccess(className);
+ final ClassLoader targetClassLoader =
+ obj.getClass().getClassLoader();
+ return Class.forName(className, false,
+ targetClassLoader);
+ } catch (ClassNotFoundException e) {
+ caughtException[0] = e;
+ }
+ return null;
+ }
+ }, stack, acc);
+
+ if (caughtException[0] != null) {
+ throw caughtException[0];
+ }
} catch (ClassNotFoundException e) {
final String msg =
"class for invoke " + opName + " not found";
@@ -1061,9 +1090,9 @@ public class RequiredModelMBean
return result;
}
- private static Method resolveMethod(Class<?> targetClass,
+ private Method resolveMethod(Class<?> targetClass,
String opMethodName,
- String[] sig)
+ final String[] sig)
throws ReflectionException {
final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
@@ -1078,30 +1107,44 @@ public class RequiredModelMBean
if (sig == null)
argClasses = null;
else {
+ final AccessControlContext stack = AccessController.getContext();
+ final ReflectionException[] caughtException = new ReflectionException[1];
final ClassLoader targetClassLoader = targetClass.getClassLoader();
- argClasses = new Class[sig.length];
- for (int i = 0; i < sig.length; i++) {
- if (tracing) {
- MODELMBEAN_LOGGER.logp(Level.FINER,
- RequiredModelMBean.class.getName(),"resolveMethod",
- "resolve type " + sig[i]);
- }
- argClasses[i] = (Class) primitiveClassMap.get(sig[i]);
- if (argClasses[i] == null) {
- try {
- argClasses[i] =
- Class.forName(sig[i], false, targetClassLoader);
- } catch (ClassNotFoundException e) {
+ argClasses = new Class<?>[sig.length];
+
+ javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Void>() {
+
+ public Void run() {
+ for (int i = 0; i < sig.length; i++) {
if (tracing) {
MODELMBEAN_LOGGER.logp(Level.FINER,
- RequiredModelMBean.class.getName(),
- "resolveMethod",
- "class not found");
+ RequiredModelMBean.class.getName(),"resolveMethod",
+ "resolve type " + sig[i]);
}
- final String msg = "Parameter class not found";
- throw new ReflectionException(e, msg);
+ argClasses[i] = (Class<?>) primitiveClassMap.get(sig[i]);
+ if (argClasses[i] == null) {
+ try {
+ ReflectUtil.checkPackageAccess(sig[i]);
+ argClasses[i] =
+ Class.forName(sig[i], false, targetClassLoader);
+ } catch (ClassNotFoundException e) {
+ if (tracing) {
+ MODELMBEAN_LOGGER.logp(Level.FINER,
+ RequiredModelMBean.class.getName(),
+ "resolveMethod",
+ "class not found");
+ }
+ final String msg = "Parameter class not found";
+ caughtException[0] = new ReflectionException(e, msg);
+ }
+ }
}
+ return null;
}
+ }, stack, acc);
+
+ if (caughtException[0] != null) {
+ throw caughtException[0];
}
}
@@ -1133,7 +1177,7 @@ public class RequiredModelMBean
/* Find a method in RequiredModelMBean as determined by the given
parameters. Return null if there is none, or if the parameters
exclude using it. Called from invoke. */
- private static Method findRMMBMethod(String opMethodName,
+ private Method findRMMBMethod(String opMethodName,
Object targetObjectField,
String opClassName,
String[] sig) {
@@ -1155,19 +1199,28 @@ public class RequiredModelMBean
if (opClassName == null)
targetClass = rmmbClass;
else {
- try {
- final ClassLoader targetClassLoader =
- rmmbClass.getClassLoader();
- targetClass = Class.forName(opClassName, false,
- targetClassLoader);
- if (!rmmbClass.isAssignableFrom(targetClass))
- return null;
- } catch (ClassNotFoundException e) {
- return null;
- }
+ AccessControlContext stack = AccessController.getContext();
+ final String className = opClassName;
+ targetClass = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Class<?>>() {
+
+ public Class<?> run() {
+ try {
+ ReflectUtil.checkPackageAccess(className);
+ final ClassLoader targetClassLoader =
+ rmmbClass.getClassLoader();
+ Class clz = Class.forName(className, false,
+ targetClassLoader);
+ if (!rmmbClass.isAssignableFrom(clz))
+ return null;
+ return clz;
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+ }, stack, acc);
}
try {
- return resolveMethod(targetClass, opMethodName, sig);
+ return targetClass != null ? resolveMethod(targetClass, opMethodName, sig) : null;
} catch (ReflectionException e) {
return null;
}
@@ -1177,12 +1231,34 @@ public class RequiredModelMBean
* Invoke the given method, and throw the somewhat unpredictable
* appropriate exception if the method itself gets an exception.
*/
- private Object invokeMethod(String opName, Method method,
- Object targetObject, Object[] opArgs)
+ private Object invokeMethod(String opName, final Method method,
+ final Object targetObject, final Object[] opArgs)
throws MBeanException, ReflectionException {
try {
- ReflectUtil.checkPackageAccess(method.getDeclaringClass());
- return MethodUtil.invoke(method, targetObject, opArgs);
+ final Throwable[] caughtException = new Throwable[1];
+ AccessControlContext stack = AccessController.getContext();
+ Object rslt = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Object>() {
+
+ public Object run() {
+ try {
+ ReflectUtil.checkPackageAccess(method.getDeclaringClass());
+ return MethodUtil.invoke(method, targetObject, opArgs);
+ } catch (InvocationTargetException e) {
+ caughtException[0] = e;
+ } catch (IllegalAccessException e) {
+ caughtException[0] = e;
+ }
+ return null;
+ }
+ }, stack, acc);
+ if (caughtException[0] != null) {
+ if (caughtException[0] instanceof Exception) {
+ throw (Exception)caughtException[0];
+ } else if(caughtException[0] instanceof Error) {
+ throw (Error)caughtException[0];
+ }
+ }
+ return rslt;
} catch (RuntimeErrorException ree) {
throw new RuntimeOperationsException(ree,
"RuntimeException occurred in RequiredModelMBean "+
@@ -1569,7 +1646,7 @@ public class RequiredModelMBean
}
// make sure response class matches type field
- String respType = attrInfo.getType();
+ final String respType = attrInfo.getType();
if (response != null) {
String responseClass = response.getClass().getName();
if (!respType.equals(responseClass)) {
@@ -1592,9 +1669,30 @@ public class RequiredModelMBean
// inequality may come from type subclassing
boolean subtype;
try {
- ClassLoader cl =
- response.getClass().getClassLoader();
- Class c = Class.forName(respType, true, cl);
+ final Class respClass = response.getClass();
+ final Exception[] caughException = new Exception[1];
+
+ AccessControlContext stack = AccessController.getContext();
+
+ Class c = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Class<?>>() {
+
+ public Class<?> run() {
+ try {
+ ReflectUtil.checkPackageAccess(respType);
+ ClassLoader cl =
+ respClass.getClassLoader();
+ return Class.forName(respType, true, cl);
+ } catch (Exception e) {
+ caughException[0] = e;
+ }
+ return null;
+ }
+ }, stack, acc);
+
+ if (caughException[0] != null) {
+ throw caughException[0];
+ }
+
subtype = c.isInstance(response);
} catch (Exception e) {
subtype = false;
@@ -2748,16 +2847,36 @@ public class RequiredModelMBean
return MBeanServerFactory.getClassLoaderRepository(server);
}
- private Class loadClass(String className)
+ private Class<?> loadClass(final String className)
throws ClassNotFoundException {
- try {
- return Class.forName(className);
- } catch (ClassNotFoundException e) {
- final ClassLoaderRepository clr =
- getClassLoaderRepository();
- if (clr == null) throw new ClassNotFoundException(className);
- return clr.loadClass(className);
+ AccessControlContext stack = AccessController.getContext();
+ final ClassNotFoundException[] caughtException = new ClassNotFoundException[1];
+
+ Class c = javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Class<?>>() {
+
+ public Class<?> run() {
+ try {
+ ReflectUtil.checkPackageAccess(className);
+ return Class.forName(className);
+ } catch (ClassNotFoundException e) {
+ final ClassLoaderRepository clr =
+ getClassLoaderRepository();
+ try {
+ if (clr == null) throw new ClassNotFoundException(className);
+ return clr.loadClass(className);
+ } catch (ClassNotFoundException ex) {
+ caughtException[0] = ex;
+ }
+ }
+ return null;
+ }
+ }, stack, acc);
+
+ if (caughtException[0] != null) {
+ throw caughtException[0];
}
+
+ return c;
}
|