Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / Safe.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
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 "2001-09-21" "perl v5.8.8" "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.Sp
244You can list opcodes by names, or use a tag name; see
245\&\*(L"Predefined Opcode Tags\*(R" in Opcode.
246.IP "permit_only (\s-1OP\s0, ...)" 8
247.IX Item "permit_only (OP, ...)"
248Permit \fIonly\fR the listed operators to be used when compiling code in
249the compartment (\fIno\fR other operators are permitted).
250.IP "deny (\s-1OP\s0, ...)" 8
251.IX Item "deny (OP, ...)"
252Deny the listed operators from being used when compiling code in the
253compartment (other operators may still be permitted).
254.IP "deny_only (\s-1OP\s0, ...)" 8
255.IX Item "deny_only (OP, ...)"
256Deny \fIonly\fR the listed operators from being used when compiling code
257in the compartment (\fIall\fR other operators will be permitted).
258.IP "trap (\s-1OP\s0, ...)" 8
259.IX Item "trap (OP, ...)"
260.PD 0
261.IP "untrap (\s-1OP\s0, ...)" 8
262.IX Item "untrap (OP, ...)"
263.PD
264The trap and untrap methods are synonyms for deny and permit
265respectfully.
266.IP "share (\s-1NAME\s0, ...)" 8
267.IX Item "share (NAME, ...)"
268This shares the variable(s) in the argument list with the compartment.
269This is almost identical to exporting variables using the Exporter
270module.
271.Sp
272Each \s-1NAME\s0 must be the \fBname\fR of a non-lexical variable, typically
273with the leading type identifier included. A bareword is treated as a
274function name.
275.Sp
276Examples of legal names are '$foo' for a scalar, '@foo' for an
277array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'
278for a glob (i.e. all symbol table entries associated with \*(L"foo\*(R",
279including scalar, array, hash, sub and filehandle).
280.Sp
281Each \s-1NAME\s0 is assumed to be in the calling package. See share_from
282for an alternative method (which share uses).
283.IP "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)" 8
284.IX Item "share_from (PACKAGE, ARRAYREF)"
285This method is similar to \fIshare()\fR but allows you to explicitly name the
286package that symbols should be shared from. The symbol names (including
287type characters) are supplied as an array reference.
288.Sp
289.Vb 1
290\& $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
291.Ve
292.IP "varglob (\s-1VARNAME\s0)" 8
293.IX Item "varglob (VARNAME)"
294This returns a glob reference for the symbol table entry of \s-1VARNAME\s0 in
295the package of the compartment. \s-1VARNAME\s0 must be the \fBname\fR of a
296variable without any leading type marker. For example,
297.Sp
298.Vb 4
299\& $cpt = new Safe 'Root';
300\& $Root::foo = "Hello world";
301\& # Equivalent version which doesn't need to know $cpt's package name:
302\& ${$cpt->varglob('foo')} = "Hello world";
303.Ve
304.IP "reval (\s-1STRING\s0)" 8
305.IX Item "reval (STRING)"
306This evaluates \s-1STRING\s0 as perl code inside the compartment.
307.Sp
308The code can only see the compartment's namespace (as returned by the
309\&\fBroot\fR method). The compartment's root package appears to be the
310\&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment.
311.Sp
312Any attempt by the code in \s-1STRING\s0 to use an operator which is not permitted
313by the compartment will cause an error (at run-time of the main program
314but at compile-time for the code in \s-1STRING\s0). The error is of the form
315\&\*(L"'%s' trapped by operation mask...\*(R".
316.Sp
317If an operation is trapped in this way, then the code in \s-1STRING\s0 will
318not be executed. If such a trapped operation occurs or any other
319compile-time or return error, then $@ is set to the error message, just
320as with an \fIeval()\fR.
321.Sp
322If there is no error, then the method returns the value of the last
323expression evaluated, or a return statement may be used, just as with
324subroutines and \fB\f(BIeval()\fB\fR. The context (list or scalar) is determined
325by the caller as usual.
326.Sp
327This behaviour differs from the beta distribution of the Safe extension
328where earlier versions of perl made it hard to mimic the return
329behaviour of the \fIeval()\fR command and the context was always scalar.
330.Sp
331Some points to note:
332.Sp
333If the entereval op is permitted then the code can use eval \*(L"...\*(R" to
334\&'hide' code which might use denied ops. This is not a major problem
335since when the code tries to execute the eval it will fail because the
336opmask is still in effect. However this technique would allow clever,
337and possibly harmful, code to 'probe' the boundaries of what is
338possible.
339.Sp
340Any string eval which is executed by code executing in a compartment,
341or by code called from code executing in a compartment, will be eval'd
342in the namespace of the compartment. This is potentially a serious
343problem.
344.Sp
345Consider a function \fIfoo()\fR in package pkg compiled outside a compartment
346but shared with it. Assume the compartment has a root package called
347\&'Root'. If \fIfoo()\fR contains an eval statement like eval '$foo = 1' then,
348normally, \f(CW$pkg::foo\fR will be set to 1. If \fIfoo()\fR is called from the
349compartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, the
350eval will actually set \f(CW$Root::pkg::foo\fR.
351.Sp
352This can easily be demonstrated by using a module, such as the Socket
353module, which uses eval \*(L"...\*(R" as part of an \s-1AUTOLOAD\s0 function. You can
354\&'use' the module outside the compartment and share an (autoloaded)
355function with the compartment. If an autoload is triggered by code in
356the compartment, or by any code anywhere that is called by any means
357from the compartment, then the eval in the Socket module's \s-1AUTOLOAD\s0
358function happens in the namespace of the compartment. Any variables
359created or used by the eval'd code are now under the control of
360the code in the compartment.
361.Sp
362A similar effect applies to \fIall\fR runtime symbol lookups in code
363called from a compartment but not compiled within it.
364.IP "rdo (\s-1FILENAME\s0)" 8
365.IX Item "rdo (FILENAME)"
366This evaluates the contents of file \s-1FILENAME\s0 inside the compartment.
367See above documentation on the \fBreval\fR method for further details.
368.IP "root (\s-1NAMESPACE\s0)" 8
369.IX Item "root (NAMESPACE)"
370This method returns the name of the package that is the root of the
371compartment's namespace.
372.Sp
373Note that this behaviour differs from version 1.00 of the Safe module
374where the root module could be used to change the namespace. That
375functionality has been withdrawn pending deeper consideration.
376.IP "mask (\s-1MASK\s0)" 8
377.IX Item "mask (MASK)"
378This is a get-or-set method for the compartment's operator mask.
379.Sp
380With no \s-1MASK\s0 argument present, it returns the current operator mask of
381the compartment.
382.Sp
383With the \s-1MASK\s0 argument present, it sets the operator mask for the
384compartment (equivalent to calling the deny_only method).
385.Sh "Some Safety Issues"
386.IX Subsection "Some Safety Issues"
387This section is currently just an outline of some of the things code in
388a compartment might do (intentionally or unintentionally) which can
389have an effect outside the compartment.
390.IP "Memory" 8
391.IX Item "Memory"
392Consuming all (or nearly all) available memory.
393.IP "\s-1CPU\s0" 8
394.IX Item "CPU"
395Causing infinite loops etc.
396.IP "Snooping" 8
397.IX Item "Snooping"
398Copying private information out of your system. Even something as
399simple as your user name is of value to others. Much useful information
400could be gleaned from your environment variables for example.
401.IP "Signals" 8
402.IX Item "Signals"
403Causing signals (especially \s-1SIGFPE\s0 and \s-1SIGALARM\s0) to affect your process.
404.Sp
405Setting up a signal handler will need to be carefully considered
406and controlled. What mask is in effect when a signal handler
407gets called? If a user can get an imported function to get an
408exception and call the user's signal handler, does that user's
409restricted mask get re-instated before the handler is called?
410Does an imported handler get called with its original mask or
411the user's one?
412.IP "State Changes" 8
413.IX Item "State Changes"
414Ops such as chdir obviously effect the process as a whole and not just
415the code in the compartment. Ops such as rand and srand have a similar
416but more subtle effect.
417.Sh "\s-1AUTHOR\s0"
418.IX Subsection "AUTHOR"
419Originally designed and implemented by Malcolm Beattie,
420mbeattie@sable.ox.ac.uk.
421.PP
422Reworked to use the Opcode module and other changes added by Tim Bunce
423<\fITim.Bunce@ig.co.uk\fR>.