blob: 1c4c77c2e80353dc5e3e856be85f7aae70245652 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{rexec} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 Restricted execution framework}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{standard}{rexec}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Basic restricted execution framework.}
6
Fred Drakea8912301998-03-14 07:08:02 +00007
Guido van Rossumbe0a8a61996-09-10 17:37:05 +00008
Fred Drake19479911998-02-13 06:58:54 +00009This module contains the \class{RExec} class, which supports
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000010\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
Fred Drakea8912301998-03-14 07:08:02 +000011\method{r_import()} methods, which are restricted versions of the standard
Guido van Rossum7b0c9d81998-05-08 13:27:38 +000012Python functions \method{eval()}, \method{execfile()} and
13the \keyword{exec} and \keyword{import} statements.
Guido van Rossumf73f79b1996-10-24 22:14:06 +000014Code executed in this restricted environment will
Guido van Rossum095538d1996-10-22 01:11:19 +000015only have access to modules and functions that are deemed safe; you
Fred Drake19479911998-02-13 06:58:54 +000016can subclass \class{RExec} to add or remove capabilities as desired.
Guido van Rossum095538d1996-10-22 01:11:19 +000017
Fred Drake19479911998-02-13 06:58:54 +000018\emph{Note:} The \class{RExec} class can prevent code from performing
Guido van Rossum095538d1996-10-22 01:11:19 +000019unsafe operations like reading or writing disk files, or using TCP/IP
20sockets. However, it does not protect against code using extremely
21large amounts of memory or CPU time.
Guido van Rossum095538d1996-10-22 01:11:19 +000022
Fred Drakea8912301998-03-14 07:08:02 +000023\begin{classdesc}{RExec}{\optional{hooks\optional{, verbose}}}
Fred Drake19479911998-02-13 06:58:54 +000024Returns an instance of the \class{RExec} class.
Guido van Rossum095538d1996-10-22 01:11:19 +000025
Fred Drakea8912301998-03-14 07:08:02 +000026\var{hooks} is an instance of the \class{RHooks} class or a subclass of it.
27If it is omitted or \code{None}, the default \class{RHooks} class is
Guido van Rossumf73f79b1996-10-24 22:14:06 +000028instantiated.
Fred Drakeffbe6871999-04-22 21:23:22 +000029Whenever the \module{rexec} module searches for a module (even a
Fred Drake19479911998-02-13 06:58:54 +000030built-in one) or reads a module's code, it doesn't actually go out to
31the file system itself. Rather, it calls methods of an \class{RHooks}
32instance that was passed to or created by its constructor. (Actually,
33the \class{RExec} object doesn't make these calls --- they are made by
34a module loader object that's part of the \class{RExec} object. This
35allows another level of flexibility, e.g. using packages.)
Guido van Rossum095538d1996-10-22 01:11:19 +000036
Fred Drake19479911998-02-13 06:58:54 +000037By providing an alternate \class{RHooks} object, we can control the
Guido van Rossum095538d1996-10-22 01:11:19 +000038file system accesses made to import a module, without changing the
39actual algorithm that controls the order in which those accesses are
Fred Drake19479911998-02-13 06:58:54 +000040made. For instance, we could substitute an \class{RHooks} object that
41passes all filesystem requests to a file server elsewhere, via some
42RPC mechanism such as ILU. Grail's applet loader uses this to support
Guido van Rossum095538d1996-10-22 01:11:19 +000043importing applets from a URL for a directory.
44
Guido van Rossumf73f79b1996-10-24 22:14:06 +000045If \var{verbose} is true, additional debugging output may be sent to
Guido van Rossum095538d1996-10-22 01:11:19 +000046standard output.
Fred Drakea8912301998-03-14 07:08:02 +000047\end{classdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000048
Fred Drake19479911998-02-13 06:58:54 +000049The \class{RExec} class has the following class attributes, which are
Fred Drakea8912301998-03-14 07:08:02 +000050used by the \method{__init__()} method. Changing them on an existing
Fred Drake19479911998-02-13 06:58:54 +000051instance won't have any effect; instead, create a subclass of
52\class{RExec} and assign them new values in the class definition.
53Instances of the new class will then use those new values. All these
54attributes are tuples of strings.
Guido van Rossum095538d1996-10-22 01:11:19 +000055
Fred Drakeda70ee11998-04-02 18:51:30 +000056\begin{memberdesc}{nok_builtin_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000057Contains the names of built-in functions which will \emph{not} be
Guido van Rossumf73f79b1996-10-24 22:14:06 +000058available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000059value for \class{RExec} is \code{('open',} \code{'reload',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000060\code{'__import__')}. (This gives the exceptions, because by far the
61majority of built-in functions are harmless. A subclass that wants to
62override this variable should probably start with the value from the
63base class and concatenate additional forbidden functions --- when new
64dangerous built-in functions are added to Python, they will also be
65added to this module.)
Fred Drakeda70ee11998-04-02 18:51:30 +000066\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000067
Fred Drakeda70ee11998-04-02 18:51:30 +000068\begin{memberdesc}{ok_builtin_modules}
Guido van Rossum095538d1996-10-22 01:11:19 +000069Contains the names of built-in modules which can be safely imported.
Fred Drake19479911998-02-13 06:58:54 +000070The value for \class{RExec} is \code{('audioop',} \code{'array',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000071\code{'binascii',} \code{'cmath',} \code{'errno',} \code{'imageop',}
72\code{'marshal',} \code{'math',} \code{'md5',} \code{'operator',}
73\code{'parser',} \code{'regex',} \code{'rotor',} \code{'select',}
74\code{'strop',} \code{'struct',} \code{'time')}. A similar remark
75about overriding this variable applies --- use the value from the base
76class as a starting point.
Fred Drakeda70ee11998-04-02 18:51:30 +000077\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000078
Fred Drakeda70ee11998-04-02 18:51:30 +000079\begin{memberdesc}{ok_path}
Fred Drakea8912301998-03-14 07:08:02 +000080Contains the directories which will be searched when an \keyword{import}
Guido van Rossum095538d1996-10-22 01:11:19 +000081is performed in the restricted environment.
Fred Drake19479911998-02-13 06:58:54 +000082The value for \class{RExec} is the same as \code{sys.path} (at the time
Guido van Rossumf73f79b1996-10-24 22:14:06 +000083the module is loaded) for unrestricted code.
Fred Drakeda70ee11998-04-02 18:51:30 +000084\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000085
Fred Drakeda70ee11998-04-02 18:51:30 +000086\begin{memberdesc}{ok_posix_names}
Guido van Rossum095538d1996-10-22 01:11:19 +000087% Should this be called ok_os_names?
Fred Drakeffbe6871999-04-22 21:23:22 +000088Contains the names of the functions in the \refmodule{os} module which will be
Guido van Rossum095538d1996-10-22 01:11:19 +000089available to programs running in the restricted environment. The
Fred Drake19479911998-02-13 06:58:54 +000090value for \class{RExec} is \code{('error',} \code{'fstat',}
Guido van Rossum095538d1996-10-22 01:11:19 +000091\code{'listdir',} \code{'lstat',} \code{'readlink',} \code{'stat',}
92\code{'times',} \code{'uname',} \code{'getpid',} \code{'getppid',}
93\code{'getcwd',} \code{'getuid',} \code{'getgid',} \code{'geteuid',}
94\code{'getegid')}.
Fred Drakeda70ee11998-04-02 18:51:30 +000095\end{memberdesc}
Guido van Rossum095538d1996-10-22 01:11:19 +000096
Fred Drakeda70ee11998-04-02 18:51:30 +000097\begin{memberdesc}{ok_sys_names}
Fred Drakeffbe6871999-04-22 21:23:22 +000098Contains the names of the functions and variables in the \refmodule{sys}
Guido van Rossumf73f79b1996-10-24 22:14:06 +000099module which will be available to programs running in the restricted
Fred Drake19479911998-02-13 06:58:54 +0000100environment. The value for \class{RExec} is \code{('ps1',}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000101\code{'ps2',} \code{'copyright',} \code{'version',} \code{'platform',}
102\code{'exit',} \code{'maxint')}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000103\end{memberdesc}
104
Guido van Rossum095538d1996-10-22 01:11:19 +0000105
Fred Drake19479911998-02-13 06:58:54 +0000106\class{RExec} instances support the following methods:
Guido van Rossum095538d1996-10-22 01:11:19 +0000107
Fred Drakeda70ee11998-04-02 18:51:30 +0000108\begin{methoddesc}{r_eval}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000109\var{code} must either be a string containing a Python expression, or
110a compiled code object, which will be evaluated in the restricted
Fred Drakea8912301998-03-14 07:08:02 +0000111environment's \module{__main__} module. The value of the expression or
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000112code object will be returned.
Fred Drakeda70ee11998-04-02 18:51:30 +0000113\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000114
Fred Drakeda70ee11998-04-02 18:51:30 +0000115\begin{methoddesc}{r_exec}{code}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000116\var{code} must either be a string containing one or more lines of
117Python code, or a compiled code object, which will be executed in the
Fred Drakea8912301998-03-14 07:08:02 +0000118restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000119\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000120
Fred Drakeda70ee11998-04-02 18:51:30 +0000121\begin{methoddesc}{r_execfile}{filename}
Guido van Rossum095538d1996-10-22 01:11:19 +0000122Execute the Python code contained in the file \var{filename} in the
Fred Drakea8912301998-03-14 07:08:02 +0000123restricted environment's \module{__main__} module.
Fred Drakeda70ee11998-04-02 18:51:30 +0000124\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000125
Fred Drakea8912301998-03-14 07:08:02 +0000126Methods whose names begin with \samp{s_} are similar to the functions
127beginning with \samp{r_}, but the code will be granted access to
Fred Drake71f894a1998-02-23 14:37:40 +0000128restricted versions of the standard I/O streams \code{sys.stdin},
Fred Drakea8912301998-03-14 07:08:02 +0000129\code{sys.stderr}, and \code{sys.stdout}.
Guido van Rossum095538d1996-10-22 01:11:19 +0000130
Fred Drakeda70ee11998-04-02 18:51:30 +0000131\begin{methoddesc}{s_eval}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000132\var{code} must be a string containing a Python expression, which will
133be evaluated in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000134\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000135
Fred Drakeda70ee11998-04-02 18:51:30 +0000136\begin{methoddesc}{s_exec}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000137\var{code} must be a string containing one or more lines of Python code,
138which will be executed in the restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000139\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000140
Fred Drakeda70ee11998-04-02 18:51:30 +0000141\begin{methoddesc}{s_execfile}{code}
Guido van Rossum095538d1996-10-22 01:11:19 +0000142Execute the Python code contained in the file \var{filename} in the
143restricted environment.
Fred Drakeda70ee11998-04-02 18:51:30 +0000144\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000145
Fred Drake19479911998-02-13 06:58:54 +0000146\class{RExec} objects must also support various methods which will be
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000147implicitly called by code executing in the restricted environment.
148Overriding these methods in a subclass is used to change the policies
149enforced by a restricted environment.
Guido van Rossum095538d1996-10-22 01:11:19 +0000150
Fred Drakeda70ee11998-04-02 18:51:30 +0000151\begin{methoddesc}{r_import}{modulename\optional{, globals\optional{,
152 locals\optional{, fromlist}}}}
Fred Drakea8912301998-03-14 07:08:02 +0000153Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000154exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000155\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000156
Fred Drakeda70ee11998-04-02 18:51:30 +0000157\begin{methoddesc}{r_open}{filename\optional{, mode\optional{, bufsize}}}
Fred Drakea8912301998-03-14 07:08:02 +0000158Method called when \function{open()} is called in the restricted
159environment. The arguments are identical to those of \function{open()},
Guido van Rossum095538d1996-10-22 01:11:19 +0000160and a file object (or a class instance compatible with file objects)
Fred Drake19479911998-02-13 06:58:54 +0000161should be returned. \class{RExec}'s default behaviour is allow opening
Guido van Rossum095538d1996-10-22 01:11:19 +0000162any file for reading, but forbidding any attempt to write a file. See
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000163the example below for an implementation of a less restrictive
Fred Drakea8912301998-03-14 07:08:02 +0000164\method{r_open()}.
Fred Drakeda70ee11998-04-02 18:51:30 +0000165\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000166
Fred Drakeda70ee11998-04-02 18:51:30 +0000167\begin{methoddesc}{r_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000168Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000169\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000170
Fred Drakeda70ee11998-04-02 18:51:30 +0000171\begin{methoddesc}{r_unload}{module}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000172Unload the module object \var{module} (i.e., remove it from the
173restricted environment's \code{sys.modules} dictionary).
Fred Drakeda70ee11998-04-02 18:51:30 +0000174\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000175
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000176And their equivalents with access to restricted standard I/O streams:
177
Fred Drakeda70ee11998-04-02 18:51:30 +0000178\begin{methoddesc}{s_import}{modulename\optional{, globals\optional{,
179 locals\optional{, fromlist}}}}
Fred Drake19479911998-02-13 06:58:54 +0000180Import the module \var{modulename}, raising an \exception{ImportError}
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000181exception if the module is considered unsafe.
Fred Drakeda70ee11998-04-02 18:51:30 +0000182\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000183
Fred Drakeda70ee11998-04-02 18:51:30 +0000184\begin{methoddesc}{s_reload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000185Reload the module object \var{module}, re-parsing and re-initializing it.
Fred Drakeda70ee11998-04-02 18:51:30 +0000186\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000187
Fred Drakeda70ee11998-04-02 18:51:30 +0000188\begin{methoddesc}{s_unload}{module}
Guido van Rossum095538d1996-10-22 01:11:19 +0000189Unload the module object \var{module}.
190% XXX what are the semantics of this?
Fred Drakeda70ee11998-04-02 18:51:30 +0000191\end{methoddesc}
Guido van Rossum095538d1996-10-22 01:11:19 +0000192
193\subsection{An example}
194
195Let us say that we want a slightly more relaxed policy than the
Fred Drake19479911998-02-13 06:58:54 +0000196standard \class{RExec} class. For example, if we're willing to allow
197files in \file{/tmp} to be written, we can subclass the \class{RExec}
198class:
Guido van Rossum095538d1996-10-22 01:11:19 +0000199
Fred Drake19479911998-02-13 06:58:54 +0000200\begin{verbatim}
Guido van Rossum095538d1996-10-22 01:11:19 +0000201class TmpWriterRExec(rexec.RExec):
202 def r_open(self, file, mode='r', buf=-1):
Guido van Rossumf73f79b1996-10-24 22:14:06 +0000203 if mode in ('r', 'rb'):
204 pass
205 elif mode in ('w', 'wb', 'a', 'ab'):
206 # check filename : must begin with /tmp/
207 if file[:5]!='/tmp/':
208 raise IOError, "can't write outside /tmp"
209 elif (string.find(file, '/../') >= 0 or
210 file[:3] == '../' or file[-3:] == '/..'):
211 raise IOError, "'..' in filename forbidden"
212 else: raise IOError, "Illegal open() mode"
Guido van Rossum095538d1996-10-22 01:11:19 +0000213 return open(file, mode, buf)
Fred Drake19479911998-02-13 06:58:54 +0000214\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000215%
Guido van Rossum095538d1996-10-22 01:11:19 +0000216Notice that the above code will occasionally forbid a perfectly valid
217filename; for example, code in the restricted environment won't be
218able to open a file called \file{/tmp/foo/../bar}. To fix this, the
Fred Drakea8912301998-03-14 07:08:02 +0000219\method{r_open()} method would have to simplify the filename to
Guido van Rossum095538d1996-10-22 01:11:19 +0000220\file{/tmp/bar}, which would require splitting apart the filename and
221performing various operations on it. In cases where security is at
222stake, it may be preferable to write simple code which is sometimes
223overly restrictive, instead of more general code that is also more
224complex and may harbor a subtle security hole.