[Python-Dev] Sandboxing Python
victor.stinner at gmail.com
Thu Mar 1 19:23:47 CET 2012
The frozendict discussion switched somewhere to sandboxing, and so I
prefer to start a new thread.
There are various ways to implement a sandbox, but I would like to
expose here how I implemented pysandbox to have your opinion.
pysandbox is written to execute quickly a short untrusted function in
a sandbox and then continue the normal execution of the program. It is
possible to "enable" the sandbox, but also later to "disable" it. It
is written for Python using only one thread and one process.
To create a sandbox, pysandbox uses various protections. The main idea
is to create an empty namespace and ensure that it is not possible to
use objects added into the namespace for escaping from the sandbox.
pysandbox only uses one thread and one process and so it doesn't
replace the existing trusted namespace, but create a new one. The
security of pysandbox depends on the sandbox namespace sealing.
I don't want to integrate pysandbox in CPython because I am not yet
conviced that the approach is secure by design. I am trying to patch
Python to help the implementation of Python security modules and of
You can find below the list of protections implemented in pysandbox.
Some of them are implemented in C.
I challenge anymore to break pysandbox! I would be happy if anyone
breaks it because it would make it more stronger.
* Make builtins read only
* Remove function attribute:
* Workaround the lack of frozendict, remove dict attributes:
* Create a proxy for objects injected to the sandbox namespace and for
the result of functions (the result of callable objects is also proxified)
Remove all builtin symbols not in the whitelist.
* Replace __import__ function to use an import whitelist
* Replace open and file functions to deny access to the filesystem
* Replace exit function
* Remove SystemExit builtin exception
* Replace sys.stdin, sys.stdout and sys.stderr
Execute arbitrary bytecode may crash Python, or lead to execution of arbitrary
* Patch code.__new__()
* Remove attributes:
More information about the Python-Dev