Search in sources :

Example 1 with ProtectionDomain

use of java.security.ProtectionDomain in project cglib by cglib.

the class AbstractClassGenerator method generate.

protected Class generate(ClassLoaderData data) {
    Class gen;
    Object save = CURRENT.get();
    CURRENT.set(this);
    try {
        ClassLoader classLoader = data.getClassLoader();
        if (classLoader == null) {
            throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker.");
        }
        synchronized (classLoader) {
            String name = generateClassName(data.getUniqueNamePredicate());
            data.reserveName(name);
            this.setClassName(name);
        }
        if (attemptLoad) {
            try {
                gen = classLoader.loadClass(getClassName());
                return gen;
            } catch (ClassNotFoundException e) {
            // ignore
            }
        }
        byte[] b = strategy.generate(this);
        String className = ClassNameReader.getClassName(new ClassReader(b));
        ProtectionDomain protectionDomain = getProtectionDomain();
        synchronized (classLoader) {
            // just in case
            if (protectionDomain == null) {
                gen = ReflectUtils.defineClass(className, b, classLoader);
            } else {
                gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain);
            }
        }
        return gen;
    } catch (RuntimeException e) {
        throw e;
    } catch (Error e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    } finally {
        CURRENT.set(save);
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassReader(org.objectweb.asm.ClassReader)

Example 2 with ProtectionDomain

use of java.security.ProtectionDomain in project elasticsearch by elastic.

the class ESPolicyUnitTests method testNullCodeSource.

/**
     * Test policy with null codesource.
     * <p>
     * This can happen when restricting privileges with doPrivileged,
     * even though ProtectionDomain's ctor javadocs might make you think
     * that the policy won't be consulted.
     */
public void testNullCodeSource() throws Exception {
    assumeTrue("test cannot run with security manager", System.getSecurityManager() == null);
    // create a policy with AllPermission
    Permission all = new AllPermission();
    PermissionCollection allCollection = all.newPermissionCollection();
    allCollection.add(all);
    ESPolicy policy = new ESPolicy(allCollection, Collections.emptyMap(), true);
    // restrict ourselves to NoPermission
    PermissionCollection noPermissions = new Permissions();
    assertFalse(policy.implies(new ProtectionDomain(null, noPermissions), new FilePermission("foo", "read")));
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) Permission(java.security.Permission) FilePermission(java.io.FilePermission) SocketPermission(java.net.SocketPermission) AllPermission(java.security.AllPermission) Permissions(java.security.Permissions) AllPermission(java.security.AllPermission) FilePermission(java.io.FilePermission)

Example 3 with ProtectionDomain

use of java.security.ProtectionDomain in project elasticsearch by elastic.

the class ESPolicyUnitTests method testNullLocation.

/**
     * test with null location
     * <p>
     * its unclear when/if this happens, see https://bugs.openjdk.java.net/browse/JDK-8129972
     */
public void testNullLocation() throws Exception {
    assumeTrue("test cannot run with security manager", System.getSecurityManager() == null);
    PermissionCollection noPermissions = new Permissions();
    ESPolicy policy = new ESPolicy(noPermissions, Collections.emptyMap(), true);
    assertFalse(policy.implies(new ProtectionDomain(new CodeSource(null, (Certificate[]) null), noPermissions), new FilePermission("foo", "read")));
}
Also used : PermissionCollection(java.security.PermissionCollection) ProtectionDomain(java.security.ProtectionDomain) Permissions(java.security.Permissions) CodeSource(java.security.CodeSource) FilePermission(java.io.FilePermission)

Example 4 with ProtectionDomain

use of java.security.ProtectionDomain in project jetty.project by eclipse.

the class TypeUtil method getLoadedFrom.

/* ------------------------------------------------------------ */
public static Resource getLoadedFrom(Class<?> clazz) {
    ProtectionDomain domain = clazz.getProtectionDomain();
    if (domain != null) {
        CodeSource source = domain.getCodeSource();
        if (source != null) {
            URL location = source.getLocation();
            if (location != null)
                return Resource.newResource(location);
        }
    }
    String rname = clazz.getName().replace('.', '/') + ".class";
    ClassLoader loader = clazz.getClassLoader();
    URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(rname);
    if (url != null) {
        try {
            return Resource.newResource(URIUtil.getJarSource(url.toString()));
        } catch (Exception e) {
            LOG.debug(e);
        }
    }
    return null;
}
Also used : ProtectionDomain(java.security.ProtectionDomain) CodeSource(java.security.CodeSource) URL(java.net.URL) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with ProtectionDomain

use of java.security.ProtectionDomain in project jetty.project by eclipse.

the class WebAppClassLoaderTest method testClassFileTranslations.

@Test
public void testClassFileTranslations() throws Exception {
    final List<Object> results = new ArrayList<Object>();
    _loader.addTransformer(new ClassFileTransformer() {

        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            results.add(loader);
            byte[] b = new byte[classfileBuffer.length];
            for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
            return b;
        }
    });
    _loader.addTransformer(new ClassFileTransformer() {

        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            results.add(className);
            byte[] b = new byte[classfileBuffer.length];
            for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
            return b;
        }
    });
    _context.setParentLoaderPriority(false);
    assertCanLoadClass("org.acme.webapp.ClassInJarA");
    assertCanLoadClass("org.acme.webapp.ClassInJarB");
    assertCanLoadClass("org.acme.other.ClassInClassesC");
    assertCanLoadClass("java.lang.String");
    assertCantLoadClass("org.eclipse.jetty.webapp.Configuration");
    assertThat("Classname Results", results, contains(_loader, "org.acme.webapp.ClassInJarA", _loader, "org.acme.webapp.ClassInJarB", _loader, "org.acme.other.ClassInClassesC"));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Aggregations

ProtectionDomain (java.security.ProtectionDomain)122 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)44 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)44 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)42 CodeSource (java.security.CodeSource)29 InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)28 Permissions (java.security.Permissions)21 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)20 AccessControlContext (java.security.AccessControlContext)20 URL (java.net.URL)15 Policy (java.security.Policy)15 Test (org.junit.Test)15 Permission (java.security.Permission)14 PermissionCollection (java.security.PermissionCollection)11 File (java.io.File)10 IOException (java.io.IOException)6 Method (java.lang.reflect.Method)6 SocketPermission (java.net.SocketPermission)6 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 FilePermission (java.io.FilePermission)5