Hi,
this is a bit out of scope in this forum, but let me try to help. All my classes are in the default package, as I guess were yours.
- If I run the code as is, with a single change, namely I’ve added the debug option to the context, i.e, the run method in Sandkiste is
public void run(){
SandboxService sandboxService = SandboxServiceImpl.getInstance();
/* configure context */
SandboxContext context = new SandboxContext();
context.setDebug(true);
context.addClassForApplicationLoader("Test");
context.addClassPermission(AccessType.PERMIT,UntrustedCode.class.getName());
context.addClassPermission(AccessType.DENY, "java.lang.System");
context.addClassPermission(AccessType.DENY, "java.io.PrintStream");
context.setRunInThread(true);
/* run code in sandbox */
SandboxedCallResult<List<String>> result = sandboxService.runSandboxed(UntrustedCode.class, context);
/* output result */
}
I’ll get the following output
Dez 20, 2014 9:46:14 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr1
Dez 20, 2014 9:46:15 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr2
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: UntrustedCode
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: net.datenwerke.sandbox.SandboxedEnvironment
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: java.lang.Object
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: java.util.List
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: java.lang.Exception
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208670712) about to load class: Test
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : ClassAccessCheck: Test
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Warnung: (2061231983) : DENY: ClassAccessCheck: Test
0 : class net.datenwerke.sandbox.SandboxSecurityManager
1 : class net.datenwerke.sandbox.SandboxLoader
2 : class java.lang.ClassLoader
3 : class UntrustedCode
4 : class net.datenwerke.sandbox.SandboxedThread
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : PackageAccessCheck: java.util
Dez 20, 2014 9:46:16 AM net.datenwerke.sandbox.SandboxContext debug
Information: (2061231983) : PackageAccessCheck: java.util
Exception in thread "main" net.datenwerke.sandbox.exception.SandboxException: java.security.AccessControlException: No class access allowed for class: Test
at net.datenwerke.sandbox.SandboxServiceImpl.run(SandboxServiceImpl.java:568)
at net.datenwerke.sandbox.SandboxServiceImpl.runSandboxed(SandboxServiceImpl.java:499)
at net.datenwerke.sandbox.SandboxServiceImpl.runSandboxed(SandboxServiceImpl.java:490)
at Sandkiste.run(Sandkiste.java:36)
at Sandkiste.main(Sandkiste.java:15)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.datenwerke.sandbox.SandboxedThread.run(SandboxedThread.java:59)
Caused by: java.security.AccessControlException: No class access allowed for class: Test
at net.datenwerke.sandbox.SandboxSecurityManager.checkClassAccess(SandboxSecurityManager.java:146)
at net.datenwerke.sandbox.SandboxLoader.loadClass(SandboxLoader.java:336)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at UntrustedCode.execute(UntrustedCode.java:11)
... 5 more
The important lines are
Warnung: (2061231983) : DENY: ClassAccessCheck: Test
0 : class net.datenwerke.sandbox.SandboxSecurityManager
1 : class net.datenwerke.sandbox.SandboxLoader
2 : class java.lang.ClassLoader
3 : class UntrustedCode
4 : class net.datenwerke.sandbox.SandboxedThread
which is where the UntrustedCode tries to access class Test which is denied, since there is no rule that allows to load class Test. Now, if we
explicitly whitelist Test (i.e. context.addClassPermission(AccessType.PERMIT, “Test”)
we get the following response
Dez 20, 2014 9:50:05 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr1
Dez 20, 2014 9:50:06 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr2
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: UntrustedCode
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: net.datenwerke.sandbox.SandboxedEnvironment
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: java.lang.Object
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: java.util.List
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: java.lang.Exception
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (208981104) about to load class: Test
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : ClassAccessCheck: Test
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PackageAccessCheck: java.io
Erlaubt!
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PackageAccessCheck: java.util
Dez 20, 2014 9:50:07 AM net.datenwerke.sandbox.SandboxContext debug
Information: (850671470) : PackageAccessCheck: java.util
which I guess is what you were having. The question is, why is Test allowed to call java.lang.System? The reason is that you’ve told the Sandbox to
load class Test not with the Sandbox loader but with the ApplicationClassloader via
context.addClassForApplicationLoader("Test");
Thus, all subsequent class loading activities triggered by Test are handled not by the SandboxLoader but by the Application ClassLoader which cannot
be monitored by the sandbox and hence the call to java.System goes trough. There should, however, not be any reason to load Test not with the
SandboxLoader. So if Sandkiste is as follows:
public void run(){
SandboxService sandboxService = SandboxServiceImpl.getInstance();
/* configure context */
SandboxContext context = new SandboxContext();
context.setDebug(true);
context.addClassPermission(AccessType.PERMIT,UntrustedCode.class.getName());
context.addClassPermission(AccessType.PERMIT, "Test");
context.setRunInThread(true);
/* run code in sandbox */
SandboxedCallResult<List<String>> result = sandboxService.runSandboxed(UntrustedCode.class, context);
/* output result */
}
Then you get the expected result. (Note that I also removed the two Deny class permissions since this is implicit as the Sandbox by default
disallows anything that is not explicitly allowed.)
Dez 20, 2014 9:54:29 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr1
Dez 20, 2014 9:54:30 AM net.datenwerke.sandbox.jvm.server.SandboxJvmServer <init>
Information: started sandbox server: SandboxRemoteServerNr2
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: UntrustedCode
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: net.datenwerke.sandbox.SandboxedEnvironment
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: java.lang.Object
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: java.util.List
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: java.lang.Exception
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: Test
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PermissionCheck: ("java.io.FilePermission" "/Users/arno/Datenwerke/Projekte/Sandbox/current/Test/bin/Test.class" "read")
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : ClassAccessCheck: Test
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: java.lang.System
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : ClassAccessCheck: java.lang.System
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Warnung: (1355039451) : DENY: ClassAccessCheck: java.lang.System
0 : class net.datenwerke.sandbox.SandboxSecurityManager
1 : class net.datenwerke.sandbox.SandboxLoader
2 : class java.lang.ClassLoader
3 : class Test
4 : class UntrustedCode
5 : class net.datenwerke.sandbox.SandboxedThread
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PackageAccessCheck: java.util
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : PackageAccessCheck: java.util
Exception in thread "main" net.datenwerke.sandbox.exception.SandboxException: java.security.AccessControlException: No class access allowed for class: java.lang.System
at net.datenwerke.sandbox.SandboxServiceImpl.run(SandboxServiceImpl.java:568)
at net.datenwerke.sandbox.SandboxServiceImpl.runSandboxed(SandboxServiceImpl.java:499)
at net.datenwerke.sandbox.SandboxServiceImpl.runSandboxed(SandboxServiceImpl.java:490)
at Sandkiste.run(Sandkiste.java:35)
at Sandkiste.main(Sandkiste.java:15)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at net.datenwerke.sandbox.SandboxedThread.run(SandboxedThread.java:59)
Caused by: java.security.AccessControlException: No class access allowed for class: java.lang.System
at net.datenwerke.sandbox.SandboxSecurityManager.checkClassAccess(SandboxSecurityManager.java:146)
at net.datenwerke.sandbox.SandboxLoader.loadClass(SandboxLoader.java:336)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at Test.print(Test.java:6)
at UntrustedCode.execute(UntrustedCode.java:12)
... 5 more
Again the important lines in the debug output are
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxLoader loadClass
Information: (1333699935) about to load class: java.lang.System
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Information: (1355039451) : ClassAccessCheck: java.lang.System
Dez 20, 2014 9:54:31 AM net.datenwerke.sandbox.SandboxContext debug
Warnung: (1355039451) : DENY: ClassAccessCheck: java.lang.System
0 : class net.datenwerke.sandbox.SandboxSecurityManager
1 : class net.datenwerke.sandbox.SandboxLoader
2 : class java.lang.ClassLoader
3 : class Test
4 : class UntrustedCode
5 : class net.datenwerke.sandbox.SandboxedThread
I hope this helps.
Cheers
Arno