Move Safe.pm into ext/Safe, and temporarily give it a Makfile.PL
[p5sagit/p5-mst-13.2.git] / ext / Safe / Safe.pm
index 0fafcbe..812d571 100644 (file)
 package Safe;
 
-use vars qw($VERSION @ISA @EXPORT_OK);
+use 5.003_11;
+use strict;
+
+$Safe::VERSION = "2.16_01";
+
+# *** Don't declare any lexicals above this point ***
+#
+# This function should return a closure which contains an eval that can't
+# see any lexicals in scope (apart from __ExPr__ which is unavoidable)
+
+sub lexless_anon_sub {
+                # $_[0] is package;
+                # $_[1] is strict flag;
+    my $__ExPr__ = $_[2];   # must be a lexical to create the closure that
+                           # can be used to pass the value into the safe
+                           # world
+
+    # Create anon sub ref in root of compartment.
+    # Uses a closure (on $__ExPr__) to pass in the code to be executed.
+    # (eval on one line to keep line numbers as expected by caller)
+    eval sprintf
+    'package %s; %s strict; sub { @_=(); eval q[my $__ExPr__;] . $__ExPr__; }',
+               $_[0], $_[1] ? 'use' : 'no';
+}
 
-require Exporter;
-require DynaLoader;
 use Carp;
-$VERSION = "1.00";
-@ISA = qw(Exporter DynaLoader);
-@EXPORT_OK = qw(op_mask ops_to_mask mask_to_ops opcode opname opdesc
-               MAXO emptymask fullmask);
+BEGIN { eval q{
+    use Carp::Heavy;
+} }
+
+use Opcode 1.01, qw(
+    opset opset_to_ops opmask_add
+    empty_opset full_opset invert_opset verify_opset
+    opdesc opcodes opmask define_optag opset_to_hex
+);
+
+*ops_to_opset = \&opset;   # Temporary alias for old Penguins
+
+
+my $default_root  = 0;
+# share *_ and functions defined in universal.c
+# Don't share stuff like *UNIVERSAL:: otherwise code from the
+# compartment can 0wn functions in UNIVERSAL
+my $default_share = [qw[
+    *_
+    &PerlIO::get_layers
+    &UNIVERSAL::isa
+    &UNIVERSAL::can
+    &UNIVERSAL::VERSION
+    &utf8::is_utf8
+    &utf8::valid
+    &utf8::encode
+    &utf8::decode
+    &utf8::upgrade
+    &utf8::downgrade
+    &utf8::native_to_unicode
+    &utf8::unicode_to_native
+    $version::VERSION
+    $version::CLASS
+    @version::ISA
+], ($] >= 5.008001 && qw[
+    &Regexp::DESTROY
+]), ($] >= 5.010 && qw[
+    &re::is_regexp
+    &re::regname
+    &re::regnames
+    &re::regnames_count
+    &Tie::Hash::NamedCapture::FETCH
+    &Tie::Hash::NamedCapture::STORE
+    &Tie::Hash::NamedCapture::DELETE
+    &Tie::Hash::NamedCapture::CLEAR
+    &Tie::Hash::NamedCapture::EXISTS
+    &Tie::Hash::NamedCapture::FIRSTKEY
+    &Tie::Hash::NamedCapture::NEXTKEY
+    &Tie::Hash::NamedCapture::SCALAR
+    &Tie::Hash::NamedCapture::flags
+    &UNIVERSAL::DOES
+    &version::()
+    &version::new
+    &version::(""
+    &version::stringify
+    &version::(0+
+    &version::numify
+    &version::normal
+    &version::(cmp
+    &version::(<=>
+    &version::vcmp
+    &version::(bool
+    &version::boolean
+    &version::(nomethod
+    &version::noop
+    &version::is_alpha
+    &version::qv
+]), ($] >= 5.011 && qw[
+    &re::regexp_pattern
+])];
+
+sub new {
+    my($class, $root, $mask) = @_;
+    my $obj = {};
+    bless $obj, $class;
+
+    if (defined($root)) {
+       croak "Can't use \"$root\" as root name"
+           if $root =~ /^main\b/ or $root !~ /^\w[:\w]*$/;
+       $obj->{Root}  = $root;
+       $obj->{Erase} = 0;
+    }
+    else {
+       $obj->{Root}  = "Safe::Root".$default_root++;
+       $obj->{Erase} = 1;
+    }
+
+    # use permit/deny methods instead till interface issues resolved
+    # XXX perhaps new Safe 'Root', mask => $mask, foo => bar, ...;
+    croak "Mask parameter to new no longer supported" if defined $mask;
+    $obj->permit_only(':default');
+
+    # We must share $_ and @_ with the compartment or else ops such
+    # as split, length and so on won't default to $_ properly, nor
+    # will passing argument to subroutines work (via @_). In fact,
+    # for reasons I don't completely understand, we need to share
+    # the whole glob *_ rather than $_ and @_ separately, otherwise
+    # @_ in non default packages within the compartment don't work.
+    $obj->share_from('main', $default_share);
+    Opcode::_safe_pkg_prep($obj->{Root}) if($Opcode::VERSION > 1.04);
+    return $obj;
+}
+
+sub DESTROY {
+    my $obj = shift;
+    $obj->erase('DESTROY') if $obj->{Erase};
+}
+
+sub erase {
+    my ($obj, $action) = @_;
+    my $pkg = $obj->root();
+    my ($stem, $leaf);
+
+    no strict 'refs';
+    $pkg = "main::$pkg\::";    # expand to full symbol table name
+    ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
+
+    # The 'my $foo' is needed! Without it you get an
+    # 'Attempt to free unreferenced scalar' warning!
+    my $stem_symtab = *{$stem}{HASH};
+
+    #warn "erase($pkg) stem=$stem, leaf=$leaf";
+    #warn " stem_symtab hash ".scalar(%$stem_symtab)."\n";
+       # ", join(', ', %$stem_symtab),"\n";
+
+#    delete $stem_symtab->{$leaf};
+
+    my $leaf_glob   = $stem_symtab->{$leaf};
+    my $leaf_symtab = *{$leaf_glob}{HASH};
+#    warn " leaf_symtab ", join(', ', %$leaf_symtab),"\n";
+    %$leaf_symtab = ();
+    #delete $leaf_symtab->{'__ANON__'};
+    #delete $leaf_symtab->{'foo'};
+    #delete $leaf_symtab->{'main::'};
+#    my $foo = undef ${"$stem\::"}{"$leaf\::"};
+
+    if ($action and $action eq 'DESTROY') {
+        delete $stem_symtab->{$leaf};
+    } else {
+        $obj->share_from('main', $default_share);
+    }
+    1;
+}
+
+
+sub reinit {
+    my $obj= shift;
+    $obj->erase;
+    $obj->share_redo;
+}
+
+sub root {
+    my $obj = shift;
+    croak("Safe root method now read-only") if @_;
+    return $obj->{Root};
+}
+
+
+sub mask {
+    my $obj = shift;
+    return $obj->{Mask} unless @_;
+    $obj->deny_only(@_);
+}
+
+# v1 compatibility methods
+sub trap   { shift->deny(@_)   }
+sub untrap { shift->permit(@_) }
+
+sub deny {
+    my $obj = shift;
+    $obj->{Mask} |= opset(@_);
+}
+sub deny_only {
+    my $obj = shift;
+    $obj->{Mask} = opset(@_);
+}
+
+sub permit {
+    my $obj = shift;
+    # XXX needs testing
+    $obj->{Mask} &= invert_opset opset(@_);
+}
+sub permit_only {
+    my $obj = shift;
+    $obj->{Mask} = invert_opset opset(@_);
+}
+
+
+sub dump_mask {
+    my $obj = shift;
+    print opset_to_hex($obj->{Mask}),"\n";
+}
+
+
+
+sub share {
+    my($obj, @vars) = @_;
+    $obj->share_from(scalar(caller), \@vars);
+}
+
+sub share_from {
+    my $obj = shift;
+    my $pkg = shift;
+    my $vars = shift;
+    my $no_record = shift || 0;
+    my $root = $obj->root();
+    croak("vars not an array ref") unless ref $vars eq 'ARRAY';
+    no strict 'refs';
+    # Check that 'from' package actually exists
+    croak("Package \"$pkg\" does not exist")
+       unless keys %{"$pkg\::"};
+    my $arg;
+    foreach $arg (@$vars) {
+       # catch some $safe->share($var) errors:
+       my ($var, $type);
+       $type = $1 if ($var = $arg) =~ s/^(\W)//;
+       # warn "share_from $pkg $type $var";
+       *{$root."::$var"} = (!$type)       ? \&{$pkg."::$var"}
+                         : ($type eq '&') ? \&{$pkg."::$var"}
+                         : ($type eq '$') ? \${$pkg."::$var"}
+                         : ($type eq '@') ? \@{$pkg."::$var"}
+                         : ($type eq '%') ? \%{$pkg."::$var"}
+                         : ($type eq '*') ?  *{$pkg."::$var"}
+                         : croak(qq(Can't share "$type$var" of unknown type));
+    }
+    $obj->share_record($pkg, $vars) unless $no_record or !$vars;
+}
+
+sub share_record {
+    my $obj = shift;
+    my $pkg = shift;
+    my $vars = shift;
+    my $shares = \%{$obj->{Shares} ||= {}};
+    # Record shares using keys of $obj->{Shares}. See reinit.
+    @{$shares}{@$vars} = ($pkg) x @$vars if @$vars;
+}
+sub share_redo {
+    my $obj = shift;
+    my $shares = \%{$obj->{Shares} ||= {}};
+    my($var, $pkg);
+    while(($var, $pkg) = each %$shares) {
+       # warn "share_redo $pkg\:: $var";
+       $obj->share_from($pkg,  [ $var ], 1);
+    }
+}
+sub share_forget {
+    delete shift->{Shares};
+}
+
+sub varglob {
+    my ($obj, $var) = @_;
+    no strict 'refs';
+    return *{$obj->root()."::$var"};
+}
+
+
+sub reval {
+    my ($obj, $expr, $strict) = @_;
+    my $root = $obj->{Root};
+
+    my $evalsub = lexless_anon_sub($root,$strict, $expr);
+    return Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
+}
+
+sub rdo {
+    my ($obj, $file) = @_;
+    my $root = $obj->{Root};
+
+    my $evalsub = eval
+           sprintf('package %s; sub { @_ = (); do $file }', $root);
+    return Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
+}
+
+
+1;
+
+__END__
 
 =head1 NAME
 
-Safe - Safe extension module for Perl
+Safe - Compile and execute code in restricted compartments
+
+=head1 SYNOPSIS
+
+  use Safe;
+
+  $compartment = new Safe;
+
+  $compartment->permit(qw(time sort :browse));
+
+  $result = $compartment->reval($unsafe_code);
 
 =head1 DESCRIPTION
 
@@ -26,16 +330,18 @@ in which perl code can be evaluated. Each compartment has
 The "root" of the namespace (i.e. "main::") 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. Code which is compiled outside
-the compartment can choose to place variables into (or share
-variables with) the compartment's namespace and only that
-data will be visible to code evaluated in the compartment.
+glob lookups and other tricks.
+
+Code which is compiled outside the compartment can choose to place
+variables into (or I<share> variables with) the compartment's namespace
+and only that data will be visible to code evaluated in the
+compartment.
 
 By default, the only variables shared with compartments are the
-"underscore" variables $_ and @_ (and, technically, the much less
-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 subroutine entry.
+"underscore" variables $_ and @_ (and, technically, the less 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 subroutine entry.
 
 =item an operator mask
 
@@ -44,19 +350,17 @@ perl code is compiled into an internal format before execution.
 Evaluating perl code (e.g. via "eval" or "do 'file'") causes
 the code to be compiled into an internal format and then,
 provided there was no error in the compilation, executed.
-Code evaulated in a compartment compiles subject to the
-compartment's operator mask. Attempting to evaulate code in a
+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.
 
-By default, the operator mask for a newly created compartment masks
-out all operations which give "access to the system" in some sense.
-This includes masking off operators such as I<system>, I<open>,
-I<chown>, and I<shmget> but does not mask off operators such as
-I<print>, I<sysread> and I<E<lt>HANDLE<gt>>. Those file operators
-are allowed since for the code in the compartment to have access
-to a filehandle, the code outside the compartment must have explicitly
-placed the filehandle variable inside the compartment.
+The default operator mask for a newly created compartment is
+the ':default' optag.
+
+It is important that you read the L<Opcode> module documentation
+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 to potentially unsafe operations can
@@ -71,92 +375,110 @@ outside the compartment) placed into the compartment. For example,
 
 =back
 
-=head2 Operator masks
-
-An operator mask exists at user-level as a string of bytes of length
-MAXO, each of which is either 0x00 or 0x01. Here, MAXO is the number
-of operators in the current version of perl. The subroutine MAXO()
-(available for export by package Safe) returns the number of operators
-in the current version of perl. Note that, unlike the beta versions of
-the Safe extension, this is a reliable count of the number of
-operators in the currently running perl executable. The presence of a
-0x01 byte at offset B<n> of the string indicates that operator number
-B<n> should be masked (i.e. disallowed).  The Safe extension makes
-available routines for converting from operator names to operator
-numbers (and I<vice versa>) and for converting from a list of operator
-names to the corresponding mask (and I<vice versa>).
 
-=head2 Methods in class Safe
+=head1 WARNING
 
-To create a new compartment, use
+The authors make B<no warranty>, implied or otherwise, about the
+suitability of this software for safety or security purposes.
 
-    $cpt = new Safe;
+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.
 
-Optional arguments are (NAMESPACE, MASK), where
+Your mileage will vary. If in any doubt B<do not use it>.
 
-=over 8
 
-=item NAMESPACE
+=head2 RECENT CHANGES
 
-is the root namespace to use for the compartment (defaults to
-"Safe::Root000000000", auto-incremented for each new compartment); and
+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.
 
-=item MASK
 
-is the operator mask to use (defaults to a fairly restrictive set).
+=head2 Methods in class Safe
 
-=back
+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.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 each case.
 
+
 =over 8
 
-=item root (NAMESPACE)
+=item permit (OP, ...)
 
-This is a get-or-set method for the compartment's namespace. With the
-NAMESPACE argument present, it sets the root namespace for the
-compartment. With no NAMESPACE argument present, it returns the
-current root namespace of the compartment.
+Permit the listed operators to be used when compiling code in the
+compartment (in I<addition> to any operators already permitted).
 
-=item mask (MASK)
+You can list opcodes by names, or use a tag name; see
+L<Opcode/"Predefined Opcode Tags">.
 
-This is a get-or-set method for the compartment's operator mask.
-With the MASK argument present, it sets the operator mask for the
-compartment. With no MASK argument present, it returns the
-current operator mask of the compartment.
+=item permit_only (OP, ...)
 
-=item trap (OP, ...)
+Permit I<only> the listed operators to be used when compiling code in
+the compartment (I<no> other operators are permitted).
+
+=item deny (OP, ...)
+
+Deny the listed operators from being used when compiling code in the
+compartment (other operators may still be permitted).
+
+=item deny_only (OP, ...)
 
-This sets bits in the compartment's operator mask corresponding
-to each operator named in the list of arguments. Each OP can be
-either the name of an operation or its number. See opcode.h or
-opcode.pl in the main perl distribution for a canonical list of
-operator names.
+Deny I<only> the listed operators from being used when compiling code
+in the compartment (I<all> other operators will be permitted).
+
+=item trap (OP, ...)
 
 =item untrap (OP, ...)
 
-This resets bits in the compartment's operator mask corresponding
-to each operator named in the list of arguments. Each OP can be
-either the name of an operation or its number. See opcode.h or
-opcode.pl in the main perl distribution for a canonical list of
-operator names.
+The trap and untrap methods are synonyms for deny and permit
+respectfully.
 
-=item share (VARNAME, ...)
+=item share (NAME, ...)
 
 This shares the variable(s) in the argument list with the compartment.
-Each VARNAME must be the B<name> of a variable with a leading type
-identifier included. Examples of legal variable names are '$foo' for
-a scalar, '@foo' for an array, '%foo' for a hash, '&foo' for a
-subroutine and '*foo' for a glob (i.e. all symbol table entries
-associated with "foo", including scalar, array, hash, sub and filehandle).
+This is almost identical to exporting variables using the L<Exporter>
+module.
+
+Each NAME must be the B<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 (i.e.  all symbol table entries associated with "foo",
+including scalar, array, hash, sub and filehandle).
+
+Each NAME is assumed to be in the calling package. See share_from
+for an alternative method (which share uses).
+
+=item 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 reference.
+
+    $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
+
 
 =item varglob (VARNAME)
 
-This returns a glob for the symbol table entry of VARNAME in the package
-of the compartment. VARNAME must be the B<name> of a variable without
-any leading type marker. For example,
+This returns a glob reference for the symbol table entry of VARNAME in
+the package of the compartment. VARNAME must be the B<name> of a
+variable without any leading type marker. For example,
 
     $cpt = new Safe 'Root';
     $Root::foo = "Hello world";
@@ -164,507 +486,149 @@ any leading type marker. For example,
     ${$cpt->varglob('foo')} = "Hello world";
 
 
-=item reval (STRING)
-
-This evaluates STRING as perl code inside the compartment. The code
-can only see the compartment's namespace (as returned by the B<root>
-method). Any attempt by code in STRING to use an operator which is
-in the compartment's mask will cause an error (at run-time of the
-main program but at compile-time for the code in STRING). The error
-is of the form "%s trapped by operation mask operation...". If an
-operation is trapped in this way, then the code in STRING 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 eval(). 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 B<eval()>. Note that 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.
+=item reval (STRING, STRICT)
 
-=item rdo (FILENAME)
+This evaluates STRING as perl code inside the compartment.
 
-This evaluates the contents of file FILENAME inside the compartment.
-See above documentation on the B<reval> method for further details.
+The code can only see the compartment's namespace (as returned by the
+B<root> method). The compartment's root package appears to be the
+C<main::> package to the code inside the compartment.
 
-=back
+Any attempt by the code in STRING 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 STRING).  The error is of the form
+"'%s' trapped by operation mask...".
 
-=head2 Subroutines in package Safe
+If an operation is trapped in this way, then the code in STRING 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 eval().
 
-The Safe package contains subroutines for manipulating operator
-names and operator masks. All are available for export by the package.
-The canonical list of operator names is the contents of the array
-op_name defined and initialised in file F<opcode.h> of the Perl
-source distribution.
+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 B<eval()>. The context (list or scalar) is determined
+by the caller as usual.
 
-=over 8
+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.
 
-=item ops_to_mask (OP, ...)
+The formerly undocumented STRICT argument sets strictness: if true
+'use strict;' is used, otherwise it uses 'no strict;'. B<Note>: if
+STRICT is omitted 'no strict;' is the default.
 
-This takes a list of operator names and returns an operator mask
-with precisely those operators masked.
+Some points to note:
 
-=item mask_to_ops (MASK)
+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 effect. However this technique would allow clever,
+and possibly harmful, code to 'probe' the boundaries of what is
+possible.
 
-This takes an operator mask and returns a list of operator names
-corresponding to those operators which are masked in MASK.
+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.
 
-=item opcode (OP, ...)
+Consider a function foo() in 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 to 1.  If foo() is called from the
+compartment (by whatever means) then instead of setting $pkg::foo, the
+eval will actually set $Root::pkg::foo.
 
-This takes a list of operator names and returns the corresponding
-list of opcodes (which can then be used as byte offsets into a mask).
+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
+the compartment, or by any code anywhere that is called by any means
+from the compartment, then the eval in the Socket module's AUTOLOAD
+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.
 
-=item opname (OP, ...)
+A similar effect applies to I<all> runtime symbol lookups in code
+called from a compartment but not compiled within it.
 
-This takes a list of opcodes and returns the corresponding list of
-operator names.
 
-=item fullmask
 
-This just returns a mask which has all operators masked.
-It returns the string "\1" x MAXO().
+=item rdo (FILENAME)
+
+This evaluates the contents of file FILENAME inside the compartment.
+See above documentation on the B<reval> method for further details.
+
+=item root (NAMESPACE)
 
-=item emptymask
+This method returns the name of the package that is the root of the
+compartment's namespace.
 
-This just returns a mask which has all operators unmasked.
-It returns the string "\0" x MAXO(). This is useful if you
-want a compartment to make use of the namespace protection
-features but do not want the default restrictive mask.
+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.
 
-=item MAXO
+=item mask (MASK)
 
-This returns the number of operators (and hence the length of an
-operator mask). Note that, unlike the beta distributions of the
-Safe extension, this is derived from a genuine integer variable
-in the perl executable and not from a preprocessor constant.
-This means that the Safe extension is more robust in the presence
-of mismatched versions of the perl executable and the Safe extension.
+This is a get-or-set method for the compartment's operator mask.
 
-=item op_mask
+With no MASK argument present, it returns the current operator mask of
+the compartment.
 
-This returns the operator mask which is actually in effect at the
-time the invocation to the subroutine is compiled. In general,
-this is probably not terribly useful.
+With the MASK argument present, it sets the operator mask for the
+compartment (equivalent to calling the deny_only method).
 
 =back
 
-=head2 AUTHOR
 
-Malcolm Beattie, mbeattie@sable.ox.ac.uk.
+=head2 Some Safety Issues
 
-=cut
+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.
 
-my $default_root = 'Root000000000';
+=over 8
 
-my $default_mask;
+=item Memory
 
-sub new {
-    my($class, $root, $mask) = @_;
-    my $obj = {};
-    bless $obj, $class;
-    $obj->root(defined($root) ? $root : ("Safe::".$default_root++));
-    $obj->mask(defined($mask) ? $mask : $default_mask);
-    # We must share $_ and @_ with the compartment or else ops such
-    # as split, length and so on won't default to $_ properly, nor
-    # will passing argument to subroutines work (via @_). In fact,
-    # for reasons I don't completely understand, we need to share
-    # the whole glob *_ rather than $_ and @_ separately, otherwise
-    # @_ in non default packages within the compartment don't work.
-    *{$obj->root . "::_"} = *_;
-    return $obj;
-}
+Consuming all (or nearly all) available memory.
 
-sub DESTROY {
-    my($obj) = @_;
-    my $root = $obj->root();
-    if ($root =~ /^Safe::(Root\d+)$/){
-       $root = $1;
-       delete $ {"Safe::"}{"$root\::"};
-    }
-}
+=item CPU
 
-sub root {
-    my $obj = shift;
-    if (@_) {
-       $obj->{Root} = $_[0];
-    } else {
-       return $obj->{Root};
-    }
-}
+Causing infinite loops etc.
 
-sub mask {
-    my $obj = shift;
-    if (@_) {
-       $obj->{Mask} = verify_mask($_[0]);
-    } else {
-       return $obj->{Mask};
-    }
-}
+=item Snooping
 
-sub verify_mask {
-    my($mask) = @_;
-    if (length($mask) != MAXO() || $mask !~ /^[\0\1]+$/) {
-       croak("argument is not a mask");
-    }
-    return $mask;
-}
+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.
 
-sub trap {
-    my $obj = shift;
-    $obj->setmaskel("\1", @_);
-}
+=item Signals
 
-sub untrap {
-    my $obj = shift;
-    $obj->setmaskel("\0", @_);
-}
+Causing signals (especially SIGFPE and SIGALARM) to affect your process.
 
-sub emptymask { "\0" x MAXO() }
-sub fullmask { "\1" x MAXO() }
+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?
 
-sub setmaskel {
-    my $obj = shift;
-    my $val = shift;
-    croak("bad value for mask element") unless $val eq "\0" || $val eq "\1";
-    my $maskref = \$obj->{Mask};
-    my ($op, $opcode);
-    foreach $op (@_) {
-       $opcode = ($op =~ /^\d/) ? $op : opcode($op);
-       substr($$maskref, $opcode, 1) = $val;
-    }
-}
+=item State Changes
 
-sub share {
-    my $obj = shift;
-    my $root = $obj->root();
-    my ($arg);
-    foreach $arg (@_) {
-       my $var;
-       ($var = $arg) =~ s/^(.)//;
-       my $caller = caller;
-       *{$root."::$var"} = ($1 eq '$') ? \${$caller."::$var"}
-                         : ($1 eq '@') ? \@{$caller."::$var"}
-                         : ($1 eq '%') ? \%{$caller."::$var"}
-                         : ($1 eq '*') ? *{$caller."::$var"}
-                         : ($1 eq '&') ? \&{$caller."::$var"}
-                         : croak(qq(No such variable type for "$1$var"));
-    }
-}
+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.
 
-sub varglob {
-    my ($obj, $var) = @_;
-    return *{$obj->root()."::$var"};
-}
+=back
 
-sub reval {
-    my ($obj, $expr) = @_;
-    my $root = $obj->{Root};
-    my $mask = $obj->{Mask};
-    verify_mask($mask);
-
-    my $evalsub = eval sprintf(<<'EOT', $root);
-       package %s;
-       sub {
-           eval $expr;
-       }
-EOT
-    return safe_call_sv($root, $mask, $evalsub);
-}
+=head2 AUTHOR
 
-sub rdo {
-    my ($obj, $file) = @_;
-    my $root = $obj->{Root};
-    my $mask = $obj->{Mask};
-    verify_mask($mask);
-
-    $file =~ s/"/\\"/g; # just in case the filename contains any double quotes
-    my $evalsub = eval sprintf(<<'EOT', $root, $file);
-       package %s;
-       sub {
-           do "%s";
-       }
-EOT
-    return safe_call_sv($root, $mask, $evalsub);
-}
+Originally designed and implemented by Malcolm Beattie.
 
-bootstrap Safe $VERSION;
+Reworked to use the Opcode module and other changes added by Tim Bunce.
 
-$default_mask = fullmask;
-my $name;
-while (defined ($name = <DATA>)) {
-    chomp $name;
-    next if $name =~ /^#/;
-    my $code = opcode($name);
-    substr($default_mask, $code, 1) = "\0";
-}
+Currently maintained by the Perl 5 Porters, <perl5-porters@perl.org>.
 
-1;
+=cut
 
-__DATA__
-null
-stub
-scalar
-pushmark
-wantarray
-const
-gvsv
-gv
-gelem
-padsv
-padav
-padhv
-padany
-pushre
-rv2gv
-rv2sv
-av2arylen
-rv2cv
-anoncode
-prototype
-refgen
-srefgen
-ref
-bless
-glob
-readline
-rcatline
-regcmaybe
-regcomp
-match
-subst
-substcont
-trans
-sassign
-aassign
-chop
-schop
-chomp
-schomp
-defined
-undef
-study
-pos
-preinc
-i_preinc
-predec
-i_predec
-postinc
-i_postinc
-postdec
-i_postdec
-pow
-multiply
-i_multiply
-divide
-i_divide
-modulo
-i_modulo
-repeat
-add
-i_add
-subtract
-i_subtract
-concat
-stringify
-left_shift
-right_shift
-lt
-i_lt
-gt
-i_gt
-le
-i_le
-ge
-i_ge
-eq
-i_eq
-ne
-i_ne
-ncmp
-i_ncmp
-slt
-sgt
-sle
-sge
-seq
-sne
-scmp
-bit_and
-bit_xor
-bit_or
-negate
-i_negate
-not
-complement
-atan2
-sin
-cos
-rand
-srand
-exp
-log
-sqrt
-int
-hex
-oct
-abs
-length
-substr
-vec
-index
-rindex
-sprintf
-formline
-ord
-chr
-crypt
-ucfirst
-lcfirst
-uc
-lc
-quotemeta
-rv2av
-aelemfast
-aelem
-aslice
-each
-values
-keys
-delete
-exists
-rv2hv
-helem
-hslice
-split
-join
-list
-lslice
-anonlist
-anonhash
-splice
-push
-pop
-shift
-unshift
-reverse
-grepstart
-grepwhile
-mapstart
-mapwhile
-range
-flip
-flop
-and
-or
-xor
-cond_expr
-andassign
-orassign
-method
-entersub
-leavesub
-caller
-warn
-die
-reset
-lineseq
-nextstate
-dbstate
-unstack
-enter
-leave
-scope
-enteriter
-iter
-enterloop
-leaveloop
-return
-last
-next
-redo
-goto
-close
-fileno
-tie
-untie
-dbmopen
-dbmclose
-sselect
-select
-getc
-read
-enterwrite
-leavewrite
-prtf
-print
-sysread
-syswrite
-send
-recv
-eof
-tell
-seek
-truncate
-fcntl
-ioctl
-sockpair
-bind
-connect
-listen
-accept
-shutdown
-gsockopt
-ssockopt
-getsockname
-ftrwrite
-ftsvtx
-open_dir
-readdir
-telldir
-seekdir
-rewinddir
-kill
-getppid
-getpgrp
-setpgrp
-getpriority
-setpriority
-time
-tms
-localtime
-alarm
-dofile
-entereval
-leaveeval
-entertry
-leavetry
-ghbyname
-ghbyaddr
-ghostent
-gnbyname
-gnbyaddr
-gnetent
-gpbyname
-gpbynumber
-gprotoent
-gsbyname
-gsbyport
-gservent
-shostent
-snetent
-sprotoent
-sservent
-ehostent
-enetent
-eprotoent
-eservent
-gpwnam
-gpwuid
-gpwent
-spwent
-epwent
-ggrnam
-ggrgid
-ggrent
-sgrent
-egrent