| 1 |
.. _sandbox: |
| 2 |
|
| 3 |
PyPy's sandboxing features |
| 4 |
========================== |
| 5 |
|
| 6 |
.. warning:: This describes the old, unmaintained version. A new version |
| 7 |
is in progress in the ``sandbox-2`` and ``py3.6-sandbox-2`` branches and in |
| 8 |
the sandboxlib_ repo. |
| 9 |
Please see its description here: |
| 10 |
https://mail.python.org/pipermail/pypy-dev/2019-August/015797.html |
| 11 |
|
| 12 |
Also note that python 3.7+ requires the _thread module, which may be |
| 13 |
a consideration in escaping the sandbox. |
| 14 |
|
| 15 |
Introduction |
| 16 |
------------ |
| 17 |
|
| 18 |
PyPy offers sandboxing at a level similar to OS-level sandboxing (e.g. |
| 19 |
SECCOMP_ on Linux), but implemented in a fully portable way. To use it, |
| 20 |
a (regular, trusted) program launches a subprocess that is a special |
| 21 |
sandboxed version of PyPy. This subprocess can run arbitrary untrusted |
| 22 |
Python code, but all its input/output is serialized to a stdin/stdout |
| 23 |
pipe instead of being directly performed. The outer process reads the |
| 24 |
pipe and decides which commands are allowed or not (sandboxing), or even |
| 25 |
reinterprets them differently (virtualization). A potential attacker |
| 26 |
can have arbitrary code run in the subprocess, but cannot actually do |
| 27 |
any input/output not controlled by the outer process. Additional |
| 28 |
barriers are put to limit the amount of RAM and CPU time used. |
| 29 |
|
| 30 |
Note that this is very different from sandboxing at the Python language |
| 31 |
level, i.e. placing restrictions on what kind of Python code the |
| 32 |
attacker is allowed to run (why? read about pysandbox_). |
| 33 |
|
| 34 |
.. _SECCOMP: https://code.google.com/p/seccompsandbox/wiki/overview |
| 35 |
.. _pysandbox: https://mail.python.org/pipermail/python-dev/2013-November/130132.html |
| 36 |
.. _sandboxlib: https://foss.heptapod.net/pypy/sandboxlib |
| 37 |
|
| 38 |
Another point of comparison: if we were instead to try to plug CPython |
| 39 |
into a special virtualizing C library, we would get a result |
| 40 |
that is not only OS-specific, but unsafe, because CPython can be |
| 41 |
segfaulted (in many ways, all of them really, really obscure). |
| 42 |
Given enough efforts, an attacker can turn almost any |
| 43 |
segfault into a vulnerability. The C code generated by |
| 44 |
PyPy is not segfaultable, as long as our code generators are correct - |
| 45 |
that's a lower number of lines of code to trust. For the paranoid, |
| 46 |
PyPy translated with sandboxing also contains systematic run-time |
| 47 |
checks (against buffer overflows for example) |
| 48 |
that are normally only present in debugging versions. |
| 49 |
|
| 50 |
.. warning:: |
| 51 |
|
| 52 |
The hard work from the PyPy side is done --- you get a fully secure |
| 53 |
version. What is only experimental and unpolished is the library to |
| 54 |
use this sandboxed PyPy from a regular Python interpreter (CPython, or |
| 55 |
an unsandboxed PyPy). Contributions welcome. |
| 56 |
|
| 57 |
.. warning:: |
| 58 |
|
| 59 |
Tested with PyPy2. May not work out of the box with PyPy3. |
| 60 |
|
| 61 |
|
| 62 |
Overview |
| 63 |
-------- |
| 64 |
|
| 65 |
One of PyPy's translation aspects is a sandboxing feature. It's "sandboxing" as |
| 66 |
in "full virtualization", but done in normal C with no OS support at all. It's |
| 67 |
a two-processes model: we can translate PyPy to a special "pypy-c-sandbox" |
| 68 |
executable, which is safe in the sense that it doesn't do any library or |
| 69 |
system calls - instead, whenever it would like to perform such an operation, it |
| 70 |
marshals the operation name and the arguments to its stdout and it waits for |
| 71 |
the marshalled result on its stdin. This pypy-c-sandbox process is meant to be |
| 72 |
run by an outer "controller" program that answers these operation requests. |
| 73 |
|
| 74 |
The pypy-c-sandbox program is obtained by adding a transformation during |
| 75 |
translation, which turns all RPython-level external function calls into |
| 76 |
stubs that do the marshalling/waiting/unmarshalling. An attacker that |
| 77 |
tries to escape the sandbox is stuck within a C program that contains no |
| 78 |
external function calls at all except for writing to stdout and reading from |
| 79 |
stdin. (It's still attackable in theory, e.g. by exploiting segfault-like |
| 80 |
situations, but as explained in the introduction we think that PyPy is |
| 81 |
rather safe against such attacks.) |
| 82 |
|
| 83 |
The outer controller is a plain Python program that can run in CPython |
| 84 |
or a regular PyPy. It can perform any virtualization it likes, by |
| 85 |
giving the subprocess any custom view on its world. For example, while |
| 86 |
the subprocess thinks it's using file handles, in reality the numbers |
| 87 |
are created by the controller process and so they need not be (and |
| 88 |
probably should not be) real OS-level file handles at all. In the demo |
| 89 |
controller I've implemented there is simply a mapping from numbers to |
| 90 |
file-like objects. The controller answers to the "os_open" operation by |
| 91 |
translating the requested path to some file or file-like object in some |
| 92 |
virtual and completely custom directory hierarchy. The file-like object |
| 93 |
is put in the mapping with any unused number >= 3 as a key, and the |
| 94 |
latter is returned to the subprocess. The "os_read" operation works by |
| 95 |
mapping the pseudo file handle given by the subprocess back to a |
| 96 |
file-like object in the controller, and reading from the file-like |
| 97 |
object. |
| 98 |
|
| 99 |
Translating an RPython program with sandboxing enabled also uses a special flag |
| 100 |
that enables all sorts of C-level assertions against index-out-of-bounds |
| 101 |
accesses. |
| 102 |
|
| 103 |
By the way, as you should have realized, it's really independent from |
| 104 |
the fact that it's PyPy that we are translating. Any RPython program |
| 105 |
should do. I've successfully tried it on the JS interpreter. The |
| 106 |
controller is only called "pypy_interact" because it emulates a file |
| 107 |
hierarchy that makes pypy-c-sandbox happy - it contains (read-only) |
| 108 |
virtual directories like /bin/lib/pypy1.2/lib-python and |
| 109 |
/bin/lib/pypy1.2/lib_pypy and it |
| 110 |
pretends that the executable is /bin/pypy-c. |
| 111 |
|
| 112 |
|
| 113 |
Howto |
| 114 |
----- |
| 115 |
|
| 116 |
Grab a copy of the pypy repository_. In the directory pypy/goal, run:: |
| 117 |
|
| 118 |
../../rpython/bin/rpython -O2 --sandbox targetpypystandalone.py |
| 119 |
|
| 120 |
If you don't have a regular PyPy installed, you should, because it's |
| 121 |
faster to translate; but you can also run the same line with ``python`` |
| 122 |
in front. |
| 123 |
|
| 124 |
.. _repository: https://github.com/pypy/pypy |
| 125 |
|
| 126 |
|
| 127 |
To run it, use the tools in the pypy/sandbox directory:: |
| 128 |
|
| 129 |
./pypy_interact.py /some/path/pypy-c-sandbox [args...] |
| 130 |
|
| 131 |
Just like with pypy-c, if you pass no argument you get the interactive |
| 132 |
prompt. In theory it's impossible to do anything bad or read a random |
| 133 |
file on the machine from this prompt. To pass a script as an argument you need |
| 134 |
to put it in a directory along with all its dependencies, and ask |
| 135 |
pypy_interact to export this directory (read-only) to the subprocess' |
| 136 |
virtual /tmp directory with the ``--tmp=DIR`` option. Example:: |
| 137 |
|
| 138 |
mkdir myexported |
| 139 |
cp script.py myexported/ |
| 140 |
./pypy_interact.py --tmp=myexported /some/path/pypy-c-sandbox /tmp/script.py |
| 141 |
|
| 142 |
This is safe to do even if script.py comes from some random |
| 143 |
untrusted source, e.g. if it is done by an HTTP server. |
| 144 |
|
| 145 |
To limit the used heapsize, use the ``--heapsize=N`` option to |
| 146 |
pypy_interact.py. You can also give a limit to the CPU time (real time) by |
| 147 |
using the ``--timeout=N`` option. |
| 148 |
|
| 149 |
Not all operations are supported; e.g. if you type os.readlink('...'), |
| 150 |
the controller crashes with an exception and the subprocess is killed. |
| 151 |
Other operations make the subprocess die directly with a "Fatal RPython |
| 152 |
error". None of this is a security hole. More importantly, *most other |
| 153 |
built-in modules are not enabled. Please read all the warnings in this |
| 154 |
page before complaining about this. Contributions welcome.* |