Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Safe.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Safe 3pm"
61 .TH Safe 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Safe \- Compile and execute code in restricted compartments
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use Safe;
72 \&
73 \& $compartment = new Safe;
74 \&
75 \& $compartment\->permit(qw(time sort :browse));
76 \&
77 \& $result = $compartment\->reval($unsafe_code);
78 .Ve
79 .SH DESCRIPTION
80 .IX Header "DESCRIPTION"
81 The Safe extension module allows the creation of compartments
82 in which perl code can be evaluated. Each compartment has
83 .IP "a new namespace" 8
84 .IX Item "a new namespace"
85 The "root" of the namespace (i.e. "main::") is changed to a
86 different package and code evaluated in the compartment cannot
87 refer to variables outside this namespace, even with run\-time
88 glob lookups and other tricks.
89 .Sp
90 Code which is compiled outside the compartment can choose to place
91 variables into (or \fIshare\fR variables with) the compartment\*(Aqs namespace
92 and only that data will be visible to code evaluated in the
93 compartment.
94 .Sp
95 By default, the only variables shared with compartments are the
96 "underscore" variables \f(CW$_\fR and \f(CW@_\fR (and, technically, the less frequently
97 used \f(CW%_\fR, the _ filehandle and so on). This is because otherwise perl
98 operators which default to \f(CW$_\fR will not work and neither will the
99 assignment of arguments to \f(CW@_\fR on subroutine entry.
100 .IP "an operator mask" 8
101 .IX Item "an operator mask"
102 Each compartment has an associated "operator mask". Recall that
103 perl code is compiled into an internal format before execution.
104 Evaluating perl code (e.g. via "eval" or "do \*(Aqfile\*(Aq") causes
105 the code to be compiled into an internal format and then,
106 provided there was no error in the compilation, executed.
107 Code evaluated in a compartment compiles subject to the
108 compartment\*(Aqs operator mask. Attempting to evaluate code in a
109 compartment which contains a masked operator will cause the
110 compilation to fail with an error. The code will not be executed.
111 .Sp
112 The default operator mask for a newly created compartment is
113 the \*(Aq:default\*(Aq optag.
114 .Sp
115 It is important that you read the Opcode module documentation
116 for more information, especially for detailed definitions of opnames,
117 optags and opsets.
118 .Sp
119 Since it is only at the compilation stage that the operator mask
120 applies, controlled access to potentially unsafe operations can
121 be achieved by having a handle to a wrapper subroutine (written
122 outside the compartment) placed into the compartment. For example,
123 .Sp
124 .Vb 5
125 \& $cpt = new Safe;
126 \& sub wrapper {
127 \& # vet arguments and perform potentially unsafe operations
128 \& }
129 \& $cpt\->share(\*(Aq&wrapper\*(Aq);
130 .Ve
131 .SH WARNING
132 .IX Header "WARNING"
133 The Safe module does not implement an effective sandbox for
134 evaluating untrusted code with the perl interpreter.
135 .PP
136 Bugs in the perl interpreter that could be abused to bypass
137 Safe restrictions are not treated as vulnerabilities. See
138 perlsecpolicy for additional information.
139 .PP
140 The authors make \fBno warranty\fR, implied or otherwise, about the
141 suitability of this software for safety or security purposes.
142 .PP
143 The authors shall not in any case be liable for special, incidental,
144 consequential, indirect or other similar damages arising from the use
145 of this software.
146 .PP
147 Your mileage will vary. If in any doubt \fBdo not use it\fR.
148 .SH METHODS
149 .IX Header "METHODS"
150 To create a new compartment, use
151 .PP
152 .Vb 1
153 \& $cpt = new Safe;
154 .Ve
155 .PP
156 Optional argument is (NAMESPACE), where NAMESPACE is the root namespace
157 to use for the compartment (defaults to "Safe::Root0", incremented for
158 each new compartment).
159 .PP
160 Note that version 1.00 of the Safe module supported a second optional
161 parameter, MASK. That functionality has been withdrawn pending deeper
162 consideration. Use the permit and deny methods described below.
163 .PP
164 The following methods can then be used on the compartment
165 object returned by the above constructor. The object argument
166 is implicit in each case.
167 .SS "permit (OP, ...)"
168 .IX Subsection "permit (OP, ...)"
169 Permit the listed operators to be used when compiling code in the
170 compartment (in \fIaddition\fR to any operators already permitted).
171 .PP
172 You can list opcodes by names, or use a tag name; see
173 "Predefined Opcode Tags" in Opcode.
174 .SS "permit_only (OP, ...)"
175 .IX Subsection "permit_only (OP, ...)"
176 Permit \fIonly\fR the listed operators to be used when compiling code in
177 the compartment (\fIno\fR other operators are permitted).
178 .SS "deny (OP, ...)"
179 .IX Subsection "deny (OP, ...)"
180 Deny the listed operators from being used when compiling code in the
181 compartment (other operators may still be permitted).
182 .SS "deny_only (OP, ...)"
183 .IX Subsection "deny_only (OP, ...)"
184 Deny \fIonly\fR the listed operators from being used when compiling code
185 in the compartment (\fIall\fR other operators will be permitted, so you probably
186 don\*(Aqt want to use this method).
187 .SS "trap (OP, ...), untrap (OP, ...)"
188 .IX Subsection "trap (OP, ...), untrap (OP, ...)"
189 The trap and untrap methods are synonyms for deny and permit
190 respectfully.
191 .SS "share (NAME, ...)"
192 .IX Subsection "share (NAME, ...)"
193 This shares the variable(s) in the argument list with the compartment.
194 This is almost identical to exporting variables using the Exporter
195 module.
196 .PP
197 Each NAME must be the \fBname\fR of a non\-lexical variable, typically
198 with the leading type identifier included. A bareword is treated as a
199 function name.
200 .PP
201 Examples of legal names are \*(Aq$foo\*(Aq for a scalar, \*(Aq@foo\*(Aq for an
202 array, \*(Aq%foo\*(Aq for a hash, \*(Aq&foo\*(Aq or \*(Aqfoo\*(Aq for a subroutine and \*(Aq*foo\*(Aq
203 for a glob (i.e. all symbol table entries associated with "foo",
204 including scalar, array, hash, sub and filehandle).
205 .PP
206 Each NAME is assumed to be in the calling package. See share_from
207 for an alternative method (which \f(CW\*(C`share\*(C'\fR uses).
208 .SS "share_from (PACKAGE, ARRAYREF)"
209 .IX Subsection "share_from (PACKAGE, ARRAYREF)"
210 This method is similar to \fBshare()\fR but allows you to explicitly name the
211 package that symbols should be shared from. The symbol names (including
212 type characters) are supplied as an array reference.
213 .PP
214 .Vb 1
215 \& $safe\->share_from(\*(Aqmain\*(Aq, [ \*(Aq$foo\*(Aq, \*(Aq%bar\*(Aq, \*(Aqfunc\*(Aq ]);
216 .Ve
217 .PP
218 Names can include package names, which are relative to the specified PACKAGE.
219 So these two calls have the same effect:
220 .PP
221 .Vb 2
222 \& $safe\->share_from(\*(AqScalar::Util\*(Aq, [ \*(Aqreftype\*(Aq ]);
223 \& $safe\->share_from(\*(Aqmain\*(Aq, [ \*(AqScalar::Util::reftype\*(Aq ]);
224 .Ve
225 .SS "varglob (VARNAME)"
226 .IX Subsection "varglob (VARNAME)"
227 This returns a glob reference for the symbol table entry of VARNAME in
228 the package of the compartment. VARNAME must be the \fBname\fR of a
229 variable without any leading type marker. For example:
230 .PP
231 .Vb 1
232 \& ${$cpt\->varglob(\*(Aqfoo\*(Aq)} = "Hello world";
233 .Ve
234 .PP
235 has the same effect as:
236 .PP
237 .Vb 2
238 \& $cpt = new Safe \*(AqRoot\*(Aq;
239 \& $Root::foo = "Hello world";
240 .Ve
241 .PP
242 but avoids the need to know \f(CW$cpt\fR\*(Aqs package name.
243 .SS "reval (STRING, STRICT)"
244 .IX Subsection "reval (STRING, STRICT)"
245 This evaluates STRING as perl code inside the compartment.
246 .PP
247 The code can only see the compartment\*(Aqs namespace (as returned by the
248 \&\fBroot\fR method). The compartment\*(Aqs root package appears to be the
249 \&\f(CW\*(C`main::\*(C'\fR package to the code inside the compartment.
250 .PP
251 Any attempt by the code in STRING to use an operator which is not permitted
252 by the compartment will cause an error (at run\-time of the main program
253 but at compile\-time for the code in STRING). The error is of the form
254 "\*(Aq%s\*(Aq trapped by operation mask...".
255 .PP
256 If an operation is trapped in this way, then the code in STRING will
257 not be executed. If such a trapped operation occurs or any other
258 compile\-time or return error, then $@ is set to the error message, just
259 as with an \fBeval()\fR.
260 .PP
261 If there is no error, then the method returns the value of the last
262 expression evaluated, or a return statement may be used, just as with
263 subroutines and \fBeval()\fR. The context (list or scalar) is determined
264 by the caller as usual.
265 .PP
266 If the return value of \fBreval()\fR is (or contains) any code reference,
267 those code references are wrapped to be themselves executed always
268 in the compartment. See "wrap_code_refs_within".
269 .PP
270 The formerly undocumented STRICT argument sets strictness: if true
271 \&\*(Aquse strict;\*(Aq is used, otherwise it uses \*(Aqno strict;\*(Aq. \fBNote\fR: if
272 STRICT is omitted \*(Aqno strict;\*(Aq is the default.
273 .PP
274 Some points to note:
275 .PP
276 If the entereval op is permitted then the code can use eval "..." to
277 \&\*(Aqhide\*(Aq code which might use denied ops. This is not a major problem
278 since when the code tries to execute the eval it will fail because the
279 opmask is still in effect. However this technique would allow clever,
280 and possibly harmful, code to \*(Aqprobe\*(Aq the boundaries of what is
281 possible.
282 .PP
283 Any string eval which is executed by code executing in a compartment,
284 or by code called from code executing in a compartment, will be eval\*(Aqd
285 in the namespace of the compartment. This is potentially a serious
286 problem.
287 .PP
288 Consider a function \fBfoo()\fR in package pkg compiled outside a compartment
289 but shared with it. Assume the compartment has a root package called
290 \&\*(AqRoot\*(Aq. If \fBfoo()\fR contains an eval statement like eval \*(Aq$foo = 1\*(Aq then,
291 normally, \f(CW$pkg::foo\fR will be set to 1. If \fBfoo()\fR is called from the
292 compartment (by whatever means) then instead of setting \f(CW$pkg::foo\fR, the
293 eval will actually set \f(CW$Root::pkg::foo\fR.
294 .PP
295 This can easily be demonstrated by using a module, such as the Socket
296 module, which uses eval "..." as part of an AUTOLOAD function. You can
297 \&\*(Aquse\*(Aq the module outside the compartment and share an (autoloaded)
298 function with the compartment. If an autoload is triggered by code in
299 the compartment, or by any code anywhere that is called by any means
300 from the compartment, then the eval in the Socket module\*(Aqs AUTOLOAD
301 function happens in the namespace of the compartment. Any variables
302 created or used by the eval\*(Aqd code are now under the control of
303 the code in the compartment.
304 .PP
305 A similar effect applies to \fIall\fR runtime symbol lookups in code
306 called from a compartment but not compiled within it.
307 .SS "rdo (FILENAME)"
308 .IX Subsection "rdo (FILENAME)"
309 This evaluates the contents of file FILENAME inside the compartment.
310 It uses the same rules as perl\*(Aqs built\-in \f(CW\*(C`do\*(C'\fR to locate the file,
311 poossibly using \f(CW@INC\fR.
312 .PP
313 See above documentation on the \fBreval\fR method for further details.
314 .SS "root (NAMESPACE)"
315 .IX Subsection "root (NAMESPACE)"
316 This method returns the name of the package that is the root of the
317 compartment\*(Aqs namespace.
318 .PP
319 Note that this behaviour differs from version 1.00 of the Safe module
320 where the root module could be used to change the namespace. That
321 functionality has been withdrawn pending deeper consideration.
322 .SS "mask (MASK)"
323 .IX Subsection "mask (MASK)"
324 This is a get\-or\-set method for the compartment\*(Aqs operator mask.
325 .PP
326 With no MASK argument present, it returns the current operator mask of
327 the compartment.
328 .PP
329 With the MASK argument present, it sets the operator mask for the
330 compartment (equivalent to calling the deny_only method).
331 .SS "wrap_code_ref (CODEREF)"
332 .IX Subsection "wrap_code_ref (CODEREF)"
333 Returns a reference to an anonymous subroutine that, when executed, will call
334 CODEREF with the Safe compartment \*(Aqin effect\*(Aq. In other words, with the
335 package namespace adjusted and the opmask enabled.
336 .PP
337 Note that the opmask doesn\*(Aqt affect the already compiled code, it only affects
338 any \fIfurther\fR compilation that the already compiled code may try to perform.
339 .PP
340 This is particularly useful when applied to code references returned from \fBreval()\fR.
341 .PP
342 (It also provides a kind of workaround for RT#60374: "Safe.pm sort {} bug with
343 \&\-Dusethreads". See <https://rt.perl.org/rt3//Public/Bug/Display.html?id=60374>
344 for \fImuch\fR more detail.)
345 .SS "wrap_code_refs_within (...)"
346 .IX Subsection "wrap_code_refs_within (...)"
347 Wraps any CODE references found within the arguments by replacing each with the
348 result of calling "wrap_code_ref" on the CODE reference. Any ARRAY or HASH
349 references in the arguments are inspected recursively.
350 .PP
351 Returns nothing.
352 .SH RISKS
353 .IX Header "RISKS"
354 This section is just an outline of some of the things code in a compartment
355 might do (intentionally or unintentionally) which can have an effect outside
356 the compartment.
357 .IP Memory 8
358 .IX Item "Memory"
359 Consuming all (or nearly all) available memory.
360 .IP CPU 8
361 .IX Item "CPU"
362 Causing infinite loops etc.
363 .IP Snooping 8
364 .IX Item "Snooping"
365 Copying private information out of your system. Even something as
366 simple as your user name is of value to others. Much useful information
367 could be gleaned from your environment variables for example.
368 .IP Signals 8
369 .IX Item "Signals"
370 Causing signals (especially SIGFPE and SIGALARM) to affect your process.
371 .Sp
372 Setting up a signal handler will need to be carefully considered
373 and controlled. What mask is in effect when a signal handler
374 gets called? If a user can get an imported function to get an
375 exception and call the user\*(Aqs signal handler, does that user\*(Aqs
376 restricted mask get re\-instated before the handler is called?
377 Does an imported handler get called with its original mask or
378 the user\*(Aqs one?
379 .IP "State Changes" 8
380 .IX Item "State Changes"
381 Ops such as chdir obviously effect the process as a whole and not just
382 the code in the compartment. Ops such as rand and srand have a similar
383 but more subtle effect.
384 .SH AUTHOR
385 .IX Header "AUTHOR"
386 Originally designed and implemented by Malcolm Beattie.
387 .PP
388 Reworked to use the Opcode module and other changes added by Tim Bunce.
389 .PP
390 Currently maintained by the Perl 5 Porters, <perl5\[email protected]>.