| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript; |
| 6 |
|
| 7 |
import java.lang.ref.SoftReference; |
| 8 |
import java.lang.reflect.UndeclaredThrowableException; |
| 9 |
import java.security.AccessController; |
| 10 |
import java.security.CodeSource; |
| 11 |
import java.security.Policy; |
| 12 |
import java.security.PrivilegedAction; |
| 13 |
import java.security.PrivilegedActionException; |
| 14 |
import java.security.PrivilegedExceptionAction; |
| 15 |
import java.security.SecureClassLoader; |
| 16 |
import java.util.Map; |
| 17 |
import java.util.WeakHashMap; |
| 18 |
|
| 19 |
import org.mozilla.classfile.ByteCode; |
| 20 |
import org.mozilla.classfile.ClassFileWriter; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class PolicySecurityController extends SecurityController |
| 34 |
{ |
| 35 |
private static final byte[] secureCallerImplBytecode = loadBytecode(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private static final Map<CodeSource,Map<ClassLoader,SoftReference<SecureCaller>>> |
| 42 |
callers = |
| 43 |
new WeakHashMap<CodeSource,Map<ClassLoader,SoftReference<SecureCaller>>>(); |
| 44 |
|
| 45 |
@Override |
| 46 |
public Class<?> getStaticSecurityDomainClassInternal() { |
| 47 |
return CodeSource.class; |
| 48 |
} |
| 49 |
|
| 50 |
private static class Loader extends SecureClassLoader |
| 51 |
implements GeneratedClassLoader |
| 52 |
{ |
| 53 |
private final CodeSource codeSource; |
| 54 |
|
| 55 |
Loader(ClassLoader parent, CodeSource codeSource) |
| 56 |
{ |
| 57 |
super(parent); |
| 58 |
this.codeSource = codeSource; |
| 59 |
} |
| 60 |
|
| 61 |
public Class<?> defineClass(String name, byte[] data) |
| 62 |
{ |
| 63 |
return defineClass(name, data, 0, data.length, codeSource); |
| 64 |
} |
| 65 |
|
| 66 |
public void linkClass(Class<?> cl) |
| 67 |
{ |
| 68 |
resolveClass(cl); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public GeneratedClassLoader createClassLoader(final ClassLoader parent, |
| 74 |
final Object securityDomain) |
| 75 |
{ |
| 76 |
return (Loader)AccessController.doPrivileged( |
| 77 |
new PrivilegedAction<Object>() |
| 78 |
{ |
| 79 |
public Object run() |
| 80 |
{ |
| 81 |
return new Loader(parent, (CodeSource)securityDomain); |
| 82 |
} |
| 83 |
}); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public Object getDynamicSecurityDomain(Object securityDomain) |
| 88 |
{ |
| 89 |
|
| 90 |
|
| 91 |
return securityDomain; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public Object callWithDomain(final Object securityDomain, final Context cx, |
| 96 |
Callable callable, Scriptable scope, Scriptable thisObj, |
| 97 |
Object[] args) |
| 98 |
{ |
| 99 |
|
| 100 |
|
| 101 |
final ClassLoader classLoader = (ClassLoader)AccessController.doPrivileged( |
| 102 |
new PrivilegedAction<Object>() { |
| 103 |
public Object run() { |
| 104 |
return cx.getApplicationClassLoader(); |
| 105 |
} |
| 106 |
}); |
| 107 |
final CodeSource codeSource = (CodeSource)securityDomain; |
| 108 |
Map<ClassLoader,SoftReference<SecureCaller>> classLoaderMap; |
| 109 |
synchronized (callers) { |
| 110 |
classLoaderMap = callers.get(codeSource); |
| 111 |
if(classLoaderMap == null) { |
| 112 |
classLoaderMap = new WeakHashMap<ClassLoader,SoftReference<SecureCaller>>(); |
| 113 |
callers.put(codeSource, classLoaderMap); |
| 114 |
} |
| 115 |
} |
| 116 |
SecureCaller caller; |
| 117 |
synchronized (classLoaderMap) { |
| 118 |
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader); |
| 119 |
if (ref != null) { |
| 120 |
caller = ref.get(); |
| 121 |
} else { |
| 122 |
caller = null; |
| 123 |
} |
| 124 |
if (caller == null) |
| 125 |
{ |
| 126 |
try |
| 127 |
{ |
| 128 |
|
| 129 |
|
| 130 |
caller = (SecureCaller)AccessController.doPrivileged( |
| 131 |
new PrivilegedExceptionAction<Object>() |
| 132 |
{ |
| 133 |
public Object run() throws Exception |
| 134 |
{ |
| 135 |
Loader loader = new Loader(classLoader, |
| 136 |
codeSource); |
| 137 |
Class<?> c = loader.defineClass( |
| 138 |
SecureCaller.class.getName() + "Impl", |
| 139 |
secureCallerImplBytecode); |
| 140 |
return c.newInstance(); |
| 141 |
} |
| 142 |
}); |
| 143 |
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller)); |
| 144 |
} |
| 145 |
catch(PrivilegedActionException ex) |
| 146 |
{ |
| 147 |
throw new UndeclaredThrowableException(ex.getCause()); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
return caller.call(callable, cx, scope, thisObj, args); |
| 152 |
} |
| 153 |
|
| 154 |
public abstract static class SecureCaller |
| 155 |
{ |
| 156 |
public abstract Object call(Callable callable, Context cx, Scriptable scope, |
| 157 |
Scriptable thisObj, Object[] args); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
private static byte[] loadBytecode() |
| 162 |
{ |
| 163 |
String secureCallerClassName = SecureCaller.class.getName(); |
| 164 |
ClassFileWriter cfw = new ClassFileWriter( |
| 165 |
secureCallerClassName + "Impl", secureCallerClassName, |
| 166 |
"<generated>"); |
| 167 |
cfw.startMethod("<init>", "()V", ClassFileWriter.ACC_PUBLIC); |
| 168 |
cfw.addALoad(0); |
| 169 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, secureCallerClassName, |
| 170 |
"<init>", "()V"); |
| 171 |
cfw.add(ByteCode.RETURN); |
| 172 |
cfw.stopMethod((short)1); |
| 173 |
String callableCallSig = |
| 174 |
"Lorg/mozilla/javascript/Context;" + |
| 175 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 176 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 177 |
"[Ljava/lang/Object;)Ljava/lang/Object;"; |
| 178 |
|
| 179 |
cfw.startMethod("call", |
| 180 |
"(Lorg/mozilla/javascript/Callable;" + callableCallSig, |
| 181 |
(short)(ClassFileWriter.ACC_PUBLIC |
| 182 |
| ClassFileWriter.ACC_FINAL)); |
| 183 |
for(int i = 1; i < 6; ++i) { |
| 184 |
cfw.addALoad(i); |
| 185 |
} |
| 186 |
cfw.addInvoke(ByteCode.INVOKEINTERFACE, |
| 187 |
"org/mozilla/javascript/Callable", "call", |
| 188 |
"(" + callableCallSig); |
| 189 |
cfw.add(ByteCode.ARETURN); |
| 190 |
cfw.stopMethod((short)6); |
| 191 |
return cfw.toByteArray(); |
| 192 |
} |
| 193 |
} |