Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / Safe.html
bloba67f08cc83a391a630b456830356a2a154fb2427
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>Safe - Compile and execute code in restricted compartments</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;Safe - Compile and execute code in restricted compartments</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
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>
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>
34 </ul>
35 <!-- INDEX END -->
37 <hr />
38 <p>
39 </p>
40 <h1><a name="name">NAME</a></h1>
41 <p>Safe - Compile and execute code in restricted compartments</p>
42 <p>
43 </p>
44 <hr />
45 <h1><a name="synopsis">SYNOPSIS</a></h1>
46 <pre>
47 use Safe;</pre>
48 <pre>
49 $compartment = new Safe;</pre>
50 <pre>
51 $compartment-&gt;permit(qw(time sort :browse));</pre>
52 <pre>
53 $result = $compartment-&gt;reval($unsafe_code);</pre>
54 <p>
55 </p>
56 <hr />
57 <h1><a name="description">DESCRIPTION</a></h1>
58 <p>The Safe extension module allows the creation of compartments
59 in which perl code can be evaluated. Each compartment has</p>
60 <dl>
61 <dt><strong><a name="item_a_new_namespace">a new namespace</a></strong>
63 <dd>
64 <p>The ``root'' of the namespace (i.e. ``main::'') is changed to a
65 different package and code evaluated in the compartment cannot
66 refer to variables outside this namespace, even with run-time
67 glob lookups and other tricks.</p>
68 </dd>
69 <dd>
70 <p>Code which is compiled outside the compartment can choose to place
71 variables into (or <em>share</em> variables with) the compartment's namespace
72 and only that data will be visible to code evaluated in the
73 compartment.</p>
74 </dd>
75 <dd>
76 <p>By default, the only variables shared with compartments are the
77 ``underscore'' variables $_ and @_ (and, technically, the less frequently
78 used %_, the _ filehandle and so on). This is because otherwise perl
79 operators which default to $_ will not work and neither will the
80 assignment of arguments to @_ on subroutine entry.</p>
81 </dd>
82 </li>
83 <dt><strong><a name="item_an_operator_mask">an operator mask</a></strong>
85 <dd>
86 <p>Each compartment has an associated ``operator mask''. Recall that
87 perl code is compiled into an internal format before execution.
88 Evaluating perl code (e.g. via ``eval'' or ``do 'file''') causes
89 the code to be compiled into an internal format and then,
90 provided there was no error in the compilation, executed.
91 Code evaluated in a compartment compiles subject to the
92 compartment's operator mask. Attempting to evaluate code in a
93 compartment which contains a masked operator will cause the
94 compilation to fail with an error. The code will not be executed.</p>
95 </dd>
96 <dd>
97 <p>The default operator mask for a newly created compartment is
98 the ':default' optag.</p>
99 </dd>
100 <dd>
101 <p>It is important that you read the <code>Opcode(3)</code> module documentation
102 for more information, especially for detailed definitions of opnames,
103 optags and opsets.</p>
104 </dd>
105 <dd>
106 <p>Since it is only at the compilation stage that the operator mask
107 applies, controlled access to potentially unsafe operations can
108 be achieved by having a handle to a wrapper subroutine (written
109 outside the compartment) placed into the compartment. For example,</p>
110 </dd>
111 <dd>
112 <pre>
113 $cpt = new Safe;
114 sub wrapper {
115 # vet arguments and perform potentially unsafe operations
117 $cpt-&gt;share('&amp;wrapper');</pre>
118 </dd>
119 </li>
120 </dl>
122 </p>
123 <hr />
124 <h1><a name="warning">WARNING</a></h1>
125 <p>The authors make <strong>no warranty</strong>, implied or otherwise, about the
126 suitability of this software for safety or security purposes.</p>
127 <p>The authors shall not in any case be liable for special, incidental,
128 consequential, indirect or other similar damages arising from the use
129 of this software.</p>
130 <p>Your mileage will vary. If in any doubt <strong>do not use it</strong>.</p>
132 </p>
133 <h2><a name="recent_changes">RECENT CHANGES</a></h2>
134 <p>The interface to the Safe module has changed quite dramatically since
135 version 1 (as supplied with Perl5.002). Study these pages carefully if
136 you have code written to use Safe version 1 because you will need to
137 makes changes.</p>
139 </p>
140 <h2><a name="methods_in_class_safe">Methods in class Safe</a></h2>
141 <p>To create a new compartment, use</p>
142 <pre>
143 $cpt = new Safe;</pre>
144 <p>Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
145 to use for the compartment (defaults to ``Safe::Root0'', incremented for
146 each new compartment).</p>
147 <p>Note that version 1.00 of the Safe module supported a second optional
148 parameter, MASK. That functionality has been withdrawn pending deeper
149 consideration. Use the permit and deny methods described below.</p>
150 <p>The following methods can then be used on the compartment
151 object returned by the above constructor. The object argument
152 is implicit in each case.</p>
153 <dl>
154 <dt><strong><a name="item_permit">permit (OP, ...)</a></strong>
156 <dd>
157 <p>Permit the listed operators to be used when compiling code in the
158 compartment (in <em>addition</em> to any operators already permitted).</p>
159 </dd>
160 <dd>
161 <p>You can list opcodes by names, or use a tag name; see
162 <a href="file://C|\msysgit\mingw\html/lib/auto/Opcode/predefined_opcode_tags.html">Predefined Opcode Tags in the Opcode manpage</a>.</p>
163 </dd>
164 </li>
165 <dt><strong><a name="item_permit_only">permit_only (OP, ...)</a></strong>
167 <dd>
168 <p>Permit <em>only</em> the listed operators to be used when compiling code in
169 the compartment (<em>no</em> other operators are permitted).</p>
170 </dd>
171 </li>
172 <dt><strong><a name="item_deny">deny (OP, ...)</a></strong>
174 <dd>
175 <p>Deny the listed operators from being used when compiling code in the
176 compartment (other operators may still be permitted).</p>
177 </dd>
178 </li>
179 <dt><strong><a name="item_deny_only">deny_only (OP, ...)</a></strong>
181 <dd>
182 <p>Deny <em>only</em> the listed operators from being used when compiling code
183 in the compartment (<em>all</em> other operators will be permitted).</p>
184 </dd>
185 </li>
186 <dt><strong><a name="item_trap">trap (OP, ...)</a></strong>
188 <dt><strong><a name="item_untrap">untrap (OP, ...)</a></strong>
190 <dd>
191 <p>The trap and untrap methods are synonyms for deny and permit
192 respectfully.</p>
193 </dd>
194 </li>
195 <dt><strong><a name="item_share">share (NAME, ...)</a></strong>
197 <dd>
198 <p>This shares the <code>variable(s)</code> in the argument list with the compartment.
199 This is almost identical to exporting variables using the <a href="file://C|\msysgit\mingw\html/lib/Exporter.html">the Exporter manpage</a>
200 module.</p>
201 </dd>
202 <dd>
203 <p>Each NAME must be the <strong>name</strong> of a non-lexical variable, typically
204 with the leading type identifier included. A bareword is treated as a
205 function name.</p>
206 </dd>
207 <dd>
208 <p>Examples of legal names are '$foo' for a scalar, '@foo' for an
209 array, '%foo' for a hash, '&amp;foo' or 'foo' for a subroutine and '*foo'
210 for a glob (i.e. all symbol table entries associated with ``foo'',
211 including scalar, array, hash, sub and filehandle).</p>
212 </dd>
213 <dd>
214 <p>Each NAME is assumed to be in the calling package. See share_from
215 for an alternative method (which share uses).</p>
216 </dd>
217 </li>
218 <dt><strong><a name="item_share_from">share_from (PACKAGE, ARRAYREF)</a></strong>
220 <dd>
221 <p>This method is similar to <a href="#item_share"><code>share()</code></a> but allows you to explicitly name the
222 package that symbols should be shared from. The symbol names (including
223 type characters) are supplied as an array reference.</p>
224 </dd>
225 <dd>
226 <pre>
227 $safe-&gt;share_from('main', [ '$foo', '%bar', 'func' ]);</pre>
228 </dd>
229 </li>
230 <dt><strong><a name="item_varglob">varglob (VARNAME)</a></strong>
232 <dd>
233 <p>This returns a glob reference for the symbol table entry of VARNAME in
234 the package of the compartment. VARNAME must be the <strong>name</strong> of a
235 variable without any leading type marker. For example,</p>
236 </dd>
237 <dd>
238 <pre>
239 $cpt = new Safe 'Root';
240 $Root::foo = &quot;Hello world&quot;;
241 # Equivalent version which doesn't need to know $cpt's package name:
242 ${$cpt-&gt;varglob('foo')} = &quot;Hello world&quot;;</pre>
243 </dd>
244 </li>
245 <dt><strong><a name="item_reval">reval (STRING)</a></strong>
247 <dd>
248 <p>This evaluates STRING as perl code inside the compartment.</p>
249 </dd>
250 <dd>
251 <p>The code can only see the compartment's namespace (as returned by the
252 <strong>root</strong> method). The compartment's root package appears to be the
253 <code>main::</code> package to the code inside the compartment.</p>
254 </dd>
255 <dd>
256 <p>Any attempt by the code in STRING to use an operator which is not permitted
257 by the compartment will cause an error (at run-time of the main program
258 but at compile-time for the code in STRING). The error is of the form
259 ``'%s' trapped by operation mask...''.</p>
260 </dd>
261 <dd>
262 <p>If an operation is trapped in this way, then the code in STRING will
263 not be executed. If such a trapped operation occurs or any other
264 compile-time or return error, then $@ is set to the error message, just
265 as with an eval().</p>
266 </dd>
267 <dd>
268 <p>If there is no error, then the method returns the value of the last
269 expression evaluated, or a return statement may be used, just as with
270 subroutines and <strong>eval()</strong>. The context (list or scalar) is determined
271 by the caller as usual.</p>
272 </dd>
273 <dd>
274 <p>This behaviour differs from the beta distribution of the Safe extension
275 where earlier versions of perl made it hard to mimic the return
276 behaviour of the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a> command and the context was always scalar.</p>
277 </dd>
278 <dd>
279 <p>Some points to note:</p>
280 </dd>
281 <dd>
282 <p>If the entereval op is permitted then the code can use eval ``...'' to
283 'hide' code which might use denied ops. This is not a major problem
284 since when the code tries to execute the eval it will fail because the
285 opmask is still in effect. However this technique would allow clever,
286 and possibly harmful, code to 'probe' the boundaries of what is
287 possible.</p>
288 </dd>
289 <dd>
290 <p>Any string eval which is executed by code executing in a compartment,
291 or by code called from code executing in a compartment, will be eval'd
292 in the namespace of the compartment. This is potentially a serious
293 problem.</p>
294 </dd>
295 <dd>
296 <p>Consider a function <code>foo()</code> in package pkg compiled outside a compartment
297 but shared with it. Assume the compartment has a root package called
298 'Root'. If <code>foo()</code> contains an eval statement like eval '$foo = 1' then,
299 normally, $pkg::foo will be set to 1. If <code>foo()</code> is called from the
300 compartment (by whatever means) then instead of setting $pkg::foo, the
301 eval will actually set $Root::pkg::foo.</p>
302 </dd>
303 <dd>
304 <p>This can easily be demonstrated by using a module, such as the Socket
305 module, which uses eval ``...'' as part of an AUTOLOAD function. You can
306 'use' the module outside the compartment and share an (autoloaded)
307 function with the compartment. If an autoload is triggered by code in
308 the compartment, or by any code anywhere that is called by any means
309 from the compartment, then the eval in the Socket module's AUTOLOAD
310 function happens in the namespace of the compartment. Any variables
311 created or used by the eval'd code are now under the control of
312 the code in the compartment.</p>
313 </dd>
314 <dd>
315 <p>A similar effect applies to <em>all</em> runtime symbol lookups in code
316 called from a compartment but not compiled within it.</p>
317 </dd>
318 </li>
319 <dt><strong><a name="item_rdo">rdo (FILENAME)</a></strong>
321 <dd>
322 <p>This evaluates the contents of file FILENAME inside the compartment.
323 See above documentation on the <strong>reval</strong> method for further details.</p>
324 </dd>
325 </li>
326 <dt><strong><a name="item_root">root (NAMESPACE)</a></strong>
328 <dd>
329 <p>This method returns the name of the package that is the root of the
330 compartment's namespace.</p>
331 </dd>
332 <dd>
333 <p>Note that this behaviour differs from version 1.00 of the Safe module
334 where the root module could be used to change the namespace. That
335 functionality has been withdrawn pending deeper consideration.</p>
336 </dd>
337 </li>
338 <dt><strong><a name="item_mask">mask (MASK)</a></strong>
340 <dd>
341 <p>This is a get-or-set method for the compartment's operator mask.</p>
342 </dd>
343 <dd>
344 <p>With no MASK argument present, it returns the current operator mask of
345 the compartment.</p>
346 </dd>
347 <dd>
348 <p>With the MASK argument present, it sets the operator mask for the
349 compartment (equivalent to calling the deny_only method).</p>
350 </dd>
351 </li>
352 </dl>
354 </p>
355 <h2><a name="some_safety_issues">Some Safety Issues</a></h2>
356 <p>This section is currently just an outline of some of the things code in
357 a compartment might do (intentionally or unintentionally) which can
358 have an effect outside the compartment.</p>
359 <dl>
360 <dt><strong><a name="item_memory">Memory</a></strong>
362 <dd>
363 <p>Consuming all (or nearly all) available memory.</p>
364 </dd>
365 </li>
366 <dt><strong><a name="item_cpu">CPU</a></strong>
368 <dd>
369 <p>Causing infinite loops etc.</p>
370 </dd>
371 </li>
372 <dt><strong><a name="item_snooping">Snooping</a></strong>
374 <dd>
375 <p>Copying private information out of your system. Even something as
376 simple as your user name is of value to others. Much useful information
377 could be gleaned from your environment variables for example.</p>
378 </dd>
379 </li>
380 <dt><strong><a name="item_signals">Signals</a></strong>
382 <dd>
383 <p>Causing signals (especially SIGFPE and SIGALARM) to affect your process.</p>
384 </dd>
385 <dd>
386 <p>Setting up a signal handler will need to be carefully considered
387 and controlled. What mask is in effect when a signal handler
388 gets called? If a user can get an imported function to get an
389 exception and call the user's signal handler, does that user's
390 restricted mask get re-instated before the handler is called?
391 Does an imported handler get called with its original mask or
392 the user's one?</p>
393 </dd>
394 </li>
395 <dt><strong><a name="item_state_changes">State Changes</a></strong>
397 <dd>
398 <p>Ops such as chdir obviously effect the process as a whole and not just
399 the code in the compartment. Ops such as rand and srand have a similar
400 but more subtle effect.</p>
401 </dd>
402 </li>
403 </dl>
405 </p>
406 <h2><a name="author">AUTHOR</a></h2>
407 <p>Originally designed and implemented by Malcolm Beattie,
408 <a href="mailto:mbeattie@sable.ox.ac.uk.">mbeattie@sable.ox.ac.uk.</a></p>
409 <p>Reworked to use the Opcode module and other changes added by Tim Bunce
410 &lt;<em><a href="mailto:Tim.Bunce@ig.co.uk">Tim.Bunce@ig.co.uk</a></em>&gt;.</p>
411 <table border="0" width="100%" cellspacing="0" cellpadding="3">
412 <tr><td class="block" style="background-color: #cccccc" valign="middle">
413 <big><strong><span class="block">&nbsp;Safe - Compile and execute code in restricted compartments</span></strong></big>
414 </td></tr>
415 </table>
417 </body>
419 </html>