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