| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "Safe 3" |
| 132 | .TH Safe 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | Safe \- Compile and execute code in restricted compartments |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use Safe; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 1 |
| 142 | \& $compartment = new Safe; |
| 143 | .Ve |
| 144 | .PP |
| 145 | .Vb 1 |
| 146 | \& $compartment->permit(qw(time sort :browse)); |
| 147 | .Ve |
| 148 | .PP |
| 149 | .Vb 1 |
| 150 | \& $result = $compartment->reval($unsafe_code); |
| 151 | .Ve |
| 152 | .SH "DESCRIPTION" |
| 153 | .IX Header "DESCRIPTION" |
| 154 | The Safe extension module allows the creation of compartments |
| 155 | in which perl code can be evaluated. Each compartment has |
| 156 | .IP "a new namespace" 8 |
| 157 | .IX Item "a new namespace" |
| 158 | The \*(L"root\*(R" of the namespace (i.e. \*(L"main::\*(R") is changed to a |
| 159 | different package and code evaluated in the compartment cannot |
| 160 | refer to variables outside this namespace, even with run-time |
| 161 | glob lookups and other tricks. |
| 162 | .Sp |
| 163 | Code which is compiled outside the compartment can choose to place |
| 164 | variables into (or \fIshare\fR variables with) the compartment's namespace |
| 165 | and only that data will be visible to code evaluated in the |
| 166 | compartment. |
| 167 | .Sp |
| 168 | By default, the only variables shared with compartments are the |
| 169 | \&\*(L"underscore\*(R" variables \f(CW$_\fR and \f(CW@_\fR (and, technically, the less frequently |
| 170 | used \f(CW%_\fR, the _ filehandle and so on). This is because otherwise perl |
| 171 | operators which default to \f(CW$_\fR will not work and neither will the |
| 172 | assignment of arguments to \f(CW@_\fR on subroutine entry. |
| 173 | .IP "an operator mask" 8 |
| 174 | .IX Item "an operator mask" |
| 175 | Each compartment has an associated \*(L"operator mask\*(R". Recall that |
| 176 | perl code is compiled into an internal format before execution. |
| 177 | Evaluating perl code (e.g. via \*(L"eval\*(R" or \*(L"do 'file'\*(R") causes |
| 178 | the code to be compiled into an internal format and then, |
| 179 | provided there was no error in the compilation, executed. |
| 180 | Code evaluated in a compartment compiles subject to the |
| 181 | compartment's operator mask. Attempting to evaluate code in a |
| 182 | compartment which contains a masked operator will cause the |
| 183 | compilation to fail with an error. The code will not be executed. |
| 184 | .Sp |
| 185 | The default operator mask for a newly created compartment is |
| 186 | the ':default' optag. |
| 187 | .Sp |
| 188 | It is important that you read the \fIOpcode\fR\|(3) module documentation |
| 189 | for more information, especially for detailed definitions of opnames, |
| 190 | optags and opsets. |
| 191 | .Sp |
| 192 | Since it is only at the compilation stage that the operator mask |
| 193 | applies, controlled access to potentially unsafe operations can |
| 194 | be achieved by having a handle to a wrapper subroutine (written |
| 195 | outside the compartment) placed into the compartment. For example, |
| 196 | .Sp |
| 197 | .Vb 5 |
| 198 | \& $cpt = new Safe; |
| 199 | \& sub wrapper { |
| 200 | \& # vet arguments and perform potentially unsafe operations |
| 201 | \& } |
| 202 | \& $cpt->share('&wrapper'); |
| 203 | .Ve |
| 204 | .SH "WARNING" |
| 205 | .IX Header "WARNING" |
| 206 | The authors make \fBno warranty\fR, implied or otherwise, about the |
| 207 | suitability of this software for safety or security purposes. |
| 208 | .PP |
| 209 | The authors shall not in any case be liable for special, incidental, |
| 210 | consequential, indirect or other similar damages arising from the use |
| 211 | of this software. |
| 212 | .PP |
| 213 | Your mileage will vary. If in any doubt \fBdo not use it\fR. |
| 214 | .Sh "\s-1RECENT\s0 \s-1CHANGES\s0" |
| 215 | .IX Subsection "RECENT CHANGES" |
| 216 | The interface to the Safe module has changed quite dramatically since |
| 217 | version 1 (as supplied with Perl5.002). Study these pages carefully if |
| 218 | you have code written to use Safe version 1 because you will need to |
| 219 | makes changes. |
| 220 | .Sh "Methods in class Safe" |
| 221 | .IX Subsection "Methods in class Safe" |
| 222 | To create a new compartment, use |
| 223 | .PP |
| 224 | .Vb 1 |
| 225 | \& $cpt = new Safe; |
| 226 | .Ve |
| 227 | .PP |
| 228 | Optional argument is (\s-1NAMESPACE\s0), where \s-1NAMESPACE\s0 is the root namespace |
| 229 | to use for the compartment (defaults to \*(L"Safe::Root0\*(R", incremented for |
| 230 | each new compartment). |
| 231 | .PP |
| 232 | Note that version 1.00 of the Safe module supported a second optional |
| 233 | parameter, \s-1MASK\s0. That functionality has been withdrawn pending deeper |
| 234 | consideration. Use the permit and deny methods described below. |
| 235 | .PP |
| 236 | The following methods can then be used on the compartment |
| 237 | object returned by the above constructor. The object argument |
| 238 | is implicit in each case. |
| 239 | .IP "permit (\s-1OP\s0, ...)" 8 |
| 240 | .IX Item "permit (OP, ...)" |
| 241 | Permit the listed operators to be used when compiling code in the |
| 242 | compartment (in \fIaddition\fR to any operators already permitted). |
| 243 | .Sp |
| 244 | You can list opcodes by names, or use a tag name; see |
| 245 | \&\*(L"Predefined Opcode Tags\*(R" in Opcode. |
| 246 | .IP "permit_only (\s-1OP\s0, ...)" 8 |
| 247 | .IX Item "permit_only (OP, ...)" |
| 248 | Permit \fIonly\fR the listed operators to be used when compiling code in |
| 249 | the compartment (\fIno\fR other operators are permitted). |
| 250 | .IP "deny (\s-1OP\s0, ...)" 8 |
| 251 | .IX Item "deny (OP, ...)" |
| 252 | Deny the listed operators from being used when compiling code in the |
| 253 | compartment (other operators may still be permitted). |
| 254 | .IP "deny_only (\s-1OP\s0, ...)" 8 |
| 255 | .IX Item "deny_only (OP, ...)" |
| 256 | Deny \fIonly\fR the listed operators from being used when compiling code |
| 257 | in the compartment (\fIall\fR other operators will be permitted). |
| 258 | .IP "trap (\s-1OP\s0, ...)" 8 |
| 259 | .IX Item "trap (OP, ...)" |
| 260 | .PD 0 |
| 261 | .IP "untrap (\s-1OP\s0, ...)" 8 |
| 262 | .IX Item "untrap (OP, ...)" |
| 263 | .PD |
| 264 | The trap and untrap methods are synonyms for deny and permit |
| 265 | respectfully. |
| 266 | .IP "share (\s-1NAME\s0, ...)" 8 |
| 267 | .IX Item "share (NAME, ...)" |
| 268 | This shares the variable(s) in the argument list with the compartment. |
| 269 | This is almost identical to exporting variables using the Exporter |
| 270 | module. |
| 271 | .Sp |
| 272 | Each \s-1NAME\s0 must be the \fBname\fR of a non-lexical variable, typically |
| 273 | with the leading type identifier included. A bareword is treated as a |
| 274 | function name. |
| 275 | .Sp |
| 276 | Examples of legal names are '$foo' for a scalar, '@foo' for an |
| 277 | array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo' |
| 278 | for a glob (i.e. all symbol table entries associated with \*(L"foo\*(R", |
| 279 | including scalar, array, hash, sub and filehandle). |
| 280 | .Sp |
| 281 | Each \s-1NAME\s0 is assumed to be in the calling package. See share_from |
| 282 | for an alternative method (which share uses). |
| 283 | .IP "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)" 8 |
| 284 | .IX Item "share_from (PACKAGE, ARRAYREF)" |
| 285 | This method is similar to \fIshare()\fR but allows you to explicitly name the |
| 286 | package that symbols should be shared from. The symbol names (including |
| 287 | type characters) are supplied as an array reference. |
| 288 | .Sp |
| 289 | .Vb 1 |
| 290 | \& $safe->share_from('main', [ '$foo', '%bar', 'func' ]); |
| 291 | .Ve |
| 292 | .IP "varglob (\s-1VARNAME\s0)" 8 |
| 293 | .IX Item "varglob (VARNAME)" |
| 294 | This returns a glob reference for the symbol table entry of \s-1VARNAME\s0 in |
| 295 | the package of the compartment. \s-1VARNAME\s0 must be the \fBname\fR of a |
| 296 | variable without any leading type marker. For example, |
| 297 | .Sp |
| 298 | .Vb 4 |
| 299 | \& $cpt = new Safe 'Root'; |
| 300 | \& $Root::foo = "Hello world"; |
| 301 | \& # Equivalent version which doesn't need to know $cpt's package name: |
| 302 | \& ${$cpt->varglob('foo')} = "Hello world"; |
| 303 | .Ve |
| 304 | .IP "reval (\s-1STRING\s0)" 8 |
| 305 | .IX Item "reval (STRING)" |
| 306 | This evaluates \s-1STRING\s0 as perl code inside the compartment. |
| 307 | .Sp |
| 308 | The code can only see the compartment's namespace (as returned by the |
| 309 | \&\fBroot\fR method). The compartment's root package appears to be the |
| 310 | \&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment. |
| 311 | .Sp |
| 312 | Any attempt by the code in \s-1STRING\s0 to use an operator which is not permitted |
| 313 | by the compartment will cause an error (at run-time of the main program |
| 314 | but at compile-time for the code in \s-1STRING\s0). The error is of the form |
| 315 | \&\*(L"'%s' trapped by operation mask...\*(R". |
| 316 | .Sp |
| 317 | If an operation is trapped in this way, then the code in \s-1STRING\s0 will |
| 318 | not be executed. If such a trapped operation occurs or any other |
| 319 | compile-time or return error, then $@ is set to the error message, just |
| 320 | as with an \fIeval()\fR. |
| 321 | .Sp |
| 322 | If there is no error, then the method returns the value of the last |
| 323 | expression evaluated, or a return statement may be used, just as with |
| 324 | subroutines and \fB\f(BIeval()\fB\fR. The context (list or scalar) is determined |
| 325 | by the caller as usual. |
| 326 | .Sp |
| 327 | This behaviour differs from the beta distribution of the Safe extension |
| 328 | where earlier versions of perl made it hard to mimic the return |
| 329 | behaviour of the \fIeval()\fR command and the context was always scalar. |
| 330 | .Sp |
| 331 | Some points to note: |
| 332 | .Sp |
| 333 | If the entereval op is permitted then the code can use eval \*(L"...\*(R" to |
| 334 | \&'hide' code which might use denied ops. This is not a major problem |
| 335 | since when the code tries to execute the eval it will fail because the |
| 336 | opmask is still in effect. However this technique would allow clever, |
| 337 | and possibly harmful, code to 'probe' the boundaries of what is |
| 338 | possible. |
| 339 | .Sp |
| 340 | Any string eval which is executed by code executing in a compartment, |
| 341 | or by code called from code executing in a compartment, will be eval'd |
| 342 | in the namespace of the compartment. This is potentially a serious |
| 343 | problem. |
| 344 | .Sp |
| 345 | Consider a function \fIfoo()\fR in package pkg compiled outside a compartment |
| 346 | but shared with it. Assume the compartment has a root package called |
| 347 | \&'Root'. If \fIfoo()\fR contains an eval statement like eval '$foo = 1' then, |
| 348 | normally, \f(CW$pkg::foo\fR will be set to 1. If \fIfoo()\fR is called from the |
| 349 | compartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, the |
| 350 | eval will actually set \f(CW$Root::pkg::foo\fR. |
| 351 | .Sp |
| 352 | This can easily be demonstrated by using a module, such as the Socket |
| 353 | module, which uses eval \*(L"...\*(R" as part of an \s-1AUTOLOAD\s0 function. You can |
| 354 | \&'use' the module outside the compartment and share an (autoloaded) |
| 355 | function with the compartment. If an autoload is triggered by code in |
| 356 | the compartment, or by any code anywhere that is called by any means |
| 357 | from the compartment, then the eval in the Socket module's \s-1AUTOLOAD\s0 |
| 358 | function happens in the namespace of the compartment. Any variables |
| 359 | created or used by the eval'd code are now under the control of |
| 360 | the code in the compartment. |
| 361 | .Sp |
| 362 | A similar effect applies to \fIall\fR runtime symbol lookups in code |
| 363 | called from a compartment but not compiled within it. |
| 364 | .IP "rdo (\s-1FILENAME\s0)" 8 |
| 365 | .IX Item "rdo (FILENAME)" |
| 366 | This evaluates the contents of file \s-1FILENAME\s0 inside the compartment. |
| 367 | See above documentation on the \fBreval\fR method for further details. |
| 368 | .IP "root (\s-1NAMESPACE\s0)" 8 |
| 369 | .IX Item "root (NAMESPACE)" |
| 370 | This method returns the name of the package that is the root of the |
| 371 | compartment's namespace. |
| 372 | .Sp |
| 373 | Note that this behaviour differs from version 1.00 of the Safe module |
| 374 | where the root module could be used to change the namespace. That |
| 375 | functionality has been withdrawn pending deeper consideration. |
| 376 | .IP "mask (\s-1MASK\s0)" 8 |
| 377 | .IX Item "mask (MASK)" |
| 378 | This is a get-or-set method for the compartment's operator mask. |
| 379 | .Sp |
| 380 | With no \s-1MASK\s0 argument present, it returns the current operator mask of |
| 381 | the compartment. |
| 382 | .Sp |
| 383 | With the \s-1MASK\s0 argument present, it sets the operator mask for the |
| 384 | compartment (equivalent to calling the deny_only method). |
| 385 | .Sh "Some Safety Issues" |
| 386 | .IX Subsection "Some Safety Issues" |
| 387 | This section is currently just an outline of some of the things code in |
| 388 | a compartment might do (intentionally or unintentionally) which can |
| 389 | have an effect outside the compartment. |
| 390 | .IP "Memory" 8 |
| 391 | .IX Item "Memory" |
| 392 | Consuming all (or nearly all) available memory. |
| 393 | .IP "\s-1CPU\s0" 8 |
| 394 | .IX Item "CPU" |
| 395 | Causing infinite loops etc. |
| 396 | .IP "Snooping" 8 |
| 397 | .IX Item "Snooping" |
| 398 | Copying private information out of your system. Even something as |
| 399 | simple as your user name is of value to others. Much useful information |
| 400 | could be gleaned from your environment variables for example. |
| 401 | .IP "Signals" 8 |
| 402 | .IX Item "Signals" |
| 403 | Causing signals (especially \s-1SIGFPE\s0 and \s-1SIGALARM\s0) to affect your process. |
| 404 | .Sp |
| 405 | Setting up a signal handler will need to be carefully considered |
| 406 | and controlled. What mask is in effect when a signal handler |
| 407 | gets called? If a user can get an imported function to get an |
| 408 | exception and call the user's signal handler, does that user's |
| 409 | restricted mask get re-instated before the handler is called? |
| 410 | Does an imported handler get called with its original mask or |
| 411 | the user's one? |
| 412 | .IP "State Changes" 8 |
| 413 | .IX Item "State Changes" |
| 414 | Ops such as chdir obviously effect the process as a whole and not just |
| 415 | the code in the compartment. Ops such as rand and srand have a similar |
| 416 | but more subtle effect. |
| 417 | .Sh "\s-1AUTHOR\s0" |
| 418 | .IX Subsection "AUTHOR" |
| 419 | Originally designed and implemented by Malcolm Beattie, |
| 420 | mbeattie@sable.ox.ac.uk. |
| 421 | .PP |
| 422 | Reworked to use the Opcode module and other changes added by Tim Bunce |
| 423 | <\fITim.Bunce@ig.co.uk\fR>. |