| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript; |
| 6 |
|
| 7 |
import java.io.ByteArrayOutputStream; |
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.lang.ref.SoftReference; |
| 11 |
import java.lang.reflect.UndeclaredThrowableException; |
| 12 |
import java.net.URL; |
| 13 |
import java.security.AccessController; |
| 14 |
import java.security.CodeSource; |
| 15 |
import java.security.PrivilegedAction; |
| 16 |
import java.security.PrivilegedActionException; |
| 17 |
import java.security.PrivilegedExceptionAction; |
| 18 |
import java.security.SecureClassLoader; |
| 19 |
import java.util.Map; |
| 20 |
import java.util.WeakHashMap; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public abstract class SecureCaller |
| 26 |
{ |
| 27 |
private static final byte[] secureCallerImplBytecode = loadBytecode(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private static final Map<CodeSource,Map<ClassLoader,SoftReference<SecureCaller>>> |
| 34 |
callers = |
| 35 |
new WeakHashMap<CodeSource,Map<ClassLoader,SoftReference<SecureCaller>>>(); |
| 36 |
|
| 37 |
public abstract Object call(Callable callable, Context cx, |
| 38 |
Scriptable scope, Scriptable thisObj, Object[] args); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static Object callSecurely(final CodeSource codeSource, Callable callable, |
| 45 |
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) |
| 46 |
{ |
| 47 |
final Thread thread = Thread.currentThread(); |
| 48 |
|
| 49 |
|
| 50 |
final ClassLoader classLoader = (ClassLoader)AccessController.doPrivileged( |
| 51 |
new PrivilegedAction<Object>() { |
| 52 |
public Object run() { |
| 53 |
return thread.getContextClassLoader(); |
| 54 |
} |
| 55 |
}); |
| 56 |
Map<ClassLoader,SoftReference<SecureCaller>> classLoaderMap; |
| 57 |
synchronized(callers) |
| 58 |
{ |
| 59 |
classLoaderMap = callers.get(codeSource); |
| 60 |
if(classLoaderMap == null) |
| 61 |
{ |
| 62 |
classLoaderMap = new WeakHashMap<ClassLoader,SoftReference<SecureCaller>>(); |
| 63 |
callers.put(codeSource, classLoaderMap); |
| 64 |
} |
| 65 |
} |
| 66 |
SecureCaller caller; |
| 67 |
synchronized(classLoaderMap) |
| 68 |
{ |
| 69 |
SoftReference<SecureCaller> ref = classLoaderMap.get(classLoader); |
| 70 |
if (ref != null) { |
| 71 |
caller = ref.get(); |
| 72 |
} else { |
| 73 |
caller = null; |
| 74 |
} |
| 75 |
if (caller == null) { |
| 76 |
try |
| 77 |
{ |
| 78 |
|
| 79 |
|
| 80 |
caller = (SecureCaller)AccessController.doPrivileged( |
| 81 |
new PrivilegedExceptionAction<Object>() |
| 82 |
{ |
| 83 |
public Object run() throws Exception |
| 84 |
{ |
| 85 |
ClassLoader effectiveClassLoader; |
| 86 |
Class<?> thisClass = getClass(); |
| 87 |
if(classLoader.loadClass(thisClass.getName()) != thisClass) { |
| 88 |
effectiveClassLoader = thisClass.getClassLoader(); |
| 89 |
} else { |
| 90 |
effectiveClassLoader = classLoader; |
| 91 |
} |
| 92 |
SecureClassLoaderImpl secCl = |
| 93 |
new SecureClassLoaderImpl(effectiveClassLoader); |
| 94 |
Class<?> c = secCl.defineAndLinkClass( |
| 95 |
SecureCaller.class.getName() + "Impl", |
| 96 |
secureCallerImplBytecode, codeSource); |
| 97 |
return c.newInstance(); |
| 98 |
} |
| 99 |
}); |
| 100 |
classLoaderMap.put(classLoader, new SoftReference<SecureCaller>(caller)); |
| 101 |
} |
| 102 |
catch(PrivilegedActionException ex) |
| 103 |
{ |
| 104 |
throw new UndeclaredThrowableException(ex.getCause()); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
return caller.call(callable, cx, scope, thisObj, args); |
| 109 |
} |
| 110 |
|
| 111 |
private static class SecureClassLoaderImpl extends SecureClassLoader |
| 112 |
{ |
| 113 |
SecureClassLoaderImpl(ClassLoader parent) |
| 114 |
{ |
| 115 |
super(parent); |
| 116 |
} |
| 117 |
|
| 118 |
Class<?> defineAndLinkClass(String name, byte[] bytes, CodeSource cs) |
| 119 |
{ |
| 120 |
Class<?> cl = defineClass(name, bytes, 0, bytes.length, cs); |
| 121 |
resolveClass(cl); |
| 122 |
return cl; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private static byte[] loadBytecode() |
| 127 |
{ |
| 128 |
return (byte[])AccessController.doPrivileged(new PrivilegedAction<Object>() |
| 129 |
{ |
| 130 |
public Object run() |
| 131 |
{ |
| 132 |
return loadBytecodePrivileged(); |
| 133 |
} |
| 134 |
}); |
| 135 |
} |
| 136 |
|
| 137 |
private static byte[] loadBytecodePrivileged() |
| 138 |
{ |
| 139 |
URL url = SecureCaller.class.getResource("SecureCallerImpl.clazz"); |
| 140 |
try |
| 141 |
{ |
| 142 |
InputStream in = url.openStream(); |
| 143 |
try |
| 144 |
{ |
| 145 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 146 |
for(;;) |
| 147 |
{ |
| 148 |
int r = in.read(); |
| 149 |
if(r == -1) |
| 150 |
{ |
| 151 |
return bout.toByteArray(); |
| 152 |
} |
| 153 |
bout.write(r); |
| 154 |
} |
| 155 |
} |
| 156 |
finally |
| 157 |
{ |
| 158 |
in.close(); |
| 159 |
} |
| 160 |
} |
| 161 |
catch(IOException e) |
| 162 |
{ |
| 163 |
throw new UndeclaredThrowableException(e); |
| 164 |
} |
| 165 |
} |
| 166 |
} |