Search in sources :

Example 31 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class DefaultMBeanServerInterceptor method checkMBeanTrustPermission.

private static void checkMBeanTrustPermission(final Class<?> theClass) throws SecurityException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        Permission perm = new MBeanTrustPermission("register");
        PrivilegedAction<ProtectionDomain> act = new PrivilegedAction<ProtectionDomain>() {

            public ProtectionDomain run() {
                return theClass.getProtectionDomain();
            }
        };
        ProtectionDomain pd = AccessController.doPrivileged(act);
        AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { pd });
        sm.checkPermission(perm, acc);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction) MBeanTrustPermission(javax.management.MBeanTrustPermission) MBeanPermission(javax.management.MBeanPermission) MBeanTrustPermission(javax.management.MBeanTrustPermission) Permission(java.security.Permission)

Example 32 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class MBeanInstantiator method getClassLoader.

private ClassLoader getClassLoader(final ObjectName name) {
    if (clr == null) {
        return null;
    }
    // Restrict to getClassLoader permission only
    Permissions permissions = new Permissions();
    permissions.add(new MBeanPermission("*", null, name, "getClassLoader"));
    ProtectionDomain protectionDomain = new ProtectionDomain(null, permissions);
    ProtectionDomain[] domains = { protectionDomain };
    AccessControlContext ctx = new AccessControlContext(domains);
    ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

        public ClassLoader run() {
            return clr.getClassLoader(name);
        }
    }, ctx);
    return loader;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) MBeanPermission(javax.management.MBeanPermission) Permissions(java.security.Permissions)

Example 33 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class Queue method dispatchEvent.

/**
     * Dispatches an event. The manner in which the event is
     * dispatched depends upon the type of the event and the
     * type of the event's source object:
     *
     * <table border=1 summary="Event types, source types, and dispatch methods">
     * <tr>
     *     <th>Event Type</th>
     *     <th>Source Type</th>
     *     <th>Dispatched To</th>
     * </tr>
     * <tr>
     *     <td>ActiveEvent</td>
     *     <td>Any</td>
     *     <td>event.dispatch()</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>Component</td>
     *     <td>source.dispatchEvent(AWTEvent)</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>MenuComponent</td>
     *     <td>source.dispatchEvent(AWTEvent)</td>
     * </tr>
     * <tr>
     *     <td>Other</td>
     *     <td>Other</td>
     *     <td>No action (ignored)</td>
     * </tr>
     * </table>
     * <p>
     * @param event an instance of <code>java.awt.AWTEvent</code>,
     *          or a subclass of it
     * @throws NullPointerException if <code>event</code> is <code>null</code>
     * @since           1.2
     */
protected void dispatchEvent(final AWTEvent event) {
    final Object src = event.getSource();
    final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {

        public Void run() {
            // dispatch the event straight away.
            if (fwDispatcher == null || isDispatchThreadImpl()) {
                dispatchEventImpl(event, src);
            } else {
                fwDispatcher.scheduleDispatch(new Runnable() {

                    @Override
                    public void run() {
                        dispatchEventImpl(event, src);
                    }
                });
            }
            return null;
        }
    };
    final AccessControlContext stack = AccessController.getContext();
    final AccessControlContext srcAcc = getAccessControlContextFrom(src);
    final AccessControlContext eventAcc = event.getAccessControlContext();
    if (srcAcc == null) {
        javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
    } else {
        javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Void>() {

            public Void run() {
                javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
                return null;
            }
        }, stack, srcAcc);
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) PrivilegedAction(java.security.PrivilegedAction)

Example 34 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class RequiredModelMBean method resolveMethod.

private Method resolveMethod(Class<?> targetClass, String opMethodName, final String[] sig) throws ReflectionException {
    final boolean tracing = MODELMBEAN_LOGGER.isLoggable(Level.FINER);
    if (tracing) {
        MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), "resolveMethod", "resolving " + targetClass.getName() + "." + opMethodName);
    }
    final Class<?>[] argClasses;
    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];
        javaSecurityAccess.doIntersectionPrivilege(new PrivilegedAction<Void>() {

            @Override
            public Void run() {
                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 {
                            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];
        }
    }
    try {
        return targetClass.getMethod(opMethodName, argClasses);
    } catch (NoSuchMethodException e) {
        final String msg = "Target method not found: " + targetClass.getName() + "." + opMethodName;
        throw new ReflectionException(e, msg);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AccessControlContext(java.security.AccessControlContext)

Example 35 with AccessControlContext

use of java.security.AccessControlContext in project jdk8u_jdk by JetBrains.

the class RMIConnectionImpl method withPermissions.

private static AccessControlContext withPermissions(Permission... perms) {
    Permissions col = new Permissions();
    for (Permission thePerm : perms) {
        col.add(thePerm);
    }
    final ProtectionDomain pd = new ProtectionDomain(null, col);
    return new AccessControlContext(new ProtectionDomain[] { pd });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AccessControlContext(java.security.AccessControlContext) Permissions(java.security.Permissions) Permission(java.security.Permission)

Aggregations

AccessControlContext (java.security.AccessControlContext)96 Subject (javax.security.auth.Subject)23 ProtectionDomain (java.security.ProtectionDomain)20 PrivilegedAction (java.security.PrivilegedAction)18 Permissions (java.security.Permissions)13 PrivilegedActionException (java.security.PrivilegedActionException)12 IOException (java.io.IOException)10 SocketPermission (java.net.SocketPermission)10 Test (org.testng.annotations.Test)8 Principal (java.security.Principal)7 DatagramSocket (java.net.DatagramSocket)5 InetAddress (java.net.InetAddress)4 MulticastSocket (java.net.MulticastSocket)4 CodeSource (java.security.CodeSource)4 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)4 ExecutorService (java.util.concurrent.ExecutorService)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 ReflectionException (javax.management.ReflectionException)4 Test (org.junit.Test)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3