blob: 35619e6368a3768fc8085c7f3fc065da72013b5f [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.}
Andrew M. Kuchlingeabd9a12003-05-13 14:16:18 +00006\versionchanged[Disabled module]{2.3}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Andrew M. Kuchlingeabd9a12003-05-13 14:16:18 +00008\begin{notice}[warning]
9 The documentation has been left in place to help in reading old code
10 that uses the module.
11\end{notice}
Fred Drakea8912301998-03-14 07:08:02 +000012
Fred Drake19479911998-02-13 06:58:54 +000013This module contains the \class{RExec} class, which supports
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000014\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
Fred Drakea8912301998-03-14 07:08:02 +000015\method{r_import()} methods, which are restricted versions of the standard
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000016Python functions \method{eval()}, \method{execfile()} and
17the \keyword{exec} and \keyword{import} statements.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000018Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000019only have access to modules and functions that are deemed safe; you
Fred Drake19479911998-02-13 06:58:54 +000020can subclass \class{RExec} to add or remove capabilities as desired.
Guido van Rossum095538d1996-10-22 01:11:19 +000021
Fred Drake80a04a42002-08-27 16:46:06 +000022\begin{notice}[warning]
23 While the \module{rexec} module is designed to perform as described
24 below, it does have a few known vulnerabilities which could be
25 exploited by carefully written code. Thus it should not be relied
26 upon in situations requiring ``production ready'' security. In such
27 situations, execution via sub-processes or very careful
28 ``cleansing'' of both code and data to be processed may be
29 necessary. Alternatively, help in patching known \module{rexec}
30 vulnerabilities would be welcomed.
31\end{notice}
32
33\begin{notice}
34 The \class{RExec} class can prevent code from performing unsafe
35 operations like reading or writing disk files, or using TCP/IP
36 sockets. However, it does not protect against code using extremely
37 large amounts of memory or processor time.
38\end{notice}
Guido van Rossum095538d1996-10-22 01:11:19 +000039
Fred Drakea8912301998-03-14 07:08:02 +000040\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000041Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000042
Fred Drakea8912301998-03-14 07:08:02 +000043\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
44If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000045instantiated.
Fred Drakeffbe6871999-04-22 21:23:22 +000046Whenever the \module{rexec} module searches for a module (even a
Fred Drake19479911998-02-13 06:58:54 +000047built-in one) or reads a module's code, it doesn't actually go out to
48the file system itself. Rather, it calls methods of an \class{RHooks}
49instance that was passed to or created by its constructor. (Actually,
50the \class{RExec} object doesn't make these calls --- they are made by
51a module loader object that's part of the \class{RExec} object. This
Fred Drake907e76b2001-07-06 20:30:11 +000052allows another level of flexibility, which can be useful when changing
53the mechanics of \keyword{import} within the restricted environment.)
Guido van Rossum095538d1996-10-22 01:11:19 +000054
Fred Drake19479911998-02-13 06:58:54 +000055By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000056file system accesses made to import a module, without changing the
57actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000058made. For instance, we could substitute an \class{RHooks} object that
59passes all filesystem requests to a file server elsewhere, via some
60RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000061importing applets from a URL for a directory.
62
Guido van Rossumf73f79b1996-10-24 22:14:06 +000063If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000064standard output.
Fred Drakea8912301998-03-14 07:08:02 +000065\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000066
Fred Drake307cb052001-06-22 18:21:53 +000067It is important to be aware that code running in a restricted
68environment can still call the \function{sys.exit()} function. To
69disallow restricted code from exiting the interpreter, always protect
70calls that cause restricted code to run with a
71\keyword{try}/\keyword{except} statement that catches the
72\exception{SystemExit} exception. Removing the \function{sys.exit()}
73function from the restricted environment is not sufficient --- the
74restricted code could still use \code{raise SystemExit}. Removing
75\exception{SystemExit} is not a reasonable option; some library code
76makes use of this and would break were it not available.
Guido van Rossum095538d1996-10-22 01:11:19 +000077
Guido van Rossum095538d1996-10-22 01:11:19 +000078
Fred Drake307cb052001-06-22 18:21:53 +000079\begin{seealso}
80 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}{Grail is a
81 Web browser written entirely in Python. It uses the
82 \module{rexec} module as a foundation for supporting
83 Python applets, and can be used as an example usage of
84 this module.}
85\end{seealso}
Guido van Rossum095538d1996-10-22 01:11:19 +000086
Guido van Rossum095538d1996-10-22 01:11:19 +000087
Fred Drake307cb052001-06-22 18:21:53 +000088\subsection{RExec Objects \label{rexec-objects}}
Guido van Rossum095538d1996-10-22 01:11:19 +000089
Fred Drake19479911998-02-13 06:58:54 +000090\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +000091
Fred Drakeda70ee11998-04-02 18:51:30 +000092\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000093\var{code} must either be a string containing a Python expression, or
94a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +000095environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +000096code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +000097\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000098
Fred Drakeda70ee11998-04-02 18:51:30 +000099\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000100\var{code} must either be a string containing one or more lines of
101Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +0000102restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000103\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000104
Fred Drakeda70ee11998-04-02 18:51:30 +0000105\begin{methoddesc}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +0000106Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +0000107restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000108\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000109
Fred Drakea8912301998-03-14 07:08:02 +0000110Methods whose names begin with \samp{s_} are similar to the functions
111beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000112restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000113\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000114
Fred Drakeda70ee11998-04-02 18:51:30 +0000115\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000116\var{code} must be a string containing a Python expression, which will
117be evaluated in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000118\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000119
Fred Drakeda70ee11998-04-02 18:51:30 +0000120\begin{methoddesc}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000121\var{code} must be a string containing one or more lines of Python code,
122which will be executed in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000123\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000124
Fred Drakeda70ee11998-04-02 18:51:30 +0000125\begin{methoddesc}{s_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000126Execute the Python code contained in the file \var{filename} in the
127restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000128\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000129
Fred Drake19479911998-02-13 06:58:54 +0000130\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000131implicitly called by code executing in the restricted environment.
132Overriding these methods in a subclass is used to change the policies
133enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000134
Fred Drakeda70ee11998-04-02 18:51:30 +0000135\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
136 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000137Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000138exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000139\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000140
Fred Drakeda70ee11998-04-02 18:51:30 +0000141\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000142Method called when \function{open()} is called in the restricted
143environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000144and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000145should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000146any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000147the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000148\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000149\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000150
Fred Drakeda70ee11998-04-02 18:51:30 +0000151\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000152Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000153\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000154
Fred Drakeda70ee11998-04-02 18:51:30 +0000155\begin{methoddesc}{r_unload}{module}
Fred Drake907e76b2001-07-06 20:30:11 +0000156Unload the module object \var{module} (remove it from the
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000157restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000158\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000159
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000160And their equivalents with access to restricted standard I/O streams:
161
Fred Drakeda70ee11998-04-02 18:51:30 +0000162\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
163 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000164Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000165exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000166\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000167
Fred Drakeda70ee11998-04-02 18:51:30 +0000168\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000169Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000170\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000171
Fred Drakeda70ee11998-04-02 18:51:30 +0000172\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000173Unload the module object \var{module}.
174% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000175\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000176
Fred Drake307cb052001-06-22 18:21:53 +0000177
178\subsection{Defining restricted environments \label{rexec-extension}}
179
180The \class{RExec} class has the following class attributes, which are
181used by the \method{__init__()} method. Changing them on an existing
182instance won't have any effect; instead, create a subclass of
183\class{RExec} and assign them new values in the class definition.
184Instances of the new class will then use those new values. All these
185attributes are tuples of strings.
186
187\begin{memberdesc}{nok_builtin_names}
188Contains the names of built-in functions which will \emph{not} be
189available to programs running in the restricted environment. The
190value for \class{RExec} is \code{('open', 'reload', '__import__')}.
191(This gives the exceptions, because by far the majority of built-in
192functions are harmless. A subclass that wants to override this
193variable should probably start with the value from the base class and
194concatenate additional forbidden functions --- when new dangerous
195built-in functions are added to Python, they will also be added to
196this module.)
197\end{memberdesc}
198
199\begin{memberdesc}{ok_builtin_modules}
200Contains the names of built-in modules which can be safely imported.
201The value for \class{RExec} is \code{('audioop', 'array', 'binascii',
202'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator',
Andrew M. Kuchlingecd57542004-08-31 13:49:36 +0000203'parser', 'regex', 'select', 'sha', '_sre', 'strop',
Fred Drake307cb052001-06-22 18:21:53 +0000204'struct', 'time')}. A similar remark about overriding this variable
205applies --- use the value from the base class as a starting point.
206\end{memberdesc}
207
208\begin{memberdesc}{ok_path}
209Contains the directories which will be searched when an \keyword{import}
210is performed in the restricted environment.
211The value for \class{RExec} is the same as \code{sys.path} (at the time
212the module is loaded) for unrestricted code.
213\end{memberdesc}
214
215\begin{memberdesc}{ok_posix_names}
216% Should this be called ok_os_names?
217Contains the names of the functions in the \refmodule{os} module which will be
218available to programs running in the restricted environment. The
219value for \class{RExec} is \code{('error', 'fstat', 'listdir',
220'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid',
221'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')}.
222\end{memberdesc}
223
224\begin{memberdesc}{ok_sys_names}
225Contains the names of the functions and variables in the \refmodule{sys}
226module which will be available to programs running in the restricted
227environment. The value for \class{RExec} is \code{('ps1', 'ps2',
228'copyright', 'version', 'platform', 'exit', 'maxint')}.
229\end{memberdesc}
230
Guido van Rossum59b2a742002-05-31 21:12:53 +0000231\begin{memberdesc}{ok_file_types}
232Contains the file types from which modules are allowed to be loaded.
233Each file type is an integer constant defined in the \refmodule{imp} module.
234The meaningful values are \constant{PY_SOURCE}, \constant{PY_COMPILED}, and
235\constant{C_EXTENSION}. The value for \class{RExec} is \code{(C_EXTENSION,
236PY_SOURCE)}. Adding \constant{PY_COMPILED} in subclasses is not recommended;
237an attacker could exit the restricted execution mode by putting a forged
238byte-compiled file (\file{.pyc}) anywhere in your file system, for example
239by writing it to \file{/tmp} or uploading it to the \file{/incoming}
240directory of your public FTP server.
241\end{memberdesc}
242
Fred Drake307cb052001-06-22 18:21:53 +0000243
Guido van Rossum095538d1996-10-22 01:11:19 +0000244\subsection{An example}
245
246Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000247standard \class{RExec} class. For example, if we're willing to allow
248files in \file{/tmp} to be written, we can subclass the \class{RExec}
249class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000250
Fred Drake19479911998-02-13 06:58:54 +0000251\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000252class TmpWriterRExec(rexec.RExec):
253 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000254 if mode in ('r', 'rb'):
255 pass
256 elif mode in ('w', 'wb', 'a', 'ab'):
257 # check filename : must begin with /tmp/
258 if file[:5]!='/tmp/':
259 raise IOError, "can't write outside /tmp"
260 elif (string.find(file, '/../') >= 0 or
261 file[:3] == '../' or file[-3:] == '/..'):
262 raise IOError, "'..' in filename forbidden"
263 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000264 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000265\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000266%
Guido van Rossum095538d1996-10-22 01:11:19 +0000267Notice that the above code will occasionally forbid a perfectly valid
268filename; for example, code in the restricted environment won't be
269able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000270\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000271\file{/tmp/bar}, which would require splitting apart the filename and
272performing various operations on it. In cases where security is at
273stake, it may be preferable to write simple code which is sometimes
274overly restrictive, instead of more general code that is also more
275complex and may harbor a subtle security hole.