home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Restricted Execution -- Python library reference</TITLE>
- Next: <A HREF="../c/cryptographic_services" TYPE="Next">Cryptographic Services</A>
- Prev: <A HREF="../i/internet_and_www" TYPE="Prev">Internet and WWW</A>
- Up: <A HREF="../t/top" TYPE="Up">Top</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>11. Restricted Execution</H1>
- In general, Python programs have complete access to the underlying
- operating system throug the various functions and classes, For
- example, a Python program can open any file for reading and writing by
- using the <CODE>open()</CODE> built-in function (provided the underlying OS
- gives you permission!). This is exactly what you want for most
- applications.
- <P>
- There exists a class of applications for which this ``openness'' is
- inappropriate. Take Grail: a web browser that accepts ``applets'',
- snippets of Python code, from anywhere on the Internet for execution
- on the local system. This can be used to improve the user interface
- of forms, for instance. Since the originator of the code is unknown,
- it is obvious that it cannot be trusted with the full resources of the
- local machine.
- <P>
- <I>Restricted execution</I> is the basic framework in Python that allows
- for the segregation of trusted and untrusted code. It is based on the
- notion that trusted Python code (a <I>supervisor</I>) 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>
- 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
- <CODE>open()</CODE> function so that it raises an exception whenever the
- <VAR>mode</VAR> parameter is <CODE>'w'</CODE>. It might also perform a
- <CODE>chroot()</CODE>-like operation on the <VAR>filename</VAR> parameter, such
- that root is always relative to some safe ``sandbox'' area of the
- filesystem. In this case, the untrusted code would still see an
- built-in <CODE>open()</CODE> function in its environment, with the same
- calling interface. The semantics would be identical too, with
- <CODE>IOError</CODE>s being raised when the supervisor determined that an
- unallowable parameter is being used.
- <P>
- The Python run-time determines whether a particular code block is
- executing in restricted execution mode based on the identity of the
- <CODE>__builtins__</CODE> object in its global variables: if this is (the
- dictionary of) the standard <CODE>__builtin__</CODE> module, the code is
- deemed to be unrestricted, else it is deemed to be restricted.
- <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>func_globals</CODE> and the
- class and instance object attribute <CODE>__dict__</CODE> are unavailable.
- <P>
- Two modules provide the framework for setting up restricted execution
- environments:
- <P>
- <DL>
- <DT><B>rexec</B><DD>--- Basic restricted execution framework.
- <P>
- <DT><B>Bastion</B><DD>--- Providing restricted access to objects.
- <P>
- </DL>
- <H2>Menu</H2><DL COMPACT>
- <DT><A HREF="../r/rexec" TYPE=Menu>rexec</A>
- <DD><DT><A HREF="../b/bastion" TYPE=Menu>Bastion</A>
- <DD></DL>
-