Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/Safe

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.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.  | will give a
.\" real vertical bar.  \*(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-|\(bv\*(Tr
.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\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" 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 3"
.TH Safe 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
Safe \- Compile and execute code in restricted compartments
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&  use Safe;
.Ve
.PP
.Vb 1
\&  $compartment = new Safe;
.Ve
.PP
.Vb 1
\&  $compartment->permit(qw(time sort :browse));
.Ve
.PP
.Vb 1
\&  $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 \fIOpcode\fR\|(3) 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('&wrapper');
.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 "\s-1RECENT\s0 \s-1CHANGES\s0"
.IX Subsection "RECENT CHANGES"
The interface to the Safe module has changed quite dramatically since
version 1 (as supplied with Perl5.002). Study these pages carefully if
you have code written to use Safe version 1 because you will need to
makes changes.
.Sh "Methods in class Safe"
.IX Subsection "Methods in class Safe"
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.
.IP "permit (\s-1OP\s0, ...)" 8
.IX Item "permit (OP, ...)"
Permit the listed operators to be used when compiling code in the
compartment (in \fIaddition\fR to any operators already permitted).
.IP "permit_only (\s-1OP\s0, ...)" 8
.IX Item "permit_only (OP, ...)"
Permit \fIonly\fR the listed operators to be used when compiling code in
the compartment (\fIno\fR other operators are permitted).
.IP "deny (\s-1OP\s0, ...)" 8
.IX Item "deny (OP, ...)"
Deny the listed operators from being used when compiling code in the
compartment (other operators may still be permitted).
.IP "deny_only (\s-1OP\s0, ...)" 8
.IX Item "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).
.IP "trap (\s-1OP\s0, ...)" 8
.IX Item "trap (OP, ...)"
.PD 0
.IP "untrap (\s-1OP\s0, ...)" 8
.IX Item "untrap (OP, ...)"
.PD
The trap and untrap methods are synonyms for deny and permit
respectfully.
.IP "share (\s-1NAME\s0, ...)" 8
.IX Item "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.
.Sp
Each \s-1NAME\s0 must be the \fBname\fR of a variable, typically with the leading
type identifier included. A bareword is treated as a function name.
.Sp
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).
.Sp
Each \s-1NAME\s0 is assumed to be in the calling package. See share_from
for an alternative method (which share uses).
.IP "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)" 8
.IX Item "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.
.Sp
.Vb 1
\&    $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
.Ve
.IP "varglob (\s-1VARNAME\s0)" 8
.IX Item "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,
.Sp
.Vb 4
\&    $cpt = new Safe 'Root';
\&    $Root::foo = "Hello world";
\&    # Equivalent version which doesn't need to know $cpt's package name:
\&    ${$cpt->varglob('foo')} = "Hello world";
.Ve
.IP "reval (\s-1STRING\s0)" 8
.IX Item "reval (STRING)"
This evaluates \s-1STRING\s0 as perl code inside the compartment.
.Sp
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.
.Sp
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 operation...\*(R".
.Sp
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.
.Sp
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.
.Sp
This behaviour differs from the beta distribution of the Safe extension
where earlier versions of perl made it hard to mimic the return
behaviour of the \fIeval()\fR command and the context was always scalar.
.Sp
Some points to note:
.Sp
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.
.Sp
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.
.Sp
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.
.Sp
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.
.Sp
A similar effect applies to \fIall\fR runtime symbol lookups in code
called from a compartment but not compiled within it.
.IP "rdo (\s-1FILENAME\s0)" 8
.IX Item "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.
.IP "root (\s-1NAMESPACE\s0)" 8
.IX Item "root (NAMESPACE)"
This method returns the name of the package that is the root of the
compartment's namespace.
.Sp
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.
.IP "mask (\s-1MASK\s0)" 8
.IX Item "mask (MASK)"
This is a get-or-set method for the compartment's operator mask.
.Sp
With no \s-1MASK\s0 argument present, it returns the current operator mask of
the compartment.
.Sp
With the \s-1MASK\s0 argument present, it sets the operator mask for the
compartment (equivalent to calling the deny_only method).
.Sh "Some Safety Issues"
.IX Subsection "Some Safety Issues"
This section is currently 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 "\s-1AUTHOR\s0"
.IX Subsection "AUTHOR"
Originally designed and implemented by Malcolm Beattie,
mbeattie@sable.ox.ac.uk.
.PP
Reworked to use the Opcode module and other changes added by Tim Bunce
<\fITim.Bunce@ig.co.uk\fR>.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.