|
| 1 | +java_jail: chroot java jail, and JSON java trace printer |
| 2 | +David Pritchard (daveagp@gmail.com), created May 2013 |
| 3 | + |
| 4 | +This is the backend for a Java version of Philip Guo's Python visualizer. |
| 5 | +Try it at: |
| 6 | + http://cscircles.cemc.uwaterloo.ca/java-visualize/ |
| 7 | + |
| 8 | +This directory serves 2 purposes: |
| 9 | + -- . serves as chroot (changed root) for executing Java programs |
| 10 | + -- ./cp/traceprinter contains a Java program to print traces of Java |
| 11 | + programs as they execute, printing the results to the same JSON |
| 12 | + format used by http://pythontutor.com/ |
| 13 | + See ./cp/traceprinter/README for documentation on that part. |
| 14 | + |
| 15 | +The significant code in this directory is the documentation, and the |
| 16 | +contents of ./cp/traceprinter. Both are released under the GNU Affero |
| 17 | +General Public License, versions 3 or later. See LICENSE or visit: |
| 18 | +http://www.gnu.org/licenses/agpl.html |
| 19 | + |
| 20 | +This project would not be possible without the package |
| 21 | +com.sun.tools.example.trace, written by Robert Field. The traceprinter |
| 22 | +package was initially created from that package. |
| 23 | + |
| 24 | +=== Setting up a chroot jail for java === |
| 25 | + |
| 26 | +The good news is that java for linux is available as a single self-contained |
| 27 | +archive (we used jdk-7u21-linux-x64.gz). |
| 28 | + |
| 29 | +The bad news is that it also uses various shared object files not contained |
| 30 | +in there, as well as various pseudo-files. So the chroot jail must also |
| 31 | +contain these. |
| 32 | + |
| 33 | +For chroot jail to work, the full contents of this directory should be: |
| 34 | + |
| 35 | +./java/: copy of unzipped java installation |
| 36 | +./{etc,lib64}/: necessary libraries |
| 37 | +./{dev,proc}/: necessary pseudo-files |
| 38 | +./cp/: we use this to hold application-specific java classpath stuff |
| 39 | + |
| 40 | +as well as README, LICENSE, and some .git files. |
| 41 | + |
| 42 | +Installation steps: |
| 43 | + |
| 44 | +(0) It is highly recommended to NOT put this directory anywhere accessible |
| 45 | + via http, just for sanity's sake. |
| 46 | +(1) Get java. As of the time of writing, a suitable link for wget is: |
| 47 | + |
| 48 | +wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u20-b26/jdk-8u20-linux-x64.tar.gz |
| 49 | + |
| 50 | +(props to http://stackoverflow.com/questions/10268583) |
| 51 | +Extract java with gunzip and tar -xvf. |
| 52 | +Rename that jdk1.8.0_20 to "java" |
| 53 | +(2) You will need to copy some files from your system into ./lib64 -- |
| 54 | + see ./lib64/.gitignore for the list of what we use. If you end up |
| 55 | + using more Java libraries, you might need to add more. We used |
| 56 | + "strace -f"/ldd/lsof to figure out what to put there. You probably |
| 57 | + can also just make a copy of all of /lib64 to ./lib64 |
| 58 | + * From Sasha Sirotkin: on Ubuntu, those files might instead need to |
| 59 | + be copied from lib/x86_64-linux-gnu to ./lib/x86_64-linux-gnu |
| 60 | +(3) copy /etc/ld.so.cache to ./etc |
| 61 | +(4) mkdir ./proc, then you will need to mount /proc to ./proc -- use |
| 62 | + EITHER: |
| 63 | + To mount once (until reboot or "umount /path/to/chroot/proc") |
| 64 | + sudo mount --bind -o ro,bind /proc /path/to/chroot/proc |
| 65 | + To mount permanently, copy this line from /etc/mtab to /etc/fstab |
| 66 | + /proc /path/to/chroot/proc none ro,bind 0 0 |
| 67 | +(5) Mount necessary devices to /dev |
| 68 | + sudo mknod -m 0666 ./dev/null c 1 3 |
| 69 | + sudo mknod -m 0666 ./dev/random c 1 8 |
| 70 | + sudo mknod -m 0444 ./dev/urandom c 1 9 |
| 71 | +(6) File system permissions. At a basic level, it is enough to |
| 72 | + allow everything to be read and executed by everyone, and written by nobody. |
| 73 | +(7) iptables considerations. If you are running arbitrary user code, |
| 74 | + you probably want to prevent them from accessing the internet. |
| 75 | + But, the JDI requires access to a local debugging port to |
| 76 | + communicate between the tracing VM and the traced VM. On our system, |
| 77 | + all sandboxed executions run with group id 1000, so we have |
| 78 | +# to allow JDI: |
| 79 | +-A OUTPUT -p tcp -d 127.0.0.1 --dport 32000:65535 -m owner --gid-owner 1000 -j ACCEPT |
| 80 | +# to deny everything else: |
| 81 | +-A OUTPUT -m owner --gid-owner 1000 -j DROP |
| 82 | + |
| 83 | +If you want to allow connections to the outgoing world, particularly in Java, |
| 84 | +you will have to copy /etc/resolv.conf into the etc of the jail. |
| 85 | + |
| 86 | +=== Testing === |
| 87 | + |
| 88 | +Assuming you have CEMC safeexec (https://github.com/cemc/safeexec) |
| 89 | +installed, here is a way how to test whether things are installed correctly. |
| 90 | + |
| 91 | +(0) Go in to ./cp and run "make" there |
| 92 | + |
| 93 | +(1) Run this command (from java_jail): |
| 94 | + |
| 95 | +./java/bin/java -cp .:cp:cp/javax.json-1.0.jar:java/lib/tools.jar traceprinter.InMemory < cp/traceprinter/test-input.txt |
| 96 | + |
| 97 | +The expected output is at cp/traceprinter/expected-output.txt |
| 98 | + |
| 99 | +(2) Try with safeexec (from java_jail): |
| 100 | +/path/to/safeexec --chroot_dir . --exec_dir / --nproc 50 --mem 3000000 --nfile 30 --env_vars CLASSPATH=/cp/:/cp/javax.json-1.0.jar:/java/lib/tools.jar --exec /java/bin/java traceprinter.InMemory < cp/traceprinter/test-input.txt |
| 101 | + |
| 102 | +Notes from different machines: on at least one machine, nfile and nproc needed to be set to 100. |
| 103 | + |
| 104 | +=== |
| 105 | + |
| 106 | +I found these links useful at some point: |
| 107 | +http://www.cyberciti.biz/faq/howto-run-nginx-in-a-chroot-jail/ |
| 108 | +http://interreality.org/~reed/java-chroot.html |
0 commit comments