]> khamidou — GitHub mirrors - minival.git/commitdiff
Better comments
authorKarim Hamidou <khamidou@gmail.com>
Mon, 1 Jan 2018 11:27:48 +0000 (12:27 +0100)
committerKarim Hamidou <khamidou@gmail.com>
Mon, 1 Jan 2018 11:27:48 +0000 (12:27 +0100)
seccompctl
seccompctl.c

index 57433a0f6934c223344f771e0cf23d158e246ef9..900effd763cc625e552b2e7eca9774aefd255060 100755 (executable)
Binary files a/seccompctl and b/seccompctl differ
index 90d3cde9c1b7992744b07c1167bc32f570dd6831..626d7b5574c8d5e0a223d35bdeb5ed2153fe752e 100644 (file)
@@ -3,7 +3,7 @@
 
 // I've always found the idea of sandboxes fascinating, so I tried to figure out how it's built. After some reading, I ended up building my own sandbox system, [Minival](https://seccomp-eval.herokuapp.com/).
 //
-// What you are reading right now is the commented source for this sandbox, which also serve as a tutorial of sorts about sandboxing. 
+// What you are reading right now is the commented source for this sandbox, which also serve as a tutorial of sorts about sandboxing.
 //
 // You shouldn't need to have more than some passing knowledge of C and UNIX syscalls to understand this tutorial, so let's jump right in!
 //
 
 // Ptrace seems fine – I heard that that's what [eval.in](https://eval.in) uses - but it would be a little annoying to implement in Python. It also has a non-negligible performance hit.
 
-// Seccomp seemed a bit better at the time, even though it didn't really have a good Python interface, which caused me to end up writing everything in C.
+// Seccomp seemed a bit better at the time, even though it didn't really have a good Python interface, so I ended up writing everything in C.
+//
+// ## How minival works
+//
+// Minival is a pretty straightforward Python webapp – people type code into a form, select a runtime then submit it. Behind the scenes, the Python app spawns a helper process that does all the sandboxing setup, and then runs the untrusted code:
+//
+// ![How minival is structured](http://khamidou.com/images/sandboxing/layout.png)
 
+// The helper will setup some helpful limits and then call execve(2) to run a Python or PERL process. Once the limits are set, the 3rd-party code is effectively restricted to the whitelist of syscalls we defined.
 
-//
+
+// Let's start by including a bunch of UNIX includes
 #include <stdio.h>
 #include <stdlib.h>
 
 
 int main(int argc, char **argv) {
     if (argc != 3) {
-        printf("usage: seccompctl <python|perl|ruby> source_file\n");
+        printf("usage: seccompctl <python|perl> source_file\n");
         exit(-1);
     }
 
-    // First, setup an rlimit limit.
+    // First, we have to setup some CPU and memory limits. We use the setrlimit(2) syscall
+    // for that. We have to do this before setting up the sandbox because we do not want
+    // people to be able to change them after that the sandbox is running.
+
+    // Limit the maximum CPU time of the process to 10 seconds.
     struct rlimit rl;
     rl.rlim_cur = 10;
-    setrlimit (RLIMIT_CPU, &rl);
+    setrlimit(RLIMIT_CPU, &rl);
+
+    // Let's also limit stack and heap usage to 16MB.
+    getrlimit(RLIMIT_STACK, &rl);
 
-    // Init the filter
+    rl.rlim_max = 16*1024*1024;
+    setrlimit(RLIMIT_STACK, &rl);
+    setrlimit(RLIMIT_DATA, &rl);
+
+    // Setup seccomp. SCMP_ACT_KILL tells the kernel to kill processes when they make a
+    // forbidden syscall.
     scmp_filter_ctx ctx;
-    ctx = seccomp_init(SCMP_ACT_KILL); // default action: kill
+    ctx = seccomp_init(SCMP_ACT_KILL);
 
-    // setup basic whitelist
+    // Let's whitelist a bunch of syscalls. We do this by calling
+    // `seccomp_rule_add` with some parameters to match syscalls.
+    //
+    // Note that I got this list mostly by trial and error; I had to run strace
+    // on the python interpreter to see which syscalls it was making.
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(access), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(arch_prctl), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
+    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(clock_gettime), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(connect), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(execve), 0);
@@ -78,6 +103,7 @@ int main(int argc, char **argv) {
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(geteuid), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getgid), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getpid), 0);
+    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getrandom), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getrlimit), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getuid), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 0);
@@ -91,15 +117,17 @@ int main(int argc, char **argv) {
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigaction), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigprocmask), 0);
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigreturn), 0);
+    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(sched_getaffinity), 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(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);
 
-    // Don't let people open files in read-write mode.
+    // Astute readers may have noted that we haven't whitelisted the open(2)
+    // syscall yet. This is because we want to prevent people from creating files.
+    // To do that, we'll only whitelist and handful of parameters: O_RDONLY, O_CLOEXEC,
+    // O_NONBLOCK, O_DIRECTORY. Because the parameters are ORed together, we have to
+    // do this on three separate lines.
     seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
             SCMP_A1(SCMP_CMP_EQ, O_RDONLY));
 
@@ -109,24 +137,31 @@ int main(int argc, char **argv) {
     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
+    // Python also tries to make so local socket connections when starting –
+    // so let's open up sockets but only if they're 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
+    // Another one-off rule – PERL needs to use fnctl for some reason, so let's
+    // open it up.
     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.
+    // We also need to prevent people from writing to any files on the file system
+    // besides stdin, stdout and stderr. To do that, we let people write to
+    // file descriptors 0, 1 and 2, since on UNIX they're the default values for stdin, stdout and sterr.
     for (int i = 0; i < 3; i++) {
         seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
                                   SCMP_A0(SCMP_CMP_EQ, i));
     }
 
-    // build and load the filter
-    seccomp_load(ctx);
+    // Now, let's load the filter!
+    if (seccomp_load(ctx) != 0) {
+        printf("Couldn't load seccomp filter! Exiting!");
+        exit(-1);
+    }
 
+    // We can now let the Python or PERL interpreters run the program safely!
     if (strncmp(argv[1], "python", 16) == 0) {
         char *args[] = { "/usr/bin/python", argv[2], 0};
         execve(args[0], (char **const) &args, NULL);