source: vendor/python/2.5/Doc/lib/librexec.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 11.8 KB
Line 
1\section{\module{rexec} ---
2 Restricted execution framework}
3
4\declaremodule{standard}{rexec}
5\modulesynopsis{Basic restricted execution framework.}
6\versionchanged[Disabled module]{2.3}
7
8\begin{notice}[warning]
9 The documentation has been left in place to help in reading old code
10 that uses the module.
11\end{notice}
12
13This module contains the \class{RExec} class, which supports
14\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
15\method{r_import()} methods, which are restricted versions of the standard
16Python functions \method{eval()}, \method{execfile()} and
17the \keyword{exec} and \keyword{import} statements.
18Code executed in this restricted environment will
19only have access to modules and functions that are deemed safe; you
20can subclass \class{RExec} to add or remove capabilities as desired.
21
22\begin{notice}[warning]
23 While the \module{rexec} module is designed to perform as described
24 below, it does have a few known vulnerabilities which could be
25 exploited by carefully written code. Thus it should not be relied
26 upon in situations requiring ``production ready'' security. In such
27 situations, execution via sub-processes or very careful
28 ``cleansing'' of both code and data to be processed may be
29 necessary. Alternatively, help in patching known \module{rexec}
30 vulnerabilities would be welcomed.
31\end{notice}
32
33\begin{notice}
34 The \class{RExec} class can prevent code from performing unsafe
35 operations like reading or writing disk files, or using TCP/IP
36 sockets. However, it does not protect against code using extremely
37 large amounts of memory or processor time.
38\end{notice}
39
40\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
41Returns an instance of the \class{RExec} class.
42
43\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
44If it is omitted or \code{None}, the default \class{RHooks} class is
45instantiated.
46Whenever the \module{rexec} module searches for a module (even a
47built-in one) or reads a module's code, it doesn't actually go out to
48the file system itself. Rather, it calls methods of an \class{RHooks}
49instance that was passed to or created by its constructor. (Actually,
50the \class{RExec} object doesn't make these calls --- they are made by
51a module loader object that's part of the \class{RExec} object. This
52allows another level of flexibility, which can be useful when changing
53the mechanics of \keyword{import} within the restricted environment.)
54
55By providing an alternate \class{RHooks} object, we can control the
56file system accesses made to import a module, without changing the
57actual algorithm that controls the order in which those accesses are
58made. For instance, we could substitute an \class{RHooks} object that
59passes all filesystem requests to a file server elsewhere, via some
60RPC mechanism such as ILU. Grail's applet loader uses this to support
61importing applets from a URL for a directory.
62
63If \var{verbose} is true, additional debugging output may be sent to
64standard output.
65\end{classdesc}
66
67It is important to be aware that code running in a restricted
68environment can still call the \function{sys.exit()} function. To
69disallow restricted code from exiting the interpreter, always protect
70calls that cause restricted code to run with a
71\keyword{try}/\keyword{except} statement that catches the
72\exception{SystemExit} exception. Removing the \function{sys.exit()}
73function from the restricted environment is not sufficient --- the
74restricted code could still use \code{raise SystemExit}. Removing
75\exception{SystemExit} is not a reasonable option; some library code
76makes use of this and would break were it not available.
77
78
79\begin{seealso}
80 \seetitle[http://grail.sourceforge.net/]{Grail Home Page}{Grail is a
81 Web browser written entirely in Python. It uses the
82 \module{rexec} module as a foundation for supporting
83 Python applets, and can be used as an example usage of
84 this module.}
85\end{seealso}
86
87
88\subsection{RExec Objects \label{rexec-objects}}
89
90\class{RExec} instances support the following methods:
91
92\begin{methoddesc}{r_eval}{code}
93\var{code} must either be a string containing a Python expression, or
94a compiled code object, which will be evaluated in the restricted
95environment's \module{__main__} module. The value of the expression or
96code object will be returned.
97\end{methoddesc}
98
99\begin{methoddesc}{r_exec}{code}
100\var{code} must either be a string containing one or more lines of
101Python code, or a compiled code object, which will be executed in the
102restricted environment's \module{__main__} module.
103\end{methoddesc}
104
105\begin{methoddesc}{r_execfile}{filename}
106Execute the Python code contained in the file \var{filename} in the
107restricted environment's \module{__main__} module.
108\end{methoddesc}
109
110Methods whose names begin with \samp{s_} are similar to the functions
111beginning with \samp{r_}, but the code will be granted access to
112restricted versions of the standard I/O streams \code{sys.stdin},
113\code{sys.stderr}, and \code{sys.stdout}.
114
115\begin{methoddesc}{s_eval}{code}
116\var{code} must be a string containing a Python expression, which will
117be evaluated in the restricted environment.
118\end{methoddesc}
119
120\begin{methoddesc}{s_exec}{code}
121\var{code} must be a string containing one or more lines of Python code,
122which will be executed in the restricted environment.
123\end{methoddesc}
124
125\begin{methoddesc}{s_execfile}{code}
126Execute the Python code contained in the file \var{filename} in the
127restricted environment.
128\end{methoddesc}
129
130\class{RExec} objects must also support various methods which will be
131implicitly called by code executing in the restricted environment.
132Overriding these methods in a subclass is used to change the policies
133enforced by a restricted environment.
134
135\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
136 locals\optional{, fromlist}}}}
137Import the module \var{modulename}, raising an \exception{ImportError}
138exception if the module is considered unsafe.
139\end{methoddesc}
140
141\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
142Method called when \function{open()} is called in the restricted
143environment. The arguments are identical to those of \function{open()},
144and a file object (or a class instance compatible with file objects)
145should be returned. \class{RExec}'s default behaviour is allow opening
146any file for reading, but forbidding any attempt to write a file. See
147the example below for an implementation of a less restrictive
148\method{r_open()}.
149\end{methoddesc}
150
151\begin{methoddesc}{r_reload}{module}
152Reload the module object \var{module}, re-parsing and re-initializing it.
153\end{methoddesc}
154
155\begin{methoddesc}{r_unload}{module}
156Unload the module object \var{module} (remove it from the
157restricted environment's \code{sys.modules} dictionary).
158\end{methoddesc}
159
160And their equivalents with access to restricted standard I/O streams:
161
162\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
163 locals\optional{, fromlist}}}}
164Import the module \var{modulename}, raising an \exception{ImportError}
165exception if the module is considered unsafe.
166\end{methoddesc}
167
168\begin{methoddesc}{s_reload}{module}
169Reload the module object \var{module}, re-parsing and re-initializing it.
170\end{methoddesc}
171
172\begin{methoddesc}{s_unload}{module}
173Unload the module object \var{module}.
174% XXX what are the semantics of this?
175\end{methoddesc}
176
177
178\subsection{Defining restricted environments \label{rexec-extension}}
179
180The \class{RExec} class has the following class attributes, which are
181used by the \method{__init__()} method. Changing them on an existing
182instance won't have any effect; instead, create a subclass of
183\class{RExec} and assign them new values in the class definition.
184Instances of the new class will then use those new values. All these
185attributes are tuples of strings.
186
187\begin{memberdesc}{nok_builtin_names}
188Contains the names of built-in functions which will \emph{not} be
189available to programs running in the restricted environment. The
190value for \class{RExec} is \code{('open', 'reload', '__import__')}.
191(This gives the exceptions, because by far the majority of built-in
192functions are harmless. A subclass that wants to override this
193variable should probably start with the value from the base class and
194concatenate additional forbidden functions --- when new dangerous
195built-in functions are added to Python, they will also be added to
196this module.)
197\end{memberdesc}
198
199\begin{memberdesc}{ok_builtin_modules}
200Contains the names of built-in modules which can be safely imported.
201The value for \class{RExec} is \code{('audioop', 'array', 'binascii',
202'cmath', 'errno', 'imageop', 'marshal', 'math', 'md5', 'operator',
203'parser', 'regex', 'select', 'sha', '_sre', 'strop',
204'struct', 'time')}. A similar remark about overriding this variable
205applies --- use the value from the base class as a starting point.
206\end{memberdesc}
207
208\begin{memberdesc}{ok_path}
209Contains the directories which will be searched when an \keyword{import}
210is performed in the restricted environment.
211The value for \class{RExec} is the same as \code{sys.path} (at the time
212the module is loaded) for unrestricted code.
213\end{memberdesc}
214
215\begin{memberdesc}{ok_posix_names}
216% Should this be called ok_os_names?
217Contains the names of the functions in the \refmodule{os} module which will be
218available to programs running in the restricted environment. The
219value for \class{RExec} is \code{('error', 'fstat', 'listdir',
220'lstat', 'readlink', 'stat', 'times', 'uname', 'getpid', 'getppid',
221'getcwd', 'getuid', 'getgid', 'geteuid', 'getegid')}.
222\end{memberdesc}
223
224\begin{memberdesc}{ok_sys_names}
225Contains the names of the functions and variables in the \refmodule{sys}
226module which will be available to programs running in the restricted
227environment. The value for \class{RExec} is \code{('ps1', 'ps2',
228'copyright', 'version', 'platform', 'exit', 'maxint')}.
229\end{memberdesc}
230
231\begin{memberdesc}{ok_file_types}
232Contains the file types from which modules are allowed to be loaded.
233Each file type is an integer constant defined in the \refmodule{imp} module.
234The meaningful values are \constant{PY_SOURCE}, \constant{PY_COMPILED}, and
235\constant{C_EXTENSION}. The value for \class{RExec} is \code{(C_EXTENSION,
236PY_SOURCE)}. Adding \constant{PY_COMPILED} in subclasses is not recommended;
237an attacker could exit the restricted execution mode by putting a forged
238byte-compiled file (\file{.pyc}) anywhere in your file system, for example
239by writing it to \file{/tmp} or uploading it to the \file{/incoming}
240directory of your public FTP server.
241\end{memberdesc}
242
243
244\subsection{An example}
245
246Let us say that we want a slightly more relaxed policy than the
247standard \class{RExec} class. For example, if we're willing to allow
248files in \file{/tmp} to be written, we can subclass the \class{RExec}
249class:
250
251\begin{verbatim}
252class TmpWriterRExec(rexec.RExec):
253 def r_open(self, file, mode='r', buf=-1):
254 if mode in ('r', 'rb'):
255 pass
256 elif mode in ('w', 'wb', 'a', 'ab'):
257 # check filename : must begin with /tmp/
258 if file[:5]!='/tmp/':
259 raise IOError, "can't write outside /tmp"
260 elif (string.find(file, '/../') >= 0 or
261 file[:3] == '../' or file[-3:] == '/..'):
262 raise IOError, "'..' in filename forbidden"
263 else: raise IOError, "Illegal open() mode"
264 return open(file, mode, buf)
265\end{verbatim}
266%
267Notice that the above code will occasionally forbid a perfectly valid
268filename; for example, code in the restricted environment won't be
269able to open a file called \file{/tmp/foo/../bar}. To fix this, the
270\method{r_open()} method would have to simplify the filename to
271\file{/tmp/bar}, which would require splitting apart the filename and
272performing various operations on it. In cases where security is at
273stake, it may be preferable to write simple code which is sometimes
274overly restrictive, instead of more general code that is also more
275complex and may harbor a subtle security hole.
Note: See TracBrowser for help on using the repository browser.