Greetings
I'm a B-Tech Software Engineering Student and I'm on my last semester, I have a course project where I have to develop a simple "Online Judge" website, I decided to hard code it from scratch using php, here's an under development snapshot of the website front page:
I have about a month or more to finish the work, I can build the website and handle everything else related to the website except for one last hurdle, which is malicious code treatment.
This online judge will only accept submissions in C/C++/Java , however I need to make sure the code is safe to run. So far I have been playing around Java's Security Manager and I have learned about policy files and granting permissions as well passing arguments to use that file to restrict permissions.
I have tested and I can confirm it is working and restricting the user from creating a Socket and also executing a command via Runtime, I can also restrict file access permissions, some examples:
Socket s = new Socket("localhost",7777);
or
Runtime.getRuntime().exec("shutdown -s -t 0");
However I'm yet to completely understand how to modify the policy file, for instance I don't know how restrict java imports as I do not want the user to do any network related operations and neither execute JFrames or applets, I'm also yet to know how to use Class Loaders and how to measure memory usage and execution time to mark TLE and MLE .
This is just on the Java side, I'm yet to learn about sandboxing techniques for C++ , I can't seem to find good step by step video or tutorial that can easily explain how to go about it.
Some techniques I heard about but not tried would be virtualization, using chroot jail technique, LXC containers on Ubuntu, systrace and a bulk of search results...nothing easily understandable for my poor brain x[
If anybody knows any easy sandboxing solutions that I could understand and use to safely execute malicious code to prevent damage or scenarios such as shutting down a machine or other unwanted actions then I would be grateful for your input.
Thank you for spending time reading this. (^-^)/
You need to set up thing like apparmor on the server which runs the program and set necessary restrictions. It would not do to search for different approaches for different languages.
We used Libsandox, an open-source sandbox environment for our institute's online judge. It has an extension in the form of Pysandbox, in which all you have to do is pass a dictionary containing various parameters viz. TL, ML, etc. to the sandbox.
At the simplest level you should be OK with a relatively tight SELinux policy and using 'chroot' and 'ulimit' to make a single-use locked down user+filesystem for each run. (warning: you need a little bit more for an open judge with heavy traffic)
Here's how: