Preventing Python "Sandbox" Escape?
I'm using python's exec(code, globals, locals)
I disable __builtins__ so no imports, exec, eval, open, etc. inside that context
but it seems you can still always do object.__subclasses__() and find every system method (eg. open())
it can't be overwritten but looking at the interpreter code is seems like it's possible to hack a workaround for this specific case.
are there other known ways to escape exec()?
14 comments
[ 5.7 ms ] story [ 70.1 ms ] threadIf you set up a new runtime environment, like a FreeBSD jail, with no access to anything and a short CPU limit, then start you could start a Python subprocess in that environment, where the only thing that gets out is data via a pipe to call you names.
An operating system like FreeBSD is built to run code in a restricted manner.
ps. browsers basically do that with javascript
for me I just want to hijack the interpreter so I don't have to write my own. no imports, no sockets.
I'm talking about the history beyond why rexec and Bastion, and restricted execution, were removed from Python in the 2.x days. See https://python.readthedocs.io/en/v2.7.2/library/restricted.h... , "In Python 2.3 these modules have been disabled due to various known and not readily fixable security holes."
They started because back in the 1.x days there was a Python web browser called Grail, and the hope was to support restricted Python applets in Grail.
Or from 10 years ago, read https://lwn.net/Articles/574215/ about the failure of 'pysandbox' where one of the ways to break out was to "[use] a traceback object to unwind the stack frame to one in the trusted namespace, then use the f_globals attribute to retrieve a global object." ... "Stinner's tale should serve as a cautionary one to anyone considering a CPython-based solution".
You might consider RestrictedPython at https://restrictedpython.readthedocs.io/en/latest/ which supports only a subset of Python, via AST-walking to limit what the code can do. I have no experience with it.
edit: here are my silly little patches: https://github.com/hananbeer/cpython-toy-sandbox/commit/fa3f...
this is of course assuming exec(globals={..}) without certain builtins and is, again, not expected to use system apis like files or sockets or anything.
If you allow any mutable object into the globals or locals dictionary, such that the exec'ed code can attach something to it, then you can't even use gc.collect() to ensure the exec'ed code can no longer be executed.
It's the curse of any sufficiently useful language. Well, maybe not Lua, but that was specifically designed for embedding. Java also began with that intention back when applets were ahead of their time, though IIRC secure sandboxing is no longer really a feature.
for me I just want to hijack the interpreter so I don't have to write my own. no imports, no sockets.
https://discuss.python.org/t/extending-subinterpreters-with-...
https://stackoverflow.com/questions/3068139
https://wiki.python.org/moin/SandboxedPython
https://github.com/jailctf/pyjailbreaker
https://healeycodes.com/running-untrusted-python-code
https://lwn.net/Articles/574215/