]> khamidou — GitHub mirrors - minival.git/commitdiff
More fixes!
authorKarim Hamidou <khamidou@gmail.com>
Sun, 31 Dec 2017 14:25:20 +0000 (15:25 +0100)
committerKarim Hamidou <khamidou@gmail.com>
Sun, 31 Dec 2017 14:25:20 +0000 (15:25 +0100)
seccompctl
seccompctl.c
templates/index.html
web.py

index 3323fb86299d63239b1b6bb7528569c4f76d9aad..57433a0f6934c223344f771e0cf23d158e246ef9 100755 (executable)
Binary files a/seccompctl and b/seccompctl differ
index f01dae97adde6ddbdce4773bf77ebcbb4ca9efc8..66be9650e35fc125e7d8dad723839911687b5323 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <sys/socket.h>
 #include <fcntl.h>
 #include <seccomp.h>
 #include <signal.h>
@@ -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;
index ceda4b9c7a83326096544fa8c695b4d5c979a23f..809c9ee893d3f85109efede46eca996607fae60f 100644 (file)
@@ -2,29 +2,64 @@
 
 <html>
     <body>
-        <p>
-        <form method="POST">
-            {{ form.hidden_tag() }}
-            {% for error in form.code.errors %}
-            <p>{{ error }}</p>
-            {% endfor %}
-            <p>
-            <b>{{ form.code.label }}</b><br /> {{ form.code(cols="80", rows="25") }}
-            </p>
+        <style>
+            html, body { margin: 0; padding: 0 }
+
+            body {
+                font-family: Helvetica, Arial, Lucida, Verdana, sans-serif;
+            }
+
+            .header {
+                margin: 0;
+                padding: 20px;
+                padding-left: 40px;
+                color: maroon;
+                background-color: linen;
+            }
+
+            .content {
+                margin-left: 40px;
+            }
+
+            textarea {
+                font-family: Monaco, Courier;
+            }
+
+            #footer {
+                color: maroon;
+                background-color: linen;
+                padding: 20px;
+                padding-left: 40px;
+            }
+
+        </style>
+
+        <div class="header">
+            <h1>Minival</h1>
+            <p>Minival lets you run code snippets online. I built it to illustrate how you can use seccomp(2) to build a simple sandbox.</p>
+        </div>
+        <div class="content">
             <p>
-            <b>Language:</b> {{ form.language }}
-            </p>
+            <form method="POST">
+                {{ form.hidden_tag() }}
+                {% for error in form.code.errors %}
+                <p>{{ error }}</p>
+                {% endfor %}
+                <p>
+                <b>{{ form.code.label }}</b><br /> <br /> {{ form.code(cols="80", rows="25") }}
+                </p>
+                <p>
+                    <b>Language:</b> {{ form.language }}
+                    <input type="submit" value="Run!">
+                </p>
+            </form>
             <p>
-            <input type="submit" value="Submit">
+            <b>Result:</b>
+            <pre>{{ result }}</pre>
             </p>
-        </form>
-        <p>
-        <b>Result:</b>
-        <pre>
-            <code>
-            {{ result }}
-            </code>
-        </pre>
-        </p>
+        </div>
+        <div id="footer">
+            Written by <a href="http://khamidou.com">Karim Hamidou</a>.
+        </div>
     </body>
 </html>
diff --git a/web.py b/web.py
index 04f1f6e6becb962ddf7cbcdf829022cbcffa61d3..906a18bee3aafaffe4cbf5c262b39e65336fef7d 100644 (file)
--- 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)