Seth Woolley's Man Viewer

Safe(3) - Safe - Compile and execute code in restricted compartments - man 3 Safe

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

Safe(3)                Perl Programmers Reference Guide                Safe(3)



NAME
       Safe - Compile and execute code in(1,8) restricted compartments

SYNOPSIS
         use Safe;

         $compartment = new Safe;

         $compartment->permit(qw(time(1,2,n) sort(1,3) :browse));

         $result = $compartment->reval($unsafe_code);

DESCRIPTION
       The Safe extension module allows the creation of compartments in(1,8) which
       perl code can be evaluated. Each compartment has

       a new namespace
               The "root" of the namespace (i.e. "main::") is changed to a
               different package and code evaluated in(1,8) the compartment cannot
               refer to variables outside this namespace, even with run-time
               glob(1,3,7,n) lookups and other tricks.

               Code which is compiled outside the compartment can choose to
               place variables into (or share variables with) the compart-
               ment's namespace and only that data will be visible to code
               evaluated in(1,8) the compartment.

               By default, the only variables shared with compartments are the
               "underscore" variables $_ and @_ (and, technically, the less(1,3)
               frequently used %_, the _ filehandle and so on). This is
               because otherwise perl operators which default to $_ will not
               work and neither will the assignment of arguments to @_ on sub-
               routine entry.

       an operator mask
               Each compartment has an associated "operator mask". Recall that
               perl code is compiled into an internal format before execution.
               Evaluating perl code (e.g. via "eval" or "do 'file(1,n)'") causes
               the code to be compiled into an internal format and then, pro-
               vided there was no error(8,n) in(1,8) the compilation, executed.  Code
               evaluated in(1,8) a compartment compiles subject to the compart-
               ment's operator mask. Attempting to evaluate code in(1,8) a compart-
               ment which contains a masked operator will cause the compila-
               tion to fail with an error. The code will not be executed.

               The default operator mask for a newly created compartment is
               the ':default' optag.

               It is important that you read(2,n,1 builtins) the Opcode(3) module documenta-
               tion for more information, especially for detailed definitions
               of opnames, optags and opsets.

               Since it is only at the compilation stage that the operator
               mask applies, controlled access(2,5) to potentially unsafe opera-
               tions can be achieved by having a handle to a wrapper subrou-
               tine (written outside the compartment) placed into the compart-
               ment. For example,

                   $cpt = new Safe;
                   sub wrapper {
                       # vet arguments and perform potentially unsafe operations
                   }
                   $cpt->share('&wrapper');

WARNING
       The authors make no warranty, implied or otherwise, about the suitabil-
       ity of this software for safety or security purposes.

       The authors shall not in(1,8) any case be liable for special, incidental,
       consequential, indirect or other similar damages arising from the use
       of this software.

       Your mileage will vary. If in(1,8) any doubt do not use it.

       RECENT CHANGES

       The interface to the Safe module has changed quite dramatically since
       version(1,3,5) 1 (as supplied with Perl5.002). Study these pages carefully if(3,n)
       you have code written to use Safe version(1,3,5) 1 because you will need to
       makes changes.

       Methods in(1,8) class Safe

       To create a new compartment, use

           $cpt = new Safe;

       Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
       to use for the compartment (defaults to "Safe::Root0", incremented for
       each new compartment).

       Note that version(1,3,5) 1.00 of the Safe module supported a second optional
       parameter, MASK.  That functionality has been withdrawn pending deeper
       consideration. Use the permit and deny methods described below.

       The following methods can then be used on the compartment object
       returned by the above constructor. The object argument is implicit in(1,8)
       each case.

       permit (OP, ...)
               Permit the listed operators to be used when compiling code in(1,8)
               the compartment (in(1,8) addition to any operators already permit-
               ted).

       permit_only (OP, ...)
               Permit only the listed operators to be used when compiling code
               in(1,8) the compartment (no other operators are permitted).

       deny (OP, ...)
               Deny the listed operators from being used when compiling code
               in(1,8) the compartment (other operators may still be permitted).

       deny_only (OP, ...)
               Deny only the listed operators from being used when compiling
               code in(1,8) the compartment (all other operators will be permit-
               ted).

       trap (OP, ...)
       untrap (OP, ...)
               The trap and untrap methods are synonyms for deny and permit
               respectfully.

       share (NAME, ...)
               This shares the variable(s) in(1,8) the argument list with the com-
               partment.  This is almost identical to exporting variables
               using the Exporter module.

               Each NAME must be the name of a non-lexical variable, typically
               with the leading type identifier included. A bareword is
               treated as a function name.

               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(1,3,7,n) (i.e.  all symbol table entries associated
               with "foo", including scalar, array, hash, sub and filehandle).

               Each NAME is assumed to be in(1,8) the calling package. See
               share_from for an alternative method (which share uses).

       share_from (PACKAGE, ARRAYREF)
               This method is similar to share() 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 ref-
               erence.

                   $safe->share_from('main', [ '$foo', '%bar', 'func' ]);

       varglob (VARNAME)
               This returns a glob(1,3,7,n) reference for the symbol table entry of
               VARNAME in(1,8) the package of the compartment. VARNAME must be the
               name of a variable without any leading type marker. For exam-
               ple,

                   $cpt = new Safe 'Root';
                   $Root::foo = "Hello world";
                   # Equivalent version(1,3,5) which doesn't need to know $cpt's package name:
                   ${$cpt->varglob('foo')} = "Hello world";

       reval (STRING)
               This evaluates STRING as perl code inside the compartment.

               The code can only see the compartment's namespace (as returned
               by the root method). The compartment's root package appears to
               be the "main::" package to the code inside the compartment.

               Any attempt by the code in(1,8) STRING to use an operator which is
               not permitted by the compartment will cause an error(8,n) (at run-
               time(1,2,n) of the main program but at compile-time for the code in(1,8)
               STRING).  The error(8,n) is of the form "'%s' trapped by operation
               mask...".

               If an operation is trapped in(1,8) this way, then the code in(1,8) STRING
               will not be executed. If such a trapped operation occurs or any
               other compile-time or return error(8,n), then $@ is set(7,n,1 builtins) to the error(8,n)
               message, just as with an eval().

               If there is no error(8,n), then the method returns the value of the
               last expression evaluated, or a return statement may be used,
               just as with subroutines and eval(). The context (list or
               scalar) is determined by the caller as usual.

               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 eval() command and the context was
               always scalar.

               Some points to note:

               If the entereval op is permitted then the code can use eval
               "..." 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(1,8) effect. However this
               technique would allow clever, and possibly harmful, code to
               'probe' the boundaries of what is possible.

               Any string(3,n) eval which is executed by code executing in(1,8) a com-
               partment, or by code called from code executing in(1,8) a compart-
               ment, will be eval'd in(1,8) the namespace of the compartment. This
               is potentially a serious problem.

               Consider a function foo() in(1,8) package pkg compiled outside a
               compartment but shared with it. Assume the compartment has a
               root package called 'Root'. If foo() contains an eval statement
               like eval '$foo = 1' then, normally, $pkg::foo will be set(7,n,1 builtins) to
               1.  If foo() is called from the compartment (by whatever means)
               then instead of setting $pkg::foo, the eval will actually set(7,n,1 builtins)
               $Root::pkg::foo.

               This can easily be demonstrated by using a module, such as the
               Socket module, which uses eval "..." as part of an AUTOLOAD
               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(1,8) the compartment, or by any
               code anywhere that is called by any means from the compartment,
               then the eval in(1,8) the Socket module's AUTOLOAD function happens
               in(1,8) the namespace of the compartment. Any variables created or
               used by the eval'd code are now under the control of the code
               in(1,8) the compartment.

               A similar effect applies to all runtime symbol lookups in(1,8) code
               called from a compartment but not compiled within it.

       rdo (FILENAME)
               This evaluates the contents of file(1,n) FILENAME inside the com-
               partment.  See above documentation on the reval method for fur-
               ther details.

       root (NAMESPACE)
               This method returns the name of the package that is the root of
               the compartment's namespace.

               Note that this behaviour differs from version(1,3,5) 1.00 of the Safe
               module where the root module could be used to change the names-
               pace. That functionality has been withdrawn pending deeper con-
               sideration.

       mask (MASK)
               This is a get-or-set method for the compartment's operator
               mask.

               With no MASK argument present, it returns the current operator
               mask of the compartment.

               With the MASK argument present, it sets the operator mask for
               the compartment (equivalent to calling the deny_only method).

       Some Safety Issues

       This section is currently just an outline of some of the things code in(1,8)
       a compartment might do (intentionally or unintentionally) which can
       have an effect outside the compartment.

       Memory  Consuming all (or nearly all) available memory.

       CPU     Causing infinite loops etc.

       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.

       Signals Causing signals (especially SIGFPE and SIGALARM) to affect your
               process.

               Setting up a signal(2,7) handler will need to be carefully consid-
               ered and controlled.  What mask is in(1,8) effect when a signal(2,7) han-
               dler gets(3,n) called?  If a user can get an imported function to
               get an exception and call the user's signal(2,7) 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?

       State Changes
               Ops such as chdir obviously effect the process as a whole and
               not just the code in(1,8) the compartment. Ops such as rand(1,3) and
               srand have a similar but more subtle effect.

       AUTHOR

       Originally designed and implemented by Malcolm Beattie, mbeat-
       tie@sable.ox.ac.uk.

       Reworked to use the Opcode module and other changes added by Tim Bunce
       <Tim.Bunce@ig.co.uk>.



perl v5.8.5                       2001-09-21                           Safe(3)

References for this manual (incoming links)