home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / r / restricted < prev    next >
Encoding:
Text File  |  1996-11-14  |  4.1 KB  |  79 lines

  1. <TITLE>Restricted Execution -- Python library reference</TITLE>
  2. Next: <A HREF="../c/cryptographic_services" TYPE="Next">Cryptographic Services</A>  
  3. Prev: <A HREF="../i/internet_and_www" TYPE="Prev">Internet and WWW</A>  
  4. Up: <A HREF="../t/top" TYPE="Up">Top</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>11. Restricted Execution</H1>
  7. In general, Python programs have complete access to the underlying
  8. operating system throug the various functions and classes, For
  9. example, a Python program can open any file for reading and writing by
  10. using the <CODE>open()</CODE> built-in function (provided the underlying OS
  11. gives you permission!).  This is exactly what you want for most
  12. applications.
  13. <P>
  14. There exists a class of applications for which this ``openness'' is
  15. inappropriate.  Take Grail: a web browser that accepts ``applets'',
  16. snippets of Python code, from anywhere on the Internet for execution
  17. on the local system.  This can be used to improve the user interface
  18. of forms, for instance.  Since the originator of the code is unknown,
  19. it is obvious that it cannot be trusted with the full resources of the
  20. local machine.
  21. <P>
  22. <I>Restricted execution</I> is the basic framework in Python that allows
  23. for the segregation of trusted and untrusted code.  It is based on the
  24. notion that trusted Python code (a <I>supervisor</I>) can create a
  25. ``padded cell' (or environment) with limited permissions, and run the
  26. untrusted code within this cell.  The untrusted code cannot break out
  27. of its cell, and can only interact with sensitive system resources
  28. through interfaces defined and managed by the trusted code.  The term
  29. ``restricted execution'' is favored over ``safe-Python''
  30. since true safety is hard to define, and is determined by the way the
  31. restricted environment is created.  Note that the restricted
  32. environments can be nested, with inner cells creating subcells of
  33. lesser, but never greater, privilege.
  34. <P>
  35. An interesting aspect of Python's restricted execution model is that
  36. the interfaces presented to untrusted code usually have the same names
  37. as those presented to trusted code.  Therefore no special interfaces
  38. need to be learned to write code designed to run in a restricted
  39. environment.  And because the exact nature of the padded cell is
  40. determined by the supervisor, different restrictions can be imposed,
  41. depending on the application.  For example, it might be deemed
  42. ``safe'' for untrusted code to read any file within a specified
  43. directory, but never to write a file.  In this case, the supervisor
  44. may redefine the built-in
  45. <CODE>open()</CODE> function so that it raises an exception whenever the
  46. <VAR>mode</VAR> parameter is <CODE>'w'</CODE>.  It might also perform a
  47. <CODE>chroot()</CODE>-like operation on the <VAR>filename</VAR> parameter, such
  48. that root is always relative to some safe ``sandbox'' area of the
  49. filesystem.  In this case, the untrusted code would still see an
  50. built-in <CODE>open()</CODE> function in its environment, with the same
  51. calling interface.  The semantics would be identical too, with
  52. <CODE>IOError</CODE>s being raised when the supervisor determined that an
  53. unallowable parameter is being used.
  54. <P>
  55. The Python run-time determines whether a particular code block is
  56. executing in restricted execution mode based on the identity of the
  57. <CODE>__builtins__</CODE> object in its global variables: if this is (the
  58. dictionary of) the standard <CODE>__builtin__</CODE> module, the code is
  59. deemed to be unrestricted, else it is deemed to be restricted.
  60. <P>
  61. Python code executing in restricted mode faces a number of limitations
  62. that are designed to prevent it from escaping from the padded cell.
  63. For instance, the function object attribute <CODE>func_globals</CODE> and the
  64. class and instance object attribute <CODE>__dict__</CODE> are unavailable.
  65. <P>
  66. Two modules provide the framework for setting up restricted execution
  67. environments:
  68. <P>
  69. <DL>
  70. <DT><B>rexec</B><DD>--- Basic restricted execution framework.
  71. <P>
  72. <DT><B>Bastion</B><DD>--- Providing restricted access to objects.
  73. <P>
  74. </DL>
  75. <H2>Menu</H2><DL COMPACT>
  76. <DT><A HREF="../r/rexec" TYPE=Menu>rexec</A>
  77. <DD><DT><A HREF="../b/bastion" TYPE=Menu>Bastion</A>
  78. <DD></DL>
  79.