|
1 | 1 | /* |
2 | | - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
30 | 30 | import java.lang.reflect.InvocationTargetException; |
31 | 31 | import java.net.URL; |
32 | 32 | import java.security.AccessControlContext; |
33 | | -import java.security.AccessControlException; |
34 | 33 | import java.security.CodeSource; |
35 | | -import java.security.DomainCombiner; |
36 | 34 | import java.security.Permission; |
37 | 35 | import java.security.PermissionCollection; |
38 | 36 | import java.security.Permissions; |
|
50 | 48 | import org.graalvm.compiler.serviceprovider.JavaVersionUtil; |
51 | 49 | import org.graalvm.nativeimage.Platform; |
52 | 50 | import org.graalvm.nativeimage.Platforms; |
53 | | -import org.graalvm.nativeimage.hosted.Feature; |
54 | 51 | import org.graalvm.word.Pointer; |
55 | 52 |
|
| 53 | +import com.oracle.svm.core.SubstrateUtil; |
56 | 54 | import com.oracle.svm.core.annotate.Alias; |
57 | | -import com.oracle.svm.core.annotate.AutomaticFeature; |
58 | 55 | import com.oracle.svm.core.annotate.Delete; |
59 | 56 | import com.oracle.svm.core.annotate.InjectAccessors; |
60 | 57 | import com.oracle.svm.core.annotate.NeverInline; |
61 | 58 | import com.oracle.svm.core.annotate.RecomputeFieldValue; |
62 | 59 | import com.oracle.svm.core.annotate.Substitute; |
63 | 60 | import com.oracle.svm.core.annotate.TargetClass; |
64 | 61 | import com.oracle.svm.core.annotate.TargetElement; |
| 62 | +import com.oracle.svm.core.graal.snippets.CEntryPointSnippets; |
| 63 | +import com.oracle.svm.core.thread.Target_java_lang_Thread; |
65 | 64 | import com.oracle.svm.core.util.VMError; |
66 | 65 | import com.oracle.svm.util.ReflectionUtil; |
67 | 66 |
|
|
79 | 78 | final class Target_java_security_AccessController { |
80 | 79 |
|
81 | 80 | @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.wrapCheckedExceptionForPrivilegedAction(ex); |
87 | | - } |
| 81 | + @TargetElement(onlyWith = JDK11OrEarlier.class) |
| 82 | + public static <T> T doPrivileged(PrivilegedAction<T> action) throws Throwable { |
| 83 | + return executePrivileged(action, null, Target_jdk_internal_reflect_Reflection.getCallerClass()); |
88 | 84 | } |
89 | 85 |
|
90 | 86 | @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.wrapCheckedExceptionForPrivilegedAction(ex); |
96 | | - } |
| 87 | + @TargetElement(onlyWith = JDK11OrEarlier.class) |
| 88 | + public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) throws Throwable { |
| 89 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 90 | + AccessControlContext acc = checkContext(context, caller); |
| 91 | + return executePrivileged(action, acc, caller); |
97 | 92 | } |
98 | 93 |
|
99 | 94 | @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.wrapCheckedExceptionForPrivilegedAction(ex); |
105 | | - } |
| 95 | + @TargetElement(onlyWith = JDK11OrEarlier.class) |
| 96 | + public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws Throwable { |
| 97 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 98 | + return 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.wrapCheckedExceptionForPrivilegedAction(ex); |
114 | | - } |
| 102 | + @TargetElement(onlyWith = JDK11OrEarlier.class) |
| 103 | + static <T> T doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context) throws Throwable { |
| 104 | + Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass(); |
| 105 | + AccessControlContext acc = checkContext(context, caller); |
| 106 | + return executePrivileged(action, acc, caller); |
115 | 107 | } |
116 | 108 |
|
117 | 109 | @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); |
| 110 | + @SuppressWarnings("deprecation") |
| 111 | + static AccessControlContext getStackAccessControlContext() { |
| 112 | + if (!CEntryPointSnippets.isIsolateInitialized()) { |
| 113 | + /* |
| 114 | + * If isolate still isn't initialized, we can assume that we are so early in the JDK |
| 115 | + * initialization that any attempt at stalk walk will fail as not even the basic |
| 116 | + * PrintWriter/Logging is available yet. This manifested when |
| 117 | + * UseDedicatedVMOperationThread hosted option was set, triggering a runtime crash. |
| 118 | + */ |
| 119 | + return null; |
123 | 120 | } |
| 121 | + return StackAccessControlContextVisitor.getFromStack(); |
124 | 122 | } |
125 | 123 |
|
126 | 124 | @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 | | - } |
| 125 | + static AccessControlContext getInheritedAccessControlContext() { |
| 126 | + return SubstrateUtil.cast(Thread.currentThread(), Target_java_lang_Thread.class).inheritedAccessControlContext; |
133 | 127 | } |
134 | 128 |
|
135 | 129 | @Substitute |
136 | | - private static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action, AccessControlContext context, Permission... perms) throws Throwable { |
137 | | - try { |
138 | | - return action.run(); |
139 | | - } catch (Throwable ex) { |
140 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
141 | | - } |
| 130 | + @TargetElement(onlyWith = JDK17OrLater.class) |
| 131 | + private static ProtectionDomain getProtectionDomain(final Class<?> caller) { |
| 132 | + return caller.getProtectionDomain(); |
142 | 133 | } |
143 | 134 |
|
144 | 135 | @Substitute |
145 | | - private static <T> T doPrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context) throws Throwable { |
| 136 | + @TargetElement(onlyWith = JDK17OrLater.class) |
| 137 | + @SuppressWarnings("deprecation") // deprecated starting JDK 17 |
| 138 | + static <T> T executePrivileged(PrivilegedExceptionAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable { |
| 139 | + if (action == null) { |
| 140 | + throw new NullPointerException("Null action"); |
| 141 | + } |
| 142 | + |
| 143 | + PrivilegedStack.push(context, caller); |
146 | 144 | try { |
147 | 145 | return action.run(); |
148 | | - } catch (Throwable ex) { |
149 | | - throw AccessControllerUtil.wrapCheckedException(ex); |
| 146 | + } catch (RuntimeException ex) { |
| 147 | + throw ex; |
| 148 | + } catch (Exception ex) { |
| 149 | + if (JavaVersionUtil.JAVA_SPEC > 11) { |
| 150 | + throw ex; |
| 151 | + } else { |
| 152 | + throw new PrivilegedActionException(ex); |
| 153 | + } |
| 154 | + } finally { |
| 155 | + PrivilegedStack.pop(); |
150 | 156 | } |
151 | 157 | } |
152 | 158 |
|
153 | 159 | @Substitute |
154 | | - private static void checkPermission(Permission perm) throws AccessControlException { |
155 | | - } |
156 | | - |
157 | | - @Substitute |
158 | | - private static AccessControlContext getContext() { |
159 | | - return AccessControllerUtil.NO_CONTEXT_SINGLETON; |
160 | | - } |
161 | | - |
162 | | - @Substitute |
163 | | - private static AccessControlContext createWrapper(DomainCombiner combiner, Class<?> caller, AccessControlContext parent, AccessControlContext context, Permission[] perms) { |
164 | | - return AccessControllerUtil.NO_CONTEXT_SINGLETON; |
165 | | - } |
166 | | -} |
167 | | - |
168 | | -@InternalVMMethod |
169 | | -class AccessControllerUtil { |
170 | | - |
171 | | - static final AccessControlContext NO_CONTEXT_SINGLETON; |
172 | | - |
173 | | - static { |
174 | | - 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); |
| 160 | + @TargetElement(onlyWith = JDK17OrLater.class) |
| 161 | + @SuppressWarnings("deprecation") // deprecated starting JDK 17 |
| 162 | + static <T> T executePrivileged(PrivilegedAction<T> action, AccessControlContext context, Class<?> caller) throws Throwable { |
| 163 | + if (action == null) { |
| 164 | + throw new NullPointerException("Null action"); |
178 | 165 | } |
179 | | - } |
180 | 166 |
|
181 | | - static Throwable wrapCheckedException(Throwable ex) { |
182 | | - if (ex instanceof Exception && !(ex instanceof RuntimeException)) { |
183 | | - return new PrivilegedActionException((Exception) ex); |
184 | | - } else { |
185 | | - return ex; |
| 167 | + PrivilegedStack.push(context, caller); |
| 168 | + try { |
| 169 | + return action.run(); |
| 170 | + } catch (RuntimeException ex) { |
| 171 | + throw ex; |
| 172 | + } catch (Exception ex) { |
| 173 | + if (JavaVersionUtil.JAVA_SPEC > 11) { |
| 174 | + throw ex; |
| 175 | + } else { |
| 176 | + throw new PrivilegedActionException(ex); |
| 177 | + } |
| 178 | + } finally { |
| 179 | + PrivilegedStack.pop(); |
186 | 180 | } |
187 | 181 | } |
188 | 182 |
|
189 | | - static Throwable wrapCheckedExceptionForPrivilegedAction(Throwable ex) { |
190 | | - if (JavaVersionUtil.JAVA_SPEC <= 11) { |
191 | | - return wrapCheckedException(ex); |
| 183 | + @Substitute |
| 184 | + @TargetElement(onlyWith = JDK17OrLater.class) |
| 185 | + @SuppressWarnings("deprecation") |
| 186 | + static AccessControlContext checkContext(AccessControlContext context, Class<?> caller) { |
| 187 | + |
| 188 | + if (context != null && context.equals(AccessControllerUtil.DISALLOWED_CONTEXT_MARKER)) { |
| 189 | + VMError.shouldNotReachHere("Non-allowed AccessControlContext that was replaced with a blank one at build time was invoked without being reinitialized at run time.\n" + |
| 190 | + "This might be an indicator of improper build time initialization, or of a non-compatible JDK version.\n" + |
| 191 | + "In order to fix this you can either:\n" + |
| 192 | + " * Annotate the offending context's field with @RecomputeFieldValue\n" + |
| 193 | + " * Implement a custom runtime accessor and annotate said field with @InjectAccessors\n" + |
| 194 | + " * If this context originates from the JDK, and it doesn't leak sensitive info, you can allow it in 'AccessControlContextReplacerFeature.duringSetup'"); |
192 | 195 | } |
193 | | - return ex; |
194 | | - } |
195 | | -} |
196 | | - |
197 | | -@AutomaticFeature |
198 | | -class AccessControlContextFeature implements Feature { |
199 | | - @Override |
200 | | - public void duringSetup(DuringSetupAccess access) { |
201 | | - access.registerObjectReplacer(AccessControlContextFeature::replaceAccessControlContext); |
202 | | - } |
203 | 196 |
|
204 | | - private static Object replaceAccessControlContext(Object obj) { |
205 | | - if (obj instanceof AccessControlContext) { |
206 | | - return AccessControllerUtil.NO_CONTEXT_SINGLETON; |
| 197 | + // check if caller is authorized to create context |
| 198 | + if (System.getSecurityManager() != null) { |
| 199 | + throw VMError.unsupportedFeature("SecurityManager isn't supported"); |
207 | 200 | } |
208 | | - return obj; |
| 201 | + return context; |
209 | 202 | } |
210 | 203 | } |
211 | 204 |
|
|
0 commit comments