Debian Codegraph @

upstream/opensuse-tumbleweed/man1/perlsec.1

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 "PERLSEC 1"
61 .TH PERLSEC 1 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 perlsec \- Perl security
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 Perl is designed to make it easy to program securely even when running
71 with extra privileges, like setuid or setgid programs. Unlike most
72 command line shells, which are based on multiple substitution passes on
73 each line of the script, Perl uses a more conventional evaluation scheme
74 with fewer hidden snags. Additionally, because the language has more
75 builtin functionality, it can rely less upon external (and possibly
76 untrustworthy) programs to accomplish its purposes.
77 .SH "SECURITY VULNERABILITY CONTACT INFORMATION"
78 .IX Header "SECURITY VULNERABILITY CONTACT INFORMATION"
79 If you believe you have found a security vulnerability in the Perl
80 interpreter or modules maintained in the core Perl codebase,
81 email the details to
82 perl\[email protected] <mailto:[email protected]>.
83 This address is a closed membership mailing list monitored by the Perl
84 security team.
85 .PP
86 See perlsecpolicy for additional information.
87 .SH "SECURITY MECHANISMS AND CONCERNS"
88 .IX Header "SECURITY MECHANISMS AND CONCERNS"
89 .SS "Taint mode"
90 .IX Subsection "Taint mode"
91 By default,
92 Perl automatically enables a set of special security checks, called \fItaint
93 mode\fR, when it detects its program running with differing real and effective
94 user or group IDs. The setuid bit in Unix permissions is mode 04000, the
95 setgid bit mode 02000; either or both may be set. You can also enable taint
96 mode explicitly by using the \fB\-T\fR command line flag. This flag is
97 \&\fIstrongly\fR suggested for server programs and any program run on behalf of
98 someone else, such as a CGI script. Once taint mode is on, it\*(Aqs on for
99 the remainder of your script.
100 .PP
101 While in this mode, Perl takes special precautions called \fItaint
102 checks\fR to prevent both obvious and subtle traps. Some of these checks
103 are reasonably simple, such as verifying that path directories aren\*(Aqt
104 writable by others; careful programmers have always used checks like
105 these. Other checks, however, are best supported by the language itself,
106 and it is these checks especially that contribute to making a set\-id Perl
107 program more secure than the corresponding C program.
108 .PP
109 Support for taint checks adds an overhead to all Perl programs,
110 whether or not you\*(Aqre using the taint features.
111 Perl 5.18 introduced C preprocessor symbols that can
112 be used to disable the taint features when building perl.
113 .PP
114 When taint is enabled,
115 you may not use data derived from outside your program to affect
116 something else outside your program\-\-at least, not by accident. All
117 command line arguments, environment variables, locale information (see
118 perllocale), results of certain system calls (\f(CWreaddir()\fR,
119 \&\f(CWreadlink()\fR, the variable of \f(CWshmread()\fR, the messages returned by
120 \&\f(CWmsgrcv()\fR, the password, gcos and shell fields returned by the
121 \&\f(CWgetpwxxx()\fR calls), and all file input are marked as "tainted".
122 Tainted data may not be used directly or indirectly in any command
123 that invokes a sub\-shell, nor in any command that modifies files,
124 directories, or processes, \fBwith the following exceptions\fR:
125 .IP \(bu 4
126 Arguments to \f(CW\*(C`print\*(C'\fR and \f(CW\*(C`syswrite\*(C'\fR are \fBnot\fR checked for taintedness.
127 .IP \(bu 4
128 Symbolic methods
129 .Sp
130 .Vb 1
131 \& $obj\->$method(@args);
132 .Ve
133 .Sp
134 and symbolic sub references
135 .Sp
136 .Vb 2
137 \& &{$foo}(@args);
138 \& $foo\->(@args);
139 .Ve
140 .Sp
141 are not checked for taintedness. This requires extra carefulness
142 unless you want external data to affect your control flow. Unless
143 you carefully limit what these symbolic values are, people are able
144 to call functions \fBoutside\fR your Perl code, such as POSIX::system,
145 in which case they are able to run arbitrary external code.
146 .IP \(bu 4
147 Hash keys are \fBnever\fR tainted.
148 .PP
149 For efficiency reasons, Perl takes a conservative view of
150 whether data is tainted. If an expression contains tainted data,
151 any subexpression may be considered tainted, even if the value
152 of the subexpression is not itself affected by the tainted data.
153 .PP
154 Because taintedness is associated with each scalar value, some
155 elements of an array or hash can be tainted and others not.
156 The keys of a hash are \fBnever\fR tainted.
157 .PP
158 For example:
159 .PP
160 .Vb 8
161 \& $arg = shift; # $arg is tainted
162 \& $hid = $arg . \*(Aqbar\*(Aq; # $hid is also tainted
163 \& $line = <>; # Tainted
164 \& $line = <STDIN>; # Also tainted
165 \& open FOO, "/home/me/bar" or die $!;
166 \& $line = <FOO>; # Still tainted
167 \& $path = $ENV{\*(AqPATH\*(Aq}; # Tainted, but see below
168 \& $data = \*(Aqabc\*(Aq; # Not tainted
169 \&
170 \& system "echo $arg"; # Insecure
171 \& system "/bin/echo", $arg; # Considered insecure
172 \& # (Perl doesn\*(Aqt know about /bin/echo)
173 \& system "echo $hid"; # Insecure
174 \& system "echo $data"; # Insecure until PATH set
175 \&
176 \& $path = $ENV{\*(AqPATH\*(Aq}; # $path now tainted
177 \&
178 \& $ENV{\*(AqPATH\*(Aq} = \*(Aq/bin:/usr/bin\*(Aq;
179 \& delete @ENV{\*(AqIFS\*(Aq, \*(AqCDPATH\*(Aq, \*(AqENV\*(Aq, \*(AqBASH_ENV\*(Aq};
180 \&
181 \& $path = $ENV{\*(AqPATH\*(Aq}; # $path now NOT tainted
182 \& system "echo $data"; # Is secure now!
183 \&
184 \& open(FOO, "< $arg"); # OK \- read\-only file
185 \& open(FOO, "> $arg"); # Not OK \- trying to write
186 \&
187 \& open(FOO,"echo $arg|"); # Not OK
188 \& open(FOO,"\-|")
189 \& or exec \*(Aqecho\*(Aq, $arg; # Also not OK
190 \&
191 \& $shout = \`echo $arg\`; # Insecure, $shout now tainted
192 \&
193 \& unlink $data, $arg; # Insecure
194 \& umask $arg; # Insecure
195 \&
196 \& exec "echo $arg"; # Insecure
197 \& exec "echo", $arg; # Insecure
198 \& exec "sh", \*(Aq\-c\*(Aq, $arg; # Very insecure!
199 \&
200 \& @files = <*.c>; # insecure (uses readdir() or similar)
201 \& @files = glob(\*(Aq*.c\*(Aq); # insecure (uses readdir() or similar)
202 \&
203 \& # In either case, the results of glob are tainted, since the list of
204 \& # filenames comes from outside of the program.
205 \&
206 \& $bad = ($arg, 23); # $bad will be tainted
207 \& $arg, \`true\`; # Insecure (although it isn\*(Aqt really)
208 .Ve
209 .PP
210 If you try to do something insecure, you will get a fatal error saying
211 something like "Insecure dependency" or "Insecure \f(CW$ENV\fR{PATH}".
212 .PP
213 The exception to the principle of "one tainted value taints the whole
214 expression" is with the ternary conditional operator \f(CW\*(C`?:\*(C'\fR. Since code
215 with a ternary conditional
216 .PP
217 .Vb 1
218 \& $result = $tainted_value ? "Untainted" : "Also untainted";
219 .Ve
220 .PP
221 is effectively
222 .PP
223 .Vb 5
224 \& if ( $tainted_value ) {
225 \& $result = "Untainted";
226 \& } else {
227 \& $result = "Also untainted";
228 \& }
229 .Ve
230 .PP
231 it doesn\*(Aqt make sense for \f(CW$result\fR to be tainted.
232 .SS "Laundering and Detecting Tainted Data"
233 .IX Subsection "Laundering and Detecting Tainted Data"
234 To test whether a variable contains tainted data, and whose use would
235 thus trigger an "Insecure dependency" message, you can use the
236 \&\f(CWtainted()\fR function of the Scalar::Util module, available in your
237 nearby CPAN mirror, and included in Perl starting from the release 5.8.0.
238 Or you may be able to use the following \f(CWis_tainted()\fR function.
239 .PP
240 .Vb 4
241 \& sub is_tainted {
242 \& local $@; # Don\*(Aqt pollute caller\*(Aqs value.
243 \& return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
244 \& }
245 .Ve
246 .PP
247 This function makes use of the fact that the presence of tainted data
248 anywhere within an expression renders the entire expression tainted. It
249 would be inefficient for every operator to test every argument for
250 taintedness. Instead, the slightly more efficient and conservative
251 approach is used that if any tainted value has been accessed within the
252 same expression, the whole expression is considered tainted.
253 .PP
254 But testing for taintedness gets you only so far. Sometimes you have just
255 to clear your data\*(Aqs taintedness. Values may be untainted by using them
256 as keys in a hash; otherwise the only way to bypass the tainting
257 mechanism is by referencing subpatterns from a regular expression match.
258 Perl presumes that if you reference a substring using \f(CW$1\fR, \f(CW$2\fR, etc. in a
259 non\-tainting pattern, that
260 you knew what you were doing when you wrote that pattern. That means using
261 a bit of thought\-\-don\*(Aqt just blindly untaint anything, or you defeat the
262 entire mechanism. It\*(Aqs better to verify that the variable has only good
263 characters (for certain values of "good") rather than checking whether it
264 has any bad characters. That\*(Aqs because it\*(Aqs far too easy to miss bad
265 characters that you never thought of.
266 .PP
267 Here\*(Aqs a test to make sure that the data contains nothing but "word"
268 characters (alphabetics, numerics, and underscores), a hyphen, an at sign,
269 or a dot.
270 .PP
271 .Vb 5
272 \& if ($data =~ /^([\-\e@\ew.]+)$/) {
273 \& $data = $1; # $data now untainted
274 \& } else {
275 \& die "Bad data in \*(Aq$data\*(Aq"; # log this somewhere
276 \& }
277 .Ve
278 .PP
279 This is fairly secure because \f(CW\*(C`/\ew+/\*(C'\fR doesn\*(Aqt normally match shell
280 metacharacters, nor are dot, dash, or at going to mean something special
281 to the shell. Use of \f(CW\*(C`/.+/\*(C'\fR would have been insecure in theory because
282 it lets everything through, but Perl doesn\*(Aqt check for that. The lesson
283 is that when untainting, you must be exceedingly careful with your patterns.
284 Laundering data using regular expression is the \fIonly\fR mechanism for
285 untainting dirty data, unless you use the strategy detailed below to fork
286 a child of lesser privilege.
287 .PP
288 The example does not untaint \f(CW$data\fR if \f(CW\*(C`use locale\*(C'\fR is in effect,
289 because the characters matched by \f(CW\*(C`\ew\*(C'\fR are determined by the locale.
290 Perl considers that locale definitions are untrustworthy because they
291 contain data from outside the program. If you are writing a
292 locale\-aware program, and want to launder data with a regular expression
293 containing \f(CW\*(C`\ew\*(C'\fR, put \f(CW\*(C`no locale\*(C'\fR ahead of the expression in the same
294 block. See "SECURITY" in perllocale for further discussion and examples.
295 .SS "Switches On the ""#!"" Line"
296 .IX Subsection "Switches On the ""#!"" Line"
297 When you make a script executable, in order to make it usable as a
298 command, the system will pass switches to perl from the script\*(Aqs #!
299 line. Perl checks that any command line switches given to a setuid
300 (or setgid) script actually match the ones set on the #! line. Some
301 Unix and Unix\-like environments impose a one\-switch limit on the #!
302 line, so you may need to use something like \f(CW\*(C`\-wU\*(C'\fR instead of \f(CW\*(C`\-w \-U\*(C'\fR
303 under such systems. (This issue should arise only in Unix or
304 Unix\-like environments that support #! and setuid or setgid scripts.)
305 .ie n .SS "Taint mode and @INC"
306 .el .SS "Taint mode and \f(CW@INC\fP"
307 .IX Subsection "Taint mode and @INC"
308 When taint mode (\f(CW\*(C`\-T\*(C'\fR) is in effect, the environment variables
309 \&\f(CW\*(C`PERL5LIB\*(C'\fR, \f(CW\*(C`PERLLIB\*(C'\fR, and \f(CW\*(C`PERL_USE_UNSAFE_INC\*(C'\fR
310 are ignored by Perl. You can still adjust \f(CW@INC\fR from outside the
311 program by using the \f(CW\*(C`\-I\*(C'\fR command line option as explained in
312 perlrun. The two environment variables are
313 ignored because they are obscured, and a user running a program could
314 be unaware that they are set, whereas the \f(CW\*(C`\-I\*(C'\fR option is clearly
315 visible and therefore permitted.
316 .PP
317 Another way to modify \f(CW@INC\fR without modifying the program, is to use
318 the \f(CW\*(C`lib\*(C'\fR pragma, e.g.:
319 .PP
320 .Vb 1
321 \& perl \-Mlib=/foo program
322 .Ve
323 .PP
324 The benefit of using \f(CW\*(C`\-Mlib=/foo\*(C'\fR over \f(CW\*(C`\-I/foo\*(C'\fR, is that the former
325 will automagically remove any duplicated directories, while the latter
326 will not.
327 .PP
328 Note that if a tainted string is added to \f(CW@INC\fR, the following
329 problem will be reported:
330 .PP
331 .Vb 1
332 \& Insecure dependency in require while running with \-T switch
333 .Ve
334 .PP
335 On versions of Perl before 5.26, activating taint mode will also remove
336 the current directory (".") from the default value of \f(CW@INC\fR. Since
337 version 5.26, the current directory isn\*(Aqt included in \f(CW@INC\fR by
338 default.
339 .SS "Cleaning Up Your Path"
340 .IX Subsection "Cleaning Up Your Path"
341 For "Insecure \f(CW$ENV{PATH}\fR" messages, you need to set \f(CW$ENV{\*(AqPATH\*(Aq}\fR to
342 a known value, and each directory in the path must be absolute and
343 non\-writable by others than its owner and group. Notice that, at least on
344 Unix\-like environments, an empty component of the PATH may be interpreted
345 as if it were \f(CW\*(C`.\*(C'\fR (the local directory), which will also trigger this
346 message. You may be surprised to get this message even if the pathname
347 to your executable is fully qualified. This is \fInot\fR generated because
348 you didn\*(Aqt supply a full path to the program; instead, it\*(Aqs generated
349 because you never set your PATH environment variable, or you didn\*(Aqt set
350 it to something that was safe. Because Perl can\*(Aqt guarantee that the
351 executable in question isn\*(Aqt itself going to turn around and execute some
352 other program that is dependent on your PATH, it makes sure you set the
353 PATH.
354 .PP
355 The PATH isn\*(Aqt the only environment variable which can cause problems.
356 Because some shells may use the variables IFS, CDPATH, ENV, and
357 BASH_ENV, Perl checks that those are either empty or untainted when
358 starting subprocesses. You may wish to add something like this to your
359 setid and taint\-checking scripts.
360 .PP
361 .Vb 1
362 \& delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer
363 .Ve
364 .PP
365 It\*(Aqs also possible to get into trouble with other operations that don\*(Aqt
366 care whether they use tainted values. Make judicious use of the file
367 tests in dealing with any user\-supplied filenames. When possible, do
368 opens and such \fBafter\fR properly dropping any special user (or group!)
369 privileges. Perl doesn\*(Aqt prevent you from
370 opening tainted filenames for reading,
371 so be careful what you print out. The tainting mechanism is intended to
372 prevent stupid mistakes, not to remove the need for thought.
373 .PP
374 Perl does not call the shell to expand wild cards when you pass \f(CW\*(C`system\*(C'\fR
375 and \f(CW\*(C`exec\*(C'\fR explicit parameter lists instead of strings with possible shell
376 wildcards in them. Unfortunately, the \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`glob\*(C'\fR, and
377 backtick functions provide no such alternate calling convention, so more
378 subterfuge will be required.
379 .PP
380 Perl provides a reasonably safe way to open a file or pipe from a setuid
381 or setgid program: just create a child process with reduced privilege who
382 does the dirty work for you. First, fork a child using the special
383 \&\f(CW\*(C`open\*(C'\fR syntax that connects the parent and child by a pipe. Now the
384 child resets its ID set and any other per\-process attributes, like
385 environment variables, umasks, current working directories, back to the
386 originals or known safe values. Then the child process, which no longer
387 has any special permissions, does the \f(CW\*(C`open\*(C'\fR or other system call.
388 Finally, the child passes the data it managed to access back to the
389 parent. Because the file or pipe was opened in the child while running
390 under less privilege than the parent, it\*(Aqs not apt to be tricked into
391 doing something it shouldn\*(Aqt.
392 .PP
393 Here\*(Aqs a way to do backticks reasonably safely. Notice how the \f(CW\*(C`exec\*(C'\fR is
394 not called with a string that the shell could expand. This is by far the
395 best way to call something that might be subjected to shell escapes: just
396 never call the shell at all.
397 .PP
398 .Vb 10
399 \& use English;
400 \& die "Can\*(Aqt fork: $!" unless defined($pid = open(KID, "\-|"));
401 \& if ($pid) { # parent
402 \& while (<KID>) {
403 \& # do something
404 \& }
405 \& close KID;
406 \& } else {
407 \& my @temp = ($EUID, $EGID);
408 \& my $orig_uid = $UID;
409 \& my $orig_gid = $GID;
410 \& $EUID = $UID;
411 \& $EGID = $GID;
412 \& # Drop privileges
413 \& $UID = $orig_uid;
414 \& $GID = $orig_gid;
415 \& # Make sure privs are really gone
416 \& ($EUID, $EGID) = @temp;
417 \& die "Can\*(Aqt drop privileges"
418 \& unless $UID == $EUID && $GID eq $EGID;
419 \& $ENV{PATH} = "/bin:/usr/bin"; # Minimal PATH.
420 \& # Consider sanitizing the environment even more.
421 \& exec \*(Aqmyprog\*(Aq, \*(Aqarg1\*(Aq, \*(Aqarg2\*(Aq
422 \& or die "can\*(Aqt exec myprog: $!";
423 \& }
424 .Ve
425 .PP
426 A similar strategy would work for wildcard expansion via \f(CW\*(C`glob\*(C'\fR, although
427 you can use \f(CW\*(C`readdir\*(C'\fR instead.
428 .PP
429 Taint checking is most useful when although you trust yourself not to have
430 written a program to give away the farm, you don\*(Aqt necessarily trust those
431 who end up using it not to try to trick it into doing something bad. This
432 is the kind of security checking that\*(Aqs useful for set\-id programs and
433 programs launched on someone else\*(Aqs behalf, like CGI programs.
434 .PP
435 This is quite different, however, from not even trusting the writer of the
436 code not to try to do something evil. That\*(Aqs the kind of trust needed
437 when someone hands you a program you\*(Aqve never seen before and says, "Here,
438 run this." For that kind of safety, you might want to check out the Safe
439 module, included standard in the Perl distribution. This module allows the
440 programmer to set up special compartments in which all system operations
441 are trapped and namespace access is carefully controlled. Safe should
442 not be considered bullet\-proof, though: it will not prevent the foreign
443 code to set up infinite loops, allocate gigabytes of memory, or even
444 abusing perl bugs to make the host interpreter crash or behave in
445 unpredictable ways. In any case it\*(Aqs better avoided completely if you\*(Aqre
446 really concerned about security.
447 .SS "Shebang Race Condition"
448 .IX Subsection "Shebang Race Condition"
449 Beyond the obvious problems that stem from giving special privileges to
450 systems as flexible as scripts, on many versions of Unix, set\-id scripts
451 are inherently insecure right from the start. The problem is a race
452 condition in the kernel. Between the time the kernel opens the file to
453 see which interpreter to run and when the (now\-set\-id) interpreter turns
454 around and reopens the file to interpret it, the file in question may have
455 changed, especially if you have symbolic links on your system.
456 .PP
457 Some Unixes, especially more recent ones, are free of this
458 inherent security bug. On such systems, when the kernel passes the name
459 of the set\-id script to open to the interpreter, rather than using a
460 pathname subject to meddling, it instead passes \fI/dev/fd/3\fR. This is a
461 special file already opened on the script, so that there can be no race
462 condition for evil scripts to exploit. On these systems, Perl should be
463 compiled with \f(CW\*(C`\-DSETUID_SCRIPTS_ARE_SECURE_NOW\*(C'\fR. The \fIConfigure\fR
464 program that builds Perl tries to figure this out for itself, so you
465 should never have to specify this yourself. Most modern releases of
466 SysVr4 and BSD 4.4 use this approach to avoid the kernel race condition.
467 .PP
468 If you don\*(Aqt have the safe version of set\-id scripts, all is not lost.
469 Sometimes this kernel "feature" can be disabled, so that the kernel
470 either doesn\*(Aqt run set\-id scripts with the set\-id or doesn\*(Aqt run them
471 at all. Either way avoids the exploitability of the race condition,
472 but doesn\*(Aqt help in actually running scripts set\-id.
473 .PP
474 If the kernel set\-id script feature isn\*(Aqt disabled, then any set\-id
475 script provides an exploitable vulnerability. Perl can\*(Aqt avoid being
476 exploitable, but will point out vulnerable scripts where it can. If Perl
477 detects that it is being applied to a set\-id script then it will complain
478 loudly that your set\-id script is insecure, and won\*(Aqt run it. When Perl
479 complains, you need to remove the set\-id bit from the script to eliminate
480 the vulnerability. Refusing to run the script doesn\*(Aqt in itself close
481 the vulnerability; it is just Perl\*(Aqs way of encouraging you to do this.
482 .PP
483 To actually run a script set\-id, if you don\*(Aqt have the safe version of
484 set\-id scripts, you\*(Aqll need to put a C wrapper around
485 the script. A C wrapper is just a compiled program that does nothing
486 except call your Perl program. Compiled programs are not subject to the
487 kernel bug that plagues set\-id scripts. Here\*(Aqs a simple wrapper, written
488 in C:
489 .PP
490 .Vb 4
491 \& #include <unistd.h>
492 \& #include <stdio.h>
493 \& #include <string.h>
494 \& #include <errno.h>
495 \&
496 \& #define REAL_PATH "/path/to/script"
497 \&
498 \& int main(int argc, char **argv)
499 \& {
500 \& execv(REAL_PATH, argv);
501 \& fprintf(stderr, "%s: %s: %s\en",
502 \& argv[0], REAL_PATH, strerror(errno));
503 \& return 127;
504 \& }
505 .Ve
506 .PP
507 Compile this wrapper into a binary executable and then make \fIit\fR rather
508 than your script setuid or setgid. Note that this wrapper isn\*(Aqt doing
509 anything to sanitise the execution environment other than ensuring
510 that a safe path to the script is used. It only avoids the shebang
511 race condition. It relies on Perl\*(Aqs own features, and on the script
512 itself being careful, to make it safe enough to run the script set\-id.
513 .SS "Protecting Your Programs"
514 .IX Subsection "Protecting Your Programs"
515 There are a number of ways to hide the source to your Perl programs,
516 with varying levels of "security".
517 .PP
518 First of all, however, you \fIcan\*(Aqt\fR take away read permission, because
519 the source code has to be readable in order to be compiled and
520 interpreted. (That doesn\*(Aqt mean that a CGI script\*(Aqs source is
521 readable by people on the web, though.) So you have to leave the
522 permissions at the socially friendly 0755 level. This lets
523 people on your local system only see your source.
524 .PP
525 Some people mistakenly regard this as a security problem. If your program does
526 insecure things, and relies on people not knowing how to exploit those
527 insecurities, it is not secure. It is often possible for someone to
528 determine the insecure things and exploit them without viewing the
529 source. Security through obscurity, the name for hiding your bugs
530 instead of fixing them, is little security indeed.
531 .PP
532 You can try using encryption via source filters (Filter::* from CPAN,
533 or Filter::Util::Call and Filter::Simple since Perl 5.8).
534 But crackers might be able to decrypt it. You can try using the byte
535 code compiler and interpreter described below, but crackers might be
536 able to de\-compile it. You can try using the native\-code compiler
537 described below, but crackers might be able to disassemble it. These
538 pose varying degrees of difficulty to people wanting to get at your
539 code, but none can definitively conceal it (this is true of every
540 language, not just Perl).
541 .PP
542 If you\*(Aqre concerned about people profiting from your code, then the
543 bottom line is that nothing but a restrictive license will give you
544 legal security. License your software and pepper it with threatening
545 statements like "This is unpublished proprietary software of XYZ Corp.
546 Your access to it does not give you permission to use it blah blah
547 blah." You should see a lawyer to be sure your license\*(Aqs wording will
548 stand up in court.
549 .SS Unicode
550 .IX Subsection "Unicode"
551 Unicode is a new and complex technology and one may easily overlook
552 certain security pitfalls. See perluniintro for an overview and
553 perlunicode for details, and "Security Implications
554 of Unicode" in perlunicode for security implications in particular.
555 .SS "Algorithmic Complexity Attacks"
556 .IX Subsection "Algorithmic Complexity Attacks"
557 Certain internal algorithms used in the implementation of Perl can
558 be attacked by choosing the input carefully to consume large amounts
559 of either time or space or both. This can lead into the so\-called
560 \&\fIDenial of Service\fR (DoS) attacks.
561 .IP \(bu 4
562 Hash Algorithm \- Hash algorithms like the one used in Perl are well
563 known to be vulnerable to collision attacks on their hash function.
564 Such attacks involve constructing a set of keys which collide into
565 the same bucket producing inefficient behavior. Such attacks often
566 depend on discovering the seed of the hash function used to map the
567 keys to buckets. That seed is then used to brute\-force a key set which
568 can be used to mount a denial of service attack. In Perl 5.8.1 changes
569 were introduced to harden Perl to such attacks, and then later in
570 Perl 5.18.0 these features were enhanced and additional protections
571 added.
572 .Sp
573 At the time of this writing, Perl 5.18.0 is considered to be
574 well\-hardened against algorithmic complexity attacks on its hash
575 implementation. This is largely owed to the following measures
576 mitigate attacks:
577 .RS 4
578 .IP "Hash Seed Randomization" 4
579 .IX Item "Hash Seed Randomization"
580 In order to make it impossible to know what seed to generate an attack
581 key set for, this seed is randomly initialized at process start. This
582 may be overridden by using the PERL_HASH_SEED environment variable, see
583 "PERL_HASH_SEED" in perlrun. This environment variable controls how
584 items are actually stored, not how they are presented via
585 \&\f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`each\*(C'\fR.
586 .IP "Hash Traversal Randomization" 4
587 .IX Item "Hash Traversal Randomization"
588 Independent of which seed is used in the hash function, \f(CW\*(C`keys\*(C'\fR,
589 \&\f(CW\*(C`values\*(C'\fR, and \f(CW\*(C`each\*(C'\fR return items in a per\-hash randomized order.
590 Modifying a hash by insertion will change the iteration order of that hash.
591 This behavior can be overridden by using \f(CWhash_traversal_mask()\fR from
592 Hash::Util or by using the PERL_PERTURB_KEYS environment variable,
593 see "PERL_PERTURB_KEYS" in perlrun. Note that this feature controls the
594 "visible" order of the keys, and not the actual order they are stored in.
595 .IP "Bucket Order Perturbance" 4
596 .IX Item "Bucket Order Perturbance"
597 When items collide into a given hash bucket the order they are stored in
598 the chain is no longer predictable in Perl 5.18. This
599 has the intention to make it harder to observe a
600 collision. This behavior can be overridden by using
601 the PERL_PERTURB_KEYS environment variable, see "PERL_PERTURB_KEYS" in perlrun.
602 .IP "New Default Hash Function" 4
603 .IX Item "New Default Hash Function"
604 The default hash function has been modified with the intention of making
605 it harder to infer the hash seed.
606 .IP "Alternative Hash Functions" 4
607 .IX Item "Alternative Hash Functions"
608 The source code includes multiple hash algorithms to choose from. While we
609 believe that the default perl hash is robust to attack, we have included the
610 hash function Siphash as a fall\-back option. At the time of release of
611 Perl 5.18.0 Siphash is believed to be of cryptographic strength. This is
612 not the default as it is much slower than the default hash.
613 .RE
614 .RS 4
615 .Sp
616 Without compiling a special Perl, there is no way to get the exact same
617 behavior of any versions prior to Perl 5.18.0. The closest one can get
618 is by setting PERL_PERTURB_KEYS to 0 and setting the PERL_HASH_SEED
619 to a known value. We do not advise those settings for production use
620 due to the above security considerations.
621 .Sp
622 \&\fBPerl has never guaranteed any ordering of the hash keys\fR, and
623 the ordering has already changed several times during the lifetime of
624 Perl 5. Also, the ordering of hash keys has always been, and continues
625 to be, affected by the insertion order and the history of changes made
626 to the hash over its lifetime.
627 .Sp
628 Also note that while the order of the hash elements might be
629 randomized, this "pseudo\-ordering" should \fBnot\fR be used for
630 applications like shuffling a list randomly (use \f(CWList::Util::shuffle()\fR
631 for that, see List::Util, a standard core module since Perl 5.8.0;
632 or the CPAN module \f(CW\*(C`Algorithm::Numerical::Shuffle\*(C'\fR), or for generating
633 permutations (use e.g. the CPAN modules \f(CW\*(C`Algorithm::Permute\*(C'\fR or
634 \&\f(CW\*(C`Algorithm::FastPermute\*(C'\fR), or for any cryptographic applications.
635 .Sp
636 Tied hashes may have their own ordering and algorithmic complexity
637 attacks.
638 .RE
639 .IP \(bu 4
640 Regular expressions \- Perl\*(Aqs regular expression engine is so called NFA
641 (Non\-deterministic Finite Automaton), which among other things means that
642 it can rather easily consume large amounts of both time and space if the
643 regular expression may match in several ways. Careful crafting of the
644 regular expressions can help but quite often there really isn\*(Aqt much
645 one can do (the book "Mastering Regular Expressions" is required
646 reading, see perlfaq2). Running out of space manifests itself by
647 Perl running out of memory.
648 .IP \(bu 4
649 Sorting \- the quicksort algorithm used in Perls before 5.8.0 to
650 implement the \fBsort()\fR function was very easy to trick into misbehaving
651 so that it consumes a lot of time. Starting from Perl 5.8.0 a different
652 sorting algorithm, mergesort, is used by default. Mergesort cannot
653 misbehave on any input.
654 .PP
655 See <https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf> for more information,
656 and any computer science textbook on algorithmic complexity.
657 .SS "Using Sudo"
658 .IX Subsection "Using Sudo"
659 The popular tool \f(CW\*(C`sudo\*(C'\fR provides a controlled way for users to be able
660 to run programs as other users. It sanitises the execution environment
661 to some extent, and will avoid the shebang race condition. If you don\*(Aqt have the safe version of set\-id scripts,
662 then \f(CW\*(C`sudo\*(C'\fR may be a more convenient way of executing a script as
663 another user than writing a C wrapper would be.
664 .PP
665 However, \f(CW\*(C`sudo\*(C'\fR sets the real user or group ID to that of the target
666 identity, not just the effective ID as set\-id bits do. As a result, Perl
667 can\*(Aqt detect that it is running under \f(CW\*(C`sudo\*(C'\fR, and so won\*(Aqt automatically
668 take its own security precautions such as turning on taint mode. Where
669 \&\f(CW\*(C`sudo\*(C'\fR configuration dictates exactly which command can be run, the
670 approved command may include a \f(CW\*(C`\-T\*(C'\fR option to perl to enable taint mode.
671 .PP
672 In general, it is necessary to evaluate the suitability of a script to
673 run under \f(CW\*(C`sudo\*(C'\fR specifically with that kind of execution environment
674 in mind. It is neither necessary nor sufficient for the same script to
675 be suitable to run in a traditional set\-id arrangement, though many of
676 the issues overlap.
677 .SH "SEE ALSO"
678 .IX Header "SEE ALSO"
679 "ENVIRONMENT" in perlrun for its description of cleaning up environment
680 variables.