|
28 | 28 |
|
29 | 29 | import java.net.URL; |
30 | 30 | import java.security.AccessControlContext; |
31 | | -import java.security.AccessControlException; |
32 | 31 | import java.security.CodeSource; |
33 | | -import java.security.DomainCombiner; |
34 | 32 | import java.security.Permission; |
35 | 33 | import java.security.PermissionCollection; |
36 | 34 | import java.security.Permissions; |
|
41 | 39 | import java.security.ProtectionDomain; |
42 | 40 | import java.security.Provider; |
43 | 41 | import java.security.SecureRandom; |
| 42 | +import java.util.Deque; |
| 43 | +import java.util.LinkedList; |
44 | 44 | import java.util.List; |
45 | 45 | import java.util.Map; |
46 | 46 | import java.util.concurrent.atomic.AtomicReference; |
|
51 | 51 | import org.graalvm.word.Pointer; |
52 | 52 |
|
53 | 53 | import com.oracle.svm.core.SubstrateOptions; |
| 54 | +import com.oracle.svm.core.SubstrateUtil; |
54 | 55 | import com.oracle.svm.core.annotate.Alias; |
55 | 56 | import com.oracle.svm.core.annotate.AutomaticFeature; |
56 | 57 | import com.oracle.svm.core.annotate.Delete; |
|
62 | 63 | import com.oracle.svm.core.annotate.TargetElement; |
63 | 64 | import com.oracle.svm.core.log.Log; |
64 | 65 | import com.oracle.svm.core.option.SubstrateOptionsParser; |
| 66 | +import com.oracle.svm.core.thread.Target_java_lang_Thread; |
65 | 67 | import com.oracle.svm.core.util.VMError; |
66 | 68 | import com.oracle.svm.util.ReflectionUtil; |
67 | 69 |
|
|
80 | 82 | final class Target_java_security_AccessController { |
81 | 83 |
|
82 | 84 | @Substitute |
83 | | - private static <T> T doPrivileged(PrivilegedAction<T> action) throws Throwable { |
84 | | - try { |
85 | | - return action.run(); |
86 | | - } catch (Throwable ex) { |
87 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
88 | | - } |
| 85 | + public static <T> T doPrivileged(PrivilegedAction<T> action) throws Throwable { |
| 86 | + return AccessControllerUtil.executePrivileged(action, null, Target_jdk_internal_reflect_Reflection.getCallerClass()); |
89 | 87 | } |
90 | 88 |
|
91 | 89 | @Substitute |
92 | | - private static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) throws Throwable { |
93 | | - try { |
94 | | - return action.run(); |
95 | | - } catch (Throwable ex) { |
96 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
97 | | - } |
| 90 | + public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) throws Throwable { |
| 91 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 92 | + AccessControlContext acc = AccessControllerUtil.checkContext(context, caller); |
| 93 | + return AccessControllerUtil.executePrivileged(action, acc, caller); |
98 | 94 | } |
99 | 95 |
|
100 | 96 | @Substitute |
101 | | - private static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) throws Throwable { |
102 | | - try { |
103 | | - return action.run(); |
104 | | - } catch (Throwable ex) { |
105 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
106 | | - } |
| 97 | + public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws Throwable { |
| 98 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 99 | + return AccessControllerUtil.executePrivileged(action, null, caller); |
107 | 100 | } |
108 | 101 |
|
109 | 102 | @Substitute |
110 | | - private static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context, Permission... perms) throws Throwable { |
111 | | - try { |
112 | | - return action.run(); |
113 | | - } catch (Throwable ex) { |
114 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
115 | | - } |
| 103 | + static <T> T doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context) throws Throwable { |
| 104 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 105 | + AccessControlContext acc = AccessControllerUtil.checkContext(context, caller); |
| 106 | + return AccessControllerUtil.executePrivileged(action, acc, caller); |
116 | 107 | } |
117 | 108 |
|
118 | 109 | @Substitute |
119 | | - private static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws Throwable { |
120 | | - try { |
121 | | - return action.run(); |
122 | | - } catch (Throwable ex) { |
123 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
124 | | - } |
| 110 | + static AccessControlContext getStackAccessControlContext() { |
| 111 | + return StackAccessControlContextVisitor.getFromStack(); |
125 | 112 | } |
126 | 113 |
|
127 | 114 | @Substitute |
128 | | - private static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action) throws Throwable { |
129 | | - try { |
130 | | - return action.run(); |
131 | | - } catch (Throwable ex) { |
132 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
133 | | - } |
| 115 | + static AccessControlContext getInheritedAccessControlContext() { |
| 116 | + return SubstrateUtil.cast(Thread.currentThread(), Target_java_lang_Thread.class).inheritedAccessControlContext; |
134 | 117 | } |
135 | 118 |
|
136 | | - @Substitute |
137 | | - private static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action, AccessControlContext context, Permission... perms) throws Throwable { |
| 119 | +} |
| 120 | + |
| 121 | +@InternalVMMethod |
| 122 | +class AccessControllerUtil { |
| 123 | + |
| 124 | + static final AccessControlContext NO_CONTEXT_SINGLETON; |
| 125 | + static final ThreadLocal<Deque<AccessControlContext>> accStack = ThreadLocal.withInitial(LinkedList::new); |
| 126 | + |
| 127 | + static { |
138 | 128 | try { |
139 | | - return action.run(); |
140 | | - } catch (Throwable ex) { |
141 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
| 129 | + NO_CONTEXT_SINGLETON = ReflectionUtil.lookupConstructor(AccessControlContext.class, ProtectionDomain[].class, boolean.class).newInstance(new ProtectionDomain[0], true); |
| 130 | + } catch (ReflectiveOperationException ex) { |
| 131 | + throw VMError.shouldNotReachHere(ex); |
142 | 132 | } |
143 | 133 | } |
144 | 134 |
|
145 | | - @Substitute |
146 | | - private static <T> T doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context) throws Throwable { |
147 | | - try { |
148 | | - return action.run(); |
149 | | - } catch (Throwable ex) { |
150 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
| 135 | + /* From JDK15's AccessController */ |
| 136 | + static AccessControlContext checkContext(AccessControlContext context, |
| 137 | + Class<?> caller) { |
| 138 | + // check if caller is authorized to create context |
| 139 | + if (System.getSecurityManager() != null && |
| 140 | + context != null && !SubstrateUtil.cast(context, Target_java_security_AccessControlContext.class).isAuthorized && |
| 141 | + context != NO_CONTEXT_SINGLETON) { |
| 142 | + ProtectionDomain callerPD = caller.getProtectionDomain(); |
| 143 | + if (callerPD != null && !callerPD.implies(SecurityConstants.CREATE_ACC_PERMISSION)) { |
| 144 | + return NO_CONTEXT_SINGLETON; |
| 145 | + } |
151 | 146 | } |
| 147 | + return context; |
152 | 148 | } |
153 | 149 |
|
154 | | - @Substitute |
155 | | - private static void checkPermission(Permission perm) throws AccessControlException { |
156 | | - } |
| 150 | + /* From JDK15's AccessController */ |
| 151 | + @SuppressWarnings("unused") |
| 152 | + static <T> T executePrivileged(PrivilegedAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable { |
| 153 | + if (action == null) { |
| 154 | + throw new NullPointerException("Null action"); |
| 155 | + } |
157 | 156 |
|
158 | | - @Substitute |
159 | | - private static AccessControlContext getContext() { |
160 | | - return AccessControllerUtil.NO_CONTEXT_SINGLETON; |
161 | | - } |
| 157 | + accStack.get().push(context); |
162 | 158 |
|
163 | | - @Substitute |
164 | | - private static AccessControlContext createWrapper(DomainCombiner combiner, Class<?> caller, AccessControlContext parent, AccessControlContext context, Permission[] perms) { |
165 | | - return AccessControllerUtil.NO_CONTEXT_SINGLETON; |
| 159 | + try { |
| 160 | + return action.run(); |
| 161 | + } catch (Throwable ex) { |
| 162 | + throw wrapCheckedException(ex); |
| 163 | + } finally { |
| 164 | + if (context != null) { |
| 165 | + accStack.get().pop(); |
| 166 | + } |
| 167 | + } |
166 | 168 | } |
167 | | -} |
168 | 169 |
|
169 | | -@InternalVMMethod |
170 | | -class AccessControllerUtil { |
| 170 | + /* From JDK15's AccessController */ |
| 171 | + @SuppressWarnings("unused") |
| 172 | + static <T> T executePrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable { |
| 173 | + if (action == null) { |
| 174 | + throw new NullPointerException("Null action"); |
| 175 | + } |
171 | 176 |
|
172 | | - static final AccessControlContext NO_CONTEXT_SINGLETON; |
| 177 | + accStack.get().push(context); |
173 | 178 |
|
174 | | - static { |
175 | 179 | try { |
176 | | - NO_CONTEXT_SINGLETON = ReflectionUtil.lookupConstructor(AccessControlContext.class, ProtectionDomain[].class, boolean.class).newInstance(new ProtectionDomain[0], true); |
177 | | - } catch (ReflectiveOperationException ex) { |
178 | | - throw VMError.shouldNotReachHere(ex); |
| 180 | + return action.run(); |
| 181 | + } catch (Throwable ex) { |
| 182 | + throw wrapCheckedException(ex); |
| 183 | + } finally { |
| 184 | + if (context != null) { |
| 185 | + accStack.get().pop(); |
| 186 | + } |
179 | 187 | } |
180 | 188 | } |
181 | 189 |
|
@@ -204,9 +212,14 @@ private static Object replaceAccessControlContext(Object obj) { |
204 | 212 | } |
205 | 213 |
|
206 | 214 | @TargetClass(java.security.AccessControlContext.class) |
| 215 | +@SuppressWarnings({"unused"}) |
207 | 216 | final class Target_java_security_AccessControlContext { |
208 | | - |
209 | 217 | @Alias protected boolean isPrivileged; |
| 218 | + @Alias protected boolean isAuthorized; |
| 219 | + |
| 220 | + @Alias |
| 221 | + Target_java_security_AccessControlContext(ProtectionDomain[] context, AccessControlContext privilegedContext) { |
| 222 | + } |
210 | 223 | } |
211 | 224 |
|
212 | 225 | @TargetClass(SecurityManager.class) |
|
0 commit comments