cpython/Doc/library/rexec.rst

291 lines
12 KiB
ReStructuredText
Raw Normal View History

2007-08-15 11:28:01 -03:00
:mod:`rexec` --- Restricted execution framework
===============================================
.. module:: rexec
:synopsis: Basic restricted execution framework.
2008-05-09 23:27:04 -03:00
:deprecated:
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line fill in actual issue number in tests ........ r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open file with `str' filename on Windows. ........ r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line fix highlighting ........ r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines welcome to 2009, Python! ........ r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines #4801 _collections module fails to build on cygwin. _PyObject_GC_TRACK is the macro version of PyObject_GC_Track, and according to documentation it should not be used for extension modules. ........ r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue4472: "configure --enable-shared doesn't work on OSX" ........ r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines Forgot to add a NEWS item in my previous checkin ........ r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue4780 ........ r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue 1627952 ........ r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue r1737832 ........ r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines Fix for issue 1149804 ........ r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines Fix for issue 4472 is incompatible with Cygwin, this patch should fix that. ........ r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line document PyMemberDef ........ r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines #4811: fix markup glitches (mostly remains of the conversion), found by Gabriel Genellina. ........ r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines Issue #4075: Use OutputDebugStringW in Py_FatalError. ........ r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines Issue #4051: Prevent conflict of UNICODE macros in cPickle. ........ r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line fix compilation on non-Windows platforms ........ r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line Issue #4615. Document how to use itertools for de-duping. ........ r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines Remove useless string literal. ........ r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines Fix indentation. ........ r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines Set eol-style correctly for mp_distributing.py. ........ r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines Make indentation consistent. ........ r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines Fix role name. ........ r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources. ........ r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines Recognize usage of the default role. ........ r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines Fix uses of the default role. ........ r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines Remove trailing whitespace. ........ r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines Remove tabs from the documentation. ........ r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines Disable the line length checker by default. ........
2009-01-03 17:55:17 -04:00
2008-05-09 23:27:04 -03:00
.. deprecated:: 2.6
The :mod:`rexec` module has been removed in Python 3.0.
2007-08-15 11:28:01 -03:00
.. versionchanged:: 2.3
Disabled module.
.. warning::
The documentation has been left in place to help in reading old code that uses
the module.
This module contains the :class:`RExec` class, which supports :meth:`r_eval`,
:meth:`r_execfile`, :meth:`r_exec`, and :meth:`r_import` methods, which are
restricted versions of the standard Python functions :meth:`eval`,
:meth:`execfile` and the :keyword:`exec` and :keyword:`import` statements. Code
executed in this restricted environment will only have access to modules and
functions that are deemed safe; you can subclass :class:`RExec` to add or remove
capabilities as desired.
.. warning::
While the :mod:`rexec` module is designed to perform as described below, it does
have a few known vulnerabilities which could be exploited by carefully written
code. Thus it should not be relied upon in situations requiring "production
ready" security. In such situations, execution via sub-processes or very
careful "cleansing" of both code and data to be processed may be necessary.
Alternatively, help in patching known :mod:`rexec` vulnerabilities would be
welcomed.
.. note::
The :class:`RExec` class can prevent code from performing unsafe operations like
reading or writing disk files, or using TCP/IP sockets. However, it does not
protect against code using extremely large amounts of memory or processor time.
.. class:: RExec([hooks[, verbose]])
Returns an instance of the :class:`RExec` class.
*hooks* is an instance of the :class:`RHooks` class or a subclass of it. If it
is omitted or ``None``, the default :class:`RHooks` class is instantiated.
Whenever the :mod:`rexec` module searches for a module (even a built-in one) or
reads a module's code, it doesn't actually go out to the file system itself.
Rather, it calls methods of an :class:`RHooks` instance that was passed to or
created by its constructor. (Actually, the :class:`RExec` object doesn't make
these calls --- they are made by a module loader object that's part of the
:class:`RExec` object. This allows another level of flexibility, which can be
useful when changing the mechanics of :keyword:`import` within the restricted
environment.)
By providing an alternate :class:`RHooks` object, we can control the file system
accesses made to import a module, without changing the actual algorithm that
controls the order in which those accesses are made. For instance, we could
substitute an :class:`RHooks` object that passes all filesystem requests to a
file server elsewhere, via some RPC mechanism such as ILU. Grail's applet
loader uses this to support importing applets from a URL for a directory.
If *verbose* is true, additional debugging output may be sent to standard
output.
It is important to be aware that code running in a restricted environment can
still call the :func:`sys.exit` function. To disallow restricted code from
exiting the interpreter, always protect calls that cause restricted code to run
with a :keyword:`try`/:keyword:`except` statement that catches the
:exc:`SystemExit` exception. Removing the :func:`sys.exit` function from the
restricted environment is not sufficient --- the restricted code could still use
``raise SystemExit``. Removing :exc:`SystemExit` is not a reasonable option;
some library code makes use of this and would break were it not available.
.. seealso::
`Grail Home Page <http://grail.sourceforge.net/>`_
Grail is a Web browser written entirely in Python. It uses the :mod:`rexec`
module as a foundation for supporting Python applets, and can be used as an
example usage of this module.
.. _rexec-objects:
RExec Objects
-------------
:class:`RExec` instances support the following methods:
.. method:: RExec.r_eval(code)
*code* must either be a string containing a Python expression, or a compiled
code object, which will be evaluated in the restricted environment's
:mod:`__main__` module. The value of the expression or code object will be
returned.
.. method:: RExec.r_exec(code)
*code* must either be a string containing one or more lines of Python code, or a
compiled code object, which will be executed in the restricted environment's
:mod:`__main__` module.
.. method:: RExec.r_execfile(filename)
Execute the Python code contained in the file *filename* in the restricted
environment's :mod:`__main__` module.
Methods whose names begin with ``s_`` are similar to the functions beginning
with ``r_``, but the code will be granted access to restricted versions of the
standard I/O streams ``sys.stdin``, ``sys.stderr``, and ``sys.stdout``.
.. method:: RExec.s_eval(code)
*code* must be a string containing a Python expression, which will be evaluated
in the restricted environment.
.. method:: RExec.s_exec(code)
*code* must be a string containing one or more lines of Python code, which will
be executed in the restricted environment.
.. method:: RExec.s_execfile(code)
Execute the Python code contained in the file *filename* in the restricted
environment.
:class:`RExec` objects must also support various methods which will be
implicitly called by code executing in the restricted environment. Overriding
these methods in a subclass is used to change the policies enforced by a
restricted environment.
.. method:: RExec.r_import(modulename[, globals[, locals[, fromlist]]])
Import the module *modulename*, raising an :exc:`ImportError` exception if the
module is considered unsafe.
.. method:: RExec.r_open(filename[, mode[, bufsize]])
Method called when :func:`open` is called in the restricted environment. The
arguments are identical to those of :func:`open`, and a file object (or a class
instance compatible with file objects) should be returned. :class:`RExec`'s
default behaviour is allow opening any file for reading, but forbidding any
attempt to write a file. See the example below for an implementation of a less
restrictive :meth:`r_open`.
.. method:: RExec.r_reload(module)
Reload the module object *module*, re-parsing and re-initializing it.
.. method:: RExec.r_unload(module)
Unload the module object *module* (remove it from the restricted environment's
``sys.modules`` dictionary).
And their equivalents with access to restricted standard I/O streams:
.. method:: RExec.s_import(modulename[, globals[, locals[, fromlist]]])
Import the module *modulename*, raising an :exc:`ImportError` exception if the
module is considered unsafe.
.. method:: RExec.s_reload(module)
Reload the module object *module*, re-parsing and re-initializing it.
.. method:: RExec.s_unload(module)
Unload the module object *module*.
2007-12-29 06:57:00 -04:00
.. XXX what are the semantics of this?
2007-08-15 11:28:01 -03:00
.. _rexec-extension:
Defining restricted environments
--------------------------------
The :class:`RExec` class has the following class attributes, which are used by
the :meth:`__init__` method. Changing them on an existing instance won't have
any effect; instead, create a subclass of :class:`RExec` and assign them new
values in the class definition. Instances of the new class will then use those
new values. All these attributes are tuples of strings.
.. attribute:: RExec.nok_builtin_names
Contains the names of built-in functions which will *not* be available to
programs running in the restricted environment. The value for :class:`RExec` is
``('open', 'reload', '__import__')``. (This gives the exceptions, because by far
the majority of built-in functions are harmless. A subclass that wants to
override this variable should probably start with the value from the base class
and concatenate additional forbidden functions --- when new dangerous built-in
functions are added to Python, they will also be added to this module.)
.. attribute:: RExec.ok_builtin_modules
Contains the names of built-in modules which can be safely imported. The value
for :class:`RExec` is ``('audioop', 'array', 'binascii', 'cmath', 'errno',
'imageop', 'marshal', 'math', 'md5', 'operator', 'parser', 'regex', 'select',
'sha', '_sre', 'strop', 'struct', 'time')``. A similar remark about overriding
this variable applies --- use the value from the base class as a starting point.
.. attribute:: RExec.ok_path
Contains the directories which will be searched when an :keyword:`import` is
performed in the restricted environment. The value for :class:`RExec` is the
same as ``sys.path`` (at the time the module is loaded) for unrestricted code.
.. attribute:: RExec.ok_posix_names
Contains the names of the functions in the :mod:`os` module which will be
available to programs running in the restricted environment. The value for
:class:`RExec` is ``('error', 'fstat', 'listdir', 'lstat', 'readlink', 'stat',
'times', 'uname', 'getpid', 'getppid', 'getcwd', 'getuid', 'getgid', 'geteuid',
'getegid')``.
2007-12-29 06:57:00 -04:00
.. Should this be called ok_os_names?
2007-08-15 11:28:01 -03:00
.. attribute:: RExec.ok_sys_names
Contains the names of the functions and variables in the :mod:`sys` module which
will be available to programs running in the restricted environment. The value
for :class:`RExec` is ``('ps1', 'ps2', 'copyright', 'version', 'platform',
'exit', 'maxint')``.
.. attribute:: RExec.ok_file_types
Contains the file types from which modules are allowed to be loaded. Each file
type is an integer constant defined in the :mod:`imp` module. The meaningful
values are :const:`PY_SOURCE`, :const:`PY_COMPILED`, and :const:`C_EXTENSION`.
The value for :class:`RExec` is ``(C_EXTENSION, PY_SOURCE)``. Adding
:const:`PY_COMPILED` in subclasses is not recommended; an attacker could exit
the restricted execution mode by putting a forged byte-compiled file
(:file:`.pyc`) anywhere in your file system, for example by writing it to
:file:`/tmp` or uploading it to the :file:`/incoming` directory of your public
FTP server.
An example
----------
Let us say that we want a slightly more relaxed policy than the standard
:class:`RExec` class. For example, if we're willing to allow files in
:file:`/tmp` to be written, we can subclass the :class:`RExec` class::
class TmpWriterRExec(rexec.RExec):
def r_open(self, file, mode='r', buf=-1):
if mode in ('r', 'rb'):
pass
elif mode in ('w', 'wb', 'a', 'ab'):
# check filename : must begin with /tmp/
Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r68133 | antoine.pitrou | 2009-01-01 16:38:03 +0100 (Thu, 01 Jan 2009) | 1 line fill in actual issue number in tests ........ r68134 | hirokazu.yamamoto | 2009-01-01 16:45:39 +0100 (Thu, 01 Jan 2009) | 2 lines Issue #4797: IOError.filename was not set when _fileio.FileIO failed to open file with `str' filename on Windows. ........ r68141 | benjamin.peterson | 2009-01-01 17:43:12 +0100 (Thu, 01 Jan 2009) | 1 line fix highlighting ........ r68142 | benjamin.peterson | 2009-01-01 18:29:49 +0100 (Thu, 01 Jan 2009) | 2 lines welcome to 2009, Python! ........ r68145 | amaury.forgeotdarc | 2009-01-02 01:03:54 +0100 (Fri, 02 Jan 2009) | 5 lines #4801 _collections module fails to build on cygwin. _PyObject_GC_TRACK is the macro version of PyObject_GC_Track, and according to documentation it should not be used for extension modules. ........ r68146 | ronald.oussoren | 2009-01-02 11:44:46 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue4472: "configure --enable-shared doesn't work on OSX" ........ r68148 | ronald.oussoren | 2009-01-02 11:48:31 +0100 (Fri, 02 Jan 2009) | 2 lines Forgot to add a NEWS item in my previous checkin ........ r68149 | ronald.oussoren | 2009-01-02 11:50:48 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue4780 ........ r68159 | ronald.oussoren | 2009-01-02 15:48:17 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue 1627952 ........ r68160 | ronald.oussoren | 2009-01-02 15:52:09 +0100 (Fri, 02 Jan 2009) | 2 lines Fix for issue r1737832 ........ r68161 | ronald.oussoren | 2009-01-02 16:00:05 +0100 (Fri, 02 Jan 2009) | 3 lines Fix for issue 1149804 ........ r68162 | ronald.oussoren | 2009-01-02 16:06:00 +0100 (Fri, 02 Jan 2009) | 3 lines Fix for issue 4472 is incompatible with Cygwin, this patch should fix that. ........ r68166 | benjamin.peterson | 2009-01-02 19:26:23 +0100 (Fri, 02 Jan 2009) | 1 line document PyMemberDef ........ r68171 | georg.brandl | 2009-01-02 21:25:14 +0100 (Fri, 02 Jan 2009) | 3 lines #4811: fix markup glitches (mostly remains of the conversion), found by Gabriel Genellina. ........ r68172 | martin.v.loewis | 2009-01-02 21:32:55 +0100 (Fri, 02 Jan 2009) | 2 lines Issue #4075: Use OutputDebugStringW in Py_FatalError. ........ r68173 | martin.v.loewis | 2009-01-02 21:40:14 +0100 (Fri, 02 Jan 2009) | 2 lines Issue #4051: Prevent conflict of UNICODE macros in cPickle. ........ r68174 | benjamin.peterson | 2009-01-02 21:47:27 +0100 (Fri, 02 Jan 2009) | 1 line fix compilation on non-Windows platforms ........ r68179 | raymond.hettinger | 2009-01-02 22:26:45 +0100 (Fri, 02 Jan 2009) | 1 line Issue #4615. Document how to use itertools for de-duping. ........ r68195 | georg.brandl | 2009-01-03 14:45:15 +0100 (Sat, 03 Jan 2009) | 2 lines Remove useless string literal. ........ r68196 | georg.brandl | 2009-01-03 15:29:53 +0100 (Sat, 03 Jan 2009) | 2 lines Fix indentation. ........ r68210 | georg.brandl | 2009-01-03 20:10:12 +0100 (Sat, 03 Jan 2009) | 2 lines Set eol-style correctly for mp_distributing.py. ........ r68214 | georg.brandl | 2009-01-03 20:44:48 +0100 (Sat, 03 Jan 2009) | 2 lines Make indentation consistent. ........ r68215 | georg.brandl | 2009-01-03 21:15:14 +0100 (Sat, 03 Jan 2009) | 2 lines Fix role name. ........ r68217 | georg.brandl | 2009-01-03 21:30:15 +0100 (Sat, 03 Jan 2009) | 2 lines Add rstlint, a little tool to find subtle markup problems and inconsistencies in the Doc sources. ........ r68218 | georg.brandl | 2009-01-03 21:38:59 +0100 (Sat, 03 Jan 2009) | 2 lines Recognize usage of the default role. ........ r68219 | georg.brandl | 2009-01-03 21:47:01 +0100 (Sat, 03 Jan 2009) | 2 lines Fix uses of the default role. ........ r68220 | georg.brandl | 2009-01-03 21:55:06 +0100 (Sat, 03 Jan 2009) | 2 lines Remove trailing whitespace. ........ r68221 | georg.brandl | 2009-01-03 22:04:55 +0100 (Sat, 03 Jan 2009) | 2 lines Remove tabs from the documentation. ........ r68222 | georg.brandl | 2009-01-03 22:11:58 +0100 (Sat, 03 Jan 2009) | 2 lines Disable the line length checker by default. ........
2009-01-03 17:55:17 -04:00
if file[:5]!='/tmp/':
2007-08-15 11:28:01 -03:00
raise IOError, "can't write outside /tmp"
elif (string.find(file, '/../') >= 0 or
file[:3] == '../' or file[-3:] == '/..'):
raise IOError, "'..' in filename forbidden"
else: raise IOError, "Illegal open() mode"
return open(file, mode, buf)
Notice that the above code will occasionally forbid a perfectly valid filename;
for example, code in the restricted environment won't be able to open a file
called :file:`/tmp/foo/../bar`. To fix this, the :meth:`r_open` method would
have to simplify the filename to :file:`/tmp/bar`, which would require splitting
apart the filename and performing various operations on it. In cases where
security is at stake, it may be preferable to write simple code which is
sometimes overly restrictive, instead of more general code that is also more
complex and may harbor a subtle security hole.