blob: 9786cf034b09e2c0b56d301e5593d53528f3a50a [file] [log] [blame]
Guido van Rossumc3d090c1996-10-22 01:10:56 +00001\chapter{Restricted Execution}
2
Guido van Rossumf73f79b1996-10-24 22:14:06 +00003In general, Python programs have complete access to the underlying
4operating system throug the various functions and classes, For
5example, a Python program can open any file for reading and writing by
6using the \code{open()} built-in function (provided the underlying OS
7gives you permission!). This is exactly what you want for most
8applications.
Guido van Rossumc3d090c1996-10-22 01:10:56 +00009
Guido van Rossumf73f79b1996-10-24 22:14:06 +000010There exists a class of applications for which this ``openness'' is
11inappropriate. Take Grail: a web browser that accepts ``applets'',
12snippets of Python code, from anywhere on the Internet for execution
13on the local system. This can be used to improve the user interface
14of forms, for instance. Since the originator of the code is unknown,
15it is obvious that it cannot be trusted with the full resources of the
16local machine.
Guido van Rossumc3d090c1996-10-22 01:10:56 +000017
Guido van Rossumf73f79b1996-10-24 22:14:06 +000018\emph{Restricted execution} is the basic framework in Python that allows
Guido van Rossumc3d090c1996-10-22 01:10:56 +000019for the segregation of trusted and untrusted code. It is based on the
20notion that trusted Python code (a \emph{supervisor}) can create a
Guido van Rossumf73f79b1996-10-24 22:14:06 +000021``padded cell' (or environment) with limited permissions, and run the
Guido van Rossumc3d090c1996-10-22 01:10:56 +000022untrusted code within this cell. The untrusted code cannot break out
23of its cell, and can only interact with sensitive system resources
Guido van Rossumf73f79b1996-10-24 22:14:06 +000024through interfaces defined and managed by the trusted code. The term
25``restricted execution'' is favored over ``safe-Python''
Guido van Rossumc3d090c1996-10-22 01:10:56 +000026since true safety is hard to define, and is determined by the way the
27restricted environment is created. Note that the restricted
28environments can be nested, with inner cells creating subcells of
Guido van Rossumf73f79b1996-10-24 22:14:06 +000029lesser, but never greater, privilege.
Guido van Rossumc3d090c1996-10-22 01:10:56 +000030
31An interesting aspect of Python's restricted execution model is that
Guido van Rossumf73f79b1996-10-24 22:14:06 +000032the interfaces presented to untrusted code usually have the same names
Guido van Rossumc3d090c1996-10-22 01:10:56 +000033as those presented to trusted code. Therefore no special interfaces
34need to be learned to write code designed to run in a restricted
35environment. And because the exact nature of the padded cell is
36determined by the supervisor, different restrictions can be imposed,
37depending on the application. For example, it might be deemed
38``safe'' for untrusted code to read any file within a specified
39directory, but never to write a file. In this case, the supervisor
40may redefine the built-in
41\code{open()} function so that it raises an exception whenever the
42\var{mode} parameter is \code{'w'}. It might also perform a
43\code{chroot()}-like operation on the \var{filename} parameter, such
44that root is always relative to some safe ``sandbox'' area of the
45filesystem. In this case, the untrusted code would still see an
Guido van Rossumf73f79b1996-10-24 22:14:06 +000046built-in \code{open()} function in its environment, with the same
Guido van Rossumc3d090c1996-10-22 01:10:56 +000047calling interface. The semantics would be identical too, with
48\code{IOError}s being raised when the supervisor determined that an
49unallowable parameter is being used.
50
Guido van Rossumf73f79b1996-10-24 22:14:06 +000051The Python run-time determines whether a particular code block is
52executing in restricted execution mode based on the identity of the
53\code{__builtins__} object in its global variables: if this is (the
54dictionary of) the standard \code{__builtin__} module, the code is
55deemed to be unrestricted, else it is deemed to be restricted.
56
57Python code executing in restricted mode faces a number of limitations
58that are designed to prevent it from escaping from the padded cell.
59For instance, the function object attribute \code{func_globals} and the
60class and instance object attribute \code{__dict__} are unavailable.
61
Guido van Rossumc3d090c1996-10-22 01:10:56 +000062Two modules provide the framework for setting up restricted execution
63environments:
64
65\begin{description}
66
67\item[rexec]
68--- Basic restricted execution framework.
69
70\item[Bastion]
71--- Providing restricted access to objects.
72
73\end{description}