From b26d1b5b45529216a8b0c3b651e3588547f22f17 Mon Sep 17 00:00:00 2001 From: Brian Seymour Date: Mon, 22 Oct 2018 16:38:52 -0500 Subject: [PATCH] added a version of piston which uses lxc instead of docker, added tests for lxc --- lxc/execute | 76 +++++++++++++++++++++++++++++++++++++++++++ lxc/executors/c | 3 ++ lxc/executors/cpp | 3 ++ lxc/executors/csharp | 3 ++ lxc/executors/go | 6 ++++ lxc/executors/java | 6 ++++ lxc/executors/nasm | 4 +++ lxc/executors/node | 2 ++ lxc/executors/php | 2 ++ lxc/executors/python2 | 2 ++ lxc/executors/python3 | 2 ++ lxc/executors/r | 2 ++ lxc/executors/ruby | 2 ++ lxc/notes.txt | 26 +++++++++++++++ lxc/shell | 3 ++ lxc/start | 6 ++++ lxc/stop | 3 ++ tests/test.c | 5 +++ tests/test.cpp | 6 ++++ tests/test.cs | 9 +++++ tests/test.go | 7 ++++ tests/test.java | 5 +++ tests/test.js | 1 + tests/test.nasm | 16 +++++++++ tests/test.php | 3 ++ tests/test.r | 1 + tests/test.rb | 1 + tests/test2.py | 1 + tests/test3.py | 1 + tests/test_all_lxc | 26 +++++++++++++++ 30 files changed, 233 insertions(+) create mode 100755 lxc/execute create mode 100755 lxc/executors/c create mode 100755 lxc/executors/cpp create mode 100755 lxc/executors/csharp create mode 100755 lxc/executors/go create mode 100755 lxc/executors/java create mode 100755 lxc/executors/nasm create mode 100755 lxc/executors/node create mode 100755 lxc/executors/php create mode 100755 lxc/executors/python2 create mode 100755 lxc/executors/python3 create mode 100755 lxc/executors/r create mode 100755 lxc/executors/ruby create mode 100644 lxc/notes.txt create mode 100755 lxc/shell create mode 100755 lxc/start create mode 100755 lxc/stop create mode 100644 tests/test.c create mode 100644 tests/test.cpp create mode 100644 tests/test.cs create mode 100644 tests/test.go create mode 100644 tests/test.java create mode 100644 tests/test.js create mode 100644 tests/test.nasm create mode 100644 tests/test.php create mode 100644 tests/test.r create mode 100644 tests/test.rb create mode 100644 tests/test2.py create mode 100644 tests/test3.py create mode 100755 tests/test_all_lxc diff --git a/lxc/execute b/lxc/execute new file mode 100755 index 0000000..8c58db8 --- /dev/null +++ b/lxc/execute @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +dir="$( cd "$( dirname "$0" )" && pwd )" + +if [ -z "$1" ]; then + echo "invalid args" + exit +fi +if [ -z "$2" ]; then + echo "invalid args" + exit +fi + +lang=$1 +epoch=$(date +%s%3N) +basepath="/var/lib/lxc/piston/rootfs" +filepath="/tmp/$epoch/code.code" +file=$(basename $2) +argpath="/tmp/$epoch/args.args" +arg=$(basename $argpath) + +# write arg file +mkdir -p $basepath/tmp/$epoch +chmod 777 $basepath/tmp/$epoch +cat $file > $basepath$filepath +echo "${@:3}" > $basepath$argpath + +bin= +case "$lang" in +"python2") + bin=python2 + ;; +"python" | "python3") + bin=python3 + ;; +"ruby") + bin=ruby + ;; +"javascript" | "js" | "node") + bin=node + ;; +"c") + bin=c + ;; +"cpp" | "c++") + bin=cpp + ;; +"go") + bin=go + ;; +"c#" | "csharp" | "cs") + bin=csharp + ;; +"r") + bin=r + ;; +"php") + bin=php + ;; +"nasm" | "asm") + bin=nasm + ;; +"java") + bin=java + ;; +*) + echo "invalid language" + exit +esac + +lxc-attach -n piston -- \ + /bin/su ubuntu \ + -c "bash /home/ubuntu/$bin $epoch 2>&1 | head -c 65536" + +#rm -f $basepath$filepath +#rm -f $basepath$argpath diff --git a/lxc/executors/c b/lxc/executors/c new file mode 100755 index 0000000..42de3a1 --- /dev/null +++ b/lxc/executors/c @@ -0,0 +1,3 @@ +cd /tmp/$1 +timeout -s KILL 10 gcc -o binary -x c code.code +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary" diff --git a/lxc/executors/cpp b/lxc/executors/cpp new file mode 100755 index 0000000..8535fb5 --- /dev/null +++ b/lxc/executors/cpp @@ -0,0 +1,3 @@ +cd /tmp/$1 +timeout -s KILL 10 g++ -o binary -x c++ code.code +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary" diff --git a/lxc/executors/csharp b/lxc/executors/csharp new file mode 100755 index 0000000..2faa0a4 --- /dev/null +++ b/lxc/executors/csharp @@ -0,0 +1,3 @@ +cd /tmp/$1 +timeout -s KILL 10 mcs $(echo code.code | sed 's/\///') -out:binary +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' mono binary" diff --git a/lxc/executors/go b/lxc/executors/go new file mode 100755 index 0000000..0f3d4ea --- /dev/null +++ b/lxc/executors/go @@ -0,0 +1,6 @@ +cd /tmp/$1 +cp code.code interim.go +file="interim.go" +GOROOT=/usr/lib/go timeout -s KILL 10 go build $file +file=${file%%.*} +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./$file" diff --git a/lxc/executors/java b/lxc/executors/java new file mode 100755 index 0000000..9a6062d --- /dev/null +++ b/lxc/executors/java @@ -0,0 +1,6 @@ +cd /tmp/$1 +cp code.code interim.java +name=$(cat interim.java | grep -Eo 'public\s+class\s+([A-Za-z0-9]+)' | sed -n 's/ */ /gp' | cut -d' ' -f3) +mv interim.java $name.java +timeout -s KILL 10 javac $name.java +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' java $name" diff --git a/lxc/executors/nasm b/lxc/executors/nasm new file mode 100755 index 0000000..446f41e --- /dev/null +++ b/lxc/executors/nasm @@ -0,0 +1,4 @@ +cd /tmp/$1 +timeout -s KILL 10 nasm -f elf64 -o binary.o code.code +timeout -s KILL 10 ld binary.o -o binary +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ./binary" diff --git a/lxc/executors/node b/lxc/executors/node new file mode 100755 index 0000000..2688a87 --- /dev/null +++ b/lxc/executors/node @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' nodejs code.code" diff --git a/lxc/executors/php b/lxc/executors/php new file mode 100755 index 0000000..e3b1fab --- /dev/null +++ b/lxc/executors/php @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' php code.code" diff --git a/lxc/executors/python2 b/lxc/executors/python2 new file mode 100755 index 0000000..c44cf3c --- /dev/null +++ b/lxc/executors/python2 @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' python2 code.code" diff --git a/lxc/executors/python3 b/lxc/executors/python3 new file mode 100755 index 0000000..b58104e --- /dev/null +++ b/lxc/executors/python3 @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' python3.6 code.code" diff --git a/lxc/executors/r b/lxc/executors/r new file mode 100755 index 0000000..6f05be5 --- /dev/null +++ b/lxc/executors/r @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' Rscript code.code" diff --git a/lxc/executors/ruby b/lxc/executors/ruby new file mode 100755 index 0000000..8f10fc3 --- /dev/null +++ b/lxc/executors/ruby @@ -0,0 +1,2 @@ +cd /tmp/$1 +timeout -s KILL 3 bash -c "cat args.args | xargs -d '\n' ruby code.code" diff --git a/lxc/notes.txt b/lxc/notes.txt new file mode 100644 index 0000000..6680574 --- /dev/null +++ b/lxc/notes.txt @@ -0,0 +1,26 @@ +# install +yum install lxc lxc-template debootstrap + +# create container +lxc-create -t download -n piston +select ubuntu, bionic, amd64 + +# start container +lxc-start -n piston -d + +# shell in and install stuff +lxc-attach -n piston +export PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin +sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu/http:\/\/mirror.math.princeton.edu\/pub\/ubuntu/' /etc/apt/sources.list +apt-get update +apt-get -y install tzdata nano dpkg-dev build-essential python python3 ruby nodejs golang php7.2 r-base mono-complete nasm openjdk-8-jdk + +# apply limits +echo 'ubuntu soft nproc 128' >> /etc/security/limits.conf +echo 'ubuntu hard nproc 128' >> /etc/security/limits.conf +echo 'ubuntu soft nofile 1024' >> /etc/security/limits.conf +echo 'ubuntu hard nofile 1024' >> /etc/security/limits.conf +echo 'runner soft nproc 16' >> /etc/security/limits.conf +echo 'runner hard nproc 16' >> /etc/security/limits.conf +echo 'runner soft nofile 512' >> /etc/security/limits.conf +echo 'runner hard nofile 512' >> /etc/security/limits.conf diff --git a/lxc/shell b/lxc/shell new file mode 100755 index 0000000..d586132 --- /dev/null +++ b/lxc/shell @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +lxc-attach -n piston diff --git a/lxc/start b/lxc/start new file mode 100755 index 0000000..1134abe --- /dev/null +++ b/lxc/start @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +rm -f /var/lib/lxc/piston/rootfs/home/ubuntu/* +cp -f executors/* /var/lib/lxc/piston/rootfs/home/ubuntu + +lxc-start -n piston -d diff --git a/lxc/stop b/lxc/stop new file mode 100755 index 0000000..92e9f76 --- /dev/null +++ b/lxc/stop @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +lxc-stop -n piston -k diff --git a/tests/test.c b/tests/test.c new file mode 100644 index 0000000..1ff459d --- /dev/null +++ b/tests/test.c @@ -0,0 +1,5 @@ +#include + +void main(void) { + printf("good\n"); +} diff --git a/tests/test.cpp b/tests/test.cpp new file mode 100644 index 0000000..785dfb4 --- /dev/null +++ b/tests/test.cpp @@ -0,0 +1,6 @@ +#include + +int main(void) { + printf("good\n"); + return 1; +} diff --git a/tests/test.cs b/tests/test.cs new file mode 100644 index 0000000..bc4d225 --- /dev/null +++ b/tests/test.cs @@ -0,0 +1,9 @@ +using System; + +namespace HelloWorld { + class Hello { + static void Main() { + Console.WriteLine("good"); + } + } +} diff --git a/tests/test.go b/tests/test.go new file mode 100644 index 0000000..7304cc2 --- /dev/null +++ b/tests/test.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("good") +} diff --git a/tests/test.java b/tests/test.java new file mode 100644 index 0000000..ca376fd --- /dev/null +++ b/tests/test.java @@ -0,0 +1,5 @@ +public class HelloWorld { + public static void main(String[] args) { + System.out.println("good"); + } +} diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..847245f --- /dev/null +++ b/tests/test.js @@ -0,0 +1 @@ +console.log('good') diff --git a/tests/test.nasm b/tests/test.nasm new file mode 100644 index 0000000..8570909 --- /dev/null +++ b/tests/test.nasm @@ -0,0 +1,16 @@ +SECTION .DATA +good: db 'good',10 +txtlen: equ $-good + +SECTION .TEXT +GLOBAL _start + +_start: +mov eax,4 +mov ebx,1 +mov ecx,good +mov edx,txtlen +int 80h +mov eax,1 +mov ebx,0 +int 80h diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..c506f3b --- /dev/null +++ b/tests/test.php @@ -0,0 +1,3 @@ +