Currently I'm trying to write the Sandbox for running untrusted Java code. The idea is to isolate Java application from accessing file system or network sockets. The solution I have at the moment is rewritten SecurityManager, that forbids any access to IO or network.
Now I want not to forbid, but to redirect calls to the file system, i.e. if application wants to write to "/home/user/application.txt" the path to the file should be replaced with something like "/temp/trusted_folder/application.txt". So basically I want to allow applications to access file system only in some particular folder and to redirect all other calls to this folder.
So here is the method from the class FileOutputStream, where SM is asked, whether there is a permission to write to the given path.
public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
if (name == null) {
throw new NullPointerException();
}
fd = new FileDescriptor();
fd.incrementAndGetUseCount();
this.append = append;
if (append) {
openAppend(name);
} else {
open(name);
}
}
Obviously, the SM does not have an access to FileOutputStream and can not change inner variables in method (like name or file) or somehow affect the execution order, except throwing the SecurityException. I do understand, that accessing inner fields is violation of Object oriented principles, I do understand, that local variable are visible and exist only inside the method, where they were declared.
So my question is: Are there any ways to allow Security Manager to replace calls to file system? If not, are there any other approaches I can use in order to do this?
I hope I was clear enough.