~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/restricted.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>30. Restricted Execution &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
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>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="rexec.html" title="30.1. rexec — Restricted execution framework"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
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> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> &raquo;</li> 
 
68
      </ul>
 
69
    </div>    
 
70
 
 
71
    <div class="document">
 
72
      <div class="documentwrapper">
 
73
        <div class="bodywrapper">
 
74
          <div class="body" role="main">
 
75
            
 
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>
 
83
</div>
 
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 &#8220;padded cell&#8217; (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 &#8220;restricted execution&#8221; is favored over &#8220;safe-Python&#8221;
 
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&#8217;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 &#8220;safe&#8221; 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 &#8220;sandbox&#8221; 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
 
109
being used.</p>
 
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
 
120
environments:</p>
 
121
<div class="toctree-wrapper compound">
 
122
<ul>
 
123
<li class="toctree-l1"><a class="reference internal" href="rexec.html">30.1. <code class="docutils literal"><span class="pre">rexec</span></code> &#8212; 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>
 
127
</ul>
 
128
</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> &#8212; Restricting access to objects</a></li>
 
130
</ul>
 
131
</div>
 
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&#8217;s restricted execution
 
138
mode in Grail is available on the Web site.</dd>
 
139
</dl>
 
140
</div>
 
141
</div>
 
142
 
 
143
 
 
144
          </div>
 
145
        </div>
 
146
      </div>
 
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> &#8212; Compile Python code</a></p>
 
152
  <h4>Next topic</h4>
 
153
  <p class="topless"><a href="rexec.html"
 
154
                        title="next chapter">30.1. <code class="docutils literal"><span class="pre">rexec</span></code> &#8212; Restricted execution framework</a></p>
 
155
<h3>This Page</h3>
 
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>
 
160
</ul>
 
161
 
 
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" />
 
169
    </form>
 
170
    <p class="searchtip" style="font-size: 90%">
 
171
    Enter search terms or a module, class or function name.
 
172
    </p>
 
173
</div>
 
174
<script type="text/javascript">$('#searchbox').show(0);</script>
 
175
        </div>
 
176
      </div>
 
177
      <div class="clearer"></div>
 
178
    </div>  
 
179
    <div class="related" role="navigation" aria-label="related navigation">
 
180
      <h3>Navigation</h3>
 
181
      <ul>
 
182
        <li class="right" style="margin-right: 10px">
 
183
          <a href="../genindex.html" title="General Index"
 
184
             >index</a></li>
 
185
        <li class="right" >
 
186
          <a href="../py-modindex.html" title="Python Module Index"
 
187
             >modules</a> |</li>
 
188
        <li class="right" >
 
189
          <a href="rexec.html" title="30.1. rexec — Restricted execution framework"
 
190
             >next</a> |</li>
 
191
        <li class="right" >
 
192
          <a href="codeop.html" title="29.2. codeop — Compile Python code"
 
193
             >previous</a> |</li>
 
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> &raquo;</li>
 
197
        <li>
 
198
          <span class="version_switcher_placeholder">2.7.12</span>
 
199
          <a href="../index.html">Documentation</a> &raquo;
 
200
        </li>
 
201
 
 
202
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li> 
 
203
      </ul>
 
204
    </div>  
 
205
    <div class="footer">
 
206
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
207
    <br />
 
208
    The Python Software Foundation is a non-profit corporation.
 
209
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
210
    <br />
 
211
    Last updated on Sep 20, 2016.
 
212
    <a href="../bugs.html">Found a bug</a>?
 
213
    <br />
 
214
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
215
    </div>
 
216
 
 
217
  </body>
 
218
</html>
 
 
b'\\ No newline at end of file'