blob: 8881c932de8295dbb0b96ac48105e3da52c89438 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{rexec}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{rexec}
3
4\modulesynopsis{Basic restricted execution framework.}
5
Fred Drakea8912301998-03-14 07:08:02 +00006
Guido van Rossumbe0a8a61996-09-10 17:37:05 +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 Drake19479911998-02-13 06:58:54 +000017\emph{Note:} The \class{RExec} class can prevent code from performing
Guido van Rossum095538d1996-10-22 01:11:19 +000018unsafe operations like reading or writing disk files, or using TCP/IP
19sockets. However, it does not protect against code using extremely
20large amounts of memory or CPU time.
Guido van Rossum095538d1996-10-22 01:11:19 +000021
Fred Drakea8912301998-03-14 07:08:02 +000022\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000023Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000024
Fred Drakea8912301998-03-14 07:08:02 +000025\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
26If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000027instantiated.
Fred Drake19479911998-02-13 06:58:54 +000028Whenever the \module{RExec} module searches for a module (even a
29built-in one) or reads a module's code, it doesn't actually go out to
30the file system itself. Rather, it calls methods of an \class{RHooks}
31instance that was passed to or created by its constructor. (Actually,
32the \class{RExec} object doesn't make these calls --- they are made by
33a module loader object that's part of the \class{RExec} object. This
34allows another level of flexibility, e.g. using packages.)
Guido van Rossum095538d1996-10-22 01:11:19 +000035
Fred Drake19479911998-02-13 06:58:54 +000036By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000037file system accesses made to import a module, without changing the
38actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000039made. For instance, we could substitute an \class{RHooks} object that
40passes all filesystem requests to a file server elsewhere, via some
41RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000042importing applets from a URL for a directory.
43
Guido van Rossumf73f79b1996-10-24 22:14:06 +000044If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000045standard output.
Fred Drakea8912301998-03-14 07:08:02 +000046\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000047
Fred Drake19479911998-02-13 06:58:54 +000048The \class{RExec} class has the following class attributes, which are
Fred Drakea8912301998-03-14 07:08:02 +000049used by the \method{__init__()} method. Changing them on an existing
Fred Drake19479911998-02-13 06:58:54 +000050instance won't have any effect; instead, create a subclass of
51\class{RExec} and assign them new values in the class definition.
52Instances of the new class will then use those new values. All these
53attributes are tuples of strings.
Guido van Rossum095538d1996-10-22 01:11:19 +000054
Fred Drakeda70ee11998-04-02 18:51:30 +000055\begin{memberdesc}{nok_builtin_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000056Contains the names of built-in functions which will \emph{not} be
Guido van Rossumf73f79b1996-10-24 22:14:06 +000057available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000058value for \class{RExec} is \code{('open',} \code{'reload',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000059\code{'__import__')}. (This gives the exceptions, because by far the
60majority of built-in functions are harmless. A subclass that wants to
61override this variable should probably start with the value from the
62base class and concatenate additional forbidden functions --- when new
63dangerous built-in functions are added to Python, they will also be
64added to this module.)
Fred Drakeda70ee11998-04-02 18:51:30 +000065\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000066
Fred Drakeda70ee11998-04-02 18:51:30 +000067\begin{memberdesc}{ok_builtin_modules}
Guido van Rossum095538d1996-10-22 01:11:19 +000068Contains the names of built-in modules which can be safely imported.
Fred Drake19479911998-02-13 06:58:54 +000069The value for \class{RExec} is \code{('audioop',} \code{'array',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000070\code{'binascii',} \code{'cmath',} \code{'errno',} \code{'imageop',}
71\code{'marshal',} \code{'math',} \code{'md5',} \code{'operator',}
72\code{'parser',} \code{'regex',} \code{'rotor',} \code{'select',}
73\code{'strop',} \code{'struct',} \code{'time')}. A similar remark
74about overriding this variable applies --- use the value from the base
75class as a starting point.
Fred Drakeda70ee11998-04-02 18:51:30 +000076\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000077
Fred Drakeda70ee11998-04-02 18:51:30 +000078\begin{memberdesc}{ok_path}
Fred Drakea8912301998-03-14 07:08:02 +000079Contains the directories which will be searched when an \keyword{import}
Guido van Rossum095538d1996-10-22 01:11:19 +000080is performed in the restricted environment.
Fred Drake19479911998-02-13 06:58:54 +000081The value for \class{RExec} is the same as \code{sys.path} (at the time
Guido van Rossumf73f79b1996-10-24 22:14:06 +000082the module is loaded) for unrestricted code.
Fred Drakeda70ee11998-04-02 18:51:30 +000083\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000084
Fred Drakeda70ee11998-04-02 18:51:30 +000085\begin{memberdesc}{ok_posix_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000086% Should this be called ok_os_names?
Fred Drakea8912301998-03-14 07:08:02 +000087Contains the names of the functions in the \module{os} module which will be
Guido van Rossum095538d1996-10-22 01:11:19 +000088available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000089value for \class{RExec} is \code{('error',} \code{'fstat',}
Guido van Rossum095538d1996-10-22 01:11:19 +000090\code{'listdir',} \code{'lstat',} \code{'readlink',} \code{'stat',}
91\code{'times',} \code{'uname',} \code{'getpid',} \code{'getppid',}
92\code{'getcwd',} \code{'getuid',} \code{'getgid',} \code{'geteuid',}
93\code{'getegid')}.
Fred Drakeda70ee11998-04-02 18:51:30 +000094\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000095
Fred Drakeda70ee11998-04-02 18:51:30 +000096\begin{memberdesc}{ok_sys_names}
Fred Drakea8912301998-03-14 07:08:02 +000097Contains the names of the functions and variables in the \module{sys}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000098module which will be available to programs running in the restricted
Fred Drake19479911998-02-13 06:58:54 +000099environment. The value for \class{RExec} is \code{('ps1',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000100\code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
101\code{'exit',} \code{'maxint')}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000102\end{memberdesc}
103
Guido van Rossum095538d1996-10-22 01:11:19 +0000104
Fred Drake19479911998-02-13 06:58:54 +0000105\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +0000106
Fred Drakeda70ee11998-04-02 18:51:30 +0000107\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000108\var{code} must either be a string containing a Python expression, or
109a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +0000110environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000111code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +0000112\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000113
Fred Drakeda70ee11998-04-02 18:51:30 +0000114\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000115\var{code} must either be a string containing one or more lines of
116Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +0000117restricted environment's \module{__main__} module.
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}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +0000121Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +0000122restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000123\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000124
Fred Drakea8912301998-03-14 07:08:02 +0000125Methods whose names begin with \samp{s_} are similar to the functions
126beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000127restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000128\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000129
Fred Drakeda70ee11998-04-02 18:51:30 +0000130\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000131\var{code} must be a string containing a Python expression, which will
132be evaluated in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000133\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000134
Fred Drakeda70ee11998-04-02 18:51:30 +0000135\begin{methoddesc}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000136\var{code} must be a string containing one or more lines of Python code,
137which will be executed in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000138\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000139
Fred Drakeda70ee11998-04-02 18:51:30 +0000140\begin{methoddesc}{s_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000141Execute the Python code contained in the file \var{filename} in the
142restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000143\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000144
Fred Drake19479911998-02-13 06:58:54 +0000145\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000146implicitly called by code executing in the restricted environment.
147Overriding these methods in a subclass is used to change the policies
148enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000149
Fred Drakeda70ee11998-04-02 18:51:30 +0000150\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
151 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000152Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000153exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000154\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000155
Fred Drakeda70ee11998-04-02 18:51:30 +0000156\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000157Method called when \function{open()} is called in the restricted
158environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000159and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000160should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000161any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000162the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000163\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000164\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000165
Fred Drakeda70ee11998-04-02 18:51:30 +0000166\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000167Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000168\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000169
Fred Drakeda70ee11998-04-02 18:51:30 +0000170\begin{methoddesc}{r_unload}{module}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000171Unload the module object \var{module} (i.e., remove it from the
172restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000173\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000174
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000175And their equivalents with access to restricted standard I/O streams:
176
Fred Drakeda70ee11998-04-02 18:51:30 +0000177\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
178 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000179Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000180exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000181\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000182
Fred Drakeda70ee11998-04-02 18:51:30 +0000183\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000184Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000185\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000186
Fred Drakeda70ee11998-04-02 18:51:30 +0000187\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000188Unload the module object \var{module}.
189% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000190\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000191
192\subsection{An example}
193
194Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000195standard \class{RExec} class. For example, if we're willing to allow
196files in \file{/tmp} to be written, we can subclass the \class{RExec}
197class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000198
Fred Drake19479911998-02-13 06:58:54 +0000199\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000200class TmpWriterRExec(rexec.RExec):
201 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000202 if mode in ('r', 'rb'):
203 pass
204 elif mode in ('w', 'wb', 'a', 'ab'):
205 # check filename : must begin with /tmp/
206 if file[:5]!='/tmp/':
207 raise IOError, "can't write outside /tmp"
208 elif (string.find(file, '/../') >= 0 or
209 file[:3] == '../' or file[-3:] == '/..'):
210 raise IOError, "'..' in filename forbidden"
211 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000212 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000213\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000214%
Guido van Rossum095538d1996-10-22 01:11:19 +0000215Notice that the above code will occasionally forbid a perfectly valid
216filename; for example, code in the restricted environment won't be
217able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000218\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000219\file{/tmp/bar}, which would require splitting apart the filename and
220performing various operations on it. In cases where security is at
221stake, it may be preferable to write simple code which is sometimes
222overly restrictive, instead of more general code that is also more
223complex and may harbor a subtle security hole.