1 \section{\module{rexec
} ---
2 Basic restricted execution framework.
}
3 \declaremodule{standard
}{rexec
}
5 \modulesynopsis{Basic restricted execution framework.
}
9 This module contains the
\class{RExec
} class, which supports
10 \method{r_eval()
},
\method{r_execfile()
},
\method{r_exec()
}, and
11 \method{r_import()
} methods, which are restricted versions of the standard
12 Python functions
\method{eval()
},
\method{execfile()
} and
13 the
\keyword{exec
} and
\keyword{import
} statements.
14 Code executed in this restricted environment will
15 only have access to modules and functions that are deemed safe; you
16 can subclass
\class{RExec
} to add or remove capabilities as desired.
18 \emph{Note:
} The
\class{RExec
} class can prevent code from performing
19 unsafe operations like reading or writing disk files, or using TCP/IP
20 sockets. However, it does not protect against code using extremely
21 large amounts of memory or CPU time.
23 \begin{classdesc
}{RExec
}{\optional{hooks
\optional{, verbose
}}}
24 Returns an instance of the
\class{RExec
} class.
26 \var{hooks
} is an instance of the
\class{RHooks
} class or a subclass of it.
27 If it is omitted or
\code{None
}, the default
\class{RHooks
} class is
29 Whenever the
\module{RExec
} module searches for a module (even a
30 built-in one) or reads a module's code, it doesn't actually go out to
31 the file system itself. Rather, it calls methods of an
\class{RHooks
}
32 instance that was passed to or created by its constructor. (Actually,
33 the
\class{RExec
} object doesn't make these calls --- they are made by
34 a module loader object that's part of the
\class{RExec
} object. This
35 allows another level of flexibility, e.g. using packages.)
37 By providing an alternate
\class{RHooks
} object, we can control the
38 file system accesses made to import a module, without changing the
39 actual algorithm that controls the order in which those accesses are
40 made. For instance, we could substitute an
\class{RHooks
} object that
41 passes all filesystem requests to a file server elsewhere, via some
42 RPC mechanism such as ILU. Grail's applet loader uses this to support
43 importing applets from a URL for a directory.
45 If
\var{verbose
} is true, additional debugging output may be sent to
49 The
\class{RExec
} class has the following class attributes, which are
50 used by the
\method{__init__()
} method. Changing them on an existing
51 instance won't have any effect; instead, create a subclass of
52 \class{RExec
} and assign them new values in the class definition.
53 Instances of the new class will then use those new values. All these
54 attributes are tuples of strings.
56 \begin{memberdesc
}{nok_builtin_names
}
57 Contains the names of built-in functions which will
\emph{not
} be
58 available to programs running in the restricted environment. The
59 value for
\class{RExec
} is
\code{('open',
} \code{'reload',
}
60 \code{'__import__')
}. (This gives the exceptions, because by far the
61 majority of built-in functions are harmless. A subclass that wants to
62 override this variable should probably start with the value from the
63 base class and concatenate additional forbidden functions --- when new
64 dangerous built-in functions are added to Python, they will also be
65 added to this module.)
68 \begin{memberdesc
}{ok_builtin_modules
}
69 Contains the names of built-in modules which can be safely imported.
70 The value for
\class{RExec
} is
\code{('audioop',
} \code{'array',
}
71 \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
75 about overriding this variable applies --- use the value from the base
76 class as a starting point.
79 \begin{memberdesc
}{ok_path
}
80 Contains the directories which will be searched when an
\keyword{import
}
81 is performed in the restricted environment.
82 The value for
\class{RExec
} is the same as
\code{sys.path
} (at the time
83 the module is loaded) for unrestricted code.
86 \begin{memberdesc
}{ok_posix_names
}
87 % Should this be called ok_os_names?
88 Contains the names of the functions in the
\module{os
} module which will be
89 available to programs running in the restricted environment. The
90 value for
\class{RExec
} is
\code{('error',
} \code{'fstat',
}
91 \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',
}
97 \begin{memberdesc
}{ok_sys_names
}
98 Contains the names of the functions and variables in the
\module{sys
}
99 module which will be available to programs running in the restricted
100 environment. The value for
\class{RExec
} is
\code{('ps1',
}
101 \code{'ps2',
} \code{'copyright',
} \code{'version',
} \code{'platform',
}
102 \code{'exit',
} \code{'maxint')
}.
106 \class{RExec
} instances support the following methods:
108 \begin{methoddesc
}{r_eval
}{code
}
109 \var{code
} must either be a string containing a Python expression, or
110 a compiled code object, which will be evaluated in the restricted
111 environment's
\module{__main__
} module. The value of the expression or
112 code object will be returned.
115 \begin{methoddesc
}{r_exec
}{code
}
116 \var{code
} must either be a string containing one or more lines of
117 Python code, or a compiled code object, which will be executed in the
118 restricted environment's
\module{__main__
} module.
121 \begin{methoddesc
}{r_execfile
}{filename
}
122 Execute the Python code contained in the file
\var{filename
} in the
123 restricted environment's
\module{__main__
} module.
126 Methods whose names begin with
\samp{s_
} are similar to the functions
127 beginning with
\samp{r_
}, but the code will be granted access to
128 restricted versions of the standard I/O streams
\code{sys.stdin
},
129 \code{sys.stderr
}, and
\code{sys.stdout
}.
131 \begin{methoddesc
}{s_eval
}{code
}
132 \var{code
} must be a string containing a Python expression, which will
133 be evaluated in the restricted environment.
136 \begin{methoddesc
}{s_exec
}{code
}
137 \var{code
} must be a string containing one or more lines of Python code,
138 which will be executed in the restricted environment.
141 \begin{methoddesc
}{s_execfile
}{code
}
142 Execute the Python code contained in the file
\var{filename
} in the
143 restricted environment.
146 \class{RExec
} objects must also support various methods which will be
147 implicitly called by code executing in the restricted environment.
148 Overriding these methods in a subclass is used to change the policies
149 enforced by a restricted environment.
151 \begin{methoddesc
}{r_import
}{modulename
\optional{, globals
\optional{,
152 locals
\optional{, fromlist
}}}}
153 Import the module
\var{modulename
}, raising an
\exception{ImportError
}
154 exception if the module is considered unsafe.
157 \begin{methoddesc
}{r_open
}{filename
\optional{, mode
\optional{, bufsize
}}}
158 Method called when
\function{open()
} is called in the restricted
159 environment. The arguments are identical to those of
\function{open()
},
160 and a file object (or a class instance compatible with file objects)
161 should be returned.
\class{RExec
}'s default behaviour is allow opening
162 any file for reading, but forbidding any attempt to write a file. See
163 the example below for an implementation of a less restrictive
167 \begin{methoddesc
}{r_reload
}{module
}
168 Reload the module object
\var{module
}, re-parsing and re-initializing it.
171 \begin{methoddesc
}{r_unload
}{module
}
172 Unload the module object
\var{module
} (i.e., remove it from the
173 restricted environment's
\code{sys.modules
} dictionary).
176 And their equivalents with access to restricted standard I/O streams:
178 \begin{methoddesc
}{s_import
}{modulename
\optional{, globals
\optional{,
179 locals
\optional{, fromlist
}}}}
180 Import the module
\var{modulename
}, raising an
\exception{ImportError
}
181 exception if the module is considered unsafe.
184 \begin{methoddesc
}{s_reload
}{module
}
185 Reload the module object
\var{module
}, re-parsing and re-initializing it.
188 \begin{methoddesc
}{s_unload
}{module
}
189 Unload the module object
\var{module
}.
190 % XXX what are the semantics of this?
193 \subsection{An example
}
195 Let us say that we want a slightly more relaxed policy than the
196 standard
\class{RExec
} class. For example, if we're willing to allow
197 files in
\file{/tmp
} to be written, we can subclass the
\class{RExec
}
201 class TmpWriterRExec(rexec.RExec):
202 def r_open(self, file, mode='r', buf=-
1):
203 if mode in ('r', 'rb'):
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"
213 return open(file, mode, buf)
216 Notice that the above code will occasionally forbid a perfectly valid
217 filename; for example, code in the restricted environment won't be
218 able to open a file called
\file{/tmp/foo/../bar
}. To fix this, the
219 \method{r_open()
} method would have to simplify the filename to
220 \file{/tmp/bar
}, which would require splitting apart the filename and
221 performing various operations on it. In cases where security is at
222 stake, it may be preferable to write simple code which is sometimes
223 overly restrictive, instead of more general code that is also more
224 complex and may harbor a subtle security hole.