home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl / 5.10.1 / Safe.pm < prev    next >
Encoding:
Perl POD Document  |  2012-12-11  |  23.5 KB  |  783 lines

  1. package Safe;
  2.  
  3. use 5.003_11;
  4. use strict;
  5. use Scalar::Util qw(reftype);
  6. use B qw(sub_generation);
  7.  
  8. $Safe::VERSION = "2.25";
  9.  
  10. # *** Don't declare any lexicals above this point ***
  11. #
  12. # This function should return a closure which contains an eval that can't
  13. # see any lexicals in scope (apart from __ExPr__ which is unavoidable)
  14.  
  15. sub lexless_anon_sub {
  16.                  # $_[0] is package;
  17.                  # $_[1] is strict flag;
  18.     my $__ExPr__ = $_[2];   # must be a lexical to create the closure that
  19.                             # can be used to pass the value into the safe
  20.                             # world
  21.  
  22.     # Create anon sub ref in root of compartment.
  23.     # Uses a closure (on $__ExPr__) to pass in the code to be executed.
  24.     # (eval on one line to keep line numbers as expected by caller)
  25.     eval sprintf
  26.     'package %s; %s strict; sub { @_=(); eval q[my $__ExPr__;] . $__ExPr__; }',
  27.                 $_[0], $_[1] ? 'use' : 'no';
  28. }
  29.  
  30. use Carp;
  31. BEGIN { eval q{
  32.     use Carp::Heavy;
  33. } }
  34.  
  35. use Opcode 1.01, qw(
  36.     opset opset_to_ops opmask_add
  37.     empty_opset full_opset invert_opset verify_opset
  38.     opdesc opcodes opmask define_optag opset_to_hex
  39. );
  40.  
  41. *ops_to_opset = \&opset;   # Temporary alias for old Penguins
  42.  
  43. # Regular expressions and other unicode-aware code may need to call
  44. # utf8->SWASHNEW (via perl's utf8.c).  That will fail unless we share the
  45. # SWASHNEW method.
  46. # Sadly we can't just add utf8::SWASHNEW to $default_share because perl's
  47. # utf8.c code does a fetchmethod on SWASHNEW to check if utf8.pm is loaded,
  48. # and sharing makes it look like the method exists.
  49. # The simplest and most robust fix is to ensure the utf8 module is loaded when
  50. # Safe is loaded. Then we can add utf8::SWASHNEW to $default_share.
  51. require utf8;
  52. # we must ensure that utf8_heavy.pl, where SWASHNEW is defined, is loaded
  53. # but without depending on knowledge of that implementation detail.
  54. # This code (//i on a unicode string) ensures utf8 is fully loaded
  55. # and also loads the ToFold SWASH.
  56. # (Swashes are cached internally by perl in PL_utf8_* variables
  57. # independent of being inside/outside of Safe. So once loaded they can be)
  58. do { my $unicode = pack('U',0xC4).'1a'; $unicode =~ /\xE4/i; };
  59. # now we can safely include utf8::SWASHNEW in $default_share defined below.
  60.  
  61. my $default_root  = 0;
  62. # share *_ and functions defined in universal.c
  63. # Don't share stuff like *UNIVERSAL:: otherwise code from the
  64. # compartment can 0wn functions in UNIVERSAL
  65. my $default_share = [qw[
  66.     *_
  67.     &PerlIO::get_layers
  68.     &UNIVERSAL::isa
  69.     &UNIVERSAL::can
  70.     &UNIVERSAL::VERSION
  71.     &utf8::is_utf8
  72.     &utf8::valid
  73.     &utf8::encode
  74.     &utf8::decode
  75.     &utf8::upgrade
  76.     &utf8::downgrade
  77.     &utf8::native_to_unicode
  78.     &utf8::unicode_to_native
  79.     &utf8::SWASHNEW
  80.     $version::VERSION
  81.     $version::CLASS
  82.     $version::STRICT
  83.     $version::LAX
  84.     @version::ISA
  85. ], ($] < 5.010 && qw[
  86.     &utf8::SWASHGET
  87. ]), ($] >= 5.008001 && qw[
  88.     &Regexp::DESTROY
  89. ]), ($] >= 5.010 && qw[
  90.     &re::is_regexp
  91.     &re::regname
  92.     &re::regnames
  93.     &re::regnames_count
  94.     &Tie::Hash::NamedCapture::FETCH
  95.     &Tie::Hash::NamedCapture::STORE
  96.     &Tie::Hash::NamedCapture::DELETE
  97.     &Tie::Hash::NamedCapture::CLEAR
  98.     &Tie::Hash::NamedCapture::EXISTS
  99.     &Tie::Hash::NamedCapture::FIRSTKEY
  100.     &Tie::Hash::NamedCapture::NEXTKEY
  101.     &Tie::Hash::NamedCapture::SCALAR
  102.     &Tie::Hash::NamedCapture::flags
  103.     &UNIVERSAL::DOES
  104.     &version::()
  105.     &version::new
  106.     &version::(""
  107.     &version::stringify
  108.     &version::(0+
  109.     &version::numify
  110.     &version::normal
  111.     &version::(cmp
  112.     &version::(<=>
  113.     &version::vcmp
  114.     &version::(bool
  115.     &version::boolean
  116.     &version::(nomethod
  117.     &version::noop
  118.     &version::is_alpha
  119.     &version::qv
  120.     &version::vxs::declare
  121.     &version::vxs::qv
  122.     &version::vxs::_VERSION
  123.     &version::vxs::new
  124.     &version::vxs::parse
  125. ]), ($] >= 5.011 && qw[
  126.     &re::regexp_pattern
  127. ])];
  128.  
  129. sub new {
  130.     my($class, $root, $mask) = @_;
  131.     my $obj = {};
  132.     bless $obj, $class;
  133.  
  134.     if (defined($root)) {
  135.         croak "Can't use \"$root\" as root name"
  136.             if $root =~ /^main\b/ or $root !~ /^\w[:\w]*$/;
  137.         $obj->{Root}  = $root;
  138.         $obj->{Erase} = 0;
  139.     }
  140.     else {
  141.         $obj->{Root}  = "Safe::Root".$default_root++;
  142.         $obj->{Erase} = 1;
  143.     }
  144.  
  145.     # use permit/deny methods instead till interface issues resolved
  146.     # XXX perhaps new Safe 'Root', mask => $mask, foo => bar, ...;
  147.     croak "Mask parameter to new no longer supported" if defined $mask;
  148.     $obj->permit_only(':default');
  149.  
  150.     # We must share $_ and @_ with the compartment or else ops such
  151.     # as split, length and so on won't default to $_ properly, nor
  152.     # will passing argument to subroutines work (via @_). In fact,
  153.     # for reasons I don't completely understand, we need to share
  154.     # the whole glob *_ rather than $_ and @_ separately, otherwise
  155.     # @_ in non default packages within the compartment don't work.
  156.     $obj->share_from('main', $default_share);
  157.  
  158.     Opcode::_safe_pkg_prep($obj->{Root}) if($Opcode::VERSION > 1.04);
  159.  
  160.     return $obj;
  161. }
  162.  
  163. sub DESTROY {
  164.     my $obj = shift;
  165.     $obj->erase('DESTROY') if $obj->{Erase};
  166. }
  167.  
  168. sub erase {
  169.     my ($obj, $action) = @_;
  170.     my $pkg = $obj->root();
  171.     my ($stem, $leaf);
  172.  
  173.     no strict 'refs';
  174.     $pkg = "main::$pkg\::";     # expand to full symbol table name
  175.     ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
  176.  
  177.     # The 'my $foo' is needed! Without it you get an
  178.     # 'Attempt to free unreferenced scalar' warning!
  179.     my $stem_symtab = *{$stem}{HASH};
  180.  
  181.     #warn "erase($pkg) stem=$stem, leaf=$leaf";
  182.     #warn " stem_symtab hash ".scalar(%$stem_symtab)."\n";
  183.     # ", join(', ', %$stem_symtab),"\n";
  184.  
  185. #    delete $stem_symtab->{$leaf};
  186.  
  187.     my $leaf_glob   = $stem_symtab->{$leaf};
  188.     my $leaf_symtab = *{$leaf_glob}{HASH};
  189. #    warn " leaf_symtab ", join(', ', %$leaf_symtab),"\n";
  190.     %$leaf_symtab = ();
  191.     #delete $leaf_symtab->{'__ANON__'};
  192.     #delete $leaf_symtab->{'foo'};
  193.     #delete $leaf_symtab->{'main::'};
  194. #    my $foo = undef ${"$stem\::"}{"$leaf\::"};
  195.  
  196.     if ($action and $action eq 'DESTROY') {
  197.         delete $stem_symtab->{$leaf};
  198.     } else {
  199.         $obj->share_from('main', $default_share);
  200.     }
  201.     1;
  202. }
  203.  
  204.  
  205. sub reinit {
  206.     my $obj= shift;
  207.     $obj->erase;
  208.     $obj->share_redo;
  209. }
  210.  
  211. sub root {
  212.     my $obj = shift;
  213.     croak("Safe root method now read-only") if @_;
  214.     return $obj->{Root};
  215. }
  216.  
  217.  
  218. sub mask {
  219.     my $obj = shift;
  220.     return $obj->{Mask} unless @_;
  221.     $obj->deny_only(@_);
  222. }
  223.  
  224. # v1 compatibility methods
  225. sub trap   { shift->deny(@_)   }
  226. sub untrap { shift->permit(@_) }
  227.  
  228. sub deny {
  229.     my $obj = shift;
  230.     $obj->{Mask} |= opset(@_);
  231. }
  232. sub deny_only {
  233.     my $obj = shift;
  234.     $obj->{Mask} = opset(@_);
  235. }
  236.  
  237. sub permit {
  238.     my $obj = shift;
  239.     # XXX needs testing
  240.     $obj->{Mask} &= invert_opset opset(@_);
  241. }
  242. sub permit_only {
  243.     my $obj = shift;
  244.     $obj->{Mask} = invert_opset opset(@_);
  245. }
  246.  
  247.  
  248. sub dump_mask {
  249.     my $obj = shift;
  250.     print opset_to_hex($obj->{Mask}),"\n";
  251. }
  252.  
  253.  
  254. sub share {
  255.     my($obj, @vars) = @_;
  256.     $obj->share_from(scalar(caller), \@vars);
  257. }
  258.  
  259.  
  260. sub share_from {
  261.     my $obj = shift;
  262.     my $pkg = shift;
  263.     my $vars = shift;
  264.     my $no_record = shift || 0;
  265.     my $root = $obj->root();
  266.     croak("vars not an array ref") unless ref $vars eq 'ARRAY';
  267.     no strict 'refs';
  268.     # Check that 'from' package actually exists
  269.     croak("Package \"$pkg\" does not exist")
  270.         unless keys %{"$pkg\::"};
  271.     my $arg;
  272.     foreach $arg (@$vars) {
  273.         # catch some $safe->share($var) errors:
  274.         my ($var, $type);
  275.         $type = $1 if ($var = $arg) =~ s/^(\W)//;
  276.         # warn "share_from $pkg $type $var";
  277.         for (1..2) { # assign twice to avoid any 'used once' warnings
  278.             *{$root."::$var"} = (!$type)   ? \&{$pkg."::$var"}
  279.                           : ($type eq '&') ? \&{$pkg."::$var"}
  280.                           : ($type eq '$') ? \${$pkg."::$var"}
  281.                           : ($type eq '@') ? \@{$pkg."::$var"}
  282.                           : ($type eq '%') ? \%{$pkg."::$var"}
  283.                           : ($type eq '*') ?  *{$pkg."::$var"}
  284.                           : croak(qq(Can't share "$type$var" of unknown type));
  285.         }
  286.     }
  287.     $obj->share_record($pkg, $vars) unless $no_record or !$vars;
  288. }
  289.  
  290.  
  291. sub share_record {
  292.     my $obj = shift;
  293.     my $pkg = shift;
  294.     my $vars = shift;
  295.     my $shares = \%{$obj->{Shares} ||= {}};
  296.     # Record shares using keys of $obj->{Shares}. See reinit.
  297.     @{$shares}{@$vars} = ($pkg) x @$vars if @$vars;
  298. }
  299.  
  300.  
  301. sub share_redo {
  302.     my $obj = shift;
  303.     my $shares = \%{$obj->{Shares} ||= {}};
  304.     my($var, $pkg);
  305.     while(($var, $pkg) = each %$shares) {
  306.         # warn "share_redo $pkg\:: $var";
  307.         $obj->share_from($pkg,  [ $var ], 1);
  308.     }
  309. }
  310.  
  311.  
  312. sub share_forget {
  313.     delete shift->{Shares};
  314. }
  315.  
  316.  
  317. sub varglob {
  318.     my ($obj, $var) = @_;
  319.     no strict 'refs';
  320.     return *{$obj->root()."::$var"};
  321. }
  322.  
  323. sub _clean_stash {
  324.     my ($root, $saved_refs) = @_;
  325.     $saved_refs ||= [];
  326.     no strict 'refs';
  327.     foreach my $hook (qw(DESTROY AUTOLOAD), grep /^\(/, keys %$root) {
  328.         push @$saved_refs, \*{$root.$hook};
  329.         delete ${$root}{$hook};
  330.     }
  331.  
  332.     for (grep /::$/, keys %$root) {
  333.         next if \%{$root.$_} eq \%$root;
  334.         _clean_stash($root.$_, $saved_refs);
  335.     }
  336. }
  337.  
  338. sub reval {
  339.     my ($obj, $expr, $strict) = @_;
  340.     my $root = $obj->{Root};
  341.  
  342.     my $evalsub = lexless_anon_sub($root, $strict, $expr);
  343.     # propagate context
  344.     my $sg = sub_generation();
  345.     my @subret = (wantarray)
  346.                ?        Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub)
  347.                : scalar Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
  348.     _clean_stash($root.'::') if $sg != sub_generation();
  349.     $obj->wrap_code_refs_within(@subret);
  350.     return (wantarray) ? @subret : $subret[0];
  351. }
  352.  
  353.  
  354. sub wrap_code_refs_within {
  355.     my $obj = shift;
  356.  
  357.     $obj->_find_code_refs('wrap_code_ref', @_);
  358. }
  359.  
  360.  
  361. sub _find_code_refs {
  362.     my $obj = shift;
  363.     my $visitor = shift;
  364.  
  365.     for my $item (@_) {
  366.         my $reftype = $item && reftype $item
  367.             or next;
  368.         if ($reftype eq 'ARRAY') {
  369.             $obj->_find_code_refs($visitor, @$item);
  370.         }
  371.         elsif ($reftype eq 'HASH') {
  372.             $obj->_find_code_refs($visitor, values %$item);
  373.         }
  374.         # XXX GLOBs?
  375.         elsif ($reftype eq 'CODE') {
  376.             $item = $obj->$visitor($item);
  377.         }
  378.     }
  379. }
  380.  
  381.  
  382. sub wrap_code_ref {
  383.     my ($obj, $sub) = @_;
  384.  
  385.     # wrap code ref $sub with _safe_call_sv so that, when called, the
  386.     # execution will happen with the compartment fully 'in effect'.
  387.  
  388.     croak "Not a CODE reference"
  389.         if reftype $sub ne 'CODE';
  390.  
  391.     my $ret = sub {
  392.         my @args = @_; # lexical to close over
  393.         my $sub_with_args = sub { $sub->(@args) };
  394.  
  395.         my @subret;
  396.         my $error;
  397.         do {
  398.             local $@;  # needed due to perl_call_sv(sv, G_EVAL|G_KEEPERR)
  399.             my $sg = sub_generation();
  400.             @subret = (wantarray)
  401.                 ?        Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args)
  402.                 : scalar Opcode::_safe_call_sv($obj->{Root}, $obj->{Mask}, $sub_with_args);
  403.             $error = $@;
  404.             _clean_stash($obj->{Root}.'::') if $sg != sub_generation();
  405.         };
  406.         if ($error) { # rethrow exception
  407.             $error =~ s/\t\(in cleanup\) //; # prefix added by G_KEEPERR
  408.             die $error;
  409.         }
  410.         return (wantarray) ? @subret : $subret[0];
  411.     };
  412.  
  413.     return $ret;
  414. }
  415.  
  416.  
  417. sub rdo {
  418.     my ($obj, $file) = @_;
  419.     my $root = $obj->{Root};
  420.  
  421.     my $sg = sub_generation();
  422.     my $evalsub = eval
  423.             sprintf('package %s; sub { @_ = (); do $file }', $root);
  424.     my @subret = (wantarray)
  425.                ?        Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub)
  426.                : scalar Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
  427.     _clean_stash($root.'::') if $sg != sub_generation();
  428.     $obj->wrap_code_refs_within(@subret);
  429.     return (wantarray) ? @subret : $subret[0];
  430. }
  431.  
  432.  
  433. 1;
  434.  
  435. __END__
  436.  
  437. =head1 NAME
  438.  
  439. Safe - Compile and execute code in restricted compartments
  440.  
  441. =head1 SYNOPSIS
  442.  
  443.   use Safe;
  444.  
  445.   $compartment = new Safe;
  446.  
  447.   $compartment->permit(qw(time sort :browse));
  448.  
  449.   $result = $compartment->reval($unsafe_code);
  450.  
  451. =head1 DESCRIPTION
  452.  
  453. The Safe extension module allows the creation of compartments
  454. in which perl code can be evaluated. Each compartment has
  455.  
  456. =over 8
  457.  
  458. =item a new namespace
  459.  
  460. The "root" of the namespace (i.e. "main::") is changed to a
  461. different package and code evaluated in the compartment cannot
  462. refer to variables outside this namespace, even with run-time
  463. glob lookups and other tricks.
  464.  
  465. Code which is compiled outside the compartment can choose to place
  466. variables into (or I<share> variables with) the compartment's namespace
  467. and only that data will be visible to code evaluated in the
  468. compartment.
  469.  
  470. By default, the only variables shared with compartments are the
  471. "underscore" variables $_ and @_ (and, technically, the less frequently
  472. used %_, the _ filehandle and so on). This is because otherwise perl
  473. operators which default to $_ will not work and neither will the
  474. assignment of arguments to @_ on subroutine entry.
  475.  
  476. =item an operator mask
  477.  
  478. Each compartment has an associated "operator mask". Recall that
  479. perl code is compiled into an internal format before execution.
  480. Evaluating perl code (e.g. via "eval" or "do 'file'") causes
  481. the code to be compiled into an internal format and then,
  482. provided there was no error in the compilation, executed.
  483. Code evaluated in a compartment compiles subject to the
  484. compartment's operator mask. Attempting to evaluate code in a
  485. compartment which contains a masked operator will cause the
  486. compilation to fail with an error. The code will not be executed.
  487.  
  488. The default operator mask for a newly created compartment is
  489. the ':default' optag.
  490.  
  491. It is important that you read the L<Opcode> module documentation
  492. for more information, especially for detailed definitions of opnames,
  493. optags and opsets.
  494.  
  495. Since it is only at the compilation stage that the operator mask
  496. applies, controlled access to potentially unsafe operations can
  497. be achieved by having a handle to a wrapper subroutine (written
  498. outside the compartment) placed into the compartment. For example,
  499.  
  500.     $cpt = new Safe;
  501.     sub wrapper {
  502.         # vet arguments and perform potentially unsafe operations
  503.     }
  504.     $cpt->share('&wrapper');
  505.  
  506. =back
  507.  
  508.  
  509. =head1 WARNING
  510.  
  511. The authors make B<no warranty>, implied or otherwise, about the
  512. suitability of this software for safety or security purposes.
  513.  
  514. The authors shall not in any case be liable for special, incidental,
  515. consequential, indirect or other similar damages arising from the use
  516. of this software.
  517.  
  518. Your mileage will vary. If in any doubt B<do not use it>.
  519.  
  520.  
  521. =head1 METHODS
  522.  
  523. To create a new compartment, use
  524.  
  525.     $cpt = new Safe;
  526.  
  527. Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
  528. to use for the compartment (defaults to "Safe::Root0", incremented for
  529. each new compartment).
  530.  
  531. Note that version 1.00 of the Safe module supported a second optional
  532. parameter, MASK.  That functionality has been withdrawn pending deeper
  533. consideration. Use the permit and deny methods described below.
  534.  
  535. The following methods can then be used on the compartment
  536. object returned by the above constructor. The object argument
  537. is implicit in each case.
  538.  
  539.  
  540. =head2 permit (OP, ...)
  541.  
  542. Permit the listed operators to be used when compiling code in the
  543. compartment (in I<addition> to any operators already permitted).
  544.  
  545. You can list opcodes by names, or use a tag name; see
  546. L<Opcode/"Predefined Opcode Tags">.
  547.  
  548. =head2 permit_only (OP, ...)
  549.  
  550. Permit I<only> the listed operators to be used when compiling code in
  551. the compartment (I<no> other operators are permitted).
  552.  
  553. =head2 deny (OP, ...)
  554.  
  555. Deny the listed operators from being used when compiling code in the
  556. compartment (other operators may still be permitted).
  557.  
  558. =head2 deny_only (OP, ...)
  559.  
  560. Deny I<only> the listed operators from being used when compiling code
  561. in the compartment (I<all> other operators will be permitted, so you probably
  562. don't want to use this method).
  563.  
  564. =head2 trap (OP, ...)
  565.  
  566. =head2 untrap (OP, ...)
  567.  
  568. The trap and untrap methods are synonyms for deny and permit
  569. respectfully.
  570.  
  571. =head2 share (NAME, ...)
  572.  
  573. This shares the variable(s) in the argument list with the compartment.
  574. This is almost identical to exporting variables using the L<Exporter>
  575. module.
  576.  
  577. Each NAME must be the B<name> of a non-lexical variable, typically
  578. with the leading type identifier included. A bareword is treated as a
  579. function name.
  580.  
  581. Examples of legal names are '$foo' for a scalar, '@foo' for an
  582. array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'
  583. for a glob (i.e.  all symbol table entries associated with "foo",
  584. including scalar, array, hash, sub and filehandle).
  585.  
  586. Each NAME is assumed to be in the calling package. See share_from
  587. for an alternative method (which C<share> uses).
  588.  
  589. =head2 share_from (PACKAGE, ARRAYREF)
  590.  
  591. This method is similar to share() but allows you to explicitly name the
  592. package that symbols should be shared from. The symbol names (including
  593. type characters) are supplied as an array reference.
  594.  
  595.     $safe->share_from('main', [ '$foo', '%bar', 'func' ]);
  596.  
  597. Names can include package names, which are relative to the specified PACKAGE.
  598. So these two calls have the same effect:
  599.  
  600.     $safe->share_from('Scalar::Util', [ 'reftype' ]);
  601.     $safe->share_from('main', [ 'Scalar::Util::reftype' ]);
  602.  
  603. =head2 varglob (VARNAME)
  604.  
  605. This returns a glob reference for the symbol table entry of VARNAME in
  606. the package of the compartment. VARNAME must be the B<name> of a
  607. variable without any leading type marker. For example:
  608.  
  609.     ${$cpt->varglob('foo')} = "Hello world";
  610.  
  611. has the same effect as:
  612.  
  613.     $cpt = new Safe 'Root';
  614.     $Root::foo = "Hello world";
  615.  
  616. but avoids the need to know $cpt's package name.
  617.  
  618.  
  619. =head2 reval (STRING, STRICT)
  620.  
  621. This evaluates STRING as perl code inside the compartment.
  622.  
  623. The code can only see the compartment's namespace (as returned by the
  624. B<root> method). The compartment's root package appears to be the
  625. C<main::> package to the code inside the compartment.
  626.  
  627. Any attempt by the code in STRING to use an operator which is not permitted
  628. by the compartment will cause an error (at run-time of the main program
  629. but at compile-time for the code in STRING).  The error is of the form
  630. "'%s' trapped by operation mask...".
  631.  
  632. If an operation is trapped in this way, then the code in STRING will
  633. not be executed. If such a trapped operation occurs or any other
  634. compile-time or return error, then $@ is set to the error message, just
  635. as with an eval().
  636.  
  637. If there is no error, then the method returns the value of the last
  638. expression evaluated, or a return statement may be used, just as with
  639. subroutines and B<eval()>. The context (list or scalar) is determined
  640. by the caller as usual.
  641.  
  642. If the return value of reval() is (or contains) any code reference,
  643. those code references are wrapped to be themselves executed always
  644. in the compartment. See L</wrap_code_refs_within>.
  645.  
  646. The formerly undocumented STRICT argument sets strictness: if true
  647. 'use strict;' is used, otherwise it uses 'no strict;'. B<Note>: if
  648. STRICT is omitted 'no strict;' is the default.
  649.  
  650. Some points to note:
  651.  
  652. If the entereval op is permitted then the code can use eval "..." to
  653. 'hide' code which might use denied ops. This is not a major problem
  654. since when the code tries to execute the eval it will fail because the
  655. opmask is still in effect. However this technique would allow clever,
  656. and possibly harmful, code to 'probe' the boundaries of what is
  657. possible.
  658.  
  659. Any string eval which is executed by code executing in a compartment,
  660. or by code called from code executing in a compartment, will be eval'd
  661. in the namespace of the compartment. This is potentially a serious
  662. problem.
  663.  
  664. Consider a function foo() in package pkg compiled outside a compartment
  665. but shared with it. Assume the compartment has a root package called
  666. 'Root'. If foo() contains an eval statement like eval '$foo = 1' then,
  667. normally, $pkg::foo will be set to 1.  If foo() is called from the
  668. compartment (by whatever means) then instead of setting $pkg::foo, the
  669. eval will actually set $Root::pkg::foo.
  670.  
  671. This can easily be demonstrated by using a module, such as the Socket
  672. module, which uses eval "..." as part of an AUTOLOAD function. You can
  673. 'use' the module outside the compartment and share an (autoloaded)
  674. function with the compartment. If an autoload is triggered by code in
  675. the compartment, or by any code anywhere that is called by any means
  676. from the compartment, then the eval in the Socket module's AUTOLOAD
  677. function happens in the namespace of the compartment. Any variables
  678. created or used by the eval'd code are now under the control of
  679. the code in the compartment.
  680.  
  681. A similar effect applies to I<all> runtime symbol lookups in code
  682. called from a compartment but not compiled within it.
  683.  
  684. =head2 rdo (FILENAME)
  685.  
  686. This evaluates the contents of file FILENAME inside the compartment.
  687. See above documentation on the B<reval> method for further details.
  688.  
  689. =head2 root (NAMESPACE)
  690.  
  691. This method returns the name of the package that is the root of the
  692. compartment's namespace.
  693.  
  694. Note that this behaviour differs from version 1.00 of the Safe module
  695. where the root module could be used to change the namespace. That
  696. functionality has been withdrawn pending deeper consideration.
  697.  
  698. =head2 mask (MASK)
  699.  
  700. This is a get-or-set method for the compartment's operator mask.
  701.  
  702. With no MASK argument present, it returns the current operator mask of
  703. the compartment.
  704.  
  705. With the MASK argument present, it sets the operator mask for the
  706. compartment (equivalent to calling the deny_only method).
  707.  
  708. =head2 wrap_code_ref (CODEREF)
  709.  
  710. Returns a reference to an anonymous subroutine that, when executed, will call
  711. CODEREF with the Safe compartment 'in effect'.  In other words, with the
  712. package namespace adjusted and the opmask enabled.
  713.  
  714. Note that the opmask doesn't affect the already compiled code, it only affects
  715. any I<further> compilation that the already compiled code may try to perform.
  716.  
  717. This is particularly useful when applied to code references returned from reval().
  718.  
  719. (It also provides a kind of workaround for RT#60374: "Safe.pm sort {} bug with
  720. -Dusethreads". See L<http://rt.perl.org/rt3//Public/Bug/Display.html?id=60374>
  721. for I<much> more detail.)
  722.  
  723. =head2 wrap_code_refs_within (...)
  724.  
  725. Wraps any CODE references found within the arguments by replacing each with the
  726. result of calling L</wrap_code_ref> on the CODE reference. Any ARRAY or HASH
  727. references in the arguments are inspected recursively.
  728.  
  729. Returns nothing.
  730.  
  731. =head1 RISKS
  732.  
  733. This section is just an outline of some of the things code in a compartment
  734. might do (intentionally or unintentionally) which can have an effect outside
  735. the compartment.
  736.  
  737. =over 8
  738.  
  739. =item Memory
  740.  
  741. Consuming all (or nearly all) available memory.
  742.  
  743. =item CPU
  744.  
  745. Causing infinite loops etc.
  746.  
  747. =item Snooping
  748.  
  749. Copying private information out of your system. Even something as
  750. simple as your user name is of value to others. Much useful information
  751. could be gleaned from your environment variables for example.
  752.  
  753. =item Signals
  754.  
  755. Causing signals (especially SIGFPE and SIGALARM) to affect your process.
  756.  
  757. Setting up a signal handler will need to be carefully considered
  758. and controlled.  What mask is in effect when a signal handler
  759. gets called?  If a user can get an imported function to get an
  760. exception and call the user's signal handler, does that user's
  761. restricted mask get re-instated before the handler is called?
  762. Does an imported handler get called with its original mask or
  763. the user's one?
  764.  
  765. =item State Changes
  766.  
  767. Ops such as chdir obviously effect the process as a whole and not just
  768. the code in the compartment. Ops such as rand and srand have a similar
  769. but more subtle effect.
  770.  
  771. =back
  772.  
  773. =head1 AUTHOR
  774.  
  775. Originally designed and implemented by Malcolm Beattie.
  776.  
  777. Reworked to use the Opcode module and other changes added by Tim Bunce.
  778.  
  779. Currently maintained by the Perl 5 Porters, <perl5-porters@perl.org>.
  780.  
  781. =cut
  782.  
  783.