I'm thinking about building a simple online service where people can solve programming exercises by submitting their solution, in form of source code, to my server where it is then interpreted/compiled and executed/tested.
By using the Java VM I could offer support for Java, Scala, Clojure, Ruby, Python and Javascript out of the box. But when I think about it in detail I'm afraid I don't know how to limit a script's resources and permissions.
I mean it should not be able to
- write to disk
- create more than X threads
- run more than X seconds
- use more than X MB memory
- execute external applications
- etc
How can I put each script in a sandbox?
From what I've read the SecurityManager doesn't seem to be able to do all that...
Well, you can use some general security system to ensure safe code execution like AppArmor or SELinux. It works not only for java, python, etc. applications, but also for bash-scripts, binary executables and so on. Haven't worked at all with SELinux, but this is a simple example of AppArmor security profile which does everything you mentioned except "running more than X seconds" - this can be done by timeout mechanism (I'm a new user, so cannon post a second link here O_o..)
What about putting each script in a sandbox - you can create several identical profiles for script1, script2 etc. This is also the way if you want different permissions for different excercises people will solve on your site.
And this is an example of using timeout:
I also want to recommend you limit compilation time for compiled proramming languages if you have any. For example, in C++ someone can write a tricky template or
That will cause cpu-intensive work at compile-time.
You have described a JVM port that is similar to the whitelisted classes enabled by the google app engine.
There is an excellent explanation of how you could sandbox a JVM here : How does google app engine sandbox work?
You can use the java scripting API. Many languages can be used as script, Java too. Also it does not require much programming to wrap a language with the scripting API. http://worldwizards.blogspot.com/2009/08/java-scripting-api-sandbox.html indicates how to provide sandboxing.