From: Karim Hamidou Date: Sun, 31 Dec 2017 14:25:20 +0000 (+0100) Subject: More fixes! X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=d51eab1b2e8882aa8e23e231422d91d253e4a739;p=minival.git More fixes! --- diff --git a/seccompctl b/seccompctl index 3323fb8..57433a0 100755 Binary files a/seccompctl and b/seccompctl differ diff --git a/seccompctl.c b/seccompctl.c index f01dae9..66be965 100644 --- a/seccompctl.c +++ b/seccompctl.c @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -54,7 +55,6 @@ int main(int argc, char **argv) { seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mprotect), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(munmap), 0); - seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(readlink), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 0); @@ -62,15 +62,32 @@ int main(int argc, char **argv) { seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigreturn), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(set_robust_list), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(set_tid_address), 0); - seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 0); + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(sched_getaffinity), 0); + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(clock_gettime), 0); + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getrandom), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(stat), 0); seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(sysinfo), 0); - seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0); - seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 2, - SCMP_A0(SCMP_CMP_EQ, O_RDONLY)); + // Don't let people open files in read-write mode. + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, + SCMP_A1(SCMP_CMP_EQ, O_RDONLY)); - // setup our rule + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, + SCMP_A1(SCMP_CMP_EQ, O_RDONLY|O_CLOEXEC)); + + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1, + SCMP_A1(SCMP_CMP_EQ, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)); + + // Disable sockets that aren't local + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1, + SCMP_A0(SCMP_CMP_EQ, AF_LOCAL)); + + // This is a one-off rule for perl + seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fcntl), 1, + SCMP_A2(SCMP_CMP_EQ, FD_CLOEXEC)); + + + // Limit writes to the stdin, stdout and stderr. for (int i = 0; i < 3; i++) { seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1, SCMP_A0(SCMP_CMP_EQ, i)); @@ -82,6 +99,9 @@ int main(int argc, char **argv) { if (strncmp(argv[1], "python", 16) == 0) { char *args[] = { "/usr/bin/python", argv[2], 0}; execve(args[0], (char **const) &args, NULL); + } else if (strncmp(argv[1], "perl", 16) == 0) { + char *args[] = { "/usr/bin/perl", argv[2], 0}; + execve(args[0], (char **const) &args, NULL); } return 0; diff --git a/templates/index.html b/templates/index.html index ceda4b9..809c9ee 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,29 +2,64 @@ -

-

- {{ form.hidden_tag() }} - {% for error in form.code.errors %} -

{{ error }}

- {% endfor %} -

- {{ form.code.label }}
{{ form.code(cols="80", rows="25") }} -

+ + +
+

Minival

+

Minival lets you run code snippets online. I built it to illustrate how you can use seccomp(2) to build a simple sandbox.

+
+

- Language: {{ form.language }} -

+ + {{ form.hidden_tag() }} + {% for error in form.code.errors %} +

{{ error }}

+ {% endfor %} +

+ {{ form.code.label }}

{{ form.code(cols="80", rows="25") }} +

+

+ Language: {{ form.language }} + +

+

- + Result: +

{{ result }}

- -

- Result: -

-            
-            {{ result }}
-            
-        
-

+
+ diff --git a/web.py b/web.py index 04f1f6e..906a18b 100644 --- a/web.py +++ b/web.py @@ -11,33 +11,30 @@ from wtforms.validators import DataRequired app = Flask(__name__) app.secret_key = os.environ['SECRET_KEY'] +app.config['TEMPLATES_AUTO_RELOAD'] = True def python_version(): return "Python {}".format(sys.version.split()[0]) -def ruby_version(): - return subprocess.check_output(['ruby', '--version']).capitalize() - - def perl_version(): return subprocess.check_output(['perl', '-e', 'print "Perl " . $]']) class EvalForm(FlaskForm): - code = TextAreaField('code', validators=[DataRequired()]) + code = TextAreaField('Paste your code:', validators=[DataRequired()]) language = SelectField( - 'Language', - choices=[('python', python_version()), ('ruby', ruby_version()), ('perl', perl_version())]) + 'Select the language:', + choices=[('python', python_version()), ('perl', perl_version())]) @app.route("/", methods=("GET", "POST",)) def main(): form = EvalForm() if form.validate_on_submit(): - res = execute_in_sandbox(form.code.data) - return render_template("index.html", form=form, result=res) + res = execute_in_sandbox(form.code.data, form.language.data) + return render_template("index.html", code=form.code.data, form=form, result=res) return render_template("index.html", form=form) @@ -59,6 +56,7 @@ def execute_in_sandbox(code, language='python'): cmd = "seccompctl" os.dup2(write_pipe, sys.stdout.fileno()) + os.dup2(write_pipe, sys.stderr.fileno()) os.execv(cmd, [cmd, language, tmp_filename]) else: os.close(write_pipe)