Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/shell/JavaPolicySecurity.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.tools.shell;
8
9 import java.io.IOException;
10 import java.security.*;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.util.Enumeration;
14
15 import org.mozilla.javascript.*;
16
17 public class JavaPolicySecurity extends SecurityProxy
18 {
19
20 @Override
21 public Class<?> getStaticSecurityDomainClassInternal() {
22 return ProtectionDomain.class;
23 }
24
25 private static class Loader extends ClassLoader
26 implements GeneratedClassLoader
27 {
28 private ProtectionDomain domain;
29
30 Loader(ClassLoader parent, ProtectionDomain domain) {
31 super(parent != null ? parent : getSystemClassLoader());
32 this.domain = domain;
33 }
34
35 public Class<?> defineClass(String name, byte[] data) {
36 return super.defineClass(name, data, 0, data.length, domain);
37 }
38
39 public void linkClass(Class<?> cl) {
40 resolveClass(cl);
41 }
42 }
43
44 private static class ContextPermissions extends PermissionCollection
45 {
46 static final long serialVersionUID = -1721494496320750721L;
47
48 // Construct PermissionCollection that permits an action only
49 // if it is permitted by staticDomain and by security context of Java stack on
50 // the moment of constructor invocation
51 ContextPermissions(ProtectionDomain staticDomain) {
52 _context = AccessController.getContext();
53 if (staticDomain != null) {
54 _statisPermissions = staticDomain.getPermissions();
55 }
56 setReadOnly();
57 }
58
59 @Override
60 public void add(Permission permission) {
61 throw new RuntimeException("NOT IMPLEMENTED");
62 }
63
64 @Override
65 public boolean implies(Permission permission) {
66 if (_statisPermissions != null) {
67 if (!_statisPermissions.implies(permission)) {
68 return false;
69 }
70 }
71 try {
72 _context.checkPermission(permission);
73 return true;
74 }catch (AccessControlException ex) {
75 return false;
76 }
77 }
78
79 @Override
80 public Enumeration<Permission> elements()
81 {
82 return new Enumeration<Permission>() {
83 public boolean hasMoreElements() { return false; }
84 public Permission nextElement() { return null; }
85 };
86 }
87
88 @Override
89 public String toString() {
90 StringBuilder sb = new StringBuilder();
91 sb.append(getClass().getName());
92 sb.append('@');
93 sb.append(Integer.toHexString(System.identityHashCode(this)));
94 sb.append(" (context=");
95 sb.append(_context);
96 sb.append(", static_permitions=");
97 sb.append(_statisPermissions);
98 sb.append(')');
99 return sb.toString();
100 }
101
102 AccessControlContext _context;
103 PermissionCollection _statisPermissions;
104 }
105
106 public JavaPolicySecurity()
107 {
108 // To trigger error on jdk-1.1 with lazy load
109 new CodeSource(null, (java.security.cert.Certificate[])null);
110 }
111
112 @Override
113 protected void callProcessFileSecure(final Context cx,
114 final Scriptable scope,
115 final String filename)
116 {
117 AccessController.doPrivileged(new PrivilegedAction<Object>() {
118 public Object run() {
119 URL url = getUrlObj(filename);
120 ProtectionDomain staticDomain = getUrlDomain(url);
121 try {
122 Main.processFileSecure(cx, scope, url.toExternalForm(),
123 staticDomain);
124 } catch (IOException ioex) {
125 throw new RuntimeException(ioex);
126 }
127 return null;
128 }
129 });
130 }
131
132 private URL getUrlObj(String url)
133 {
134 URL urlObj;
135 try {
136 urlObj = new URL(url);
137 } catch (MalformedURLException ex) {
138 // Assume as Main.processFileSecure it is file, need to build its
139 // URL
140 String curDir = System.getProperty("user.dir");
141 curDir = curDir.replace('\\', '/');
142 if (!curDir.endsWith("/")) {
143 curDir = curDir+'/';
144 }
145 try {
146 URL curDirURL = new URL("file:"+curDir);
147 urlObj = new URL(curDirURL, url);
148 } catch (MalformedURLException ex2) {
149 throw new RuntimeException
150 ("Can not construct file URL for '"+url+"':"
151 +ex2.getMessage());
152 }
153 }
154 return urlObj;
155 }
156
157 private ProtectionDomain getUrlDomain(URL url)
158 {
159 CodeSource cs;
160 cs = new CodeSource(url, (java.security.cert.Certificate[])null);
161 PermissionCollection pc = Policy.getPolicy().getPermissions(cs);
162 return new ProtectionDomain(cs, pc);
163 }
164
165 @Override
166 public GeneratedClassLoader
167 createClassLoader(final ClassLoader parentLoader, Object securityDomain)
168 {
169 final ProtectionDomain domain = (ProtectionDomain)securityDomain;
170 return AccessController.doPrivileged(new PrivilegedAction<Loader>() {
171 public Loader run() {
172 return new Loader(parentLoader, domain);
173 }
174 });
175 }
176
177 @Override
178 public Object getDynamicSecurityDomain(Object securityDomain)
179 {
180 ProtectionDomain staticDomain = (ProtectionDomain)securityDomain;
181 return getDynamicDomain(staticDomain);
182 }
183
184 private ProtectionDomain getDynamicDomain(ProtectionDomain staticDomain) {
185 ContextPermissions p = new ContextPermissions(staticDomain);
186 ProtectionDomain contextDomain = new ProtectionDomain(null, p);
187 return contextDomain;
188 }
189
190 @Override
191 public Object callWithDomain(Object securityDomain,
192 final Context cx,
193 final Callable callable,
194 final Scriptable scope,
195 final Scriptable thisObj,
196 final Object[] args)
197 {
198 ProtectionDomain staticDomain = (ProtectionDomain)securityDomain;
199 // There is no direct way in Java to intersect permissions according
200 // stack context with additional domain.
201 // The following implementation first constructs ProtectionDomain
202 // that allows actions only allowed by both staticDomain and current
203 // stack context, and then constructs AccessController for this dynamic
204 // domain.
205 // If this is too slow, alternative solution would be to generate
206 // class per domain with a proxy method to call to infect
207 // java stack.
208 // Another optimization in case of scripts coming from "world" domain,
209 // that is having minimal default privileges is to construct
210 // one AccessControlContext based on ProtectionDomain
211 // with least possible privileges and simply call
212 // AccessController.doPrivileged with this untrusted context
213
214 ProtectionDomain dynamicDomain = getDynamicDomain(staticDomain);
215 ProtectionDomain[] tmp = { dynamicDomain };
216 AccessControlContext restricted = new AccessControlContext(tmp);
217
218 PrivilegedAction<Object> action = new PrivilegedAction<Object>() {
219 public Object run() {
220 return callable.call(cx, scope, thisObj, args);
221 }
222 };
223
224 return AccessController.doPrivileged(action, restricted);
225 }
226 }