.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.13)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.el \{\
.    de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Safe 3pm"
.TH Safe 3pm "2017-03-22" "perl v5.10.1" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Safe \- Compile and execute code in restricted compartments
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&  use Safe;
\&
\&  $compartment = new Safe;
\&
\&  $compartment\->permit(qw(time sort :browse));
\&
\&  $result = $compartment\->reval($unsafe_code);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
The Safe extension module allows the creation of compartments
in which perl code can be evaluated. Each compartment has
.IP "a new namespace" 8
.IX Item "a new namespace"
The \*(L"root\*(R" of the namespace (i.e. \*(L"main::\*(R") is changed to a
different package and code evaluated in the compartment cannot
refer to variables outside this namespace, even with run-time
glob lookups and other tricks.
.Sp
Code which is compiled outside the compartment can choose to place
variables into (or \fIshare\fR variables with) the compartment's namespace
and only that data will be visible to code evaluated in the
compartment.
.Sp
By default, the only variables shared with compartments are the
\&\*(L"underscore\*(R" variables \f(CW$_\fR and \f(CW@_\fR (and, technically, the less frequently
used \f(CW%_\fR, the _ filehandle and so on). This is because otherwise perl
operators which default to \f(CW$_\fR will not work and neither will the
assignment of arguments to \f(CW@_\fR on subroutine entry.
.IP "an operator mask" 8
.IX Item "an operator mask"
Each compartment has an associated \*(L"operator mask\*(R". Recall that
perl code is compiled into an internal format before execution.
Evaluating perl code (e.g. via \*(L"eval\*(R" or \*(L"do 'file'\*(R") causes
the code to be compiled into an internal format and then,
provided there was no error in the compilation, executed.
Code evaluated in a compartment compiles subject to the
compartment's operator mask. Attempting to evaluate code in a
compartment which contains a masked operator will cause the
compilation to fail with an error. The code will not be executed.
.Sp
The default operator mask for a newly created compartment is
the ':default' optag.
.Sp
It is important that you read the Opcode module documentation
for more information, especially for detailed definitions of opnames,
optags and opsets.
.Sp
Since it is only at the compilation stage that the operator mask
applies, controlled access to potentially unsafe operations can
be achieved by having a handle to a wrapper subroutine (written
outside the compartment) placed into the compartment. For example,
.Sp
.Vb 5
\&    $cpt = new Safe;
\&    sub wrapper {
\&        # vet arguments and perform potentially unsafe operations
\&    }
\&    $cpt\->share(\*(Aq&wrapper\*(Aq);
.Ve
.SH "WARNING"
.IX Header "WARNING"
The authors make \fBno warranty\fR, implied or otherwise, about the
suitability of this software for safety or security purposes.
.PP
The authors shall not in any case be liable for special, incidental,
consequential, indirect or other similar damages arising from the use
of this software.
.PP
Your mileage will vary. If in any doubt \fBdo not use it\fR.
.SH "METHODS"
.IX Header "METHODS"
To create a new compartment, use
.PP
.Vb 1
\&    $cpt = new Safe;
.Ve
.PP
Optional argument is (\s-1NAMESPACE\s0), where \s-1NAMESPACE\s0 is the root namespace
to use for the compartment (defaults to \*(L"Safe::Root0\*(R", incremented for
each new compartment).
.PP
Note that version 1.00 of the Safe module supported a second optional
parameter, \s-1MASK\s0.  That functionality has been withdrawn pending deeper
consideration. Use the permit and deny methods described below.
.PP
The following methods can then be used on the compartment
object returned by the above constructor. The object argument
is implicit in each case.
.SS "permit (\s-1OP\s0, ...)"
.IX Subsection "permit (OP, ...)"
Permit the listed operators to be used when compiling code in the
compartment (in \fIaddition\fR to any operators already permitted).
.PP
You can list opcodes by names, or use a tag name; see
\&\*(L"Predefined Opcode Tags\*(R" in Opcode.
.SS "permit_only (\s-1OP\s0, ...)"
.IX Subsection "permit_only (OP, ...)"
Permit \fIonly\fR the listed operators to be used when compiling code in
the compartment (\fIno\fR other operators are permitted).
.SS "deny (\s-1OP\s0, ...)"
.IX Subsection "deny (OP, ...)"
Deny the listed operators from being used when compiling code in the
compartment (other operators may still be permitted).
.SS "deny_only (\s-1OP\s0, ...)"
.IX Subsection "deny_only (OP, ...)"
Deny \fIonly\fR the listed operators from being used when compiling code
in the compartment (\fIall\fR other operators will be permitted, so you probably
don't want to use this method).
.SS "trap (\s-1OP\s0, ...)"
.IX Subsection "trap (OP, ...)"
.SS "untrap (\s-1OP\s0, ...)"
.IX Subsection "untrap (OP, ...)"
The trap and untrap methods are synonyms for deny and permit
respectfully.
.SS "share (\s-1NAME\s0, ...)"
.IX Subsection "share (NAME, ...)"
This shares the variable(s) in the argument list with the compartment.
This is almost identical to exporting variables using the Exporter
module.
.PP
Each \s-1NAME\s0 must be the \fBname\fR of a non-lexical variable, typically
with the leading type identifier included. A bareword is treated as a
function name.
.PP
Examples of legal names are '$foo' for a scalar, '@foo' for an
array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'
for a glob (i.e.  all symbol table entries associated with \*(L"foo\*(R",
including scalar, array, hash, sub and filehandle).
.PP
Each \s-1NAME\s0 is assumed to be in the calling package. See share_from
for an alternative method (which \f(CW\*(C`share\*(C'\fR uses).
.SS "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)"
.IX Subsection "share_from (PACKAGE, ARRAYREF)"
This method is similar to \fIshare()\fR but allows you to explicitly name the
package that symbols should be shared from. The symbol names (including
type characters) are supplied as an array reference.
.PP
.Vb 1
\&    $safe\->share_from(\*(Aqmain\*(Aq, [ \*(Aq$foo\*(Aq, \*(Aq%bar\*(Aq, \*(Aqfunc\*(Aq ]);
.Ve
.PP
Names can include package names, which are relative to the specified \s-1PACKAGE\s0.
So these two calls have the same effect:
.PP
.Vb 2
\&    $safe\->share_from(\*(AqScalar::Util\*(Aq, [ \*(Aqreftype\*(Aq ]);
\&    $safe\->share_from(\*(Aqmain\*(Aq, [ \*(AqScalar::Util::reftype\*(Aq ]);
.Ve
.SS "varglob (\s-1VARNAME\s0)"
.IX Subsection "varglob (VARNAME)"
This returns a glob reference for the symbol table entry of \s-1VARNAME\s0 in
the package of the compartment. \s-1VARNAME\s0 must be the \fBname\fR of a
variable without any leading type marker. For example:
.PP
.Vb 1
\&    ${$cpt\->varglob(\*(Aqfoo\*(Aq)} = "Hello world";
.Ve
.PP
has the same effect as:
.PP
.Vb 2
\&    $cpt = new Safe \*(AqRoot\*(Aq;
\&    $Root::foo = "Hello world";
.Ve
.PP
but avoids the need to know \f(CW$cpt\fR's package name.
.SS "reval (\s-1STRING\s0, \s-1STRICT\s0)"
.IX Subsection "reval (STRING, STRICT)"
This evaluates \s-1STRING\s0 as perl code inside the compartment.
.PP
The code can only see the compartment's namespace (as returned by the
\&\fBroot\fR method). The compartment's root package appears to be the
\&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment.
.PP
Any attempt by the code in \s-1STRING\s0 to use an operator which is not permitted
by the compartment will cause an error (at run-time of the main program
but at compile-time for the code in \s-1STRING\s0).  The error is of the form
\&\*(L"'%s' trapped by operation mask...\*(R".
.PP
If an operation is trapped in this way, then the code in \s-1STRING\s0 will
not be executed. If such a trapped operation occurs or any other
compile-time or return error, then $@ is set to the error message, just
as with an \fIeval()\fR.
.PP
If there is no error, then the method returns the value of the last
expression evaluated, or a return statement may be used, just as with
subroutines and \fB\f(BIeval()\fB\fR. The context (list or scalar) is determined
by the caller as usual.
.PP
If the return value of \fIreval()\fR is (or contains) any code reference,
those code references are wrapped to be themselves executed always
in the compartment. See \*(L"wrap_code_refs_within\*(R".
.PP
The formerly undocumented \s-1STRICT\s0 argument sets strictness: if true
\&'use strict;' is used, otherwise it uses 'no strict;'. \fBNote\fR: if
\&\s-1STRICT\s0 is omitted 'no strict;' is the default.
.PP
Some points to note:
.PP
If the entereval op is permitted then the code can use eval \*(L"...\*(R" to
\&'hide' code which might use denied ops. This is not a major problem
since when the code tries to execute the eval it will fail because the
opmask is still in effect. However this technique would allow clever,
and possibly harmful, code to 'probe' the boundaries of what is
possible.
.PP
Any string eval which is executed by code executing in a compartment,
or by code called from code executing in a compartment, will be eval'd
in the namespace of the compartment. This is potentially a serious
problem.
.PP
Consider a function \fIfoo()\fR in package pkg compiled outside a compartment
but shared with it. Assume the compartment has a root package called
\&'Root'. If \fIfoo()\fR contains an eval statement like eval '$foo = 1' then,
normally, \f(CW$pkg::foo\fR will be set to 1.  If \fIfoo()\fR is called from the
compartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, the
eval will actually set \f(CW$Root::pkg::foo\fR.
.PP
This can easily be demonstrated by using a module, such as the Socket
module, which uses eval \*(L"...\*(R" as part of an \s-1AUTOLOAD\s0 function. You can
\&'use' the module outside the compartment and share an (autoloaded)
function with the compartment. If an autoload is triggered by code in
the compartment, or by any code anywhere that is called by any means
from the compartment, then the eval in the Socket module's \s-1AUTOLOAD\s0
function happens in the namespace of the compartment. Any variables
created or used by the eval'd code are now under the control of
the code in the compartment.
.PP
A similar effect applies to \fIall\fR runtime symbol lookups in code
called from a compartment but not compiled within it.
.SS "rdo (\s-1FILENAME\s0)"
.IX Subsection "rdo (FILENAME)"
This evaluates the contents of file \s-1FILENAME\s0 inside the compartment.
See above documentation on the \fBreval\fR method for further details.
.SS "root (\s-1NAMESPACE\s0)"
.IX Subsection "root (NAMESPACE)"
This method returns the name of the package that is the root of the
compartment's namespace.
.PP
Note that this behaviour differs from version 1.00 of the Safe module
where the root module could be used to change the namespace. That
functionality has been withdrawn pending deeper consideration.
.SS "mask (\s-1MASK\s0)"
.IX Subsection "mask (MASK)"
This is a get-or-set method for the compartment's operator mask.
.PP
With no \s-1MASK\s0 argument present, it returns the current operator mask of
the compartment.
.PP
With the \s-1MASK\s0 argument present, it sets the operator mask for the
compartment (equivalent to calling the deny_only method).
.SS "wrap_code_ref (\s-1CODEREF\s0)"
.IX Subsection "wrap_code_ref (CODEREF)"
Returns a reference to an anonymous subroutine that, when executed, will call
\&\s-1CODEREF\s0 with the Safe compartment 'in effect'.  In other words, with the
package namespace adjusted and the opmask enabled.
.PP
Note that the opmask doesn't affect the already compiled code, it only affects
any \fIfurther\fR compilation that the already compiled code may try to perform.
.PP
This is particularly useful when applied to code references returned from \fIreval()\fR.
.PP
(It also provides a kind of workaround for RT#60374: \*(L"Safe.pm sort {} bug with
\&\-Dusethreads\*(R". See <http://rt.perl.org/rt3//Public/Bug/Display.html?id=60374>
for \fImuch\fR more detail.)
.SS "wrap_code_refs_within (...)"
.IX Subsection "wrap_code_refs_within (...)"
Wraps any \s-1CODE\s0 references found within the arguments by replacing each with the
result of calling \*(L"wrap_code_ref\*(R" on the \s-1CODE\s0 reference. Any \s-1ARRAY\s0 or \s-1HASH\s0
references in the arguments are inspected recursively.
.PP
Returns nothing.
.SH "RISKS"
.IX Header "RISKS"
This section is just an outline of some of the things code in a compartment
might do (intentionally or unintentionally) which can have an effect outside
the compartment.
.IP "Memory" 8
.IX Item "Memory"
Consuming all (or nearly all) available memory.
.IP "\s-1CPU\s0" 8
.IX Item "CPU"
Causing infinite loops etc.
.IP "Snooping" 8
.IX Item "Snooping"
Copying private information out of your system. Even something as
simple as your user name is of value to others. Much useful information
could be gleaned from your environment variables for example.
.IP "Signals" 8
.IX Item "Signals"
Causing signals (especially \s-1SIGFPE\s0 and \s-1SIGALARM\s0) to affect your process.
.Sp
Setting up a signal handler will need to be carefully considered
and controlled.  What mask is in effect when a signal handler
gets called?  If a user can get an imported function to get an
exception and call the user's signal handler, does that user's
restricted mask get re-instated before the handler is called?
Does an imported handler get called with its original mask or
the user's one?
.IP "State Changes" 8
.IX Item "State Changes"
Ops such as chdir obviously effect the process as a whole and not just
the code in the compartment. Ops such as rand and srand have a similar
but more subtle effect.
.SH "AUTHOR"
.IX Header "AUTHOR"
Originally designed and implemented by Malcolm Beattie.
.PP
Reworked to use the Opcode module and other changes added by Tim Bunce.
.PP
Currently maintained by the Perl 5 Porters, <perl5\-porters@perl.org>.
