blob: 8778ae13fb6997fe3241be62364603346f0fb8fc [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{rexec}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-rexec}
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00003\stmodindex{rexec}
Fred Drakea8912301998-03-14 07:08:02 +00004
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00005
Fred Drake19479911998-02-13 06:58:54 +00006This module contains the \class{RExec} class, which supports
Guido van Rossum7b0c9d81998-05-08 13:27:38 +00007\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
Fred Drakea8912301998-03-14 07:08:02 +00008\method{r_import()} methods, which are restricted versions of the standard
Guido van Rossum7b0c9d81998-05-08 13:27:38 +00009Python functions \method{eval()}, \method{execfile()} and
10the \keyword{exec} and \keyword{import} statements.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000011Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000012only have access to modules and functions that are deemed safe; you
Fred Drake19479911998-02-13 06:58:54 +000013can subclass \class{RExec} to add or remove capabilities as desired.
Guido van Rossum095538d1996-10-22 01:11:19 +000014
Fred Drake19479911998-02-13 06:58:54 +000015\emph{Note:} The \class{RExec} class can prevent code from performing
Guido van Rossum095538d1996-10-22 01:11:19 +000016unsafe operations like reading or writing disk files, or using TCP/IP
17sockets. However, it does not protect against code using extremely
18large amounts of memory or CPU time.
Guido van Rossum095538d1996-10-22 01:11:19 +000019
Fred Drakea8912301998-03-14 07:08:02 +000020\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000021Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000022
Fred Drakea8912301998-03-14 07:08:02 +000023\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
24If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000025instantiated.
Fred Drake19479911998-02-13 06:58:54 +000026Whenever the \module{RExec} module searches for a module (even a
27built-in one) or reads a module's code, it doesn't actually go out to
28the file system itself. Rather, it calls methods of an \class{RHooks}
29instance that was passed to or created by its constructor. (Actually,
30the \class{RExec} object doesn't make these calls --- they are made by
31a module loader object that's part of the \class{RExec} object. This
32allows another level of flexibility, e.g. using packages.)
Guido van Rossum095538d1996-10-22 01:11:19 +000033
Fred Drake19479911998-02-13 06:58:54 +000034By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000035file system accesses made to import a module, without changing the
36actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000037made. For instance, we could substitute an \class{RHooks} object that
38passes all filesystem requests to a file server elsewhere, via some
39RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000040importing applets from a URL for a directory.
41
Guido van Rossumf73f79b1996-10-24 22:14:06 +000042If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000043standard output.
Fred Drakea8912301998-03-14 07:08:02 +000044\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000045
Fred Drake19479911998-02-13 06:58:54 +000046The \class{RExec} class has the following class attributes, which are
Fred Drakea8912301998-03-14 07:08:02 +000047used by the \method{__init__()} method. Changing them on an existing
Fred Drake19479911998-02-13 06:58:54 +000048instance won't have any effect; instead, create a subclass of
49\class{RExec} and assign them new values in the class definition.
50Instances of the new class will then use those new values. All these
51attributes are tuples of strings.
Guido van Rossum095538d1996-10-22 01:11:19 +000052
Fred Drakeda70ee11998-04-02 18:51:30 +000053\begin{memberdesc}{nok_builtin_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000054Contains the names of built-in functions which will \emph{not} be
Guido van Rossumf73f79b1996-10-24 22:14:06 +000055available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000056value for \class{RExec} is \code{('open',} \code{'reload',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000057\code{'__import__')}. (This gives the exceptions, because by far the
58majority of built-in functions are harmless. A subclass that wants to
59override this variable should probably start with the value from the
60base class and concatenate additional forbidden functions --- when new
61dangerous built-in functions are added to Python, they will also be
62added to this module.)
Fred Drakeda70ee11998-04-02 18:51:30 +000063\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000064
Fred Drakeda70ee11998-04-02 18:51:30 +000065\begin{memberdesc}{ok_builtin_modules}
Guido van Rossum095538d1996-10-22 01:11:19 +000066Contains the names of built-in modules which can be safely imported.
Fred Drake19479911998-02-13 06:58:54 +000067The value for \class{RExec} is \code{('audioop',} \code{'array',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000068\code{'binascii',} \code{'cmath',} \code{'errno',} \code{'imageop',}
69\code{'marshal',} \code{'math',} \code{'md5',} \code{'operator',}
70\code{'parser',} \code{'regex',} \code{'rotor',} \code{'select',}
71\code{'strop',} \code{'struct',} \code{'time')}. A similar remark
72about overriding this variable applies --- use the value from the base
73class as a starting point.
Fred Drakeda70ee11998-04-02 18:51:30 +000074\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000075
Fred Drakeda70ee11998-04-02 18:51:30 +000076\begin{memberdesc}{ok_path}
Fred Drakea8912301998-03-14 07:08:02 +000077Contains the directories which will be searched when an \keyword{import}
Guido van Rossum095538d1996-10-22 01:11:19 +000078is performed in the restricted environment.
Fred Drake19479911998-02-13 06:58:54 +000079The value for \class{RExec} is the same as \code{sys.path} (at the time
Guido van Rossumf73f79b1996-10-24 22:14:06 +000080the module is loaded) for unrestricted code.
Fred Drakeda70ee11998-04-02 18:51:30 +000081\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000082
Fred Drakeda70ee11998-04-02 18:51:30 +000083\begin{memberdesc}{ok_posix_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000084% Should this be called ok_os_names?
Fred Drakea8912301998-03-14 07:08:02 +000085Contains the names of the functions in the \module{os} module which will be
Guido van Rossum095538d1996-10-22 01:11:19 +000086available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000087value for \class{RExec} is \code{('error',} \code{'fstat',}
Guido van Rossum095538d1996-10-22 01:11:19 +000088\code{'listdir',} \code{'lstat',} \code{'readlink',} \code{'stat',}
89\code{'times',} \code{'uname',} \code{'getpid',} \code{'getppid',}
90\code{'getcwd',} \code{'getuid',} \code{'getgid',} \code{'geteuid',}
91\code{'getegid')}.
Fred Drakeda70ee11998-04-02 18:51:30 +000092\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000093
Fred Drakeda70ee11998-04-02 18:51:30 +000094\begin{memberdesc}{ok_sys_names}
Fred Drakea8912301998-03-14 07:08:02 +000095Contains the names of the functions and variables in the \module{sys}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000096module which will be available to programs running in the restricted
Fred Drake19479911998-02-13 06:58:54 +000097environment. The value for \class{RExec} is \code{('ps1',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000098\code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
99\code{'exit',} \code{'maxint')}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000100\end{memberdesc}
101
Guido van Rossum095538d1996-10-22 01:11:19 +0000102
Fred Drake19479911998-02-13 06:58:54 +0000103\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +0000104
Fred Drakeda70ee11998-04-02 18:51:30 +0000105\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000106\var{code} must either be a string containing a Python expression, or
107a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +0000108environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000109code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +0000110\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000111
Fred Drakeda70ee11998-04-02 18:51:30 +0000112\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000113\var{code} must either be a string containing one or more lines of
114Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +0000115restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000116\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000117
Fred Drakeda70ee11998-04-02 18:51:30 +0000118\begin{methoddesc}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +0000119Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +0000120restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000121\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000122
Fred Drakea8912301998-03-14 07:08:02 +0000123Methods whose names begin with \samp{s_} are similar to the functions
124beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000125restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000126\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000127
Fred Drakeda70ee11998-04-02 18:51:30 +0000128\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000129\var{code} must be a string containing a Python expression, which will
130be evaluated in the restricted environment.
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}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000134\var{code} must be a string containing one or more lines of Python code,
135which will be executed in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000136\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000137
Fred Drakeda70ee11998-04-02 18:51:30 +0000138\begin{methoddesc}{s_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000139Execute the Python code contained in the file \var{filename} in the
140restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000141\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000142
Fred Drake19479911998-02-13 06:58:54 +0000143\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000144implicitly called by code executing in the restricted environment.
145Overriding these methods in a subclass is used to change the policies
146enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000147
Fred Drakeda70ee11998-04-02 18:51:30 +0000148\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
149 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000150Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000151exception if the module is considered unsafe.
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}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000155Method called when \function{open()} is called in the restricted
156environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000157and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000158should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000159any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000160the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000161\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000162\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000163
Fred Drakeda70ee11998-04-02 18:51:30 +0000164\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000165Reload the module object \var{module}, re-parsing and re-initializing it.
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}{r_unload}{module}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000169Unload the module object \var{module} (i.e., remove it from the
170restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000171\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000172
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000173And their equivalents with access to restricted standard I/O streams:
174
Fred Drakeda70ee11998-04-02 18:51:30 +0000175\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
176 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000177Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000178exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000179\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000180
Fred Drakeda70ee11998-04-02 18:51:30 +0000181\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000182Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000183\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000184
Fred Drakeda70ee11998-04-02 18:51:30 +0000185\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000186Unload the module object \var{module}.
187% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000188\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000189
190\subsection{An example}
191
192Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000193standard \class{RExec} class. For example, if we're willing to allow
194files in \file{/tmp} to be written, we can subclass the \class{RExec}
195class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000196
Fred Drake19479911998-02-13 06:58:54 +0000197\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000198class TmpWriterRExec(rexec.RExec):
199 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000200 if mode in ('r', 'rb'):
201 pass
202 elif mode in ('w', 'wb', 'a', 'ab'):
203 # check filename : must begin with /tmp/
204 if file[:5]!='/tmp/':
205 raise IOError, "can't write outside /tmp"
206 elif (string.find(file, '/../') >= 0 or
207 file[:3] == '../' or file[-3:] == '/..'):
208 raise IOError, "'..' in filename forbidden"
209 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000210 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000211\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000212%
Guido van Rossum095538d1996-10-22 01:11:19 +0000213Notice that the above code will occasionally forbid a perfectly valid
214filename; for example, code in the restricted environment won't be
215able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000216\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000217\file{/tmp/bar}, which would require splitting apart the filename and
218performing various operations on it. In cases where security is at
219stake, it may be preferable to write simple code which is sometimes
220overly restrictive, instead of more general code that is also more
221complex and may harbor a subtle security hole.