blob: 20c76e5f20a02c566ec5b01055c30064b9d8971c [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
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
Fred Drake87b8f311999-04-23 17:26:24 +00006using the \function{open()} built-in function (provided the underlying
Fred Drake8ee679f2001-07-14 02:50:55 +00007operating system gives you permission!). This is exactly what you want
8for most applications.
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
Fred Drake8ee679f2001-07-14 02:50:55 +000011inappropriate. Take Grail: a Web browser that accepts ``applets,''
Guido van Rossumf73f79b1996-10-24 22:14:06 +000012snippets 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
Fred Drake87b8f311999-04-23 17:26:24 +000040may redefine the built-in \function{open()} function so that it raises
41an exception whenever the \var{mode} parameter is \code{'w'}. It
42might also perform a \cfunction{chroot()}-like operation on the
43\var{filename} parameter, such that root is always relative to some
44safe ``sandbox'' area of the filesystem. In this case, the untrusted
45code would still see an built-in \function{open()} function in its
46environment, with the same calling interface. The semantics would be
47identical too, with \exception{IOError}s being raised when the
48supervisor determined that an unallowable parameter is being used.
Guido van Rossumc3d090c1996-10-22 01:10:56 +000049
Guido van Rossumf73f79b1996-10-24 22:14:06 +000050The Python run-time determines whether a particular code block is
51executing in restricted execution mode based on the identity of the
52\code{__builtins__} object in its global variables: if this is (the
Fred Drake87b8f311999-04-23 17:26:24 +000053dictionary of) the standard \refmodule[builtin]{__builtin__} module,
54the code is deemed to be unrestricted, else it is deemed to be
55restricted.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000056
57Python code executing in restricted mode faces a number of limitations
58that are designed to prevent it from escaping from the padded cell.
Fred Drake87b8f311999-04-23 17:26:24 +000059For instance, the function object attribute \member{func_globals} and
60the class and instance object attribute \member{__dict__} are
61unavailable.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000062
Guido van Rossumc3d090c1996-10-22 01:10:56 +000063Two modules provide the framework for setting up restricted execution
64environments:
65
Fred Drakeb91e9341998-07-23 17:59:49 +000066\localmoduletable
Fred Drake5406e701998-04-09 14:02:02 +000067
68\begin{seealso}
Fred Drakefcc16332001-10-04 20:40:07 +000069 \seetitle[http://www.python.org/doc/howto/rexec/]
70 {Restricted Execution HOWTO}
71 {Andrew Kuchling's tutorial on the use of the restricted
72 execution facilities in Python.}
73 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}
74 {Grail, an Internet browser written in Python, uses these
75 modules to support Python applets. More
76 information on the use of Python's restricted execution
77 mode in Grail is available on the Web site.}
Fred Drake5406e701998-04-09 14:02:02 +000078\end{seealso}