/Doc/library/rexec.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 289 lines · 194 code · 95 blank · 0 comment · 0 complexity · 504d0fe3f6013f835ecc1447f7d08101 MD5 · raw file

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