Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Safe.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Safe 3"
132.TH Safe 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134Safe \- Compile and execute code in restricted compartments
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& use Safe;
139.Ve
140.PP
141.Vb 1
142\& $compartment = new Safe;
143.Ve
144.PP
145.Vb 1
146\& $compartment->permit(qw(time sort :browse));
147.Ve
148.PP
149.Vb 1
150\& $result = $compartment->reval($unsafe_code);
151.Ve
152.SH "DESCRIPTION"
153.IX Header "DESCRIPTION"
154The Safe extension module allows the creation of compartments
155in which perl code can be evaluated. Each compartment has
156.IP "a new namespace" 8
157.IX Item "a new namespace"
158The \*(L"root\*(R" of the namespace (i.e. \*(L"main::\*(R") is changed to a
159different package and code evaluated in the compartment cannot
160refer to variables outside this namespace, even with run-time
161glob lookups and other tricks.
162.Sp
163Code which is compiled outside the compartment can choose to place
164variables into (or \fIshare\fR variables with) the compartment's namespace
165and only that data will be visible to code evaluated in the
166compartment.
167.Sp
168By default, the only variables shared with compartments are the
169\&\*(L"underscore\*(R" variables \f(CW$_\fR and \f(CW@_\fR (and, technically, the less frequently
170used \f(CW%_\fR, the _ filehandle and so on). This is because otherwise perl
171operators which default to \f(CW$_\fR will not work and neither will the
172assignment of arguments to \f(CW@_\fR on subroutine entry.
173.IP "an operator mask" 8
174.IX Item "an operator mask"
175Each compartment has an associated \*(L"operator mask\*(R". Recall that
176perl code is compiled into an internal format before execution.
177Evaluating perl code (e.g. via \*(L"eval\*(R" or \*(L"do 'file'\*(R") causes
178the code to be compiled into an internal format and then,
179provided there was no error in the compilation, executed.
180Code evaluated in a compartment compiles subject to the
181compartment's operator mask. Attempting to evaluate code in a
182compartment which contains a masked operator will cause the
183compilation to fail with an error. The code will not be executed.
184.Sp
185The default operator mask for a newly created compartment is
186the ':default' optag.
187.Sp
188It is important that you read the \fIOpcode\fR\|(3) module documentation
189for more information, especially for detailed definitions of opnames,
190optags and opsets.
191.Sp
192Since it is only at the compilation stage that the operator mask
193applies, controlled access to potentially unsafe operations can
194be achieved by having a handle to a wrapper subroutine (written
195outside the compartment) placed into the compartment. For example,
196.Sp
197.Vb 5
198\& $cpt = new Safe;
199\& sub wrapper {
200\& # vet arguments and perform potentially unsafe operations
201\& }
202\& $cpt->share('&wrapper');
203.Ve
204.SH "WARNING"
205.IX Header "WARNING"
206The authors make \fBno warranty\fR, implied or otherwise, about the
207suitability of this software for safety or security purposes.
208.PP
209The authors shall not in any case be liable for special, incidental,
210consequential, indirect or other similar damages arising from the use
211of this software.
212.PP
213Your mileage will vary. If in any doubt \fBdo not use it\fR.
214.Sh "\s-1RECENT\s0 \s-1CHANGES\s0"
215.IX Subsection "RECENT CHANGES"
216The interface to the Safe module has changed quite dramatically since
217version 1 (as supplied with Perl5.002). Study these pages carefully if
218you have code written to use Safe version 1 because you will need to
219makes changes.
220.Sh "Methods in class Safe"
221.IX Subsection "Methods in class Safe"
222To create a new compartment, use
223.PP
224.Vb 1
225\& $cpt = new Safe;
226.Ve
227.PP
228Optional argument is (\s-1NAMESPACE\s0), where \s-1NAMESPACE\s0 is the root namespace
229to use for the compartment (defaults to \*(L"Safe::Root0\*(R", incremented for
230each new compartment).
231.PP
232Note that version 1.00 of the Safe module supported a second optional
233parameter, \s-1MASK\s0. That functionality has been withdrawn pending deeper
234consideration. Use the permit and deny methods described below.
235.PP
236The following methods can then be used on the compartment
237object returned by the above constructor. The object argument
238is implicit in each case.
239.IP "permit (\s-1OP\s0, ...)" 8
240.IX Item "permit (OP, ...)"
241Permit the listed operators to be used when compiling code in the
242compartment (in \fIaddition\fR to any operators already permitted).
243.IP "permit_only (\s-1OP\s0, ...)" 8
244.IX Item "permit_only (OP, ...)"
245Permit \fIonly\fR the listed operators to be used when compiling code in
246the compartment (\fIno\fR other operators are permitted).
247.IP "deny (\s-1OP\s0, ...)" 8
248.IX Item "deny (OP, ...)"
249Deny the listed operators from being used when compiling code in the
250compartment (other operators may still be permitted).
251.IP "deny_only (\s-1OP\s0, ...)" 8
252.IX Item "deny_only (OP, ...)"
253Deny \fIonly\fR the listed operators from being used when compiling code
254in the compartment (\fIall\fR other operators will be permitted).
255.IP "trap (\s-1OP\s0, ...)" 8
256.IX Item "trap (OP, ...)"
257.PD 0
258.IP "untrap (\s-1OP\s0, ...)" 8
259.IX Item "untrap (OP, ...)"
260.PD
261The trap and untrap methods are synonyms for deny and permit
262respectfully.
263.IP "share (\s-1NAME\s0, ...)" 8
264.IX Item "share (NAME, ...)"
265This shares the variable(s) in the argument list with the compartment.
266This is almost identical to exporting variables using the Exporter
267module.
268.Sp
269Each \s-1NAME\s0 must be the \fBname\fR of a variable, typically with the leading
270type identifier included. A bareword is treated as a function name.
271.Sp
272Examples of legal names are '$foo' for a scalar, '@foo' for an
273array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'
274for a glob (i.e. all symbol table entries associated with \*(L"foo\*(R",
275including scalar, array, hash, sub and filehandle).
276.Sp
277Each \s-1NAME\s0 is assumed to be in the calling package. See share_from
278for an alternative method (which share uses).
279.IP "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)" 8
280.IX Item "share_from (PACKAGE, ARRAYREF)"
281This method is similar to \fIshare()\fR but allows you to explicitly name the
282package that symbols should be shared from. The symbol names (including
283type characters) are supplied as an array reference.
284.Sp
285.Vb 1
286\& $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
287.Ve
288.IP "varglob (\s-1VARNAME\s0)" 8
289.IX Item "varglob (VARNAME)"
290This returns a glob reference for the symbol table entry of \s-1VARNAME\s0 in
291the package of the compartment. \s-1VARNAME\s0 must be the \fBname\fR of a
292variable without any leading type marker. For example,
293.Sp
294.Vb 4
295\& $cpt = new Safe 'Root';
296\& $Root::foo = "Hello world";
297\& # Equivalent version which doesn't need to know $cpt's package name:
298\& ${$cpt->varglob('foo')} = "Hello world";
299.Ve
300.IP "reval (\s-1STRING\s0)" 8
301.IX Item "reval (STRING)"
302This evaluates \s-1STRING\s0 as perl code inside the compartment.
303.Sp
304The code can only see the compartment's namespace (as returned by the
305\&\fBroot\fR method). The compartment's root package appears to be the
306\&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment.
307.Sp
308Any attempt by the code in \s-1STRING\s0 to use an operator which is not permitted
309by the compartment will cause an error (at run-time of the main program
310but at compile-time for the code in \s-1STRING\s0). The error is of the form
311\&\*(L"%s trapped by operation mask operation...\*(R".
312.Sp
313If an operation is trapped in this way, then the code in \s-1STRING\s0 will
314not be executed. If such a trapped operation occurs or any other
315compile-time or return error, then $@ is set to the error message, just
316as with an \fIeval()\fR.
317.Sp
318If there is no error, then the method returns the value of the last
319expression evaluated, or a return statement may be used, just as with
320subroutines and \fB\f(BIeval()\fB\fR. The context (list or scalar) is determined
321by the caller as usual.
322.Sp
323This behaviour differs from the beta distribution of the Safe extension
324where earlier versions of perl made it hard to mimic the return
325behaviour of the \fIeval()\fR command and the context was always scalar.
326.Sp
327Some points to note:
328.Sp
329If the entereval op is permitted then the code can use eval \*(L"...\*(R" to
330\&'hide' code which might use denied ops. This is not a major problem
331since when the code tries to execute the eval it will fail because the
332opmask is still in effect. However this technique would allow clever,
333and possibly harmful, code to 'probe' the boundaries of what is
334possible.
335.Sp
336Any string eval which is executed by code executing in a compartment,
337or by code called from code executing in a compartment, will be eval'd
338in the namespace of the compartment. This is potentially a serious
339problem.
340.Sp
341Consider a function \fIfoo()\fR in package pkg compiled outside a compartment
342but shared with it. Assume the compartment has a root package called
343\&'Root'. If \fIfoo()\fR contains an eval statement like eval '$foo = 1' then,
344normally, \f(CW$pkg::foo\fR will be set to 1. If \fIfoo()\fR is called from the
345compartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, the
346eval will actually set \f(CW$Root::pkg::foo\fR.
347.Sp
348This can easily be demonstrated by using a module, such as the Socket
349module, which uses eval \*(L"...\*(R" as part of an \s-1AUTOLOAD\s0 function. You can
350\&'use' the module outside the compartment and share an (autoloaded)
351function with the compartment. If an autoload is triggered by code in
352the compartment, or by any code anywhere that is called by any means
353from the compartment, then the eval in the Socket module's \s-1AUTOLOAD\s0
354function happens in the namespace of the compartment. Any variables
355created or used by the eval'd code are now under the control of
356the code in the compartment.
357.Sp
358A similar effect applies to \fIall\fR runtime symbol lookups in code
359called from a compartment but not compiled within it.
360.IP "rdo (\s-1FILENAME\s0)" 8
361.IX Item "rdo (FILENAME)"
362This evaluates the contents of file \s-1FILENAME\s0 inside the compartment.
363See above documentation on the \fBreval\fR method for further details.
364.IP "root (\s-1NAMESPACE\s0)" 8
365.IX Item "root (NAMESPACE)"
366This method returns the name of the package that is the root of the
367compartment's namespace.
368.Sp
369Note that this behaviour differs from version 1.00 of the Safe module
370where the root module could be used to change the namespace. That
371functionality has been withdrawn pending deeper consideration.
372.IP "mask (\s-1MASK\s0)" 8
373.IX Item "mask (MASK)"
374This is a get-or-set method for the compartment's operator mask.
375.Sp
376With no \s-1MASK\s0 argument present, it returns the current operator mask of
377the compartment.
378.Sp
379With the \s-1MASK\s0 argument present, it sets the operator mask for the
380compartment (equivalent to calling the deny_only method).
381.Sh "Some Safety Issues"
382.IX Subsection "Some Safety Issues"
383This section is currently just an outline of some of the things code in
384a compartment might do (intentionally or unintentionally) which can
385have an effect outside the compartment.
386.IP "Memory" 8
387.IX Item "Memory"
388Consuming all (or nearly all) available memory.
389.IP "\s-1CPU\s0" 8
390.IX Item "CPU"
391Causing infinite loops etc.
392.IP "Snooping" 8
393.IX Item "Snooping"
394Copying private information out of your system. Even something as
395simple as your user name is of value to others. Much useful information
396could be gleaned from your environment variables for example.
397.IP "Signals" 8
398.IX Item "Signals"
399Causing signals (especially \s-1SIGFPE\s0 and \s-1SIGALARM\s0) to affect your process.
400.Sp
401Setting up a signal handler will need to be carefully considered
402and controlled. What mask is in effect when a signal handler
403gets called? If a user can get an imported function to get an
404exception and call the user's signal handler, does that user's
405restricted mask get re-instated before the handler is called?
406Does an imported handler get called with its original mask or
407the user's one?
408.IP "State Changes" 8
409.IX Item "State Changes"
410Ops such as chdir obviously effect the process as a whole and not just
411the code in the compartment. Ops such as rand and srand have a similar
412but more subtle effect.
413.Sh "\s-1AUTHOR\s0"
414.IX Subsection "AUTHOR"
415Originally designed and implemented by Malcolm Beattie,
416mbeattie@sable.ox.ac.uk.
417.PP
418Reworked to use the Opcode module and other changes added by Tim Bunce
419<\fITim.Bunce@ig.co.uk\fR>.