home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _c7c350e88a28098e257d79a0e8dfed86 < prev    next >
Text File  |  2000-03-23  |  15KB  |  321 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Safe - Compile and execute code in restricted compartments</TITLE>
  5. <LINK REL="stylesheet" HREF="../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Safe - Compile and execute code in restricted compartments</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  25.     <LI><A HREF="#warning">WARNING</A></LI>
  26.     <UL>
  27.  
  28.         <LI><A HREF="#recent changes">RECENT CHANGES</A></LI>
  29.         <LI><A HREF="#methods in class safe">Methods in class Safe</A></LI>
  30.         <LI><A HREF="#some safety issues">Some Safety Issues</A></LI>
  31.         <LI><A HREF="#author">AUTHOR</A></LI>
  32.     </UL>
  33.  
  34. </UL>
  35. <!-- INDEX END -->
  36.  
  37. <HR>
  38. <P>
  39. <H1><A NAME="name">NAME</A></H1>
  40. <P>Safe - Compile and execute code in restricted compartments</P>
  41. <P>
  42. <HR>
  43. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  44. <UL>
  45. <LI>Linux</LI>
  46. <LI>Solaris</LI>
  47. <LI>Windows</LI>
  48. </UL>
  49. <HR>
  50. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  51. <PRE>
  52.   use Safe;</PRE>
  53. <PRE>
  54.   $compartment = new Safe;</PRE>
  55. <PRE>
  56.   $compartment->permit(qw(time sort :browse));</PRE>
  57. <PRE>
  58.   $result = $compartment->reval($unsafe_code);</PRE>
  59. <P>
  60. <HR>
  61. <H1><A NAME="description">DESCRIPTION</A></H1>
  62. <P>The Safe extension module allows the creation of compartments
  63. in which perl code can be evaluated. Each compartment has</P>
  64. <DL>
  65. <DT><STRONG><A NAME="item_a_new_namespace">a new namespace</A></STRONG><BR>
  66. <DD>
  67. The ``root'' of the namespace (i.e. ``main::'') is changed to a
  68. different package and code evaluated in the compartment cannot
  69. refer to variables outside this namespace, even with run-time
  70. glob lookups and other tricks.
  71. <P>Code which is compiled outside the compartment can choose to place
  72. variables into (or <EM>share</EM> variables with) the compartment's namespace
  73. and only that data will be visible to code evaluated in the
  74. compartment.</P>
  75. <P>By default, the only variables shared with compartments are the
  76. ``underscore'' variables $_ and @_ (and, technically, the less frequently
  77. used %_, the _ filehandle and so on). This is because otherwise perl
  78. operators which default to $_ will not work and neither will the
  79. assignment of arguments to @_ on subroutine entry.</P>
  80. <P></P>
  81. <DT><STRONG><A NAME="item_an_operator_mask">an operator mask</A></STRONG><BR>
  82. <DD>
  83. Each compartment has an associated ``operator mask''. Recall that
  84. perl code is compiled into an internal format before execution.
  85. Evaluating perl code (e.g. via ``eval'' or ``do 'file''') causes
  86. the code to be compiled into an internal format and then,
  87. provided there was no error in the compilation, executed.
  88. Code evaluated in a compartment compiles subject to the
  89. compartment's operator mask. Attempting to evaluate code in a
  90. compartment which contains a masked operator will cause the
  91. compilation to fail with an error. The code will not be executed.
  92. <P>The default operator mask for a newly created compartment is
  93. the ':default' optag.</P>
  94. <P>It is important that you read the <CODE>Opcode(3)</CODE> module documentation
  95. for more information, especially for detailed definitions of opnames,
  96. optags and opsets.</P>
  97. <P>Since it is only at the compilation stage that the operator mask
  98. applies, controlled access to potentially unsafe operations can
  99. be achieved by having a handle to a wrapper subroutine (written
  100. outside the compartment) placed into the compartment. For example,</P>
  101. <PRE>
  102.     $cpt = new Safe;
  103.     sub wrapper {
  104.         # vet arguments and perform potentially unsafe operations
  105.     }
  106.     $cpt->share('&wrapper');</PRE>
  107. <P></P></DL>
  108. <P>
  109. <HR>
  110. <H1><A NAME="warning">WARNING</A></H1>
  111. <P>The authors make <STRONG>no warranty</STRONG>, implied or otherwise, about the
  112. suitability of this software for safety or security purposes.</P>
  113. <P>The authors shall not in any case be liable for special, incidental,
  114. consequential, indirect or other similar damages arising from the use
  115. of this software.</P>
  116. <P>Your mileage will vary. If in any doubt <STRONG>do not use it</STRONG>.</P>
  117. <P>
  118. <H2><A NAME="recent changes">RECENT CHANGES</A></H2>
  119. <P>The interface to the Safe module has changed quite dramatically since
  120. version 1 (as supplied with Perl5.002). Study these pages carefully if
  121. you have code written to use Safe version 1 because you will need to
  122. makes changes.</P>
  123. <P>
  124. <H2><A NAME="methods in class safe">Methods in class Safe</A></H2>
  125. <P>To create a new compartment, use</P>
  126. <PRE>
  127.     $cpt = new Safe;</PRE>
  128. <P>Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
  129. to use for the compartment (defaults to ``Safe::Root0'', incremented for
  130. each new compartment).</P>
  131. <P>Note that version 1.00 of the Safe module supported a second optional
  132. parameter, MASK.  That functionality has been withdrawn pending deeper
  133. consideration. Use the permit and deny methods described below.</P>
  134. <P>The following methods can then be used on the compartment
  135. object returned by the above constructor. The object argument
  136. is implicit in each case.</P>
  137. <DL>
  138. <DT><STRONG><A NAME="item_permit">permit (OP, ...)</A></STRONG><BR>
  139. <DD>
  140. Permit the listed operators to be used when compiling code in the
  141. compartment (in <EM>addition</EM> to any operators already permitted).
  142. <P></P>
  143. <DT><STRONG><A NAME="item_permit_only">permit_only (OP, ...)</A></STRONG><BR>
  144. <DD>
  145. Permit <EM>only</EM> the listed operators to be used when compiling code in
  146. the compartment (<EM>no</EM> other operators are permitted).
  147. <P></P>
  148. <DT><STRONG><A NAME="item_deny">deny (OP, ...)</A></STRONG><BR>
  149. <DD>
  150. Deny the listed operators from being used when compiling code in the
  151. compartment (other operators may still be permitted).
  152. <P></P>
  153. <DT><STRONG><A NAME="item_deny_only">deny_only (OP, ...)</A></STRONG><BR>
  154. <DD>
  155. Deny <EM>only</EM> the listed operators from being used when compiling code
  156. in the compartment (<EM>all</EM> other operators will be permitted).
  157. <P></P>
  158. <DT><STRONG><A NAME="item_trap">trap (OP, ...)</A></STRONG><BR>
  159. <DD>
  160. <DT><STRONG><A NAME="item_untrap">untrap (OP, ...)</A></STRONG><BR>
  161. <DD>
  162. The trap and untrap methods are synonyms for deny and permit
  163. respectfully.
  164. <P></P>
  165. <DT><STRONG><A NAME="item_share">share (NAME, ...)</A></STRONG><BR>
  166. <DD>
  167. This shares the <CODE>variable(s)</CODE> in the argument list with the compartment.
  168. This is almost identical to exporting variables using the <EM>Exporter(3)</EM>
  169. module.
  170. <P>Each NAME must be the <STRONG>name</STRONG> of a variable, typically with the leading
  171. type identifier included. A bareword is treated as a function name.</P>
  172. <P>Examples of legal names are '$foo' for a scalar, '@foo' for an
  173. array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'
  174. for a glob (i.e.  all symbol table entries associated with ``foo'',
  175. including scalar, array, hash, sub and filehandle).</P>
  176. <P>Each NAME is assumed to be in the calling package. See share_from
  177. for an alternative method (which share uses).</P>
  178. <P></P>
  179. <DT><STRONG><A NAME="item_share_from">share_from (PACKAGE, ARRAYREF)</A></STRONG><BR>
  180. <DD>
  181. This method is similar to <A HREF="#item_share"><CODE>share()</CODE></A> but allows you to explicitly name the
  182. package that symbols should be shared from. The symbol names (including
  183. type characters) are supplied as an array reference.
  184. <PRE>
  185.     $safe->share_from('main', [ '$foo', '%bar', 'func' ]);</PRE>
  186. <P></P>
  187. <DT><STRONG><A NAME="item_varglob">varglob (VARNAME)</A></STRONG><BR>
  188. <DD>
  189. This returns a glob reference for the symbol table entry of VARNAME in
  190. the package of the compartment. VARNAME must be the <STRONG>name</STRONG> of a
  191. variable without any leading type marker. For example,
  192. <PRE>
  193.     $cpt = new Safe 'Root';
  194.     $Root::foo = "Hello world";
  195.     # Equivalent version which doesn't need to know $cpt's package name:
  196.     ${$cpt->varglob('foo')} = "Hello world";</PRE>
  197. <P></P>
  198. <DT><STRONG><A NAME="item_reval">reval (STRING)</A></STRONG><BR>
  199. <DD>
  200. This evaluates STRING as perl code inside the compartment.
  201. <P>The code can only see the compartment's namespace (as returned by the
  202. <STRONG>root</STRONG> method). The compartment's root package appears to be the
  203. <CODE>main::</CODE> package to the code inside the compartment.</P>
  204. <P>Any attempt by the code in STRING to use an operator which is not permitted
  205. by the compartment will cause an error (at run-time of the main program
  206. but at compile-time for the code in STRING).  The error is of the form
  207. ``%s trapped by operation mask operation...''.</P>
  208. <P>If an operation is trapped in this way, then the code in STRING will
  209. not be executed. If such a trapped operation occurs or any other
  210. compile-time or return error, then $@ is set to the error message, just
  211. as with an eval().</P>
  212. <P>If there is no error, then the method returns the value of the last
  213. expression evaluated, or a return statement may be used, just as with
  214. subroutines and <STRONG>eval()</STRONG>. The context (list or scalar) is determined
  215. by the caller as usual.</P>
  216. <P>This behaviour differs from the beta distribution of the Safe extension
  217. where earlier versions of perl made it hard to mimic the return
  218. behaviour of the <A HREF="../lib/Pod/perlfunc.html#item_eval"><CODE>eval()</CODE></A> command and the context was always scalar.</P>
  219. <P>Some points to note:</P>
  220. <P>If the entereval op is permitted then the code can use eval ``...'' to
  221. 'hide' code which might use denied ops. This is not a major problem
  222. since when the code tries to execute the eval it will fail because the
  223. opmask is still in effect. However this technique would allow clever,
  224. and possibly harmful, code to 'probe' the boundaries of what is
  225. possible.</P>
  226. <P>Any string eval which is executed by code executing in a compartment,
  227. or by code called from code executing in a compartment, will be eval'd
  228. in the namespace of the compartment. This is potentially a serious
  229. problem.</P>
  230. <P>Consider a function <CODE>foo()</CODE> in package pkg compiled outside a compartment
  231. but shared with it. Assume the compartment has a root package called
  232. 'Root'. If <CODE>foo()</CODE> contains an eval statement like eval '$foo = 1' then,
  233. normally, $pkg::foo will be set to 1.  If <CODE>foo()</CODE> is called from the
  234. compartment (by whatever means) then instead of setting $pkg::foo, the
  235. eval will actually set $Root::pkg::foo.</P>
  236. <P>This can easily be demonstrated by using a module, such as the Socket
  237. module, which uses eval ``...'' as part of an AUTOLOAD function. You can
  238. 'use' the module outside the compartment and share an (autoloaded)
  239. function with the compartment. If an autoload is triggered by code in
  240. the compartment, or by any code anywhere that is called by any means
  241. from the compartment, then the eval in the Socket module's AUTOLOAD
  242. function happens in the namespace of the compartment. Any variables
  243. created or used by the eval'd code are now under the control of
  244. the code in the compartment.</P>
  245. <P>A similar effect applies to <EM>all</EM> runtime symbol lookups in code
  246. called from a compartment but not compiled within it.</P>
  247. <P></P>
  248. <DT><STRONG><A NAME="item_rdo">rdo (FILENAME)</A></STRONG><BR>
  249. <DD>
  250. This evaluates the contents of file FILENAME inside the compartment.
  251. See above documentation on the <STRONG>reval</STRONG> method for further details.
  252. <P></P>
  253. <DT><STRONG><A NAME="item_root">root (NAMESPACE)</A></STRONG><BR>
  254. <DD>
  255. This method returns the name of the package that is the root of the
  256. compartment's namespace.
  257. <P>Note that this behaviour differs from version 1.00 of the Safe module
  258. where the root module could be used to change the namespace. That
  259. functionality has been withdrawn pending deeper consideration.</P>
  260. <P></P>
  261. <DT><STRONG><A NAME="item_mask">mask (MASK)</A></STRONG><BR>
  262. <DD>
  263. This is a get-or-set method for the compartment's operator mask.
  264. <P>With no MASK argument present, it returns the current operator mask of
  265. the compartment.</P>
  266. <P>With the MASK argument present, it sets the operator mask for the
  267. compartment (equivalent to calling the deny_only method).</P>
  268. <P></P></DL>
  269. <P>
  270. <H2><A NAME="some safety issues">Some Safety Issues</A></H2>
  271. <P>This section is currently just an outline of some of the things code in
  272. a compartment might do (intentionally or unintentionally) which can
  273. have an effect outside the compartment.</P>
  274. <DL>
  275. <DT><STRONG><A NAME="item_Memory">Memory</A></STRONG><BR>
  276. <DD>
  277. Consuming all (or nearly all) available memory.
  278. <P></P>
  279. <DT><STRONG><A NAME="item_CPU">CPU</A></STRONG><BR>
  280. <DD>
  281. Causing infinite loops etc.
  282. <P></P>
  283. <DT><STRONG><A NAME="item_Snooping">Snooping</A></STRONG><BR>
  284. <DD>
  285. Copying private information out of your system. Even something as
  286. simple as your user name is of value to others. Much useful information
  287. could be gleaned from your environment variables for example.
  288. <P></P>
  289. <DT><STRONG><A NAME="item_Signals">Signals</A></STRONG><BR>
  290. <DD>
  291. Causing signals (especially SIGFPE and SIGALARM) to affect your process.
  292. <P>Setting up a signal handler will need to be carefully considered
  293. and controlled.  What mask is in effect when a signal handler
  294. gets called?  If a user can get an imported function to get an
  295. exception and call the user's signal handler, does that user's
  296. restricted mask get re-instated before the handler is called?
  297. Does an imported handler get called with its original mask or
  298. the user's one?</P>
  299. <P></P>
  300. <DT><STRONG><A NAME="item_State_Changes">State Changes</A></STRONG><BR>
  301. <DD>
  302. Ops such as chdir obviously effect the process as a whole and not just
  303. the code in the compartment. Ops such as rand and srand have a similar
  304. but more subtle effect.
  305. <P></P></DL>
  306. <P>
  307. <H2><A NAME="author">AUTHOR</A></H2>
  308. <P>Originally designed and implemented by Malcolm Beattie,
  309. <A HREF="mailto:mbeattie@sable.ox.ac.uk.">mbeattie@sable.ox.ac.uk.</A></P>
  310. <P>Reworked to use the Opcode module and other changes added by Tim Bunce
  311. <<EM><A HREF="mailto:Tim.Bunce@ig.co.uk">Tim.Bunce@ig.co.uk</A></EM>>.</P>
  312. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  313. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  314. <STRONG><P CLASS=block> Safe - Compile and execute code in restricted compartments</P></STRONG>
  315. </TD></TR>
  316. </TABLE>
  317.  
  318. </BODY>
  319.  
  320. </HTML>
  321.