Debian Codegraph @

upstream/debian-unstable/man1/perlsec.1

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