1 2.. _restricted: 3 4******************** 5Restricted Execution 6******************** 7 8.. warning:: 9 10 In Python 2.3 these modules have been disabled due to various known and not 11 readily fixable security holes. The modules are still documented here to help 12 in reading old code that uses the :mod:`rexec` and :mod:`Bastion` modules. 13 14*Restricted execution* is the basic framework in Python that allows for the 15segregation of trusted and untrusted code. The framework is based on the notion 16that trusted Python code (a *supervisor*) can create a "padded cell' (or 17environment) with limited permissions, and run the untrusted code within this 18cell. The untrusted code cannot break out of its cell, and can only interact 19with sensitive system resources through interfaces defined and managed by the 20trusted code. The term "restricted execution" is favored over "safe-Python" 21since true safety is hard to define, and is determined by the way the restricted 22environment is created. Note that the restricted environments can be nested, 23with inner cells creating subcells of lesser, but never greater, privilege. 24 25An interesting aspect of Python's restricted execution model is that the 26interfaces presented to untrusted code usually have the same names as those 27presented to trusted code. Therefore no special interfaces need to be learned 28to write code designed to run in a restricted environment. And because the 29exact nature of the padded cell is determined by the supervisor, different 30restrictions can be imposed, depending on the application. For example, it 31might be deemed "safe" for untrusted code to read any file within a specified 32directory, but never to write a file. In this case, the supervisor may redefine 33the built-in :func:`open` function so that it raises an exception whenever the 34*mode* parameter is ``'w'``. It might also perform a :c:func:`chroot`\ -like 35operation on the *filename* parameter, such that root is always relative to some 36safe "sandbox" area of the filesystem. In this case, the untrusted code would 37still see a built-in :func:`open` function in its environment, with the same 38calling interface. The semantics would be identical too, with :exc:`IOError`\ s 39being raised when the supervisor determined that an unallowable parameter is 40being used. 41 42The Python run-time determines whether a particular code block is executing in 43restricted execution mode based on the identity of the ``__builtins__`` object 44in its global variables: if this is (the dictionary of) the standard 45:mod:`__builtin__` module, the code is deemed to be unrestricted, else it is 46deemed to be restricted. 47 48Python code executing in restricted mode faces a number of limitations that are 49designed to prevent it from escaping from the padded cell. For instance, the 50function object attribute :attr:`func_globals` and the class and instance object 51attribute :attr:`~object.__dict__` are unavailable. 52 53Two modules provide the framework for setting up restricted execution 54environments: 55 56 57.. toctree:: 58 59 rexec.rst 60 bastion.rst 61 62.. seealso:: 63 64 `Grail Home Page <http://grail.sourceforge.net/>`_ 65 Grail, an Internet browser written in Python, uses these modules to support 66 Python applets. More information on the use of Python's restricted execution 67 mode in Grail is available on the Web site. 68 69