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