Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 1 | \chapter{Restricted Execution \label{restricted}} |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 3 | In general, Python programs have complete access to the underlying |
| 4 | operating system throug the various functions and classes, For |
| 5 | example, a Python program can open any file for reading and writing by |
Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 6 | using the \function{open()} built-in function (provided the underlying |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 7 | operating system gives you permission!). This is exactly what you want |
| 8 | for most applications. |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 10 | There exists a class of applications for which this ``openness'' is |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 11 | inappropriate. Take Grail: a Web browser that accepts ``applets,'' |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 12 | snippets of Python code, from anywhere on the Internet for execution |
| 13 | on the local system. This can be used to improve the user interface |
| 14 | of forms, for instance. Since the originator of the code is unknown, |
| 15 | it is obvious that it cannot be trusted with the full resources of the |
| 16 | local machine. |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 18 | \emph{Restricted execution} is the basic framework in Python that allows |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 19 | for the segregation of trusted and untrusted code. It is based on the |
| 20 | notion that trusted Python code (a \emph{supervisor}) can create a |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 21 | ``padded cell' (or environment) with limited permissions, and run the |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 22 | untrusted code within this cell. The untrusted code cannot break out |
| 23 | of its cell, and can only interact with sensitive system resources |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 24 | through interfaces defined and managed by the trusted code. The term |
| 25 | ``restricted execution'' is favored over ``safe-Python'' |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 26 | since true safety is hard to define, and is determined by the way the |
| 27 | restricted environment is created. Note that the restricted |
| 28 | environments can be nested, with inner cells creating subcells of |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 29 | lesser, but never greater, privilege. |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 30 | |
| 31 | An interesting aspect of Python's restricted execution model is that |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 32 | the interfaces presented to untrusted code usually have the same names |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 33 | as those presented to trusted code. Therefore no special interfaces |
| 34 | need to be learned to write code designed to run in a restricted |
| 35 | environment. And because the exact nature of the padded cell is |
| 36 | determined by the supervisor, different restrictions can be imposed, |
| 37 | depending on the application. For example, it might be deemed |
| 38 | ``safe'' for untrusted code to read any file within a specified |
| 39 | directory, but never to write a file. In this case, the supervisor |
Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 40 | may redefine the built-in \function{open()} function so that it raises |
| 41 | an exception whenever the \var{mode} parameter is \code{'w'}. It |
| 42 | might also perform a \cfunction{chroot()}-like operation on the |
| 43 | \var{filename} parameter, such that root is always relative to some |
| 44 | safe ``sandbox'' area of the filesystem. In this case, the untrusted |
| 45 | code would still see an built-in \function{open()} function in its |
| 46 | environment, with the same calling interface. The semantics would be |
| 47 | identical too, with \exception{IOError}s being raised when the |
| 48 | supervisor determined that an unallowable parameter is being used. |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 50 | The Python run-time determines whether a particular code block is |
| 51 | executing in restricted execution mode based on the identity of the |
| 52 | \code{__builtins__} object in its global variables: if this is (the |
Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 53 | dictionary of) the standard \refmodule[builtin]{__builtin__} module, |
| 54 | the code is deemed to be unrestricted, else it is deemed to be |
| 55 | restricted. |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 56 | |
| 57 | Python code executing in restricted mode faces a number of limitations |
| 58 | that are designed to prevent it from escaping from the padded cell. |
Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 59 | For instance, the function object attribute \member{func_globals} and |
| 60 | the class and instance object attribute \member{__dict__} are |
| 61 | unavailable. |
Guido van Rossum | f73f79b | 1996-10-24 22:14:06 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | c3d090c | 1996-10-22 01:10:56 +0000 | [diff] [blame] | 63 | Two modules provide the framework for setting up restricted execution |
| 64 | environments: |
| 65 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 66 | \localmoduletable |
Fred Drake | 5406e70 | 1998-04-09 14:02:02 +0000 | [diff] [blame] | 67 | |
| 68 | \begin{seealso} |
Fred Drake | 87b8f31 | 1999-04-23 17:26:24 +0000 | [diff] [blame] | 69 | \seetext{Andrew Kuchling, ``Restricted Execution HOWTO.'' Available |
| 70 | online at \url{http://www.python.org/doc/howto/rexec/}.} |
| 71 | |
| 72 | \seetext{Grail, an Internet browser written in Python, is available |
| 73 | at \url{http://grail.cnri.reston.va.us/grail/}. More |
| 74 | information on the use of Python's restricted execution |
| 75 | mode in Grail is available on the Web site.} |
Fred Drake | 5406e70 | 1998-04-09 14:02:02 +0000 | [diff] [blame] | 76 | \end{seealso} |