1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>30. Restricted Execution — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="The Python Standard Library" href="index.html" />
34
<link rel="next" title="30.1. rexec — Restricted execution framework" href="rexec.html" />
35
<link rel="prev" title="29.2. codeop — Compile Python code" href="codeop.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="rexec.html" title="30.1. rexec — Restricted execution framework"
55
accesskey="N">next</a> |</li>
57
<a href="codeop.html" title="29.2. codeop — Compile Python code"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> »</li>
71
<div class="document">
72
<div class="documentwrapper">
73
<div class="bodywrapper">
74
<div class="body" role="main">
76
<div class="section" id="restricted-execution">
77
<span id="restricted"></span><h1>30. Restricted Execution<a class="headerlink" href="#restricted-execution" title="Permalink to this headline">¶</a></h1>
78
<div class="admonition warning">
79
<p class="first admonition-title">Warning</p>
80
<p class="last">In Python 2.3 these modules have been disabled due to various known and not
81
readily fixable security holes. The modules are still documented here to help
82
in reading old code that uses the <a class="reference internal" href="rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a> and <a class="reference internal" href="bastion.html#module-Bastion" title="Bastion: Providing restricted access to objects. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">Bastion</span></code></a> modules.</p>
84
<p><em>Restricted execution</em> is the basic framework in Python that allows for the
85
segregation of trusted and untrusted code. The framework is based on the notion
86
that trusted Python code (a <em>supervisor</em>) can create a “padded cell’ (or
87
environment) with limited permissions, and run the untrusted code within this
88
cell. The untrusted code cannot break out of its cell, and can only interact
89
with sensitive system resources through interfaces defined and managed by the
90
trusted code. The term “restricted execution” is favored over “safe-Python”
91
since true safety is hard to define, and is determined by the way the restricted
92
environment is created. Note that the restricted environments can be nested,
93
with inner cells creating subcells of lesser, but never greater, privilege.</p>
94
<p>An interesting aspect of Python’s restricted execution model is that the
95
interfaces presented to untrusted code usually have the same names as those
96
presented to trusted code. Therefore no special interfaces need to be learned
97
to write code designed to run in a restricted environment. And because the
98
exact nature of the padded cell is determined by the supervisor, different
99
restrictions can be imposed, depending on the application. For example, it
100
might be deemed “safe” for untrusted code to read any file within a specified
101
directory, but never to write a file. In this case, the supervisor may redefine
102
the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function so that it raises an exception whenever the
103
<em>mode</em> parameter is <code class="docutils literal"><span class="pre">'w'</span></code>. It might also perform a <code class="xref c c-func docutils literal"><span class="pre">chroot()</span></code>-like
104
operation on the <em>filename</em> parameter, such that root is always relative to some
105
safe “sandbox” area of the filesystem. In this case, the untrusted code would
106
still see a built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function in its environment, with the same
107
calling interface. The semantics would be identical too, with <a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a>s
108
being raised when the supervisor determined that an unallowable parameter is
110
<p>The Python run-time determines whether a particular code block is executing in
111
restricted execution mode based on the identity of the <code class="docutils literal"><span class="pre">__builtins__</span></code> object
112
in its global variables: if this is (the dictionary of) the standard
113
<a class="reference internal" href="__builtin__.html#module-__builtin__" title="__builtin__: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal"><span class="pre">__builtin__</span></code></a> module, the code is deemed to be unrestricted, else it is
114
deemed to be restricted.</p>
115
<p>Python code executing in restricted mode faces a number of limitations that are
116
designed to prevent it from escaping from the padded cell. For instance, the
117
function object attribute <code class="xref py py-attr docutils literal"><span class="pre">func_globals</span></code> and the class and instance object
118
attribute <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal"><span class="pre">__dict__</span></code></a> are unavailable.</p>
119
<p>Two modules provide the framework for setting up restricted execution
121
<div class="toctree-wrapper compound">
123
<li class="toctree-l1"><a class="reference internal" href="rexec.html">30.1. <code class="docutils literal"><span class="pre">rexec</span></code> — Restricted execution framework</a><ul>
124
<li class="toctree-l2"><a class="reference internal" href="rexec.html#rexec-objects">30.1.1. RExec Objects</a></li>
125
<li class="toctree-l2"><a class="reference internal" href="rexec.html#defining-restricted-environments">30.1.2. Defining restricted environments</a></li>
126
<li class="toctree-l2"><a class="reference internal" href="rexec.html#an-example">30.1.3. An example</a></li>
129
<li class="toctree-l1"><a class="reference internal" href="bastion.html">30.2. <code class="docutils literal"><span class="pre">Bastion</span></code> — Restricting access to objects</a></li>
132
<div class="admonition seealso">
133
<p class="first admonition-title">See also</p>
134
<dl class="last docutils">
135
<dt><a class="reference external" href="http://grail.sourceforge.net/">Grail Home Page</a></dt>
136
<dd>Grail, an Internet browser written in Python, uses these modules to support
137
Python applets. More information on the use of Python’s restricted execution
138
mode in Grail is available on the Web site.</dd>
147
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
148
<div class="sphinxsidebarwrapper">
149
<h4>Previous topic</h4>
150
<p class="topless"><a href="codeop.html"
151
title="previous chapter">29.2. <code class="docutils literal"><span class="pre">codeop</span></code> — Compile Python code</a></p>
153
<p class="topless"><a href="rexec.html"
154
title="next chapter">30.1. <code class="docutils literal"><span class="pre">rexec</span></code> — Restricted execution framework</a></p>
156
<ul class="this-page-menu">
157
<li><a href="../bugs.html">Report a Bug</a></li>
158
<li><a href="../_sources/library/restricted.txt"
159
rel="nofollow">Show Source</a></li>
162
<div id="searchbox" style="display: none" role="search">
163
<h3>Quick search</h3>
164
<form class="search" action="../search.html" method="get">
165
<input type="text" name="q" />
166
<input type="submit" value="Go" />
167
<input type="hidden" name="check_keywords" value="yes" />
168
<input type="hidden" name="area" value="default" />
170
<p class="searchtip" style="font-size: 90%">
171
Enter search terms or a module, class or function name.
174
<script type="text/javascript">$('#searchbox').show(0);</script>
177
<div class="clearer"></div>
179
<div class="related" role="navigation" aria-label="related navigation">
182
<li class="right" style="margin-right: 10px">
183
<a href="../genindex.html" title="General Index"
186
<a href="../py-modindex.html" title="Python Module Index"
189
<a href="rexec.html" title="30.1. rexec — Restricted execution framework"
192
<a href="codeop.html" title="29.2. codeop — Compile Python code"
194
<li><img src="../_static/py.png" alt=""
195
style="vertical-align: middle; margin-top: -1px"/></li>
196
<li><a href="https://www.python.org/">Python</a> »</li>
198
<span class="version_switcher_placeholder">2.7.12</span>
199
<a href="../index.html">Documentation</a> »
202
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
206
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
208
The Python Software Foundation is a non-profit corporation.
209
<a href="https://www.python.org/psf/donations/">Please donate.</a>
211
Last updated on Sep 20, 2016.
212
<a href="../bugs.html">Found a bug</a>?
214
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'