Ethan
Ethan

Reputation: 255

Is it possible to use numpy in a pypy sandbox and write out to files?

I'd like to be able to run untrusted python scripts to generate and save images using numpy and matplotlib. Is that possible?

So:

  1. Can I import numpy and matplotlib when running in a pypy sandbox? I tried copying the numpy site-packages directory into my temp sandbox directory, but when I try to "import numpy" I get this error: "ImportError: No module named _numpypy".

  2. Is it possible to write out to a file from a pypy sandbox? I think the file must be inside the pypy sandbox temp directory, but even then I get an IOError: "IOError: [Errno 2] No such file or directory: '/tmp/out.txt'".

Here's the simple script I'm trying to run in the sandbox, untrusted.py:

# This import will fail.
import numpy

# This works fine.
print 'SOMETHING'

# This works fine.
with open('/tmp/in.txt', 'r') as i:
    print i.read()

# This throws an IOError.
with open('/tmp/out.txt', 'w') as f:
    f.write('TESTING')

Here's the command I'm using to run it:

./pypy/sandbox/pypy_interact.py --tmp=mydir ./pypy/goal/pypy-c untrusted.py

More info about pypy sandboxing here: http://doc.pypy.org/en/latest/sandbox.html

Thanks a lot!

Upvotes: 3

Views: 1090

Answers (1)

Armin Rigo
Armin Rigo

Reputation: 12900

  1. The _numpy module is not included by default with the sandboxed version, because nobody carefully checked that it is safe to include it. Roughly speaking it should be, but there are many details that can go wrong: for example, maybe some obscure sequence of calls can lead to an out-of-bound memory access (contrast this with the sandboxing approach taken on the rest of PyPy, which works out of the box for all internal object manipulations, because they are done on GC-managed arrays rather than on raw malloc()ed memory). There is also the risk that some obscure function implemented in a C library used by _numpy would offer an escape point to an attacker. All in all I'd say that the approach does not work for _numpy (unless someone paranoid enough would really check its safety after every _numpy change, which is very unlikely to occur). Consider using an OS-level sandboxing instead, with either a regular PyPy or CPython.

  2. File writes are one example of the big warning in the docs that says: "The hard work from the PyPy side is done — you get a fully secure version. What is only experimental and unpolished is the library to use this sandboxed PyPy from a regular Python interpreter (CPython, or an unsandboxed PyPy). Contributions welcome."

Upvotes: 3

Related Questions