From: Karim Hamidou Date: Sun, 31 Dec 2017 18:47:58 +0000 (+0100) Subject: More docs X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=806c6c3e1ed98727be27ef3a62696b8a368901e6;p=minival.git More docs --- diff --git a/seccompctl.c b/seccompctl.c index 66be965..90d3cde 100644 --- a/seccompctl.c +++ b/seccompctl.c @@ -1,20 +1,51 @@ -#define _GNU_SOURCE - +// [eval.in](http://eval.in) is a really cool website that lets you run +// source code under a variety of interpreters. + +// 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. +// +// 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! +// +// ## What's a sandbox? +// +// Simply put, a sandbox is a restricted environment that lets you run 3rd-party code without any risks. For example, browsing the web usually doesn't crash you computer or let hackers steal your personal data. That's because your browser runs Javascript code in a sandbox, to prevents it from doing dangerous things (mostly). +// +// What we're going to build here a similar, but simpler type of sandbox. We want to let people run simply Python and Perl scripts on our server. +// Because some people have a tendency to break things, we need to limit users in what they can do. Basically, we want to let people run simple scripts that will write to stdout and that's about it. To do that there's a couple of ways we could go with: +// 1. we could use a language feature to limit what 3rd-party code can do ([rpython](https://docs.python.org/2/library/restricted.html) or [Safe PERL](http://perldoc.perl.org/Safe.html) are examples of this). +// 2. we could ask the operating system to restrict what the program can do. It's hard to break out of a sandbox if the OS kills the process whenever it tries to do an I/O operation. +// 3. we could do some complicated static analysis of the code. Google's [NaCL](https://media.blackhat.com/bh-us-12/Briefings/Rohlf/BH_US_12_Rohlf_Google_Native_Client_WP.pdf) uses this approach. + +// 1/ is hard to implement because we'd have to think through every possible way people could use Python to break out of the sandbox. +// 3/ would require an expertise about assembly and binary that I don't have. +// +// That leaves us with 2/ – using OS primitives to somehow constrain processes. + +// After reading a lot of Stackoverflow questions, I found out that there really are two ways to limit processes: +// 1. use ptrace(2), a debugging interface that lets you [peek around a process](http://man7.org/linux/man-pages/man2/ptrace.2.html) +// 2. use seccomp(2), a Linux-only system call that lets a process define a whitelist of system calls it's allowed to make. + +// 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. + + +// #include #include -#include #include #include #include #include #include +#include +#include +#include #include #include #include -#include -#include - int main(int argc, char **argv) { diff --git a/templates/index.html b/templates/index.html index 809c9ee..2d8f120 100644 --- a/templates/index.html +++ b/templates/index.html @@ -36,7 +36,7 @@

Minival

-

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

+

Minival lets you run code snippets online. It's an example of how you can use seccomp(2) to build a simple sandbox.
You can find the full tutorial here.