Sandboxing Python code is notoriously difficult due to the power of the reflection facilities built into the language. At a minimum one has to take away the import
mechanism and most of the built-in functions and global variables, and even then there are holes ({}.__class__.__base__.__subclasses__()
, for instance).
In both Python 2 and 3, the 'sys' module is built into the interpreter and preloaded before user code begins to execute (even in -S
mode). If you can get a handle to the sys
module, then you have access to the global list of loaded modules (sys.modules
) which enables you to do all sorts of naughty things.
So, the question: Starting from an empty module, without using the import machinery at all (no import
statement, no __import__
, no imp
library, etc), and also without using anything normally found in __builtins__
unless you can get a handle to it some other way, is it possible to acquire a reference to either sys
or sys.modules
? (Each points to the other.) Am interested in both 2.x and 3.x answers.