Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > e1011ddec34cda34f3a002b121247943 > files > 897

python-docs-2.7.17-1.1.mga7.noarch.rpm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>30. Restricted Execution &#8212; Python 2.7.17 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.17 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="30.1. rexec — Restricted execution framework" href="rexec.html" />
    <link rel="prev" title="29.2. codeop — Compile Python code" href="codeop.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/2/library/restricted.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="rexec.html" title="30.1. rexec — Restricted execution framework"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="codeop.html" title="29.2. codeop — Compile Python code"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Standard Library</a> &#187;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="restricted-execution">
<span id="restricted"></span><h1>30. Restricted Execution<a class="headerlink" href="#restricted-execution" title="Permalink to this headline">¶</a></h1>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>In Python 2.3 these modules have been disabled due to various known and not
readily fixable security holes.  The modules are still documented here to help
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 notranslate"><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 notranslate"><span class="pre">Bastion</span></code></a> modules.</p>
</div>
<p><em>Restricted execution</em> is the basic framework in Python that allows for the
segregation of trusted and untrusted code.  The framework is based on the notion
that trusted Python code (a <em>supervisor</em>) can create a “padded cell’ (or
environment) with limited permissions, and run the untrusted code within this
cell.  The untrusted code cannot break out of its cell, and can only interact
with sensitive system resources through interfaces defined and managed by the
trusted code.  The term “restricted execution” is favored over “safe-Python”
since true safety is hard to define, and is determined by the way the restricted
environment is created.  Note that the restricted environments can be nested,
with inner cells creating subcells of lesser, but never greater, privilege.</p>
<p>An interesting aspect of Python’s restricted execution model is that the
interfaces presented to untrusted code usually have the same names as those
presented to trusted code.  Therefore no special interfaces need to be learned
to write code designed to run in a restricted environment.  And because the
exact nature of the padded cell is determined by the supervisor, different
restrictions can be imposed, depending on the application.  For example, it
might be deemed “safe” for untrusted code to read any file within a specified
directory, but never to write a file.  In this case, the supervisor may redefine
the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function so that it raises an exception whenever the
<em>mode</em> parameter is <code class="docutils literal notranslate"><span class="pre">'w'</span></code>.  It might also perform a <code class="xref c c-func docutils literal notranslate"><span class="pre">chroot()</span></code>-like
operation on the <em>filename</em> parameter, such that root is always relative to some
safe “sandbox” area of the filesystem.  In this case, the untrusted code would
still see a built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function in its environment, with the same
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 notranslate"><span class="pre">IOError</span></code></a>s
being raised when the supervisor determined that an unallowable parameter is
being used.</p>
<p>The Python run-time determines whether a particular code block is executing in
restricted execution mode based on the identity of the <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> object
in its global variables: if this is (the dictionary of) the standard
<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 notranslate"><span class="pre">__builtin__</span></code></a> module, the code is deemed to be unrestricted, else it is
deemed to be restricted.</p>
<p>Python code executing in restricted mode faces a number of limitations that are
designed to prevent it from escaping from the padded cell. For instance, the
function object attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_globals</span></code> and the class and instance object
attribute <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> are unavailable.</p>
<p>Two modules provide the framework for setting up restricted execution
environments:</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="rexec.html">30.1. <code class="xref py py-mod docutils literal notranslate"><span class="pre">rexec</span></code> — Restricted execution framework</a><ul>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#rexec-objects">30.1.1. RExec Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#defining-restricted-environments">30.1.2. Defining restricted environments</a></li>
<li class="toctree-l2"><a class="reference internal" href="rexec.html#an-example">30.1.3. An example</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="bastion.html">30.2. <code class="xref py py-mod docutils literal notranslate"><span class="pre">Bastion</span></code> — Restricting access to objects</a></li>
</ul>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference external" href="http://grail.sourceforge.net/">Grail Home Page</a></dt><dd><p>Grail, an Internet browser written in Python, uses these modules to support
Python applets.  More information on the use of Python’s restricted execution
mode in Grail is available on the Web site.</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="codeop.html"
                        title="previous chapter">29.2. <code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="rexec.html"
                        title="next chapter">30.1. <code class="xref py py-mod docutils literal notranslate"><span class="pre">rexec</span></code> — Restricted execution framework</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/library/restricted.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="rexec.html" title="30.1. rexec — Restricted execution framework"
             >next</a> |</li>
        <li class="right" >
          <a href="codeop.html" title="29.2. codeop — Compile Python code"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 19, 2019.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
    </div>

  </body>
</html>