2828
2929import java .net .URL ;
3030import java .security .AccessControlContext ;
31- import java .security .AccessControlException ;
3231import java .security .CodeSource ;
33- import java .security .DomainCombiner ;
3432import java .security .Permission ;
3533import java .security .PermissionCollection ;
3634import java .security .Permissions ;
4139import java .security .ProtectionDomain ;
4240import java .security .Provider ;
4341import java .security .SecureRandom ;
42+ import java .util .Deque ;
43+ import java .util .LinkedList ;
4444import java .util .List ;
4545import java .util .Map ;
4646import java .util .concurrent .atomic .AtomicReference ;
5050import org .graalvm .nativeimage .hosted .Feature ;
5151import org .graalvm .word .Pointer ;
5252
53+ import com .oracle .svm .core .SubstrateUtil ;
5354import com .oracle .svm .core .annotate .Alias ;
5455import com .oracle .svm .core .annotate .AutomaticFeature ;
5556import com .oracle .svm .core .annotate .Delete ;
5960import com .oracle .svm .core .annotate .Substitute ;
6061import com .oracle .svm .core .annotate .TargetClass ;
6162import com .oracle .svm .core .annotate .TargetElement ;
63+ import com .oracle .svm .core .thread .Target_java_lang_Thread ;
6264import com .oracle .svm .core .util .VMError ;
6365import com .oracle .svm .util .ReflectionUtil ;
6466
7779final class Target_java_security_AccessController {
7880
7981 @ Substitute
80- private static <T > T doPrivileged (PrivilegedAction <T > action ) throws Throwable {
81- try {
82- return action .run ();
83- } catch (Throwable ex ) {
84- throw AccessControllerUtil .wrapCheckedException (ex );
85- }
82+ public static <T > T doPrivileged (PrivilegedAction <T > action ) throws Throwable {
83+ return AccessControllerUtil .executePrivileged (action , null , Target_jdk_internal_reflect_Reflection .getCallerClass ());
8684 }
8785
8886 @ Substitute
89- private static <T > T doPrivilegedWithCombiner (PrivilegedAction <T > action ) throws Throwable {
90- try {
91- return action .run ();
92- } catch (Throwable ex ) {
93- throw AccessControllerUtil .wrapCheckedException (ex );
94- }
87+ public static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context ) throws Throwable {
88+ Class <?> caller = Target_jdk_internal_reflect_Reflection .getCallerClass ();
89+ AccessControlContext acc = AccessControllerUtil .checkContext (context , caller );
90+ return AccessControllerUtil .executePrivileged (action , acc , caller );
9591 }
9692
9793 @ Substitute
98- private static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context ) throws Throwable {
99- try {
100- return action .run ();
101- } catch (Throwable ex ) {
102- throw AccessControllerUtil .wrapCheckedException (ex );
103- }
94+ public static <T > T doPrivileged (PrivilegedExceptionAction <T > action ) throws Throwable {
95+ Class <?> caller = Target_jdk_internal_reflect_Reflection .getCallerClass ();
96+ return AccessControllerUtil .executePrivileged (action , null , caller );
10497 }
10598
10699 @ Substitute
107- private static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context , Permission ... perms ) throws Throwable {
108- try {
109- return action .run ();
110- } catch (Throwable ex ) {
111- throw AccessControllerUtil .wrapCheckedException (ex );
112- }
100+ static <T > T doPrivileged (PrivilegedExceptionAction <T > action , AccessControlContext context ) throws Throwable {
101+ Class <?> caller = Target_jdk_internal_reflect_Reflection .getCallerClass ();
102+ AccessControlContext acc = AccessControllerUtil .checkContext (context , caller );
103+ return AccessControllerUtil .executePrivileged (action , acc , caller );
113104 }
114105
115106 @ Substitute
116- private static <T > T doPrivileged (PrivilegedExceptionAction <T > action ) throws Throwable {
117- try {
118- return action .run ();
119- } catch (Throwable ex ) {
120- throw AccessControllerUtil .wrapCheckedException (ex );
121- }
107+ static AccessControlContext getStackAccessControlContext () {
108+ return StackAccessControlContextVisitor .getFromStack ();
122109 }
123110
124111 @ Substitute
125- private static <T > T doPrivilegedWithCombiner (PrivilegedExceptionAction <T > action ) throws Throwable {
126- try {
127- return action .run ();
128- } catch (Throwable ex ) {
129- throw AccessControllerUtil .wrapCheckedException (ex );
130- }
112+ static AccessControlContext getInheritedAccessControlContext () {
113+ return SubstrateUtil .cast (Thread .currentThread (), Target_java_lang_Thread .class ).inheritedAccessControlContext ;
131114 }
132115
133- @ Substitute
134- private static <T > T doPrivilegedWithCombiner (PrivilegedExceptionAction <T > action , AccessControlContext context , Permission ... perms ) throws Throwable {
116+ }
117+
118+ @ InternalVMMethod
119+ class AccessControllerUtil {
120+
121+ static final AccessControlContext NO_CONTEXT_SINGLETON ;
122+ static final ThreadLocal <Deque <AccessControlContext >> accStack = ThreadLocal .withInitial (LinkedList ::new );
123+
124+ static {
135125 try {
136- return action . run ( );
137- } catch (Throwable ex ) {
138- throw AccessControllerUtil . wrapCheckedException (ex );
126+ NO_CONTEXT_SINGLETON = ReflectionUtil . lookupConstructor ( AccessControlContext . class , ProtectionDomain []. class , boolean . class ). newInstance ( new ProtectionDomain [ 0 ], true );
127+ } catch (ReflectiveOperationException ex ) {
128+ throw VMError . shouldNotReachHere (ex );
139129 }
140130 }
141131
142- @ Substitute
143- private static <T > T doPrivileged (PrivilegedExceptionAction <T > action , AccessControlContext context ) throws Throwable {
144- try {
145- return action .run ();
146- } catch (Throwable ex ) {
147- throw AccessControllerUtil .wrapCheckedException (ex );
132+ /* From JDK15's AccessController */
133+ static AccessControlContext checkContext (AccessControlContext context ,
134+ Class <?> caller ) {
135+ // check if caller is authorized to create context
136+ if (System .getSecurityManager () != null &&
137+ context != null && !SubstrateUtil .cast (context , Target_java_security_AccessControlContext .class ).isAuthorized &&
138+ context != NO_CONTEXT_SINGLETON ) {
139+ ProtectionDomain callerPD = caller .getProtectionDomain ();
140+ if (callerPD != null && !callerPD .implies (SecurityConstants .CREATE_ACC_PERMISSION )) {
141+ return NO_CONTEXT_SINGLETON ;
142+ }
148143 }
144+ return context ;
149145 }
150146
151- @ Substitute
152- private static void checkPermission (Permission perm ) throws AccessControlException {
153- }
147+ /* From JDK15's AccessController */
148+ @ SuppressWarnings ("unused" )
149+ static <T > T executePrivileged (PrivilegedAction <T > action , AccessControlContext context , Class <?> caller ) throws Throwable {
150+ if (action == null ) {
151+ throw new NullPointerException ("Null action" );
152+ }
154153
155- @ Substitute
156- private static AccessControlContext getContext () {
157- return AccessControllerUtil .NO_CONTEXT_SINGLETON ;
158- }
154+ accStack .get ().push (context );
159155
160- @ Substitute
161- private static AccessControlContext createWrapper (DomainCombiner combiner , Class <?> caller , AccessControlContext parent , AccessControlContext context , Permission [] perms ) {
162- return AccessControllerUtil .NO_CONTEXT_SINGLETON ;
156+ try {
157+ return action .run ();
158+ } catch (Throwable ex ) {
159+ throw wrapCheckedException (ex );
160+ } finally {
161+ if (context != null ) {
162+ accStack .get ().pop ();
163+ }
164+ }
163165 }
164- }
165166
166- @ InternalVMMethod
167- class AccessControllerUtil {
167+ /* From JDK15's AccessController */
168+ @ SuppressWarnings ("unused" )
169+ static <T > T executePrivileged (PrivilegedExceptionAction <T > action , AccessControlContext context , Class <?> caller ) throws Throwable {
170+ if (action == null ) {
171+ throw new NullPointerException ("Null action" );
172+ }
168173
169- static final AccessControlContext NO_CONTEXT_SINGLETON ;
174+ accStack . get (). push ( context ) ;
170175
171- static {
172176 try {
173- NO_CONTEXT_SINGLETON = ReflectionUtil .lookupConstructor (AccessControlContext .class , ProtectionDomain [].class , boolean .class ).newInstance (new ProtectionDomain [0 ], true );
174- } catch (ReflectiveOperationException ex ) {
175- throw VMError .shouldNotReachHere (ex );
177+ return action .run ();
178+ } catch (Throwable ex ) {
179+ throw wrapCheckedException (ex );
180+ } finally {
181+ if (context != null ) {
182+ accStack .get ().pop ();
183+ }
176184 }
177185 }
178186
@@ -186,6 +194,7 @@ static Throwable wrapCheckedException(Throwable ex) {
186194}
187195
188196@ AutomaticFeature
197+ @ SuppressWarnings ({"unused" })
189198class AccessControlContextFeature implements Feature {
190199 @ Override
191200 public void duringSetup (DuringSetupAccess access ) {
@@ -201,9 +210,14 @@ private static Object replaceAccessControlContext(Object obj) {
201210}
202211
203212@ TargetClass (java .security .AccessControlContext .class )
213+ @ SuppressWarnings ({"unused" })
204214final class Target_java_security_AccessControlContext {
205-
206215 @ Alias protected boolean isPrivileged ;
216+ @ Alias protected boolean isAuthorized ;
217+
218+ @ Alias
219+ Target_java_security_AccessControlContext (ProtectionDomain [] context , AccessControlContext privilegedContext ) {
220+ }
207221}
208222
209223@ TargetClass (SecurityManager .class )
@@ -333,6 +347,7 @@ final class Target_javax_crypto_JceSecurity_IdentityWrapper {
333347class JceSecurityAccessor {
334348 private static volatile SecureRandom RANDOM ;
335349
350+ @ SuppressWarnings ({"unused" })
336351 static SecureRandom get () {
337352 SecureRandom result = RANDOM ;
338353 if (result == null ) {
@@ -430,6 +445,7 @@ final class Target_java_security_Policy_PolicyInfo {
430445}
431446
432447@ TargetClass (java .security .Policy .class )
448+ @ SuppressWarnings ({"unused" })
433449final class Target_java_security_Policy {
434450
435451 @ Delete @ TargetElement (onlyWith = JDK8OrEarlier .class ) //
@@ -527,6 +543,7 @@ private void engineRefresh() {
527543
528544@ Delete ("Substrate VM does not use SecurityManager, so loading a security policy file would be misleading" )
529545@ TargetClass (className = "sun.security.provider.PolicyFile" )
546+ @ SuppressWarnings ({"unused" })
530547final class Target_sun_security_provider_PolicyFile {
531548}
532549
@@ -572,5 +589,6 @@ final class Target_sun_security_jca_ProviderConfig_ProviderLoader {
572589}
573590
574591/** Dummy class to have a class with the file's name. */
592+ @ SuppressWarnings ({"unused" })
575593public final class SecuritySubstitutions {
576594}
0 commit comments