blob: 5d4b157367f6d49b3cb5b1757a59f500cd99c441 [file] [log] [blame]
Fred Drake87b8f311999-04-23 17:26:24 +00001\chapter{Restricted Execution \label{restricted}}
Guido van Rossumc3d090c1996-10-22 01:10:56 +00002
Andrew M. Kuchlingeabd9a12003-05-13 14:16:18 +00003\begin{notice}[warning]
4 In Python 2.3 these modules have been disabled due to various known
5 and not readily fixable security holes. The modules are still
6 documented here to help in reading old code that uses the
7 \module{rexec} and \module{Bastion} modules.
8\end{notice}
Guido van Rossumc3d090c1996-10-22 01:10:56 +00009
Guido van Rossumf73f79b1996-10-24 22:14:06 +000010\emph{Restricted execution} is the basic framework in Python that allows
Andrew M. Kuchlingeabd9a12003-05-13 14:16:18 +000011for the segregation of trusted and untrusted code. The framework is based on the
Guido van Rossumc3d090c1996-10-22 01:10:56 +000012notion that trusted Python code (a \emph{supervisor}) can create a
Guido van Rossumf73f79b1996-10-24 22:14:06 +000013``padded cell' (or environment) with limited permissions, and run the
Guido van Rossumc3d090c1996-10-22 01:10:56 +000014untrusted code within this cell. The untrusted code cannot break out
15of its cell, and can only interact with sensitive system resources
Guido van Rossumf73f79b1996-10-24 22:14:06 +000016through interfaces defined and managed by the trusted code. The term
17``restricted execution'' is favored over ``safe-Python''
Guido van Rossumc3d090c1996-10-22 01:10:56 +000018since true safety is hard to define, and is determined by the way the
19restricted environment is created. Note that the restricted
20environments can be nested, with inner cells creating subcells of
Guido van Rossumf73f79b1996-10-24 22:14:06 +000021lesser, but never greater, privilege.
Guido van Rossumc3d090c1996-10-22 01:10:56 +000022
23An interesting aspect of Python's restricted execution model is that
Guido van Rossumf73f79b1996-10-24 22:14:06 +000024the interfaces presented to untrusted code usually have the same names
Guido van Rossumc3d090c1996-10-22 01:10:56 +000025as those presented to trusted code. Therefore no special interfaces
26need to be learned to write code designed to run in a restricted
27environment. And because the exact nature of the padded cell is
28determined by the supervisor, different restrictions can be imposed,
29depending on the application. For example, it might be deemed
30``safe'' for untrusted code to read any file within a specified
31directory, but never to write a file. In this case, the supervisor
Fred Drake87b8f311999-04-23 17:26:24 +000032may redefine the built-in \function{open()} function so that it raises
33an exception whenever the \var{mode} parameter is \code{'w'}. It
34might also perform a \cfunction{chroot()}-like operation on the
35\var{filename} parameter, such that root is always relative to some
36safe ``sandbox'' area of the filesystem. In this case, the untrusted
37code would still see an built-in \function{open()} function in its
38environment, with the same calling interface. The semantics would be
39identical too, with \exception{IOError}s being raised when the
40supervisor determined that an unallowable parameter is being used.
Guido van Rossumc3d090c1996-10-22 01:10:56 +000041
Guido van Rossumf73f79b1996-10-24 22:14:06 +000042The Python run-time determines whether a particular code block is
43executing in restricted execution mode based on the identity of the
44\code{__builtins__} object in its global variables: if this is (the
Fred Drake87b8f311999-04-23 17:26:24 +000045dictionary of) the standard \refmodule[builtin]{__builtin__} module,
46the code is deemed to be unrestricted, else it is deemed to be
47restricted.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000048
49Python code executing in restricted mode faces a number of limitations
50that are designed to prevent it from escaping from the padded cell.
Fred Drake87b8f311999-04-23 17:26:24 +000051For instance, the function object attribute \member{func_globals} and
52the class and instance object attribute \member{__dict__} are
53unavailable.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000054
Guido van Rossumc3d090c1996-10-22 01:10:56 +000055Two modules provide the framework for setting up restricted execution
56environments:
57
Fred Drakeb91e9341998-07-23 17:59:49 +000058\localmoduletable
Fred Drake5406e701998-04-09 14:02:02 +000059
60\begin{seealso}
Fred Drakefcc16332001-10-04 20:40:07 +000061 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}
62 {Grail, an Internet browser written in Python, uses these
63 modules to support Python applets. More
64 information on the use of Python's restricted execution
65 mode in Grail is available on the Web site.}
Fred Drake5406e701998-04-09 14:02:02 +000066\end{seealso}