blob: 3bbebdcff378e98daf81da1b850af774c9a7f4e6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{rexec} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 Restricted execution framework}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{standard}{rexec}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Basic restricted execution framework.}
6
Fred Drakea8912301998-03-14 07:08:02 +00007
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00008
Fred Drake19479911998-02-13 06:58:54 +00009This module contains the \class{RExec} class, which supports
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000010\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
Fred Drakea8912301998-03-14 07:08:02 +000011\method{r_import()} methods, which are restricted versions of the standard
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000012Python functions \method{eval()}, \method{execfile()} and
13the \keyword{exec} and \keyword{import} statements.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000014Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000015only have access to modules and functions that are deemed safe; you
Fred Drake19479911998-02-13 06:58:54 +000016can subclass \class{RExec} to add or remove capabilities as desired.
Guido van Rossum095538d1996-10-22 01:11:19 +000017
Fred Drake19479911998-02-13 06:58:54 +000018\emph{Note:} The \class{RExec} class can prevent code from performing
Guido van Rossum095538d1996-10-22 01:11:19 +000019unsafe operations like reading or writing disk files, or using TCP/IP
20sockets. However, it does not protect against code using extremely
21large amounts of memory or CPU time.
Guido van Rossum095538d1996-10-22 01:11:19 +000022
Fred Drakea8912301998-03-14 07:08:02 +000023\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000024Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000025
Fred Drakea8912301998-03-14 07:08:02 +000026\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
27If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000028instantiated.
Fred Drakeffbe6871999-04-22 21:23:22 +000029Whenever the \module{rexec} module searches for a module (even a
Fred Drake19479911998-02-13 06:58:54 +000030built-in one) or reads a module's code, it doesn't actually go out to
31the file system itself. Rather, it calls methods of an \class{RHooks}
32instance that was passed to or created by its constructor. (Actually,
33the \class{RExec} object doesn't make these calls --- they are made by
34a module loader object that's part of the \class{RExec} object. This
35allows another level of flexibility, e.g. using packages.)
Guido van Rossum095538d1996-10-22 01:11:19 +000036
Fred Drake19479911998-02-13 06:58:54 +000037By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000038file system accesses made to import a module, without changing the
39actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000040made. For instance, we could substitute an \class{RHooks} object that
41passes all filesystem requests to a file server elsewhere, via some
42RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000043importing applets from a URL for a directory.
44
Guido van Rossumf73f79b1996-10-24 22:14:06 +000045If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000046standard output.
Fred Drakea8912301998-03-14 07:08:02 +000047\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000048
Fred Drake307cb052001-06-22 18:21:53 +000049It is important to be aware that code running in a restricted
50environment can still call the \function{sys.exit()} function. To
51disallow restricted code from exiting the interpreter, always protect
52calls that cause restricted code to run with a
53\keyword{try}/\keyword{except} statement that catches the
54\exception{SystemExit} exception. Removing the \function{sys.exit()}
55function from the restricted environment is not sufficient --- the
56restricted code could still use \code{raise SystemExit}. Removing
57\exception{SystemExit} is not a reasonable option; some library code
58makes use of this and would break were it not available.
Guido van Rossum095538d1996-10-22 01:11:19 +000059
Guido van Rossum095538d1996-10-22 01:11:19 +000060
Fred Drake307cb052001-06-22 18:21:53 +000061\begin{seealso}
62 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}{Grail is a
63 Web browser written entirely in Python. It uses the
64 \module{rexec} module as a foundation for supporting
65 Python applets, and can be used as an example usage of
66 this module.}
67\end{seealso}
Guido van Rossum095538d1996-10-22 01:11:19 +000068
Guido van Rossum095538d1996-10-22 01:11:19 +000069
Fred Drake307cb052001-06-22 18:21:53 +000070\subsection{RExec Objects \label{rexec-objects}}
Guido van Rossum095538d1996-10-22 01:11:19 +000071
Fred Drake19479911998-02-13 06:58:54 +000072\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +000073
Fred Drakeda70ee11998-04-02 18:51:30 +000074\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000075\var{code} must either be a string containing a Python expression, or
76a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +000077environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +000078code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +000079\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000080
Fred Drakeda70ee11998-04-02 18:51:30 +000081\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000082\var{code} must either be a string containing one or more lines of
83Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +000084restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +000085\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000086
Fred Drakeda70ee11998-04-02 18:51:30 +000087\begin{methoddesc}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +000088Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +000089restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +000090\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000091
Fred Drakea8912301998-03-14 07:08:02 +000092Methods whose names begin with \samp{s_} are similar to the functions
93beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +000094restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +000095\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +000096
Fred Drakeda70ee11998-04-02 18:51:30 +000097\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +000098\var{code} must be a string containing a Python expression, which will
99be evaluated in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000100\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000101
Fred Drakeda70ee11998-04-02 18:51:30 +0000102\begin{methoddesc}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000103\var{code} must be a string containing one or more lines of Python code,
104which will be executed in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000105\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000106
Fred Drakeda70ee11998-04-02 18:51:30 +0000107\begin{methoddesc}{s_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000108Execute the Python code contained in the file \var{filename} in the
109restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000110\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000111
Fred Drake19479911998-02-13 06:58:54 +0000112\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000113implicitly called by code executing in the restricted environment.
114Overriding these methods in a subclass is used to change the policies
115enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000116
Fred Drakeda70ee11998-04-02 18:51:30 +0000117\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
118 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000119Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000120exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000121\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000122
Fred Drakeda70ee11998-04-02 18:51:30 +0000123\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000124Method called when \function{open()} is called in the restricted
125environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000126and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000127should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000128any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000129the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000130\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000131\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000132
Fred Drakeda70ee11998-04-02 18:51:30 +0000133\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000134Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000135\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000136
Fred Drakeda70ee11998-04-02 18:51:30 +0000137\begin{methoddesc}{r_unload}{module}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000138Unload the module object \var{module} (i.e., remove it from the
139restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000140\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000141
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000142And their equivalents with access to restricted standard I/O streams:
143
Fred Drakeda70ee11998-04-02 18:51:30 +0000144\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
145 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000146Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000147exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000148\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000149
Fred Drakeda70ee11998-04-02 18:51:30 +0000150\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000151Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000152\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000153
Fred Drakeda70ee11998-04-02 18:51:30 +0000154\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000155Unload the module object \var{module}.
156% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000157\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000158
Fred Drake307cb052001-06-22 18:21:53 +0000159
160\subsection{Defining restricted environments \label{rexec-extension}}
161
162The \class{RExec} class has the following class attributes, which are
163used by the \method{__init__()} method. Changing them on an existing
164instance won't have any effect; instead, create a subclass of
165\class{RExec} and assign them new values in the class definition.
166Instances of the new class will then use those new values. All these
167attributes are tuples of strings.
168
169\begin{memberdesc}{nok_builtin_names}
170Contains the names of built-in functions which will \emph{not} be
171available to programs running in the restricted environment. The
172value for \class{RExec} is \code{('open', 'reload', '__import__')}.
173(This gives the exceptions, because by far the majority of built-in
174functions are harmless. A subclass that wants to override this
175variable should probably start with the value from the base class and
176concatenate additional forbidden functions --- when new dangerous
177built-in functions are added to Python, they will also be added to
178this module.)
179\end{memberdesc}
180
181\begin{memberdesc}{ok_builtin_modules}
182Contains the names of built-in modules which can be safely imported.
183The value for \class{RExec} is \code{('audioop', 'array', 'binascii',
184'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator',
185'parser', 'regex', 'rotor', 'select', 'sha', '_sre', 'strop',
186'struct', 'time')}. A similar remark about overriding this variable
187applies --- use the value from the base class as a starting point.
188\end{memberdesc}
189
190\begin{memberdesc}{ok_path}
191Contains the directories which will be searched when an \keyword{import}
192is performed in the restricted environment.
193The value for \class{RExec} is the same as \code{sys.path} (at the time
194the module is loaded) for unrestricted code.
195\end{memberdesc}
196
197\begin{memberdesc}{ok_posix_names}
198% Should this be called ok_os_names?
199Contains the names of the functions in the \refmodule{os} module which will be
200available to programs running in the restricted environment. The
201value for \class{RExec} is \code{('error', 'fstat', 'listdir',
202'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid',
203'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')}.
204\end{memberdesc}
205
206\begin{memberdesc}{ok_sys_names}
207Contains the names of the functions and variables in the \refmodule{sys}
208module which will be available to programs running in the restricted
209environment. The value for \class{RExec} is \code{('ps1', 'ps2',
210'copyright', 'version', 'platform', 'exit', 'maxint')}.
211\end{memberdesc}
212
213
Guido van Rossum095538d1996-10-22 01:11:19 +0000214\subsection{An example}
215
216Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000217standard \class{RExec} class. For example, if we're willing to allow
218files in \file{/tmp} to be written, we can subclass the \class{RExec}
219class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000220
Fred Drake19479911998-02-13 06:58:54 +0000221\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000222class TmpWriterRExec(rexec.RExec):
223 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000224 if mode in ('r', 'rb'):
225 pass
226 elif mode in ('w', 'wb', 'a', 'ab'):
227 # check filename : must begin with /tmp/
228 if file[:5]!='/tmp/':
229 raise IOError, "can't write outside /tmp"
230 elif (string.find(file, '/../') >= 0 or
231 file[:3] == '../' or file[-3:] == '/..'):
232 raise IOError, "'..' in filename forbidden"
233 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000234 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000235\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000236%
Guido van Rossum095538d1996-10-22 01:11:19 +0000237Notice that the above code will occasionally forbid a perfectly valid
238filename; for example, code in the restricted environment won't be
239able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000240\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000241\file{/tmp/bar}, which would require splitting apart the filename and
242performing various operations on it. In cases where security is at
243stake, it may be preferable to write simple code which is sometimes
244overly restrictive, instead of more general code that is also more
245complex and may harbor a subtle security hole.