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