Path: blob/master/test/jdk/com/sun/management/DiagnosticCommandMBean/DcmdMBeanPermissionsTest.java
41155 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 715025626* @summary Permissions Tests for the DiagnosticCommandMBean27* @author Frederic Parain28*29* @modules java.logging30* java.management31*32* @run main/othervm -Djava.security.manager=allow DcmdMBeanPermissionsTest33*/3435import java.lang.management.ManagementFactory;36import java.lang.reflect.Constructor;37import java.lang.reflect.InvocationTargetException;38import java.lang.reflect.ReflectPermission;39import java.security.Permission;40import java.util.HashSet;41import java.util.Iterator;42import javax.management.Descriptor;43import javax.management.InstanceNotFoundException;44import javax.management.IntrospectionException;45import javax.management.MBeanException;46import javax.management.MBeanInfo;47import javax.management.MBeanOperationInfo;48import javax.management.MBeanPermission;49import javax.management.MBeanServer;50import javax.management.MalformedObjectNameException;51import javax.management.ObjectName;52import javax.management.ReflectionException;53import javax.management.RuntimeMBeanException;5455/**56*57* @author fparain58*/59public class DcmdMBeanPermissionsTest {6061private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =62"com.sun.management:type=DiagnosticCommand";6364static public class CustomSecurityManager extends SecurityManager {6566private HashSet<Permission> grantedPermissions;6768public CustomSecurityManager() {69grantedPermissions = new HashSet<Permission>();70}7172public final void grantPermission(final Permission perm) {73grantedPermissions.add(perm);74}7576public final void denyPermission(final Permission perm) {77Iterator<Permission> it = grantedPermissions.iterator();78while (it.hasNext()) {79Permission p = it.next();80if (p.equals(perm)) {81it.remove();82}83}84}8586public final void checkPermission(final Permission perm) {87for (Permission p : grantedPermissions) {88if (p.implies(perm)) {89return;90}91}92throw new SecurityException(perm.toString());93}94};9596static Permission createPermission(String classname, String name,97String action) {98Permission permission = null;99try {100Class c = Class.forName(classname);101if (action == null) {102try {103Constructor constructor = c.getConstructor(String.class);104permission = (Permission) constructor.newInstance(name);105106} catch (InstantiationException | IllegalAccessException107| IllegalArgumentException | InvocationTargetException108| NoSuchMethodException | SecurityException ex) {109ex.printStackTrace();110throw new RuntimeException("TEST FAILED");111}112}113if (permission == null) {114try {115Constructor constructor = c.getConstructor(String.class,116String.class);117permission = (Permission) constructor.newInstance(118name,119action);120} catch (InstantiationException | IllegalAccessException121| IllegalArgumentException | InvocationTargetException122| NoSuchMethodException | SecurityException ex) {123ex.printStackTrace();124throw new RuntimeException("TEST FAILED");125}126}127} catch (ClassNotFoundException ex) {128ex.printStackTrace();129throw new RuntimeException("TEST FAILED");130}131if (permission == null) {132throw new RuntimeException("TEST FAILED");133}134return permission;135}136137// return true if invokation triggered a SecurityException138static boolean invokeOperation(MBeanServer mbs, ObjectName on,139MBeanOperationInfo opInfo) {140try {141if (opInfo.getSignature().length == 0) {142mbs.invoke(on, opInfo.getName(),143new Object[0], new String[0]);144} else {145mbs.invoke(on, opInfo.getName(),146new Object[1], new String[]{ String[].class.getName()});147}148} catch (SecurityException ex) {149ex.printStackTrace();150return true;151} catch (RuntimeMBeanException ex) {152if (ex.getCause() instanceof SecurityException) {153//ex.printStackTrace();154return true;155}156} catch (MBeanException | InstanceNotFoundException157| ReflectionException ex) {158throw new RuntimeException("TEST FAILED");159}160return false;161}162163static void testOperation(MBeanServer mbs, CustomSecurityManager sm,164ObjectName on, MBeanOperationInfo opInfo) {165System.out.println("Testing " + opInfo.getName());166Descriptor desc = opInfo.getDescriptor();167if (desc.getFieldValue("dcmd.permissionClass") == null) {168// No special permission required, execution should not trigger169// any security exception170if (invokeOperation(mbs, on, opInfo)) {171throw new RuntimeException("TEST FAILED");172}173} else {174// Building the required permission175Permission reqPerm = createPermission(176(String)desc.getFieldValue("dcmd.permissionClass"),177(String)desc.getFieldValue("dcmd.permissionName"),178(String)desc.getFieldValue("dcmd.permissionAction"));179// Paranoid mode: check that the SecurityManager has not already180// been granted the permission181sm.denyPermission(reqPerm);182// A special permission is required for this operation,183// invoking it without the permission granted must trigger184// a security exception185if(!invokeOperation(mbs, on, opInfo)) {186throw new RuntimeException("TEST FAILED");187}188// grant the permission and re-try invoking the operation189sm.grantPermission(reqPerm);190if(invokeOperation(mbs, on, opInfo)) {191throw new RuntimeException("TEST FAILED");192}193// Clean up194sm.denyPermission(reqPerm);195}196}197198public static void main(final String[] args) {199final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();200ObjectName on = null;201try {202on = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);203} catch (MalformedObjectNameException ex) {204ex.printStackTrace();205throw new RuntimeException("TEST FAILED");206}207MBeanInfo info = null;208try {209info = mbs.getMBeanInfo(on);210} catch (InstanceNotFoundException | IntrospectionException211| ReflectionException ex) {212ex.printStackTrace();213throw new RuntimeException("TEST FAILED");214}215CustomSecurityManager sm = new CustomSecurityManager();216System.setSecurityManager(sm);217// Set of permission required to run the test cleanly218// Some permissions are required by the MBeanServer and other219// platform services (RuntimePermission("createClassLoader"),220// ReflectPermission("suppressAccessChecks"),221// java.util.logging.LoggingPermission("control"),222// RuntimePermission("exitVM.97")).223// Other permissions are required by commands being invoked224// in the test (for instance, RuntimePermission("modifyThreadGroup")225// and RuntimePermission("modifyThread") are checked when226// runFinalization() is invoked by the gcRunFinalization command.227sm.grantPermission(new RuntimePermission("createClassLoader"));228sm.grantPermission(new ReflectPermission("suppressAccessChecks"));229sm.grantPermission(new java.util.logging.LoggingPermission("control", ""));230sm.grantPermission(new java.lang.RuntimePermission("exitVM.*"));231sm.grantPermission(new java.lang.RuntimePermission("modifyThreadGroup"));232sm.grantPermission(new java.lang.RuntimePermission("modifyThread"));233sm.grantPermission(new java.security.SecurityPermission("getProperty.jdk.jar.disabledAlgorithms"));234for(MBeanOperationInfo opInfo : info.getOperations()) {235Permission opPermission = new MBeanPermission(info.getClassName(),236opInfo.getName(),237on,238"invoke");239sm.grantPermission(opPermission);240testOperation(mbs, sm, on, opInfo);241sm.denyPermission(opPermission);242}243System.out.println("TEST PASSED");244}245}246247248