Debian Codegraph @

rhino/src/org/mozilla/javascript/PolicySecurityController.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
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 * A security controller relying on Java {@link Policy} in effect. When you use
24 * this security controller, your securityDomain objects must be instances of
25 * {@link CodeSource} representing the location from where you load your
26 * scripts. Any Java policy "grant" statements matching the URL and certificate
27 * in code sources will apply to the scripts. If you specify any certificates
28 * within your {@link CodeSource} objects, it is your responsibility to verify
29 * (or not) that the script source files are signed in whatever
30 * implementation-specific way you're using.
31 * @author Attila Szegedi
32 */
33 public class PolicySecurityController extends SecurityController
34 {
35 private static final byte[] secureCallerImplBytecode = loadBytecode();
36
37 // We're storing a CodeSource -> (ClassLoader -> SecureRenderer), since we
38 // need to have one renderer per class loader. We're using weak hash maps
39 // and soft references all the way, since we don't want to interfere with
40 // cleanup of either CodeSource or ClassLoader objects.
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 // No separate notion of dynamic security domain - just return what was
90 // passed in.
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 // Run in doPrivileged as we might be checked for "getClassLoader"
100 // runtime permission
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 // Run in doPrivileged as we'll be checked for
129 // "createClassLoader" runtime permission
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 }