blob: 4b1a10028c98abbeb8ca6f92f19761dbbf8ffe62 [file] [log] [blame]
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00001\section{Standard Module \sectcode{rexec}}
2\stmodindex{rexec}
3\renewcommand{\indexsubitem}{(in module rexec)}
4
Guido van Rossum095538d1996-10-22 01:11:19 +00005This module contains the \code{RExec} class, which supports
6\code{r_exec()}, \code{r_eval()}, \code{r_execfile()}, and
7\code{r_import()} methods, which are restricted versions of the standard
8Python functions \code{exec()}, \code{eval()}, \code{execfile()}, and
Guido van Rossumf73f79b1996-10-24 22:14:06 +00009the \code{import} statement.
10Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000011only have access to modules and functions that are deemed safe; you
12can subclass \code{RExec} to add or remove capabilities as desired.
13
14\emph{Note:} The \code{RExec} class can prevent code from performing
15unsafe operations like reading or writing disk files, or using TCP/IP
16sockets. However, it does not protect against code using extremely
17large amounts of memory or CPU time.
Guido van Rossum095538d1996-10-22 01:11:19 +000018
Guido van Rossumf73f79b1996-10-24 22:14:06 +000019\begin{funcdesc}{RExec}{\optional{hooks\optional{\, verbose}}}
Guido van Rossum095538d1996-10-22 01:11:19 +000020Returns an instance of the \code{RExec} class.
21
Guido van Rossum095538d1996-10-22 01:11:19 +000022\var{hooks} is an instance of the \code{RHooks} class or a subclass of it.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000023If it is omitted or \code{None}, the default \code{RHooks} class is
24instantiated.
Guido van Rossum095538d1996-10-22 01:11:19 +000025Whenever the RExec module searches for a module (even a built-in one)
26or reads a module's code, it doesn't actually go out to the file
27system itself. Rather, it calls methods of an RHooks instance that
28was passed to or created by its constructor. (Actually, the RExec
29object doesn't make these calls---they are made by a module loader
30object that's part of the RExec object. This allows another level of
31flexibility, e.g. using packages.)
32
Guido van Rossumf73f79b1996-10-24 22:14:06 +000033By providing an alternate RHooks object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000034file system accesses made to import a module, without changing the
35actual algorithm that controls the order in which those accesses are
36made. For instance, we could substitute an RHooks object that passes
37all filesystem requests to a file server elsewhere, via some RPC
38mechanism such as ILU. Grail's applet loader uses this to support
39importing applets from a URL for a directory.
40
Guido van Rossumf73f79b1996-10-24 22:14:06 +000041If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000042standard output.
43\end{funcdesc}
44
Guido van Rossumf73f79b1996-10-24 22:14:06 +000045The RExec class has the following class attributes, which are used by the
Guido van Rossum095538d1996-10-22 01:11:19 +000046\code{__init__} method. Changing them on an existing instance won't
47have any effect; instead, create a subclass of \code{RExec} and assign
48them new values in the class definition. Instances of the new class
49will then use those new values. All these attributes are tuples of
50strings.
51
52\renewcommand{\indexsubitem}{(RExec object attribute)}
53\begin{datadesc}{nok_builtin_names}
54Contains 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
56value for \code{RExec} is \code{('open',} \code{'reload',}
57\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.)
Guido van Rossum095538d1996-10-22 01:11:19 +000063\end{datadesc}
64
65\begin{datadesc}{ok_builtin_modules}
66Contains the names of built-in modules which can be safely imported.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000067The value for \code{RExec} is \code{('audioop',} \code{'array',}
68\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.
Guido van Rossum095538d1996-10-22 01:11:19 +000074\end{datadesc}
75
76\begin{datadesc}{ok_path}
77Contains the directories which will be searched when an \code{import}
78is performed in the restricted environment.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000079The value for \code{RExec} is the same as \code{sys.path} (at the time
80the module is loaded) for unrestricted code.
Guido van Rossum095538d1996-10-22 01:11:19 +000081\end{datadesc}
82
83\begin{datadesc}{ok_posix_names}
84% Should this be called ok_os_names?
85Contains the names of the functions in the \code{os} module which will be
86available to programs running in the restricted environment. The
87value for \code{RExec} is \code{('error',} \code{'fstat',}
88\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')}.
92\end{datadesc}
93
94\begin{datadesc}{ok_sys_names}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000095Contains the names of the functions and variables in the \code{sys}
96module which will be available to programs running in the restricted
97environment. The value for \code{RExec} is \code{('ps1',}
98\code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
99\code{'exit',} \code{'maxint')}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000100\end{datadesc}
101
102RExec instances support the following methods:
103\renewcommand{\indexsubitem}{(RExec object method)}
104
105\begin{funcdesc}{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
108environment's \code{__main__} module. The value of the expression or
109code object will be returned.
Guido van Rossum095538d1996-10-22 01:11:19 +0000110\end{funcdesc}
111
112\begin{funcdesc}{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
115restricted environment's \code{__main__} module.
Guido van Rossum095538d1996-10-22 01:11:19 +0000116\end{funcdesc}
117
118\begin{funcdesc}{r_execfile}{filename}
119Execute the Python code contained in the file \var{filename} in the
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000120restricted environment's \code{__main__} module.
Guido van Rossum095538d1996-10-22 01:11:19 +0000121\end{funcdesc}
122
123Methods whose names begin with \code{s_} are similar to the functions
124beginning with \code{r_}, but the code will be granted access to
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000125restricted versions of the standard I/O streans \code{sys.stdin},
126\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000127
128\begin{funcdesc}{s_eval}{code}
129\var{code} must be a string containing a Python expression, which will
130be evaluated in the restricted environment.
131\end{funcdesc}
132
133\begin{funcdesc}{s_exec}{code}
134\var{code} must be a string containing one or more lines of Python code,
135which will be executed in the restricted environment.
136\end{funcdesc}
137
138\begin{funcdesc}{s_execfile}{code}
139Execute the Python code contained in the file \var{filename} in the
140restricted environment.
141\end{funcdesc}
142
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000143\code{RExec} objects must also support various methods which will be
144implicitly 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
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000148\begin{funcdesc}{r_import}{modulename\optional{\, globals\, locals\, fromlist}}
149Import the module \var{modulename}, raising an \code{ImportError}
150exception if the module is considered unsafe.
Guido van Rossum095538d1996-10-22 01:11:19 +0000151\end{funcdesc}
152
153\begin{funcdesc}{r_open}{filename\optional{\, mode\optional{\, bufsize}}}
154Method called when \code{open()} is called in the restricted
155environment. The arguments are identical to those of \code{open()},
156and a file object (or a class instance compatible with file objects)
157should be returned. \code{RExec}'s default behaviour is allow opening
158any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000159the example below for an implementation of a less restrictive
160\code{r_open()}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000161\end{funcdesc}
162
163\begin{funcdesc}{r_reload}{module}
164Reload the module object \var{module}, re-parsing and re-initializing it.
165\end{funcdesc}
166
167\begin{funcdesc}{r_unload}{module}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000168Unload the module object \var{module} (i.e., remove it from the
169restricted environment's \code{sys.modules} dictionary).
Guido van Rossum095538d1996-10-22 01:11:19 +0000170\end{funcdesc}
171
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000172And their equivalents with access to restricted standard I/O streams:
173
Guido van Rossum095538d1996-10-22 01:11:19 +0000174\begin{funcdesc}{s_import}{modulename\optional{\, globals, locals, fromlist}}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000175Import the module \var{modulename}, raising an \code{ImportError}
176exception if the module is considered unsafe.
Guido van Rossum095538d1996-10-22 01:11:19 +0000177\end{funcdesc}
178
179\begin{funcdesc}{s_reload}{module}
180Reload the module object \var{module}, re-parsing and re-initializing it.
181\end{funcdesc}
182
183\begin{funcdesc}{s_unload}{module}
184Unload the module object \var{module}.
185% XXX what are the semantics of this?
186\end{funcdesc}
187
188\subsection{An example}
189
190Let us say that we want a slightly more relaxed policy than the
191standard RExec class. For example, if we're willing to allow files in
192\file{/tmp} to be written, we can subclass the \code{RExec} class:
193
194\bcode\begin{verbatim}
195class TmpWriterRExec(rexec.RExec):
196 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000197 if mode in ('r', 'rb'):
198 pass
199 elif mode in ('w', 'wb', 'a', 'ab'):
200 # check filename : must begin with /tmp/
201 if file[:5]!='/tmp/':
202 raise IOError, "can't write outside /tmp"
203 elif (string.find(file, '/../') >= 0 or
204 file[:3] == '../' or file[-3:] == '/..'):
205 raise IOError, "'..' in filename forbidden"
206 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000207 return open(file, mode, buf)
208\end{verbatim}\ecode
209
210Notice that the above code will occasionally forbid a perfectly valid
211filename; for example, code in the restricted environment won't be
212able to open a file called \file{/tmp/foo/../bar}. To fix this, the
213\code{r_open} method would have to simplify the filename to
214\file{/tmp/bar}, which would require splitting apart the filename and
215performing various operations on it. In cases where security is at
216stake, it may be preferable to write simple code which is sometimes
217overly restrictive, instead of more general code that is also more
218complex and may harbor a subtle security hole.