Debian Codegraph @

upstream/debian-unstable/man3/Safe.3perl

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