blob: 71ae9a362711f5b4472fda6b24a4931182c7d081 [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
Fred Drake19479911998-02-13 06:58:54 +00008This module contains the \class{RExec} class, which supports
Guido van Rossum7b0c9d81998-05-08 13:27:38 +00009\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
Fred Drakea8912301998-03-14 07:08:02 +000010\method{r_import()} methods, which are restricted versions of the standard
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000011Python functions \method{eval()}, \method{execfile()} and
12the \keyword{exec} and \keyword{import} statements.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000013Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000014only have access to modules and functions that are deemed safe; you
Fred Drake19479911998-02-13 06:58:54 +000015can subclass \class{RExec} to add or remove capabilities as desired.
Guido van Rossum095538d1996-10-22 01:11:19 +000016
Fred Drake80a04a42002-08-27 16:46:06 +000017\begin{notice}[warning]
18 While the \module{rexec} module is designed to perform as described
19 below, it does have a few known vulnerabilities which could be
20 exploited by carefully written code. Thus it should not be relied
21 upon in situations requiring ``production ready'' security. In such
22 situations, execution via sub-processes or very careful
23 ``cleansing'' of both code and data to be processed may be
24 necessary. Alternatively, help in patching known \module{rexec}
25 vulnerabilities would be welcomed.
26\end{notice}
27
28\begin{notice}
29 The \class{RExec} class can prevent code from performing unsafe
30 operations like reading or writing disk files, or using TCP/IP
31 sockets. However, it does not protect against code using extremely
32 large amounts of memory or processor time.
33\end{notice}
Guido van Rossum095538d1996-10-22 01:11:19 +000034
Fred Drakea8912301998-03-14 07:08:02 +000035\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000036Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000037
Fred Drakea8912301998-03-14 07:08:02 +000038\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
39If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000040instantiated.
Fred Drakeffbe6871999-04-22 21:23:22 +000041Whenever the \module{rexec} module searches for a module (even a
Fred Drake19479911998-02-13 06:58:54 +000042built-in one) or reads a module's code, it doesn't actually go out to
43the file system itself. Rather, it calls methods of an \class{RHooks}
44instance that was passed to or created by its constructor. (Actually,
45the \class{RExec} object doesn't make these calls --- they are made by
46a module loader object that's part of the \class{RExec} object. This
Fred Drake907e76b2001-07-06 20:30:11 +000047allows another level of flexibility, which can be useful when changing
48the mechanics of \keyword{import} within the restricted environment.)
Guido van Rossum095538d1996-10-22 01:11:19 +000049
Fred Drake19479911998-02-13 06:58:54 +000050By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000051file system accesses made to import a module, without changing the
52actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000053made. For instance, we could substitute an \class{RHooks} object that
54passes all filesystem requests to a file server elsewhere, via some
55RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000056importing applets from a URL for a directory.
57
Guido van Rossumf73f79b1996-10-24 22:14:06 +000058If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000059standard output.
Fred Drakea8912301998-03-14 07:08:02 +000060\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000061
Fred Drake307cb052001-06-22 18:21:53 +000062It is important to be aware that code running in a restricted
63environment can still call the \function{sys.exit()} function. To
64disallow restricted code from exiting the interpreter, always protect
65calls that cause restricted code to run with a
66\keyword{try}/\keyword{except} statement that catches the
67\exception{SystemExit} exception. Removing the \function{sys.exit()}
68function from the restricted environment is not sufficient --- the
69restricted code could still use \code{raise SystemExit}. Removing
70\exception{SystemExit} is not a reasonable option; some library code
71makes use of this and would break were it not available.
Guido van Rossum095538d1996-10-22 01:11:19 +000072
Guido van Rossum095538d1996-10-22 01:11:19 +000073
Fred Drake307cb052001-06-22 18:21:53 +000074\begin{seealso}
75 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}{Grail is a
76 Web browser written entirely in Python. It uses the
77 \module{rexec} module as a foundation for supporting
78 Python applets, and can be used as an example usage of
79 this module.}
80\end{seealso}
Guido van Rossum095538d1996-10-22 01:11:19 +000081
Guido van Rossum095538d1996-10-22 01:11:19 +000082
Fred Drake307cb052001-06-22 18:21:53 +000083\subsection{RExec Objects \label{rexec-objects}}
Guido van Rossum095538d1996-10-22 01:11:19 +000084
Fred Drake19479911998-02-13 06:58:54 +000085\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +000086
Fred Drakeda70ee11998-04-02 18:51:30 +000087\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000088\var{code} must either be a string containing a Python expression, or
89a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +000090environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +000091code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +000092\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000093
Fred Drakeda70ee11998-04-02 18:51:30 +000094\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000095\var{code} must either be a string containing one or more lines of
96Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +000097restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +000098\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000099
Fred Drakeda70ee11998-04-02 18:51:30 +0000100\begin{methoddesc}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +0000101Execute the Python code contained in the file \var{filename} 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 Drakea8912301998-03-14 07:08:02 +0000105Methods whose names begin with \samp{s_} are similar to the functions
106beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000107restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000108\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000109
Fred Drakeda70ee11998-04-02 18:51:30 +0000110\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000111\var{code} must be a string containing a Python expression, which will
112be evaluated in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000113\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000114
Fred Drakeda70ee11998-04-02 18:51:30 +0000115\begin{methoddesc}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000116\var{code} must be a string containing one or more lines of Python code,
117which will be executed 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_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000121Execute the Python code contained in the file \var{filename} in the
122restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000123\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000124
Fred Drake19479911998-02-13 06:58:54 +0000125\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000126implicitly called by code executing in the restricted environment.
127Overriding these methods in a subclass is used to change the policies
128enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000129
Fred Drakeda70ee11998-04-02 18:51:30 +0000130\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
131 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000132Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000133exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000134\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000135
Fred Drakeda70ee11998-04-02 18:51:30 +0000136\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000137Method called when \function{open()} is called in the restricted
138environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000139and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000140should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000141any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000142the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000143\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000144\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000145
Fred Drakeda70ee11998-04-02 18:51:30 +0000146\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000147Reload the module object \var{module}, re-parsing and re-initializing it.
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}{r_unload}{module}
Fred Drake907e76b2001-07-06 20:30:11 +0000151Unload the module object \var{module} (remove it from the
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000152restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000153\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000154
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000155And their equivalents with access to restricted standard I/O streams:
156
Fred Drakeda70ee11998-04-02 18:51:30 +0000157\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
158 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000159Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000160exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000161\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000162
Fred Drakeda70ee11998-04-02 18:51:30 +0000163\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000164Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000165\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000166
Fred Drakeda70ee11998-04-02 18:51:30 +0000167\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000168Unload the module object \var{module}.
169% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000170\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000171
Fred Drake307cb052001-06-22 18:21:53 +0000172
173\subsection{Defining restricted environments \label{rexec-extension}}
174
175The \class{RExec} class has the following class attributes, which are
176used by the \method{__init__()} method. Changing them on an existing
177instance won't have any effect; instead, create a subclass of
178\class{RExec} and assign them new values in the class definition.
179Instances of the new class will then use those new values. All these
180attributes are tuples of strings.
181
182\begin{memberdesc}{nok_builtin_names}
183Contains the names of built-in functions which will \emph{not} be
184available to programs running in the restricted environment. The
185value for \class{RExec} is \code{('open', 'reload', '__import__')}.
186(This gives the exceptions, because by far the majority of built-in
187functions are harmless. A subclass that wants to override this
188variable should probably start with the value from the base class and
189concatenate additional forbidden functions --- when new dangerous
190built-in functions are added to Python, they will also be added to
191this module.)
192\end{memberdesc}
193
194\begin{memberdesc}{ok_builtin_modules}
195Contains the names of built-in modules which can be safely imported.
196The value for \class{RExec} is \code{('audioop', 'array', 'binascii',
197'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator',
198'parser', 'regex', 'rotor', 'select', 'sha', '_sre', 'strop',
199'struct', 'time')}. A similar remark about overriding this variable
200applies --- use the value from the base class as a starting point.
201\end{memberdesc}
202
203\begin{memberdesc}{ok_path}
204Contains the directories which will be searched when an \keyword{import}
205is performed in the restricted environment.
206The value for \class{RExec} is the same as \code{sys.path} (at the time
207the module is loaded) for unrestricted code.
208\end{memberdesc}
209
210\begin{memberdesc}{ok_posix_names}
211% Should this be called ok_os_names?
212Contains the names of the functions in the \refmodule{os} module which will be
213available to programs running in the restricted environment. The
214value for \class{RExec} is \code{('error', 'fstat', 'listdir',
215'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid',
216'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')}.
217\end{memberdesc}
218
219\begin{memberdesc}{ok_sys_names}
220Contains the names of the functions and variables in the \refmodule{sys}
221module which will be available to programs running in the restricted
222environment. The value for \class{RExec} is \code{('ps1', 'ps2',
223'copyright', 'version', 'platform', 'exit', 'maxint')}.
224\end{memberdesc}
225
Guido van Rossum59b2a742002-05-31 21:12:53 +0000226\begin{memberdesc}{ok_file_types}
227Contains the file types from which modules are allowed to be loaded.
228Each file type is an integer constant defined in the \refmodule{imp} module.
229The meaningful values are \constant{PY_SOURCE}, \constant{PY_COMPILED}, and
230\constant{C_EXTENSION}. The value for \class{RExec} is \code{(C_EXTENSION,
231PY_SOURCE)}. Adding \constant{PY_COMPILED} in subclasses is not recommended;
232an attacker could exit the restricted execution mode by putting a forged
233byte-compiled file (\file{.pyc}) anywhere in your file system, for example
234by writing it to \file{/tmp} or uploading it to the \file{/incoming}
235directory of your public FTP server.
236\end{memberdesc}
237
Fred Drake307cb052001-06-22 18:21:53 +0000238
Guido van Rossum095538d1996-10-22 01:11:19 +0000239\subsection{An example}
240
241Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000242standard \class{RExec} class. For example, if we're willing to allow
243files in \file{/tmp} to be written, we can subclass the \class{RExec}
244class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000245
Fred Drake19479911998-02-13 06:58:54 +0000246\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000247class TmpWriterRExec(rexec.RExec):
248 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000249 if mode in ('r', 'rb'):
250 pass
251 elif mode in ('w', 'wb', 'a', 'ab'):
252 # check filename : must begin with /tmp/
253 if file[:5]!='/tmp/':
254 raise IOError, "can't write outside /tmp"
255 elif (string.find(file, '/../') >= 0 or
256 file[:3] == '../' or file[-3:] == '/..'):
257 raise IOError, "'..' in filename forbidden"
258 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000259 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000260\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000261%
Guido van Rossum095538d1996-10-22 01:11:19 +0000262Notice that the above code will occasionally forbid a perfectly valid
263filename; for example, code in the restricted environment won't be
264able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000265\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000266\file{/tmp/bar}, which would require splitting apart the filename and
267performing various operations on it. In cases where security is at
268stake, it may be preferable to write simple code which is sometimes
269overly restrictive, instead of more general code that is also more
270complex and may harbor a subtle security hole.