blob: e301f81e349396db1f24a955b2eb82ba7ada2518 [file] [log] [blame]
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00001\section{Standard Module \sectcode{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
Fred Drakea8912301998-03-14 07:08:02 +00007\method{r_exec()}, \method{r_eval()}, \method{r_execfile()}, and
8\method{r_import()} methods, which are restricted versions of the standard
9Python functions \method{exec()}, \method{eval()}, \method{execfile()}, and
10the \keyword{import} statement.
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 Drake19479911998-02-13 06:58:54 +000053\setindexsubitem{(RExec object attribute)}
Guido van Rossum095538d1996-10-22 01:11:19 +000054\begin{datadesc}{nok_builtin_names}
55Contains the names of built-in functions which will \emph{not} be
Guido van Rossumf73f79b1996-10-24 22:14:06 +000056available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000057value for \class{RExec} is \code{('open',} \code{'reload',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000058\code{'__import__')}. (This gives the exceptions, because by far the
59majority of built-in functions are harmless. A subclass that wants to
60override this variable should probably start with the value from the
61base class and concatenate additional forbidden functions --- when new
62dangerous built-in functions are added to Python, they will also be
63added to this module.)
Guido van Rossum095538d1996-10-22 01:11:19 +000064\end{datadesc}
65
66\begin{datadesc}{ok_builtin_modules}
67Contains the names of built-in modules which can be safely imported.
Fred Drake19479911998-02-13 06:58:54 +000068The value for \class{RExec} is \code{('audioop',} \code{'array',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000069\code{'binascii',} \code{'cmath',} \code{'errno',} \code{'imageop',}
70\code{'marshal',} \code{'math',} \code{'md5',} \code{'operator',}
71\code{'parser',} \code{'regex',} \code{'rotor',} \code{'select',}
72\code{'strop',} \code{'struct',} \code{'time')}. A similar remark
73about overriding this variable applies --- use the value from the base
74class as a starting point.
Guido van Rossum095538d1996-10-22 01:11:19 +000075\end{datadesc}
76
77\begin{datadesc}{ok_path}
Fred Drakea8912301998-03-14 07:08:02 +000078Contains the directories which will be searched when an \keyword{import}
Guido van Rossum095538d1996-10-22 01:11:19 +000079is performed in the restricted environment.
Fred Drake19479911998-02-13 06:58:54 +000080The value for \class{RExec} is the same as \code{sys.path} (at the time
Guido van Rossumf73f79b1996-10-24 22:14:06 +000081the module is loaded) for unrestricted code.
Guido van Rossum095538d1996-10-22 01:11:19 +000082\end{datadesc}
83
84\begin{datadesc}{ok_posix_names}
85% Should this be called ok_os_names?
Fred Drakea8912301998-03-14 07:08:02 +000086Contains the names of the functions in the \module{os} module which will be
Guido van Rossum095538d1996-10-22 01:11:19 +000087available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000088value for \class{RExec} is \code{('error',} \code{'fstat',}
Guido van Rossum095538d1996-10-22 01:11:19 +000089\code{'listdir',} \code{'lstat',} \code{'readlink',} \code{'stat',}
90\code{'times',} \code{'uname',} \code{'getpid',} \code{'getppid',}
91\code{'getcwd',} \code{'getuid',} \code{'getgid',} \code{'geteuid',}
92\code{'getegid')}.
93\end{datadesc}
94
95\begin{datadesc}{ok_sys_names}
Fred Drakea8912301998-03-14 07:08:02 +000096Contains the names of the functions and variables in the \module{sys}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000097module which will be available to programs running in the restricted
Fred Drake19479911998-02-13 06:58:54 +000098environment. The value for \class{RExec} is \code{('ps1',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000099\code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
100\code{'exit',} \code{'maxint')}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000101\end{datadesc}
102
Fred Drake19479911998-02-13 06:58:54 +0000103\class{RExec} instances support the following methods:
104\setindexsubitem{(RExec object method)}
Guido van Rossum095538d1996-10-22 01:11:19 +0000105
106\begin{funcdesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000107\var{code} must either be a string containing a Python expression, or
108a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +0000109environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000110code object will be returned.
Guido van Rossum095538d1996-10-22 01:11:19 +0000111\end{funcdesc}
112
113\begin{funcdesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000114\var{code} must either be a string containing one or more lines of
115Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +0000116restricted environment's \module{__main__} module.
Guido van Rossum095538d1996-10-22 01:11:19 +0000117\end{funcdesc}
118
119\begin{funcdesc}{r_execfile}{filename}
120Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +0000121restricted environment's \module{__main__} module.
Guido van Rossum095538d1996-10-22 01:11:19 +0000122\end{funcdesc}
123
Fred Drakea8912301998-03-14 07:08:02 +0000124Methods whose names begin with \samp{s_} are similar to the functions
125beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000126restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000127\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000128
129\begin{funcdesc}{s_eval}{code}
130\var{code} must be a string containing a Python expression, which will
131be evaluated in the restricted environment.
132\end{funcdesc}
133
134\begin{funcdesc}{s_exec}{code}
135\var{code} must be a string containing one or more lines of Python code,
136which will be executed in the restricted environment.
137\end{funcdesc}
138
139\begin{funcdesc}{s_execfile}{code}
140Execute the Python code contained in the file \var{filename} in the
141restricted environment.
142\end{funcdesc}
143
Fred Drake19479911998-02-13 06:58:54 +0000144\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000145implicitly called by code executing in the restricted environment.
146Overriding these methods in a subclass is used to change the policies
147enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000148
Fred Drakea8912301998-03-14 07:08:02 +0000149\begin{funcdesc}{r_import}{modulename\optional{, globals, locals, fromlist}}
150Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000151exception if the module is considered unsafe.
Guido van Rossum095538d1996-10-22 01:11:19 +0000152\end{funcdesc}
153
Fred Drakecce10901998-03-17 06:33:25 +0000154\begin{funcdesc}{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()}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000162\end{funcdesc}
163
164\begin{funcdesc}{r_reload}{module}
165Reload the module object \var{module}, re-parsing and re-initializing it.
166\end{funcdesc}
167
168\begin{funcdesc}{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).
Guido van Rossum095538d1996-10-22 01:11:19 +0000171\end{funcdesc}
172
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000173And their equivalents with access to restricted standard I/O streams:
174
Fred Drakecce10901998-03-17 06:33:25 +0000175\begin{funcdesc}{s_import}{modulename\optional{, globals, locals, fromlist}}
Fred Drake19479911998-02-13 06:58:54 +0000176Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000177exception if the module is considered unsafe.
Guido van Rossum095538d1996-10-22 01:11:19 +0000178\end{funcdesc}
179
180\begin{funcdesc}{s_reload}{module}
181Reload the module object \var{module}, re-parsing and re-initializing it.
182\end{funcdesc}
183
184\begin{funcdesc}{s_unload}{module}
185Unload the module object \var{module}.
186% XXX what are the semantics of this?
187\end{funcdesc}
188
189\subsection{An example}
190
191Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000192standard \class{RExec} class. For example, if we're willing to allow
193files in \file{/tmp} to be written, we can subclass the \class{RExec}
194class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000195
Fred Drake19479911998-02-13 06:58:54 +0000196\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000197class TmpWriterRExec(rexec.RExec):
198 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000199 if mode in ('r', 'rb'):
200 pass
201 elif mode in ('w', 'wb', 'a', 'ab'):
202 # check filename : must begin with /tmp/
203 if file[:5]!='/tmp/':
204 raise IOError, "can't write outside /tmp"
205 elif (string.find(file, '/../') >= 0 or
206 file[:3] == '../' or file[-3:] == '/..'):
207 raise IOError, "'..' in filename forbidden"
208 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000209 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000210\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000211%
Guido van Rossum095538d1996-10-22 01:11:19 +0000212Notice that the above code will occasionally forbid a perfectly valid
213filename; for example, code in the restricted environment won't be
214able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000215\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000216\file{/tmp/bar}, which would require splitting apart the filename and
217performing various operations on it. In cases where security is at
218stake, it may be preferable to write simple code which is sometimes
219overly restrictive, instead of more general code that is also more
220complex and may harbor a subtle security hole.