| 1 | # Safe::Hole - make a hole to the original main compartment in the Safe compartment |
| 2 | # Copyright 1999-2001, Sey Nakajima, All rights reserved. |
| 3 | # This program is free software under the GPL. |
| 4 | package Safe::Hole; |
| 5 | |
| 6 | require 5.005; |
| 7 | use Carp; |
| 8 | use strict; |
| 9 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
| 10 | |
| 11 | require Exporter; |
| 12 | require DynaLoader; |
| 13 | |
| 14 | @ISA = qw(Exporter DynaLoader); |
| 15 | |
| 16 | # Items to export into callers namespace by default. Note: do not export |
| 17 | # names by default without a very good reason. Use EXPORT_OK instead. |
| 18 | # Do not simply export all your public functions/methods/constants. |
| 19 | @EXPORT = qw( |
| 20 | ); |
| 21 | $VERSION = '0.14'; |
| 22 | |
| 23 | bootstrap Safe::Hole $VERSION; |
| 24 | |
| 25 | sub new { |
| 26 | my ( $class, $args ) = @_; |
| 27 | my $self = bless {}, $class; |
| 28 | $args = { ROOT => $args || 'main' } unless ref $args eq 'HASH'; |
| 29 | if ( $args->{ROOT} ) { |
| 30 | $self->{PACKAGE} = $args->{ROOT}; |
| 31 | no strict 'refs'; |
| 32 | $self->{STASH} = \%{"$args->{ROOT}::"}; |
| 33 | } |
| 34 | else { |
| 35 | $self->{INC} = [ \%INC, \@INC ]; |
| 36 | $self->{OPMASK} = _get_current_opmask(); |
| 37 | $self->{PACKAGE} = 'main'; |
| 38 | $self->{STASH} = \%main::; |
| 39 | } |
| 40 | $self; |
| 41 | } |
| 42 | |
| 43 | sub call { |
| 44 | my $self = shift; |
| 45 | my $coderef = shift; |
| 46 | my @args = @_; |
| 47 | |
| 48 | # _hole_call_sv() does not seem to like being ripped off the stack |
| 49 | # so we need some fancy footwork to catch and re-throw the error |
| 50 | |
| 51 | my ( @r, $did_not_die ); |
| 52 | my $wantarray = wantarray; |
| 53 | |
| 54 | local (*INC), do { |
| 55 | *INC = $_ for @{ $self->{INC} }; |
| 56 | } if $self->{INC}; |
| 57 | |
| 58 | # Safe::Hole::User contains nothing but is a placeholder so that |
| 59 | # things that are called via Safe::Hole can Carp::croak properly. |
| 60 | |
| 61 | package Safe::Hole::User; # Package name on a different line to keep it from being indexed |
| 62 | |
| 63 | my $inner_call = sub { |
| 64 | eval { |
| 65 | @_ = @args; |
| 66 | if ($wantarray) { |
| 67 | @r = &$coderef; |
| 68 | } |
| 69 | else { |
| 70 | @r = scalar &$coderef; |
| 71 | } |
| 72 | $did_not_die = 1; |
| 73 | }; |
| 74 | }; |
| 75 | |
| 76 | Safe::Hole::_hole_call_sv( $self->{STASH}, ${ $self->{OPMASK} || \undef }, $inner_call ); |
| 77 | |
| 78 | die $@ unless $did_not_die; |
| 79 | return wantarray ? @r : $r[0]; |
| 80 | } |
| 81 | |
| 82 | sub root { |
| 83 | my $self = shift; |
| 84 | $self->{PACKAGE}; |
| 85 | } |
| 86 | |
| 87 | sub wrap { |
| 88 | my ( $self, $ref, $cpt, $name ) = @_; |
| 89 | my ( $result, $typechar, $word ); |
| 90 | no strict 'refs'; |
| 91 | if ( $cpt && $name ) { |
| 92 | croak "Safe object required" unless ref($cpt) eq 'Safe'; |
| 93 | if ( $name =~ /^(\W)(\w+(::\w+)*)$/ ) { |
| 94 | ( $typechar, $word ) = ( $1, $2 ); |
| 95 | } |
| 96 | else { |
| 97 | croak "'$name' not a valid name"; |
| 98 | } |
| 99 | } |
| 100 | my $type = ref $ref; |
| 101 | if ( $type eq '' ) { |
| 102 | croak "reference required"; |
| 103 | } |
| 104 | elsif ( $type eq 'CODE' ) { |
| 105 | $result = sub { $self->call( $ref, @_ ); }; |
| 106 | if ( $typechar eq '&' ) { |
| 107 | *{ $cpt->root() . "::$word" } = $result; |
| 108 | } |
| 109 | elsif ($typechar) { |
| 110 | croak "'$name' type mismatch with $type"; |
| 111 | } |
| 112 | } |
| 113 | elsif ( %{ $type . '::' } ) { |
| 114 | my $wrapclass = ref($self) . '::' . $self->root() . '::' . $type; |
| 115 | *{ $wrapclass . '::AUTOLOAD' } = sub { |
| 116 | $self->call( |
| 117 | sub { |
| 118 | no strict; |
| 119 | my $self = shift; |
| 120 | return if $AUTOLOAD =~ /::DESTROY$/; |
| 121 | my $name = $AUTOLOAD; |
| 122 | $name =~ s/.*://; |
| 123 | $self->{OBJ}->$name(@_); |
| 124 | }, |
| 125 | @_ |
| 126 | ); |
| 127 | } |
| 128 | unless defined &{ $wrapclass . '::AUTOLOAD' }; |
| 129 | $result = bless { OBJ => $ref }, $wrapclass; |
| 130 | if ( $typechar eq '$' ) { |
| 131 | ${ $cpt->varglob($word) } = $result; |
| 132 | } |
| 133 | elsif ($typechar) { |
| 134 | croak "'$name' type mismatch with object (must be scalar)"; |
| 135 | } |
| 136 | } |
| 137 | else { |
| 138 | croak "type '$type' is not supported"; |
| 139 | } |
| 140 | $result; |
| 141 | } |
| 142 | |
| 143 | # Autoload methods go after =cut, and are processed by the autosplit program. |
| 144 | |
| 145 | 1; |
| 146 | __END__ |
| 147 | |
| 148 | =head1 NAME |
| 149 | |
| 150 | Safe::Hole - make a hole to the original main compartment in the Safe compartment |
| 151 | |
| 152 | =head1 SYNOPSIS |
| 153 | |
| 154 | use Safe; |
| 155 | use Safe::Hole; |
| 156 | $cpt = new Safe; |
| 157 | $hole = new Safe::Hole {}; |
| 158 | sub test { Test->test; } |
| 159 | $Testobj = new Test; |
| 160 | # $cpt->share('&test'); # alternate as next line |
| 161 | $hole->wrap(\&test, $cpt, '&test'); |
| 162 | # ${$cpt->varglob('Testobj')} = $Testobj; # alternate as next line |
| 163 | $hole->wrap($Testobj, $cpt, '$Testobj'); |
| 164 | $cpt->reval('test; $Testobj->test;'); |
| 165 | print $@ if $@; |
| 166 | package Test; |
| 167 | sub new { bless {},shift(); } |
| 168 | sub test { my $self = shift; $self->test2; } |
| 169 | sub test2 { print "Test->test2 called\n"; } |
| 170 | |
| 171 | =head1 DESCRIPTION |
| 172 | |
| 173 | We can call outside defined subroutines from the Safe compartment |
| 174 | using share(), or can call methods through the object that is copied |
| 175 | into the Safe compartment using varglob(). But that subroutines or |
| 176 | methods are executed in the Safe compartment too, so they cannot call |
| 177 | another subroutines that are dinamically qualified with the package |
| 178 | name such as class methods nor can they compile code that uses opcodes |
| 179 | that are forbidden within the compartment. |
| 180 | |
| 181 | Through Safe::Hole, we can execute outside defined subroutines in the |
| 182 | original main compartment from the Safe compartment. |
| 183 | |
| 184 | Note that if a subroutine called through Safe::Hole::call does a |
| 185 | Carp::croak() it will report the error as having occured within |
| 186 | Safe::Hole. This can be avoided by including Safe::Hole::User in the |
| 187 | @ISA for the package containing the subroutine. |
| 188 | |
| 189 | =head2 Methods |
| 190 | |
| 191 | =over 4 |
| 192 | |
| 193 | =item new [NAMESPACE] |
| 194 | |
| 195 | Class method. Backward compatible constructor. |
| 196 | NAMESPACE is the alternate root namespace that makes the compartment |
| 197 | in which call() method execute the subroutine. Default of NAMESPACE |
| 198 | means the current 'main'. This emulates the behaviour of |
| 199 | Safe-Hole-0.08 and earlier. |
| 200 | |
| 201 | =item new \%arguments |
| 202 | |
| 203 | Class method. Constructor. |
| 204 | The constructor is called with a hash reference providing the |
| 205 | constructor arguments. The argument ROOT specifies the alternate root |
| 206 | namespace for the object. If the ROOT argument is not specified then |
| 207 | Safe::Hole object will attempt restore as much as it can of the |
| 208 | environment in which it was constrtucted. This includes the opcode |
| 209 | mask, C<%INC> and C<@INC>. If a root namespace is specified then it |
| 210 | would not make sense to restore the %INC and @INC from main:: so this |
| 211 | is not done. Also if a root namespace is given the opcode mask is not |
| 212 | restored either. |
| 213 | |
| 214 | =item call $coderef [,@args] |
| 215 | |
| 216 | Object method. |
| 217 | Call the subroutine refered by $coderef in the compartment that is |
| 218 | specified with constructor new. @args are passed as the arguments to |
| 219 | the called $coderef. Note that the arguments are not currently passed |
| 220 | by reference although this may change in a future version. |
| 221 | |
| 222 | =item wrap $ref [,$cpt ,$name] |
| 223 | |
| 224 | Object method. |
| 225 | If $ref is a code reference, this method returns the anonymous |
| 226 | subroutine reference that calls $ref using call() method of Safe::Hole (see |
| 227 | above). |
| 228 | If $ref is a class object, this method makes a wrapper class of that object |
| 229 | and returns a new object of the wrapper class. Through the wrapper class, |
| 230 | all original class methods called using call() method of Safe::Hole. |
| 231 | If $cpt as Safe object and $name as subroutine or scalar name specified, |
| 232 | this method works like share() method of Safe. When $ref is a code reference |
| 233 | $name must like '&subroutine'. When $ref is a object $name must like '$var'. |
| 234 | Name $name may not be same as referent of $ref. For example: |
| 235 | $hole->wrap(\&foo, $cpt, '&bar'); |
| 236 | $hole->wrap(sub{...}, $cpt, '&foo'); |
| 237 | $hole->wrap($objfoo, $cpt, '$objbar'); |
| 238 | |
| 239 | =item root |
| 240 | |
| 241 | Object method. |
| 242 | Return the namespace that is specified with constructor new(). |
| 243 | If no namespace was then root() returns 'main'. |
| 244 | |
| 245 | =back |
| 246 | |
| 247 | =head2 Warning |
| 248 | |
| 249 | You MUST NOT share the Safe::Hole object with the Safe compartment. If you do it |
| 250 | the Safe compartment is NOT safe. |
| 251 | |
| 252 | This module provides a means to go from a state where an opcode is |
| 253 | denied back to a state where it is not. Reasonable care has been |
| 254 | taken to ensure that programs cannot simply manipulate the internals |
| 255 | to the Safe::Hole object to reduce the opmask in effect. However there |
| 256 | may still be a way that the authors have not considered. In |
| 257 | particular it relies on the fact that a Perl program cannot change |
| 258 | stuff inside the magic on a Perl variable. If you install a module |
| 259 | that allows a Perl program to fiddle inside the magic then this |
| 260 | assuption breaks down. One would hope that any system that was |
| 261 | running un-trusted code would not have such a module installed. |
| 262 | |
| 263 | =head1 AUTHORS |
| 264 | |
| 265 | Sey Nakajima <[email protected]> (Initial version) |
| 266 | |
| 267 | Brian McCauley <[email protected]> (Maintenance) |
| 268 | |
| 269 | Todd Rinaldo <[email protected]> (Maintenance) |
| 270 | |
| 271 | =head1 SEE ALSO |
| 272 | |
| 273 | Safe(3). |