📄 safe.3
字号:
Your mileage will vary. If in any doubt \fBdo not use it\fR..Sh "\s-1RECENT\s0 \s-1CHANGES\s0".IX Subsection "RECENT CHANGES"The interface to the Safe module has changed quite dramatically sinceversion 1 (as supplied with Perl5.002). Study these pages carefully ifyou have code written to use Safe version 1 because you will need tomakes changes..Sh "Methods in class Safe".IX Subsection "Methods in class Safe"To create a new compartment, use.PP.Vb 1\& $cpt = new Safe;.Ve.PPOptional argument is (\s-1NAMESPACE\s0), where \s-1NAMESPACE\s0 is the root namespaceto use for the compartment (defaults to \*(L"Safe::Root0\*(R", incremented foreach new compartment)..PPNote that version 1.00 of the Safe module supported a second optionalparameter, \s-1MASK\s0. That functionality has been withdrawn pending deeperconsideration. Use the permit and deny methods described below..PPThe following methods can then be used on the compartmentobject returned by the above constructor. The object argumentis implicit in each case..IP "permit (\s-1OP\s0, ...)" 8.IX Item "permit (OP, ...)"Permit the listed operators to be used when compiling code in thecompartment (in \fIaddition\fR to any operators already permitted)..SpYou can list opcodes by names, or use a tag name; see\&\*(L"Predefined Opcode Tags\*(R" in Opcode..IP "permit_only (\s-1OP\s0, ...)" 8.IX Item "permit_only (OP, ...)"Permit \fIonly\fR the listed operators to be used when compiling code inthe compartment (\fIno\fR other operators are permitted)..IP "deny (\s-1OP\s0, ...)" 8.IX Item "deny (OP, ...)"Deny the listed operators from being used when compiling code in thecompartment (other operators may still be permitted)..IP "deny_only (\s-1OP\s0, ...)" 8.IX Item "deny_only (OP, ...)"Deny \fIonly\fR the listed operators from being used when compiling codein the compartment (\fIall\fR other operators will be permitted)..IP "trap (\s-1OP\s0, ...)" 8.IX Item "trap (OP, ...)".PD 0.IP "untrap (\s-1OP\s0, ...)" 8.IX Item "untrap (OP, ...)".PDThe trap and untrap methods are synonyms for deny and permitrespectfully..IP "share (\s-1NAME\s0, ...)" 8.IX Item "share (NAME, ...)"This shares the variable(s) in the argument list with the compartment.This is almost identical to exporting variables using the Exportermodule..SpEach \s-1NAME\s0 must be the \fBname\fR of a non-lexical variable, typicallywith the leading type identifier included. A bareword is treated as afunction name..SpExamples of legal names are '$foo' for a scalar, '@foo' for anarray, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo'for a glob (i.e. all symbol table entries associated with \*(L"foo\*(R",including scalar, array, hash, sub and filehandle)..SpEach \s-1NAME\s0 is assumed to be in the calling package. See share_fromfor an alternative method (which share uses)..IP "share_from (\s-1PACKAGE\s0, \s-1ARRAYREF\s0)" 8.IX Item "share_from (PACKAGE, ARRAYREF)"This method is similar to \fIshare()\fR but allows you to explicitly name thepackage that symbols should be shared from. The symbol names (includingtype characters) are supplied as an array reference..Sp.Vb 1\& $safe\->share_from(\*(Aqmain\*(Aq, [ \*(Aq$foo\*(Aq, \*(Aq%bar\*(Aq, \*(Aqfunc\*(Aq ]);.Ve.IP "varglob (\s-1VARNAME\s0)" 8.IX Item "varglob (VARNAME)"This returns a glob reference for the symbol table entry of \s-1VARNAME\s0 inthe package of the compartment. \s-1VARNAME\s0 must be the \fBname\fR of avariable without any leading type marker. For example,.Sp.Vb 4\& $cpt = new Safe \*(AqRoot\*(Aq;\& $Root::foo = "Hello world";\& # Equivalent version which doesn\*(Aqt need to know $cpt\*(Aqs package name:\& ${$cpt\->varglob(\*(Aqfoo\*(Aq)} = "Hello world";.Ve.IP "reval (\s-1STRING\s0)" 8.IX Item "reval (STRING)"This evaluates \s-1STRING\s0 as perl code inside the compartment..SpThe code can only see the compartment's namespace (as returned by the\&\fBroot\fR method). The compartment's root package appears to be the\&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment..SpAny attempt by the code in \s-1STRING\s0 to use an operator which is not permittedby the compartment will cause an error (at run-time of the main programbut at compile-time for the code in \s-1STRING\s0). The error is of the form\&\*(L"'%s' trapped by operation mask...\*(R"..SpIf an operation is trapped in this way, then the code in \s-1STRING\s0 willnot be executed. If such a trapped operation occurs or any othercompile-time or return error, then $@ is set to the error message, justas with an \fIeval()\fR..SpIf there is no error, then the method returns the value of the lastexpression evaluated, or a return statement may be used, just as withsubroutines and \fB\f(BIeval()\fB\fR. The context (list or scalar) is determinedby the caller as usual..SpThis behaviour differs from the beta distribution of the Safe extensionwhere earlier versions of perl made it hard to mimic the returnbehaviour of the \fIeval()\fR command and the context was always scalar..SpSome points to note:.SpIf the entereval op is permitted then the code can use eval \*(L"...\*(R" to\&'hide' code which might use denied ops. This is not a major problemsince when the code tries to execute the eval it will fail because theopmask is still in effect. However this technique would allow clever,and possibly harmful, code to 'probe' the boundaries of what ispossible..SpAny string eval which is executed by code executing in a compartment,or by code called from code executing in a compartment, will be eval'din the namespace of the compartment. This is potentially a seriousproblem..SpConsider a function \fIfoo()\fR in package pkg compiled outside a compartmentbut shared with it. Assume the compartment has a root package called\&'Root'. If \fIfoo()\fR contains an eval statement like eval '$foo = 1' then,normally, \f(CW$pkg::foo\fR will be set to 1. If \fIfoo()\fR is called from thecompartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, theeval will actually set \f(CW$Root::pkg::foo\fR..SpThis can easily be demonstrated by using a module, such as the Socketmodule, which uses eval \*(L"...\*(R" as part of an \s-1AUTOLOAD\s0 function. You can\&'use' the module outside the compartment and share an (autoloaded)function with the compartment. If an autoload is triggered by code inthe compartment, or by any code anywhere that is called by any meansfrom the compartment, then the eval in the Socket module's \s-1AUTOLOAD\s0function happens in the namespace of the compartment. Any variablescreated or used by the eval'd code are now under the control ofthe code in the compartment..SpA similar effect applies to \fIall\fR runtime symbol lookups in codecalled from a compartment but not compiled within it..IP "rdo (\s-1FILENAME\s0)" 8.IX Item "rdo (FILENAME)"This evaluates the contents of file \s-1FILENAME\s0 inside the compartment.See above documentation on the \fBreval\fR method for further details..IP "root (\s-1NAMESPACE\s0)" 8.IX Item "root (NAMESPACE)"This method returns the name of the package that is the root of thecompartment's namespace..SpNote that this behaviour differs from version 1.00 of the Safe modulewhere the root module could be used to change the namespace. Thatfunctionality has been withdrawn pending deeper consideration..IP "mask (\s-1MASK\s0)" 8.IX Item "mask (MASK)"This is a get-or-set method for the compartment's operator mask..SpWith no \s-1MASK\s0 argument present, it returns the current operator mask ofthe compartment..SpWith the \s-1MASK\s0 argument present, it sets the operator mask for thecompartment (equivalent to calling the deny_only method)..Sh "Some Safety Issues".IX Subsection "Some Safety Issues"This section is currently just an outline of some of the things code ina compartment might do (intentionally or unintentionally) which canhave an effect outside the compartment..IP "Memory" 8.IX Item "Memory"Consuming all (or nearly all) available memory..IP "\s-1CPU\s0" 8.IX Item "CPU"Causing infinite loops etc..IP "Snooping" 8.IX Item "Snooping"Copying private information out of your system. Even something assimple as your user name is of value to others. Much useful informationcould be gleaned from your environment variables for example..IP "Signals" 8.IX Item "Signals"Causing signals (especially \s-1SIGFPE\s0 and \s-1SIGALARM\s0) to affect your process..SpSetting up a signal handler will need to be carefully consideredand controlled. What mask is in effect when a signal handlergets called? If a user can get an imported function to get anexception and call the user's signal handler, does that user'srestricted mask get re-instated before the handler is called?Does an imported handler get called with its original mask orthe user's one?.IP "State Changes" 8.IX Item "State Changes"Ops such as chdir obviously effect the process as a whole and not justthe code in the compartment. Ops such as rand and srand have a similarbut more subtle effect..Sh "\s-1AUTHOR\s0".IX Subsection "AUTHOR"Originally designed and implemented by Malcolm Beattie..PPReworked to use the Opcode module and other changes added by Tim Bunce..PPCurrently maintained by the Perl 5 Porters, <perl5\-porters@perl.org>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -