Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perlsec.html
bloba62364f9983651f87515d34b683bac5ab0963452
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perlsec - Perl security</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perlsec - Perl security</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#laundering_and_detecting_tainted_data">Laundering and Detecting Tainted Data</a></li>
27 <li><a href="#switches_on_the____line">Switches On the ``#!'' Line</a></li>
28 <li><a href="#taint_mode_and__inc">Taint mode and @INC</a></li>
29 <li><a href="#cleaning_up_your_path">Cleaning Up Your Path</a></li>
30 <li><a href="#security_bugs">Security Bugs</a></li>
31 <li><a href="#protecting_your_programs">Protecting Your Programs</a></li>
32 <li><a href="#unicode">Unicode</a></li>
33 <li><a href="#algorithmic_complexity_attacks">Algorithmic Complexity Attacks</a></li>
34 </ul>
36 <li><a href="#see_also">SEE ALSO</a></li>
37 </ul>
38 <!-- INDEX END -->
40 <hr />
41 <p>
42 </p>
43 <h1><a name="name">NAME</a></h1>
44 <p>perlsec - Perl security</p>
45 <p>
46 </p>
47 <hr />
48 <h1><a name="description">DESCRIPTION</a></h1>
49 <p>Perl is designed to make it easy to program securely even when running
50 with extra privileges, like setuid or setgid programs. Unlike most
51 command line shells, which are based on multiple substitution passes on
52 each line of the script, Perl uses a more conventional evaluation scheme
53 with fewer hidden snags. Additionally, because the language has more
54 builtin functionality, it can rely less upon external (and possibly
55 untrustworthy) programs to accomplish its purposes.</p>
56 <p>Perl automatically enables a set of special security checks, called <em>taint
57 mode</em>, when it detects its program running with differing real and effective
58 user or group IDs. The setuid bit in Unix permissions is mode 04000, the
59 setgid bit mode 02000; either or both may be set. You can also enable taint
60 mode explicitly by using the <strong>-T</strong> command line flag. This flag is
61 <em>strongly</em> suggested for server programs and any program run on behalf of
62 someone else, such as a CGI script. Once taint mode is on, it's on for
63 the remainder of your script.</p>
64 <p>While in this mode, Perl takes special precautions called <em>taint
65 checks</em> to prevent both obvious and subtle traps. Some of these checks
66 are reasonably simple, such as verifying that path directories aren't
67 writable by others; careful programmers have always used checks like
68 these. Other checks, however, are best supported by the language itself,
69 and it is these checks especially that contribute to making a set-id Perl
70 program more secure than the corresponding C program.</p>
71 <p>You may not use data derived from outside your program to affect
72 something else outside your program--at least, not by accident. All
73 command line arguments, environment variables, locale information (see
74 <a href="file://C|\msysgit\mingw\html/pod/perllocale.html">the perllocale manpage</a>), results of certain system calls (<code>readdir()</code>,
75 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_readlink"><code>readlink()</code></a>, the variable of <code>shmread()</code>, the messages returned by
76 <code>msgrcv()</code>, the password, gcos and shell fields returned by the
77 <code>getpwxxx()</code> calls), and all file input are marked as ``tainted''.
78 Tainted data may not be used directly or indirectly in any command
79 that invokes a sub-shell, nor in any command that modifies files,
80 directories, or processes, <strong>with the following exceptions</strong>:</p>
81 <ul>
82 <li>
83 <p>Arguments to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print</code></a> and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_syswrite"><code>syswrite</code></a> are <strong>not</strong> checked for taintedness.</p>
84 </li>
85 <li>
86 <p>Symbolic methods</p>
87 <pre>
88 $obj-&gt;$method(@args);</pre>
89 <p>and symbolic sub references</p>
90 <pre>
91 &amp;{$foo}(@args);
92 $foo-&gt;(@args);</pre>
93 <p>are not checked for taintedness. This requires extra carefulness
94 unless you want external data to affect your control flow. Unless
95 you carefully limit what these symbolic values are, people are able
96 to call functions <strong>outside</strong> your Perl code, such as POSIX::system,
97 in which case they are able to run arbitrary external code.</p>
98 </li>
99 </ul>
100 <p>For efficiency reasons, Perl takes a conservative view of
101 whether data is tainted. If an expression contains tainted data,
102 any subexpression may be considered tainted, even if the value
103 of the subexpression is not itself affected by the tainted data.</p>
104 <p>Because taintedness is associated with each scalar value, some
105 elements of an array or hash can be tainted and others not.
106 The keys of a hash are never tainted.</p>
107 <p>For example:</p>
108 <pre>
109 $arg = shift; # $arg is tainted
110 $hid = $arg, 'bar'; # $hid is also tainted
111 $line = &lt;&gt;; # Tainted
112 $line = &lt;STDIN&gt;; # Also tainted
113 open FOO, &quot;/home/me/bar&quot; or die $!;
114 $line = &lt;FOO&gt;; # Still tainted
115 $path = $ENV{'PATH'}; # Tainted, but see below
116 $data = 'abc'; # Not tainted</pre>
117 <pre>
118 system &quot;echo $arg&quot;; # Insecure
119 system &quot;/bin/echo&quot;, $arg; # Considered insecure
120 # (Perl doesn't know about /bin/echo)
121 system &quot;echo $hid&quot;; # Insecure
122 system &quot;echo $data&quot;; # Insecure until PATH set</pre>
123 <pre>
124 $path = $ENV{'PATH'}; # $path now tainted</pre>
125 <pre>
126 $ENV{'PATH'} = '/bin:/usr/bin';
127 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};</pre>
128 <pre>
129 $path = $ENV{'PATH'}; # $path now NOT tainted
130 system &quot;echo $data&quot;; # Is secure now!</pre>
131 <pre>
132 open(FOO, &quot;&lt; $arg&quot;); # OK - read-only file
133 open(FOO, &quot;&gt; $arg&quot;); # Not OK - trying to write</pre>
134 <pre>
135 open(FOO,&quot;echo $arg|&quot;); # Not OK
136 open(FOO,&quot;-|&quot;)
137 or exec 'echo', $arg; # Also not OK</pre>
138 <pre>
139 $shout = `echo $arg`; # Insecure, $shout now tainted</pre>
140 <pre>
141 unlink $data, $arg; # Insecure
142 umask $arg; # Insecure</pre>
143 <pre>
144 exec &quot;echo $arg&quot;; # Insecure
145 exec &quot;echo&quot;, $arg; # Insecure
146 exec &quot;sh&quot;, '-c', $arg; # Very insecure!</pre>
147 <pre>
148 @files = &lt;*.c&gt;; # insecure (uses readdir() or similar)
149 @files = glob('*.c'); # insecure (uses readdir() or similar)</pre>
150 <pre>
151 # In Perl releases older than 5.6.0 the &lt;*.c&gt; and glob('*.c') would
152 # have used an external program to do the filename expansion; but in
153 # either case the result is tainted since the list of filenames comes
154 # from outside of the program.</pre>
155 <pre>
156 $bad = ($arg, 23); # $bad will be tainted
157 $arg, `true`; # Insecure (although it isn't really)</pre>
158 <p>If you try to do something insecure, you will get a fatal error saying
159 something like ``Insecure dependency'' or ``Insecure $ENV{PATH}''.</p>
160 <p>The exception to the principle of ``one tainted value taints the whole
161 expression'' is with the ternary conditional operator <code>?:</code>. Since code
162 with a ternary conditional</p>
163 <pre>
164 $result = $tainted_value ? &quot;Untainted&quot; : &quot;Also untainted&quot;;</pre>
165 <p>is effectively</p>
166 <pre>
167 if ( $tainted_value ) {
168 $result = &quot;Untainted&quot;;
169 } else {
170 $result = &quot;Also untainted&quot;;
171 }</pre>
172 <p>it doesn't make sense for <code>$result</code> to be tainted.</p>
174 </p>
175 <h2><a name="laundering_and_detecting_tainted_data">Laundering and Detecting Tainted Data</a></h2>
176 <p>To test whether a variable contains tainted data, and whose use would
177 thus trigger an ``Insecure dependency'' message, you can use the
178 <code>tainted()</code> function of the Scalar::Util module, available in your
179 nearby CPAN mirror, and included in Perl starting from the release 5.8.0.
180 Or you may be able to use the following <code>is_tainted()</code> function.</p>
181 <pre>
182 sub is_tainted {
183 return ! eval { eval(&quot;#&quot; . substr(join(&quot;&quot;, @_), 0, 0)); 1 };
184 }</pre>
185 <p>This function makes use of the fact that the presence of tainted data
186 anywhere within an expression renders the entire expression tainted. It
187 would be inefficient for every operator to test every argument for
188 taintedness. Instead, the slightly more efficient and conservative
189 approach is used that if any tainted value has been accessed within the
190 same expression, the whole expression is considered tainted.</p>
191 <p>But testing for taintedness gets you only so far. Sometimes you have just
192 to clear your data's taintedness. Values may be untainted by using them
193 as keys in a hash; otherwise the only way to bypass the tainting
194 mechanism is by referencing subpatterns from a regular expression match.
195 Perl presumes that if you reference a substring using $1, $2, etc., that
196 you knew what you were doing when you wrote the pattern. That means using
197 a bit of thought--don't just blindly untaint anything, or you defeat the
198 entire mechanism. It's better to verify that the variable has only good
199 characters (for certain values of ``good'') rather than checking whether it
200 has any bad characters. That's because it's far too easy to miss bad
201 characters that you never thought of.</p>
202 <p>Here's a test to make sure that the data contains nothing but ``word''
203 characters (alphabetics, numerics, and underscores), a hyphen, an at sign,
204 or a dot.</p>
205 <pre>
206 if ($data =~ /^([-\@\w.]+)$/) {
207 $data = $1; # $data now untainted
208 } else {
209 die &quot;Bad data in '$data'&quot;; # log this somewhere
210 }</pre>
211 <p>This is fairly secure because <code>/\w+/</code> doesn't normally match shell
212 metacharacters, nor are dot, dash, or at going to mean something special
213 to the shell. Use of <code>/.+/</code> would have been insecure in theory because
214 it lets everything through, but Perl doesn't check for that. The lesson
215 is that when untainting, you must be exceedingly careful with your patterns.
216 Laundering data using regular expression is the <em>only</em> mechanism for
217 untainting dirty data, unless you use the strategy detailed below to fork
218 a child of lesser privilege.</p>
219 <p>The example does not untaint <code>$data</code> if <code>use locale</code> is in effect,
220 because the characters matched by <code>\w</code> are determined by the locale.
221 Perl considers that locale definitions are untrustworthy because they
222 contain data from outside the program. If you are writing a
223 locale-aware program, and want to launder data with a regular expression
224 containing <code>\w</code>, put <code>no locale</code> ahead of the expression in the same
225 block. See <a href="file://C|\msysgit\mingw\html/pod/perllocale.html#security">SECURITY in the perllocale manpage</a> for further discussion and examples.</p>
227 </p>
228 <h2><a name="switches_on_the____line">Switches On the ``#!'' Line</a></h2>
229 <p>When you make a script executable, in order to make it usable as a
230 command, the system will pass switches to perl from the script's #!
231 line. Perl checks that any command line switches given to a setuid
232 (or setgid) script actually match the ones set on the #! line. Some
233 Unix and Unix-like environments impose a one-switch limit on the #!
234 line, so you may need to use something like <code>-wU</code> instead of <code>-w -U</code>
235 under such systems. (This issue should arise only in Unix or
236 Unix-like environments that support #! and setuid or setgid scripts.)</p>
238 </p>
239 <h2><a name="taint_mode_and__inc">Taint mode and @INC</a></h2>
240 <p>When the taint mode (<code>-T</code>) is in effect, the ``.'' directory is removed
241 from <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__inc"><code>@INC</code></a>, and the environment variables <code>PERL5LIB</code> and <code>PERLLIB</code>
242 are ignored by Perl. You can still adjust <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__inc"><code>@INC</code></a> from outside the
243 program by using the <code>-I</code> command line option as explained in
244 <a href="file://C|\msysgit\mingw\html/pod/perlrun.html">the perlrun manpage</a>. The two environment variables are ignored because
245 they are obscured, and a user running a program could be unaware that
246 they are set, whereas the <code>-I</code> option is clearly visible and
247 therefore permitted.</p>
248 <p>Another way to modify <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__inc"><code>@INC</code></a> without modifying the program, is to use
249 the <code>lib</code> pragma, e.g.:</p>
250 <pre>
251 perl -Mlib=/foo program</pre>
252 <p>The benefit of using <code>-Mlib=/foo</code> over <code>-I/foo</code>, is that the former
253 will automagically remove any duplicated directories, while the later
254 will not.</p>
255 <p>Note that if a tainted string is added to <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__inc"><code>@INC</code></a>, the following
256 problem will be reported:</p>
257 <pre>
258 Insecure dependency in require while running with -T switch</pre>
260 </p>
261 <h2><a name="cleaning_up_your_path">Cleaning Up Your Path</a></h2>
262 <p>For ``Insecure <code>$ENV{PATH}</code>'' messages, you need to set <code>$ENV{'PATH'}</code> to
263 a known value, and each directory in the path must be absolute and
264 non-writable by others than its owner and group. You may be surprised to
265 get this message even if the pathname to your executable is fully
266 qualified. This is <em>not</em> generated because you didn't supply a full path
267 to the program; instead, it's generated because you never set your PATH
268 environment variable, or you didn't set it to something that was safe.
269 Because Perl can't guarantee that the executable in question isn't itself
270 going to turn around and execute some other program that is dependent on
271 your PATH, it makes sure you set the PATH.</p>
272 <p>The PATH isn't the only environment variable which can cause problems.
273 Because some shells may use the variables IFS, CDPATH, ENV, and
274 BASH_ENV, Perl checks that those are either empty or untainted when
275 starting subprocesses. You may wish to add something like this to your
276 setid and taint-checking scripts.</p>
277 <pre>
278 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer</pre>
279 <p>It's also possible to get into trouble with other operations that don't
280 care whether they use tainted values. Make judicious use of the file
281 tests in dealing with any user-supplied filenames. When possible, do
282 opens and such <strong>after</strong> properly dropping any special user (or group!)
283 privileges. Perl doesn't prevent you from opening tainted filenames for reading,
284 so be careful what you print out. The tainting mechanism is intended to
285 prevent stupid mistakes, not to remove the need for thought.</p>
286 <p>Perl does not call the shell to expand wild cards when you pass <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system</code></a>
287 and <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec</code></a> explicit parameter lists instead of strings with possible shell
288 wildcards in them. Unfortunately, the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_glob"><code>glob</code></a>, and
289 backtick functions provide no such alternate calling convention, so more
290 subterfuge will be required.</p>
291 <p>Perl provides a reasonably safe way to open a file or pipe from a setuid
292 or setgid program: just create a child process with reduced privilege who
293 does the dirty work for you. First, fork a child using the special
294 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> syntax that connects the parent and child by a pipe. Now the
295 child resets its ID set and any other per-process attributes, like
296 environment variables, umasks, current working directories, back to the
297 originals or known safe values. Then the child process, which no longer
298 has any special permissions, does the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> or other system call.
299 Finally, the child passes the data it managed to access back to the
300 parent. Because the file or pipe was opened in the child while running
301 under less privilege than the parent, it's not apt to be tricked into
302 doing something it shouldn't.</p>
303 <p>Here's a way to do backticks reasonably safely. Notice how the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec</code></a> is
304 not called with a string that the shell could expand. This is by far the
305 best way to call something that might be subjected to shell escapes: just
306 never call the shell at all.</p>
307 <pre>
308 use English '-no_match_vars';
309 die &quot;Can't fork: $!&quot; unless defined($pid = open(KID, &quot;-|&quot;));
310 if ($pid) { # parent
311 while (&lt;KID&gt;) {
312 # do something
314 close KID;
315 } else {
316 my @temp = ($EUID, $EGID);
317 my $orig_uid = $UID;
318 my $orig_gid = $GID;
319 $EUID = $UID;
320 $EGID = $GID;
321 # Drop privileges
322 $UID = $orig_uid;
323 $GID = $orig_gid;
324 # Make sure privs are really gone
325 ($EUID, $EGID) = @temp;
326 die &quot;Can't drop privileges&quot;
327 unless $UID == $EUID &amp;&amp; $GID eq $EGID;
328 $ENV{PATH} = &quot;/bin:/usr/bin&quot;; # Minimal PATH.
329 # Consider sanitizing the environment even more.
330 exec 'myprog', 'arg1', 'arg2'
331 or die &quot;can't exec myprog: $!&quot;;
332 }</pre>
333 <p>A similar strategy would work for wildcard expansion via <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_glob"><code>glob</code></a>, although
334 you can use <code>readdir</code> instead.</p>
335 <p>Taint checking is most useful when although you trust yourself not to have
336 written a program to give away the farm, you don't necessarily trust those
337 who end up using it not to try to trick it into doing something bad. This
338 is the kind of security checking that's useful for set-id programs and
339 programs launched on someone else's behalf, like CGI programs.</p>
340 <p>This is quite different, however, from not even trusting the writer of the
341 code not to try to do something evil. That's the kind of trust needed
342 when someone hands you a program you've never seen before and says, ``Here,
343 run this.'' For that kind of safety, check out the Safe module,
344 included standard in the Perl distribution. This module allows the
345 programmer to set up special compartments in which all system operations
346 are trapped and namespace access is carefully controlled.</p>
348 </p>
349 <h2><a name="security_bugs">Security Bugs</a></h2>
350 <p>Beyond the obvious problems that stem from giving special privileges to
351 systems as flexible as scripts, on many versions of Unix, set-id scripts
352 are inherently insecure right from the start. The problem is a race
353 condition in the kernel. Between the time the kernel opens the file to
354 see which interpreter to run and when the (now-set-id) interpreter turns
355 around and reopens the file to interpret it, the file in question may have
356 changed, especially if you have symbolic links on your system.</p>
357 <p>Fortunately, sometimes this kernel ``feature'' can be disabled.
358 Unfortunately, there are two ways to disable it. The system can simply
359 outlaw scripts with any set-id bit set, which doesn't help much.
360 Alternately, it can simply ignore the set-id bits on scripts. If the
361 latter is true, Perl can emulate the setuid and setgid mechanism when it
362 notices the otherwise useless setuid/gid bits on Perl scripts. It does
363 this via a special executable called <em>suidperl</em> that is automatically
364 invoked for you if it's needed.</p>
365 <p>However, if the kernel set-id script feature isn't disabled, Perl will
366 complain loudly that your set-id script is insecure. You'll need to
367 either disable the kernel set-id script feature, or put a C wrapper around
368 the script. A C wrapper is just a compiled program that does nothing
369 except call your Perl program. Compiled programs are not subject to the
370 kernel bug that plagues set-id scripts. Here's a simple wrapper, written
371 in C:</p>
372 <pre>
373 #define REAL_PATH &quot;/path/to/script&quot;
374 main(ac, av)
375 char **av;
377 execv(REAL_PATH, av);
378 }</pre>
379 <p>Compile this wrapper into a binary executable and then make <em>it</em> rather
380 than your script setuid or setgid.</p>
381 <p>In recent years, vendors have begun to supply systems free of this
382 inherent security bug. On such systems, when the kernel passes the name
383 of the set-id script to open to the interpreter, rather than using a
384 pathname subject to meddling, it instead passes <em>/dev/fd/3</em>. This is a
385 special file already opened on the script, so that there can be no race
386 condition for evil scripts to exploit. On these systems, Perl should be
387 compiled with <code>-DSETUID_SCRIPTS_ARE_SECURE_NOW</code>. The <em>Configure</em>
388 program that builds Perl tries to figure this out for itself, so you
389 should never have to specify this yourself. Most modern releases of
390 SysVr4 and BSD 4.4 use this approach to avoid the kernel race condition.</p>
391 <p>Prior to release 5.6.1 of Perl, bugs in the code of <em>suidperl</em> could
392 introduce a security hole.</p>
394 </p>
395 <h2><a name="protecting_your_programs">Protecting Your Programs</a></h2>
396 <p>There are a number of ways to hide the source to your Perl programs,
397 with varying levels of ``security''.</p>
398 <p>First of all, however, you <em>can't</em> take away read permission, because
399 the source code has to be readable in order to be compiled and
400 interpreted. (That doesn't mean that a CGI script's source is
401 readable by people on the web, though.) So you have to leave the
402 permissions at the socially friendly 0755 level. This lets
403 people on your local system only see your source.</p>
404 <p>Some people mistakenly regard this as a security problem. If your program does
405 insecure things, and relies on people not knowing how to exploit those
406 insecurities, it is not secure. It is often possible for someone to
407 determine the insecure things and exploit them without viewing the
408 source. Security through obscurity, the name for hiding your bugs
409 instead of fixing them, is little security indeed.</p>
410 <p>You can try using encryption via source filters (Filter::* from CPAN,
411 or Filter::Util::Call and Filter::Simple since Perl 5.8).
412 But crackers might be able to decrypt it. You can try using the byte
413 code compiler and interpreter described below, but crackers might be
414 able to de-compile it. You can try using the native-code compiler
415 described below, but crackers might be able to disassemble it. These
416 pose varying degrees of difficulty to people wanting to get at your
417 code, but none can definitively conceal it (this is true of every
418 language, not just Perl).</p>
419 <p>If you're concerned about people profiting from your code, then the
420 bottom line is that nothing but a restrictive licence will give you
421 legal security. License your software and pepper it with threatening
422 statements like ``This is unpublished proprietary software of XYZ Corp.
423 Your access to it does not give you permission to use it blah blah
424 blah.'' You should see a lawyer to be sure your licence's wording will
425 stand up in court.</p>
427 </p>
428 <h2><a name="unicode">Unicode</a></h2>
429 <p>Unicode is a new and complex technology and one may easily overlook
430 certain security pitfalls. See <a href="file://C|\msysgit\mingw\html/pod/perluniintro.html">the perluniintro manpage</a> for an overview and
431 <a href="file://C|\msysgit\mingw\html/pod/perlunicode.html">the perlunicode manpage</a> for details, and <a href="file://C|\msysgit\mingw\html/pod/perlunicode.html#security_implications_of_unicode">Security Implications of Unicode in the perlunicode manpage</a> for security implications in particular.</p>
433 </p>
434 <h2><a name="algorithmic_complexity_attacks">Algorithmic Complexity Attacks</a></h2>
435 <p>Certain internal algorithms used in the implementation of Perl can
436 be attacked by choosing the input carefully to consume large amounts
437 of either time or space or both. This can lead into the so-called
438 <em>Denial of Service</em> (DoS) attacks.</p>
439 <ul>
440 <li>
441 <p>Hash Function - the algorithm used to ``order'' hash elements has been
442 changed several times during the development of Perl, mainly to be
443 reasonably fast. In Perl 5.8.1 also the security aspect was taken
444 into account.</p>
445 <p>In Perls before 5.8.1 one could rather easily generate data that as
446 hash keys would cause Perl to consume large amounts of time because
447 internal structure of hashes would badly degenerate. In Perl 5.8.1
448 the hash function is randomly perturbed by a pseudorandom seed which
449 makes generating such naughty hash keys harder.
450 See <a href="file://C|\msysgit\mingw\html/pod/perlrun.html#perl_hash_seed">PERL_HASH_SEED in the perlrun manpage</a> for more information.</p>
451 <p>The random perturbation is done by default but if one wants for some
452 reason emulate the old behaviour one can set the environment variable
453 PERL_HASH_SEED to zero (or any other integer). One possible reason
454 for wanting to emulate the old behaviour is that in the new behaviour
455 consecutive runs of Perl will order hash keys differently, which may
456 confuse some applications (like Data::Dumper: the outputs of two
457 different runs are no more identical).</p>
458 <p><strong>Perl has never guaranteed any ordering of the hash keys</strong>, and the
459 ordering has already changed several times during the lifetime of
460 Perl 5. Also, the ordering of hash keys has always been, and
461 continues to be, affected by the insertion order.</p>
462 <p>Also note that while the order of the hash elements might be
463 randomised, this ``pseudoordering'' should <strong>not</strong> be used for
464 applications like shuffling a list randomly (use List::Util::shuffle()
465 for that, see <a href="file://C|\msysgit\mingw\html/lib/List/Util.html">the List::Util manpage</a>, a standard core module since Perl 5.8.0;
466 or the CPAN module Algorithm::Numerical::Shuffle), or for generating
467 permutations (use e.g. the CPAN modules Algorithm::Permute or
468 Algorithm::FastPermute), or for any cryptographic applications.</p>
469 </li>
470 <li>
471 <p>Regular expressions - Perl's regular expression engine is so called
472 NFA (Non-Finite Automaton), which among other things means that it can
473 rather easily consume large amounts of both time and space if the
474 regular expression may match in several ways. Careful crafting of the
475 regular expressions can help but quite often there really isn't much
476 one can do (the book ``Mastering Regular Expressions'' is required
477 reading, see <a href="file://C|\msysgit\mingw\html/pod/perlfaq2.html">the perlfaq2 manpage</a>). Running out of space manifests itself by
478 Perl running out of memory.</p>
479 </li>
480 <li>
481 <p>Sorting - the quicksort algorithm used in Perls before 5.8.0 to
482 implement the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sort"><code>sort()</code></a> function is very easy to trick into misbehaving
483 so that it consumes a lot of time. Nothing more is required than
484 resorting a list already sorted. Starting from Perl 5.8.0 a different
485 sorting algorithm, mergesort, is used. Mergesort is insensitive to
486 its input data, so it cannot be similarly fooled.</p>
487 </li>
488 </ul>
489 <p>See <a href="http://www.cs.rice.edu/~scrosby/hash/">http://www.cs.rice.edu/~scrosby/hash/</a> for more information,
490 and any computer science text book on the algorithmic complexity.</p>
492 </p>
493 <hr />
494 <h1><a name="see_also">SEE ALSO</a></h1>
495 <p><a href="file://C|\msysgit\mingw\html/pod/perlrun.html">the perlrun manpage</a> for its description of cleaning up environment variables.</p>
496 <table border="0" width="100%" cellspacing="0" cellpadding="3">
497 <tr><td class="block" style="background-color: #cccccc" valign="middle">
498 <big><strong><span class="block">&nbsp;perlsec - Perl security</span></strong></big>
499 </td></tr>
500 </table>
502 </body>
504 </html>