8008623: Better handling of MBeanServers

Reviewed-by: dfuchs, dholmes, skoivu
This commit is contained in:
Jaroslav Bachorik 2013-03-21 09:26:55 +01:00
parent d76b61b2e8
commit bf2d40f6db
3 changed files with 41 additions and 10 deletions

View File

@ -1973,8 +1973,7 @@ public class DefaultMBeanServerInterceptor implements MBeanServerInterceptor {
* does not add it to the list that is consulted by * does not add it to the list that is consulted by
* ClassLoaderRepository.loadClass. * ClassLoaderRepository.loadClass.
*/ */
final ModifiableClassLoaderRepository clr = final ModifiableClassLoaderRepository clr = getInstantiatorCLR();
instantiator.getClassLoaderRepository();
if (clr == null) { if (clr == null) {
final RuntimeException wrapped = final RuntimeException wrapped =
new IllegalArgumentException( new IllegalArgumentException(
@ -2000,8 +1999,7 @@ public class DefaultMBeanServerInterceptor implements MBeanServerInterceptor {
* Removes the MBean from the default loader repository. * Removes the MBean from the default loader repository.
*/ */
if (loader != server.getClass().getClassLoader()) { if (loader != server.getClass().getClassLoader()) {
final ModifiableClassLoaderRepository clr = final ModifiableClassLoaderRepository clr = getInstantiatorCLR();
instantiator.getClassLoaderRepository();
if (clr != null) { if (clr != null) {
clr.removeClassLoader(logicalName); clr.removeClassLoader(logicalName);
} }
@ -2060,5 +2058,12 @@ public class DefaultMBeanServerInterceptor implements MBeanServerInterceptor {
return ResourceContext.NONE; return ResourceContext.NONE;
} }
private ModifiableClassLoaderRepository getInstantiatorCLR() {
return AccessController.doPrivileged(new PrivilegedAction<ModifiableClassLoaderRepository>() {
@Override
public ModifiableClassLoaderRepository run() {
return instantiator != null ? instantiator.getClassLoaderRepository() : null;
}
});
}
} }

View File

@ -32,6 +32,7 @@ import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.security.AccessController; import java.security.AccessController;
import java.security.Permission; import java.security.Permission;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -227,8 +228,16 @@ public final class JmxMBeanServer
clr = new ClassLoaderRepositorySupport(); clr = new ClassLoaderRepositorySupport();
instantiator = new MBeanInstantiator(clr); instantiator = new MBeanInstantiator(clr);
} }
final MBeanInstantiator fInstantiator = instantiator;
this.secureClr = new this.secureClr = new
SecureClassLoaderRepository(instantiator.getClassLoaderRepository()); SecureClassLoaderRepository(AccessController.doPrivileged(new PrivilegedAction<ClassLoaderRepository>() {
@Override
public ClassLoaderRepository run() {
return fInstantiator.getClassLoaderRepository();
}
})
);
if (delegate == null) if (delegate == null)
delegate = new MBeanServerDelegateImpl(); delegate = new MBeanServerDelegateImpl();
if (outer == null) if (outer == null)
@ -1242,8 +1251,14 @@ public final class JmxMBeanServer
class loader. The ClassLoaderRepository knows how class loader. The ClassLoaderRepository knows how
to handle that case. */ to handle that case. */
ClassLoader myLoader = outerShell.getClass().getClassLoader(); ClassLoader myLoader = outerShell.getClass().getClassLoader();
final ModifiableClassLoaderRepository loaders = final ModifiableClassLoaderRepository loaders = AccessController.doPrivileged(new PrivilegedAction<ModifiableClassLoaderRepository>() {
instantiator.getClassLoaderRepository();
@Override
public ModifiableClassLoaderRepository run() {
return instantiator.getClassLoaderRepository();
}
});
if (loaders != null) { if (loaders != null) {
loaders.addClassLoader(myLoader); loaders.addClassLoader(myLoader);

View File

@ -622,6 +622,7 @@ public class MBeanInstantiator {
* Return the Default Loader Repository used by this instantiator object. * Return the Default Loader Repository used by this instantiator object.
**/ **/
public ModifiableClassLoaderRepository getClassLoaderRepository() { public ModifiableClassLoaderRepository getClassLoaderRepository() {
checkMBeanPermission((String)null, null, null, "getClassLoaderRepository");
return clr; return clr;
} }
@ -733,9 +734,19 @@ public class MBeanInstantiator {
String member, String member,
ObjectName objectName, ObjectName objectName,
String actions) { String actions) {
if (clazz != null) {
checkMBeanPermission(clazz.getName(), member, objectName, actions);
}
}
private static void checkMBeanPermission(String classname,
String member,
ObjectName objectName,
String actions)
throws SecurityException {
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
if (clazz != null && sm != null) { if (sm != null) {
Permission perm = new MBeanPermission(clazz.getName(), Permission perm = new MBeanPermission(classname,
member, member,
objectName, objectName,
actions); actions);