File: perlsec.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (892 lines) | stat: -rw-r--r-- 59,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlsec - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlsec</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlsec
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlsec</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#SECURITY-VULNERABILITY-CONTACT-INFORMATION">SECURITY VULNERABILITY CONTACT INFORMATION</a><li><a href="#SECURITY-MECHANISMS-AND-CONCERNS">SECURITY MECHANISMS AND CONCERNS</a><ul><li><a href="#Taint-mode">Taint mode</a><li><a href="#Laundering-and-Detecting-Tainted-Data">Laundering and Detecting Tainted Data</a><li><a href="#Switches-On-the-%22%23!%22-Line">Switches On the "#!" Line</a><li><a href="#Taint-mode-and-%40INC">Taint mode and @INC</a><li><a href="#Cleaning-Up-Your-Path">Cleaning Up Your Path</a><li><a href="#Security-Bugs">Security Bugs</a><li><a href="#Protecting-Your-Programs">Protecting Your Programs</a><li><a href="#Unicode">Unicode</a><li><a href="#Algorithmic-Complexity-Attacks">Algorithmic Complexity Attacks</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlsec - Perl security</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>Perl is designed to make it easy to program securely even when running
with extra privileges, like setuid or setgid programs.  Unlike most
command line shells, which are based on multiple substitution passes on
each line of the script, Perl uses a more conventional evaluation scheme
with fewer hidden snags.  Additionally, because the language has more
builtin functionality, it can rely less upon external (and possibly
untrustworthy) programs to accomplish its purposes.</p>
<a name="SECURITY-VULNERABILITY-CONTACT-INFORMATION"></a><h1>SECURITY VULNERABILITY CONTACT INFORMATION</h1>
<p>If you believe you have found a security vulnerability in Perl, please
email the details to perl5-security-report@perl.org. This creates a new
Request Tracker ticket in a special queue which isn't initially publicly
accessible. The email will also be copied to a closed subscription
unarchived mailing list which includes all the core committers, who will
be able to help assess the impact of issues, figure out a resolution, and
help co-ordinate the release of patches to mitigate or fix the problem
across all platforms on which Perl is supported. Please only use this
address for security issues in the Perl core, not for modules
independently distributed on CPAN.</p>
<p>When sending an initial request to the security email address, please
don't Cc any other parties, because if they reply to all, the reply will
generate yet another new ticket. Once you have received an initial reply
with a <code class="inline"><span class="s">[</span><span class="w">perl</span> <span class="c">#NNNNNN]</span></code>
 ticket number in  the headline, it's okay to Cc
subsequent replies to third parties: all emails to the
perl5-security-report address with the ticket number in the subject line
will be added to the ticket; without it, a new ticket will be created.</p>
<a name="SECURITY-MECHANISMS-AND-CONCERNS"></a><h1>SECURITY MECHANISMS AND CONCERNS</h1>
<a name="Taint-mode"></a><h2>Taint mode</h2>
<p>Perl automatically enables a set of special security checks, called <i>taint
mode</i>, when it detects its program running with differing real and effective
user or group IDs.  The setuid bit in Unix permissions is mode 04000, the
setgid bit mode 02000; either or both may be set.  You can also enable taint
mode explicitly by using the <b>-T</b> command line flag.  This flag is
<i>strongly</i> suggested for server programs and any program run on behalf of
someone else, such as a CGI script.  Once taint mode is on, it's on for
the remainder of your script.</p>
<p>While in this mode, Perl takes special precautions called <i>taint
checks</i> to prevent both obvious and subtle traps.  Some of these checks
are reasonably simple, such as verifying that path directories aren't
writable by others; careful programmers have always used checks like
these.  Other checks, however, are best supported by the language itself,
and it is these checks especially that contribute to making a set-id Perl
program more secure than the corresponding C program.</p>
<p>You may not use data derived from outside your program to affect
something else outside your program--at least, not by accident.  All
command line arguments, environment variables, locale information (see
<a href="perllocale.html">perllocale</a>), results of certain system calls (<code class="inline"><a class="l_k" href="functions/readdir.html">readdir()</a></code>,
<code class="inline"><a class="l_k" href="functions/readlink.html">readlink()</a></code>, the variable of <code class="inline"><a class="l_k" href="functions/shmread.html">shmread()</a></code>, the messages returned by
<code class="inline"><a class="l_k" href="functions/msgrcv.html">msgrcv()</a></code>, the password, gcos and shell fields returned by the
<code class="inline"><span class="i">getpwxxx</span><span class="s">(</span><span class="s">)</span></code>
 calls), and all file input are marked as "tainted".
Tainted data may not be used directly or indirectly in any command
that invokes a sub-shell, nor in any command that modifies files,
directories, or processes, <b>with the following exceptions</b>:</p>
<ul>
<li>
<p>Arguments to <code class="inline"><a class="l_k" href="functions/print.html">print</a></code> and <code class="inline"><a class="l_k" href="functions/syswrite.html">syswrite</a></code> are <b>not</b> checked for taintedness.</p>
</li>
<li>
<p>Symbolic methods</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span><span class="i">-&gt;$method</span><span class="s">(</span><span class="i">@args</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>and symbolic sub references</p>
<pre class="verbatim"><ol><li>    <span class="i">&amp;</span>{<span class="i">$foo</span>}<span class="s">(</span><span class="i">@args</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$foo</span>-&gt;<span class="s">(</span><span class="i">@args</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>are not checked for taintedness.  This requires extra carefulness
unless you want external data to affect your control flow.  Unless
you carefully limit what these symbolic values are, people are able
to call functions <b>outside</b> your Perl code, such as POSIX::system,
in which case they are able to run arbitrary external code.</p>
</li>
<li>
<p>Hash keys are <b>never</b> tainted.</p>
</li>
</ul>
<p>For efficiency reasons, Perl takes a conservative view of
whether data is tainted.  If an expression contains tainted data,
any subexpression may be considered tainted, even if the value
of the subexpression is not itself affected by the tainted data.</p>
<p>Because taintedness is associated with each scalar value, some
elements of an array or hash can be tainted and others not.
The keys of a hash are <b>never</b> tainted.</p>
<p>For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$arg</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span>		<span class="c"># $arg is tainted</span></li><li>    <span class="i">$hid</span> = <span class="i">$arg</span> . <span class="q">&#39;bar&#39;</span><span class="sc">;</span>	<span class="c"># $hid is also tainted</span></li><li>    <span class="i">$line</span> = &lt;&gt;<span class="sc">;</span>			<span class="c"># Tainted</span></li><li>    <span class="i">$line</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span>		<span class="c"># Also tainted</span></li><li>    <a class="l_k" href="functions/open.html">open</a> <span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;/home/me/bar&quot;</span> <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li><li>    <span class="i">$line</span> = <span class="q">&lt;FOO&gt;</span><span class="sc">;</span>		<span class="c"># Still tainted</span></li><li>    <span class="i">$path</span> = <span class="i">$ENV</span>{<span class="q">&#39;PATH&#39;</span>}<span class="sc">;</span>	<span class="c"># Tainted, but see below</span></li><li>    <span class="i">$data</span> = <span class="q">&#39;abc&#39;</span><span class="sc">;</span>		<span class="c"># Not tainted</span></li><li></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;echo $arg&quot;</span><span class="sc">;</span>		<span class="c"># Insecure</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;/bin/echo&quot;</span><span class="cm">,</span> <span class="i">$arg</span><span class="sc">;</span>	<span class="c"># Considered insecure</span></li><li>				<span class="c"># (Perl doesn&#39;t know about /bin/echo)</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;echo $hid&quot;</span><span class="sc">;</span>		<span class="c"># Insecure</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;echo $data&quot;</span><span class="sc">;</span>	<span class="c"># Insecure until PATH set</span></li><li></li><li>    <span class="i">$path</span> = <span class="i">$ENV</span>{<span class="q">&#39;PATH&#39;</span>}<span class="sc">;</span>	<span class="c"># $path now tainted</span></li><li></li><li>    <span class="i">$ENV</span>{<span class="q">&#39;PATH&#39;</span>} = <span class="q">&#39;/bin:/usr/bin&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/delete.html">delete</a> <span class="i">@ENV</span>{<span class="q">&#39;IFS&#39;</span><span class="cm">,</span> <span class="q">&#39;CDPATH&#39;</span><span class="cm">,</span> <span class="q">&#39;ENV&#39;</span><span class="cm">,</span> <span class="q">&#39;BASH_ENV&#39;</span>}<span class="sc">;</span></li><li></li><li>    <span class="i">$path</span> = <span class="i">$ENV</span>{<span class="q">&#39;PATH&#39;</span>}<span class="sc">;</span>	<span class="c"># $path now NOT tainted</span></li><li>    <a class="l_k" href="functions/system.html">system</a> <span class="q">&quot;echo $data&quot;</span><span class="sc">;</span>	<span class="c"># Is secure now!</span></li><li></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $arg&quot;</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># OK - read-only file</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&gt; $arg&quot;</span><span class="s">)</span><span class="sc">;</span> 	<span class="c"># Not OK - trying to write</span></li><li></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span><span class="q">&quot;echo $arg|&quot;</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># Not OK</span></li><li>    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span><span class="q">&quot;-|&quot;</span><span class="s">)</span></li><li>	<a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/exec.html">exec</a> <span class="q">&#39;echo&#39;</span><span class="cm">,</span> <span class="i">$arg</span><span class="sc">;</span>	<span class="c"># Also not OK</span></li><li></li><li>    <span class="i">$shout</span> = <span class="q">`echo $arg`</span><span class="sc">;</span>	<span class="c"># Insecure, $shout now tainted</span></li><li></li><li>    <a class="l_k" href="functions/unlink.html">unlink</a> <span class="i">$data</span><span class="cm">,</span> <span class="i">$arg</span><span class="sc">;</span>		<span class="c"># Insecure</span></li><li>    <a class="l_k" href="functions/umask.html">umask</a> <span class="i">$arg</span><span class="sc">;</span>			<span class="c"># Insecure</span></li><li></li><li>    <a class="l_k" href="functions/exec.html">exec</a> <span class="q">&quot;echo $arg&quot;</span><span class="sc">;</span>		<span class="c"># Insecure</span></li><li>    <a class="l_k" href="functions/exec.html">exec</a> <span class="q">&quot;echo&quot;</span><span class="cm">,</span> <span class="i">$arg</span><span class="sc">;</span>		<span class="c"># Insecure</span></li><li>    <a class="l_k" href="functions/exec.html">exec</a> <span class="q">&quot;sh&quot;</span><span class="cm">,</span> <span class="q">&#39;-c&#39;</span><span class="cm">,</span> <span class="i">$arg</span><span class="sc">;</span>	<span class="c"># Very insecure!</span></li><li></li><li>    <span class="i">@files</span> = <span class="q">&lt;*.c&gt;</span><span class="sc">;</span>		<span class="c"># insecure (uses readdir() or similar)</span></li><li>    <span class="i">@files</span> = <a class="l_k" href="functions/glob.html">glob</a><span class="s">(</span><span class="q">&#39;*.c&#39;</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># insecure (uses readdir() or similar)</span></li><li></li><li>    <span class="c"># In either case, the results of glob are tainted, since the list of</span></li><li>    <span class="c"># filenames comes from outside of the program.</span></li><li></li><li>    <span class="i">$bad</span> = <span class="s">(</span><span class="i">$arg</span><span class="cm">,</span> <span class="n">23</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># $bad will be tainted</span></li><li>    <span class="i">$arg</span><span class="cm">,</span> <span class="q">`true`</span><span class="sc">;</span>		<span class="c"># Insecure (although it isn&#39;t really)</span></li></ol></pre><p>If you try to do something insecure, you will get a fatal error saying
something like "Insecure dependency" or "Insecure $ENV{PATH}".</p>
<p>The exception to the principle of "one tainted value taints the whole
expression" is with the ternary conditional operator <code class="inline">?:</code>.  Since code
with a ternary conditional</p>
<pre class="verbatim"><ol><li>    <span class="i">$result</span> = <span class="i">$tainted_value</span> ? <span class="q">&quot;Untainted&quot;</span> <span class="co">:</span> <span class="q">&quot;Also untainted&quot;</span><span class="sc">;</span></li></ol></pre><p>is effectively</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span> <span class="i">$tainted_value</span> <span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$result</span> = <span class="q">&quot;Untainted&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> else <span class="s">{</span></li><li>        <span class="i">$result</span> = <span class="q">&quot;Also untainted&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>it doesn't make sense for <code class="inline"><span class="i">$result</span></code>
 to be tainted.</p>
<a name="Laundering-and-Detecting-Tainted-Data"></a><h2>Laundering and Detecting Tainted Data</h2>
<p>To test whether a variable contains tainted data, and whose use would
thus trigger an "Insecure dependency" message, you can use the
<code class="inline"><span class="i">tainted</span><span class="s">(</span><span class="s">)</span></code>
 function of the Scalar::Util module, available in your
nearby CPAN mirror, and included in Perl starting from the release 5.8.0.
Or you may be able to use the following <code class="inline"><span class="i">is_tainted</span><span class="s">(</span><span class="s">)</span></code>
 function.</p>
<pre class="verbatim"><ol><li><a name="is_tainted"></a>    sub <span class="m">is_tainted</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/local.html">local</a> <span class="i">$@</span><span class="sc">;</span>   <span class="c"># Don&#39;t pollute caller&#39;s value.</span></li><li>        <a class="l_k" href="functions/return.html">return</a> ! <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <a class="l_k" href="functions/eval.html">eval</a><span class="s">(</span><span class="q">&quot;#&quot;</span> . <a class="l_k" href="functions/substr.html">substr</a><span class="s">(</span><a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">&quot;&quot;</span><span class="cm">,</span> <span class="i">@_</span><span class="s">)</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span> <span class="n">1</span> <span class="s">}</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>This function makes use of the fact that the presence of tainted data
anywhere within an expression renders the entire expression tainted.  It
would be inefficient for every operator to test every argument for
taintedness.  Instead, the slightly more efficient and conservative
approach is used that if any tainted value has been accessed within the
same expression, the whole expression is considered tainted.</p>
<p>But testing for taintedness gets you only so far.  Sometimes you have just
to clear your data's taintedness.  Values may be untainted by using them
as keys in a hash; otherwise the only way to bypass the tainting
mechanism is by referencing subpatterns from a regular expression match.
Perl presumes that if you reference a substring using $1, $2, etc. in a
non-tainting pattern, that
you knew what you were doing when you wrote that pattern.  That means using
a bit of thought--don't just blindly untaint anything, or you defeat the
entire mechanism.  It's better to verify that the variable has only good
characters (for certain values of "good") rather than checking whether it
has any bad characters.  That's because it's far too easy to miss bad
characters that you never thought of.</p>
<p>Here's a test to make sure that the data contains nothing but "word"
characters (alphabetics, numerics, and underscores), a hyphen, an at sign,
or a dot.</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$data</span> =~ <span class="q">/^([-\@\w.]+)$/</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$data</span> = <span class="i">$1</span><span class="sc">;</span> 			<span class="c"># $data now untainted</span></li><li>    <span class="s">}</span> else <span class="s">{</span></li><li>	<a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Bad data in &#39;$data&#39;&quot;</span><span class="sc">;</span> 	<span class="c"># log this somewhere</span></li><li>    <span class="s">}</span></li></ol></pre><p>This is fairly secure because <code class="inline"><span class="q">/\w+/</span></code>
 doesn't normally match shell
metacharacters, nor are dot, dash, or at going to mean something special
to the shell.  Use of <code class="inline"><span class="q">/.+/</span></code>
 would have been insecure in theory because
it lets everything through, but Perl doesn't check for that.  The lesson
is that when untainting, you must be exceedingly careful with your patterns.
Laundering data using regular expression is the <i>only</i> mechanism for
untainting dirty data, unless you use the strategy detailed below to fork
a child of lesser privilege.</p>
<p>The example does not untaint <code class="inline"><span class="i">$data</span></code>
 if <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect,
because the characters matched by <code class="inline">\<span class="w">w</span></code>
 are determined by the locale.
Perl considers that locale definitions are untrustworthy because they
contain data from outside the program.  If you are writing a
locale-aware program, and want to launder data with a regular expression
containing <code class="inline">\<span class="w">w</span></code>
, put <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">locale</span></code>
 ahead of the expression in the same
block.  See <a href="perllocale.html#SECURITY">SECURITY in perllocale</a> for further discussion and examples.</p>
<a name="Switches-On-the-%22%23!%22-Line"></a><h2>Switches On the "#!" Line</h2>
<p>When you make a script executable, in order to make it usable as a
command, the system will pass switches to perl from the script's #!
line.  Perl checks that any command line switches given to a setuid
(or setgid) script actually match the ones set on the #! line.  Some
Unix and Unix-like environments impose a one-switch limit on the #!
line, so you may need to use something like <code class="inline">-<span class="w">wU</span></code>
 instead of <code class="inline">-w -<span class="w">U</span></code>

under such systems.  (This issue should arise only in Unix or
Unix-like environments that support #! and setuid or setgid scripts.)</p>
<a name="Taint-mode-and-%40INC"></a><h2>Taint mode and @INC</h2>
<p>When the taint mode (<code class="inline">-T</code>
) is in effect, the "." directory is removed
from <code class="inline"><span class="i">@INC</span></code>
, and the environment variables <code class="inline"><span class="w">PERL5LIB</span></code>
 and <code class="inline"><span class="w">PERLLIB</span></code>

are ignored by Perl.  You can still adjust <code class="inline"><span class="i">@INC</span></code>
 from outside the
program by using the <code class="inline">-<span class="w">I</span></code>
 command line option as explained in
<a href="perlrun.html">perlrun</a>.  The two environment variables are ignored because
they are obscured, and a user running a program could be unaware that
they are set, whereas the <code class="inline">-<span class="w">I</span></code>
 option is clearly visible and
therefore permitted.</p>
<p>Another way to modify <code class="inline"><span class="i">@INC</span></code>
 without modifying the program, is to use
the <code class="inline"><span class="w">lib</span></code>
 pragma, e.g.:</p>
<pre class="verbatim"><ol><li>  perl -Mlib=/foo program</li></ol></pre><p>The benefit of using <code class="inline">-Mlib=/foo</code> over <code class="inline">-<span class="w">I</span>/<span class="w">foo</span></code>
, is that the former
will automagically remove any duplicated directories, while the latter
will not.</p>
<p>Note that if a tainted string is added to <code class="inline"><span class="i">@INC</span></code>
, the following
problem will be reported:</p>
<pre class="verbatim"><ol><li>  <span class="w">Insecure</span> <span class="w">dependency</span> <span class="w">in</span> <a class="l_k" href="functions/require.html">require</a> while <span class="w">running</span> <span class="w">with</span> -T switch</li></ol></pre><a name="Cleaning-Up-Your-Path"></a><h2>Cleaning Up Your Path</h2>
<p>For "Insecure <code class="inline"><span class="i">$ENV</span>{<span class="w">PATH</span>}</code>
" messages, you need to set <code class="inline"><span class="i">$ENV</span>{<span class="q">&#39;PATH&#39;</span>}</code>
 to
a known value, and each directory in the path must be absolute and
non-writable by others than its owner and group.  You may be surprised to
get this message even if the pathname to your executable is fully
qualified.  This is <i>not</i> generated because you didn't supply a full path
to the program; instead, it's generated because you never set your PATH
environment variable, or you didn't set it to something that was safe.
Because Perl can't guarantee that the executable in question isn't itself
going to turn around and execute some other program that is dependent on
your PATH, it makes sure you set the PATH.</p>
<p>The PATH isn't the only environment variable which can cause problems.
Because some shells may use the variables IFS, CDPATH, ENV, and
BASH_ENV, Perl checks that those are either empty or untainted when
starting subprocesses.  You may wish to add something like this to your
setid and taint-checking scripts.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/delete.html">delete</a> <span class="i">@ENV</span>{<span class="q">qw(IFS CDPATH ENV BASH_ENV)</span>}<span class="sc">;</span>   <span class="c"># Make %ENV safer</span></li></ol></pre><p>It's also possible to get into trouble with other operations that don't
care whether they use tainted values.  Make judicious use of the file
tests in dealing with any user-supplied filenames.  When possible, do
opens and such <b>after</b> properly dropping any special user (or group!)
privileges.  Perl doesn't prevent you from
opening tainted filenames for reading,
so be careful what you print out.  The tainting mechanism is intended to
prevent stupid mistakes, not to remove the need for thought.</p>
<p>Perl does not call the shell to expand wild cards when you pass <code class="inline"><a class="l_k" href="functions/system.html">system</a></code>
and <code class="inline"><a class="l_k" href="functions/exec.html">exec</a></code> explicit parameter lists instead of strings with possible shell
wildcards in them.  Unfortunately, the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>, <code class="inline"><a class="l_k" href="functions/glob.html">glob</a></code>, and
backtick functions provide no such alternate calling convention, so more
subterfuge will be required.</p>
<p>Perl provides a reasonably safe way to open a file or pipe from a setuid
or setgid program: just create a child process with reduced privilege who
does the dirty work for you.  First, fork a child using the special
<code class="inline"><a class="l_k" href="functions/open.html">open</a></code> syntax that connects the parent and child by a pipe.  Now the
child resets its ID set and any other per-process attributes, like
environment variables, umasks, current working directories, back to the
originals or known safe values.  Then the child process, which no longer
has any special permissions, does the <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> or other system call.
Finally, the child passes the data it managed to access back to the
parent.  Because the file or pipe was opened in the child while running
under less privilege than the parent, it's not apt to be tricked into
doing something it shouldn't.</p>
<p>Here's a way to do backticks reasonably safely.  Notice how the <code class="inline"><a class="l_k" href="functions/exec.html">exec</a></code> is
not called with a string that the shell could expand.  This is by far the
best way to call something that might be subjected to shell escapes: just
never call the shell at all.</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/use.html">use</a> <span class="w">English</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t fork: $!&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$pid</span> = <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">KID</span><span class="cm">,</span> <span class="q">&quot;-|&quot;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$pid</span><span class="s">)</span> <span class="s">{</span>           <span class="c"># parent</span></li><li>            <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="q">&lt;KID&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>                <span class="c"># do something</span></li><li>            <span class="s">}</span></li><li>            <a class="l_k" href="functions/close.html">close</a> <span class="w">KID</span><span class="sc">;</span></li><li>        <span class="s">}</span> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">@temp</span>     = <span class="s">(</span><span class="i">$EUID</span><span class="cm">,</span> <span class="i">$EGID</span><span class="s">)</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$orig_uid</span> = <span class="i">$UID</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/my.html">my</a> <span class="i">$orig_gid</span> = <span class="i">$GID</span><span class="sc">;</span></li><li>            <span class="i">$EUID</span> = <span class="i">$UID</span><span class="sc">;</span></li><li>            <span class="i">$EGID</span> = <span class="i">$GID</span><span class="sc">;</span></li><li>            <span class="c"># Drop privileges</span></li><li>            <span class="i">$UID</span>  = <span class="i">$orig_uid</span><span class="sc">;</span></li><li>            <span class="i">$GID</span>  = <span class="i">$orig_gid</span><span class="sc">;</span></li><li>            <span class="c"># Make sure privs are really gone</span></li><li>            <span class="s">(</span><span class="i">$EUID</span><span class="cm">,</span> <span class="i">$EGID</span><span class="s">)</span> = <span class="i">@temp</span><span class="sc">;</span></li><li>            <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t drop privileges&quot;</span></li><li>                <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$UID</span> == <span class="i">$EUID</span>  &amp;&amp; <span class="i">$GID</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="i">$EGID</span><span class="sc">;</span></li><li>            <span class="i">$ENV</span>{<span class="w">PATH</span>} = <span class="q">&quot;/bin:/usr/bin&quot;</span><span class="sc">;</span> <span class="c"># Minimal PATH.</span></li><li>	    <span class="c"># Consider sanitizing the environment even more.</span></li><li>            <a class="l_k" href="functions/exec.html">exec</a> <span class="q">&#39;myprog&#39;</span><span class="cm">,</span> <span class="q">&#39;arg1&#39;</span><span class="cm">,</span> <span class="q">&#39;arg2&#39;</span></li><li>                <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can&#39;t exec myprog: $!&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li></ol></pre><p>A similar strategy would work for wildcard expansion via <code class="inline"><a class="l_k" href="functions/glob.html">glob</a></code>, although
you can use <code class="inline"><a class="l_k" href="functions/readdir.html">readdir</a></code> instead.</p>
<p>Taint checking is most useful when although you trust yourself not to have
written a program to give away the farm, you don't necessarily trust those
who end up using it not to try to trick it into doing something bad.  This
is the kind of security checking that's useful for set-id programs and
programs launched on someone else's behalf, like CGI programs.</p>
<p>This is quite different, however, from not even trusting the writer of the
code not to try to do something evil.  That's the kind of trust needed
when someone hands you a program you've never seen before and says, "Here,
run this."  For that kind of safety, you might want to check out the Safe
module, included standard in the Perl distribution.  This module allows the
programmer to set up special compartments in which all system operations
are trapped and namespace access is carefully controlled.  Safe should
not be considered bullet-proof, though: it will not prevent the foreign
code to set up infinite loops, allocate gigabytes of memory, or even
abusing perl bugs to make the host interpreter crash or behave in
unpredictable ways.  In any case it's better avoided completely if you're
really concerned about security.</p>
<a name="Security-Bugs"></a><h2>Security Bugs</h2>
<p>Beyond the obvious problems that stem from giving special privileges to
systems as flexible as scripts, on many versions of Unix, set-id scripts
are inherently insecure right from the start.  The problem is a race
condition in the kernel.  Between the time the kernel opens the file to
see which interpreter to run and when the (now-set-id) interpreter turns
around and reopens the file to interpret it, the file in question may have
changed, especially if you have symbolic links on your system.</p>
<p>Fortunately, sometimes this kernel "feature" can be disabled.
Unfortunately, there are two ways to disable it.  The system can simply
outlaw scripts with any set-id bit set, which doesn't help much.
Alternately, it can simply ignore the set-id bits on scripts.</p>
<p>However, if the kernel set-id script feature isn't disabled, Perl will
complain loudly that your set-id script is insecure.  You'll need to
either disable the kernel set-id script feature, or put a C wrapper around
the script.  A C wrapper is just a compiled program that does nothing
except call your Perl program.   Compiled programs are not subject to the
kernel bug that plagues set-id scripts.  Here's a simple wrapper, written
in C:</p>
<pre class="verbatim"><ol><li>    <span class="c">#include &lt;unistd.h&gt;</span></li><li>    <span class="c">#include &lt;stdio.h&gt;</span></li><li>    <span class="c">#include &lt;string.h&gt;</span></li><li>    <span class="c">#include &lt;errno.h&gt;</span></li><li></li><li>    <span class="c">#define REAL_PATH &quot;/path/to/script&quot;</span></li><li></li><li>    <a class="l_k" href="functions/int.html">int</a> <span class="i">main</span><span class="s">(</span><a class="l_k" href="functions/int.html">int</a> <span class="w">argc</span><span class="cm">,</span> <span class="w">char</span> **<span class="w">argv</span><span class="s">)</span></li><li>    <span class="s">{</span></li><li>        <span class="i">execv</span><span class="s">(</span><span class="w">REAL_PATH</span><span class="cm">,</span> <span class="w">argv</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="i">fprintf</span><span class="s">(</span><span class="w">stderr</span><span class="cm">,</span> <span class="q">&quot;%s: %s: %s\n&quot;</span><span class="cm">,</span></li><li>                        <span class="w">argv</span><span class="s">[</span><span class="n">0</span><span class="s">]</span><span class="cm">,</span> <span class="w">REAL_PATH</span><span class="cm">,</span> <span class="i">strerror</span><span class="s">(</span><span class="w">errno</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/return.html">return</a> <span class="n">127</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Compile this wrapper into a binary executable and then make <i>it</i> rather
than your script setuid or setgid.</p>
<p>In recent years, vendors have begun to supply systems free of this
inherent security bug.  On such systems, when the kernel passes the name
of the set-id script to open to the interpreter, rather than using a
pathname subject to meddling, it instead passes <i>/dev/fd/3</i>.  This is a
special file already opened on the script, so that there can be no race
condition for evil scripts to exploit.  On these systems, Perl should be
compiled with <code class="inline">-<span class="w">DSETUID_SCRIPTS_ARE_SECURE_NOW</span></code>
.  The <i>Configure</i>
program that builds Perl tries to figure this out for itself, so you
should never have to specify this yourself.  Most modern releases of
SysVr4 and BSD 4.4 use this approach to avoid the kernel race condition.</p>
<a name="Protecting-Your-Programs"></a><h2>Protecting Your Programs</h2>
<p>There are a number of ways to hide the source to your Perl programs,
with varying levels of "security".</p>
<p>First of all, however, you <i>can't</i> take away read permission, because
the source code has to be readable in order to be compiled and
interpreted.  (That doesn't mean that a CGI script's source is
readable by people on the web, though.)  So you have to leave the
permissions at the socially friendly 0755 level.  This lets 
people on your local system only see your source.</p>
<p>Some people mistakenly regard this as a security problem.  If your program does
insecure things, and relies on people not knowing how to exploit those
insecurities, it is not secure.  It is often possible for someone to
determine the insecure things and exploit them without viewing the
source.  Security through obscurity, the name for hiding your bugs
instead of fixing them, is little security indeed.</p>
<p>You can try using encryption via source filters (Filter::* from CPAN,
or Filter::Util::Call and Filter::Simple since Perl 5.8).
But crackers might be able to decrypt it.  You can try using the byte
code compiler and interpreter described below, but crackers might be
able to de-compile it.  You can try using the native-code compiler
described below, but crackers might be able to disassemble it.  These
pose varying degrees of difficulty to people wanting to get at your
code, but none can definitively conceal it (this is true of every
language, not just Perl).</p>
<p>If you're concerned about people profiting from your code, then the
bottom line is that nothing but a restrictive license will give you
legal security.  License your software and pepper it with threatening
statements like "This is unpublished proprietary software of XYZ Corp.
Your access to it does not give you permission to use it blah blah
blah."  You should see a lawyer to be sure your license's wording will
stand up in court.</p>
<a name="Unicode"></a><h2>Unicode</h2>
<p>Unicode is a new and complex technology and one may easily overlook
certain security pitfalls.  See <a href="perluniintro.html">perluniintro</a> for an overview and
<a href="perlunicode.html">perlunicode</a> for details, and <a href="perlunicode.html#Security-Implications-of-Unicode">Security Implications of Unicode in perlunicode</a> for security implications in particular.</p>
<a name="Algorithmic-Complexity-Attacks"></a><h2>Algorithmic Complexity Attacks</h2>
<p>Certain internal algorithms used in the implementation of Perl can
be attacked by choosing the input carefully to consume large amounts
of either time or space or both.  This can lead into the so-called
<i>Denial of Service</i> (DoS) attacks.</p>
<ul>
<li>
<p>Hash Algorithm - Hash algorithms like the one used in Perl are well
known to be vulnerable to collision attacks on their hash function.
Such attacks involve constructing a set of keys which collide into
the same bucket producing inefficient behavior.  Such attacks often
depend on discovering the seed of the hash function used to map the
keys to buckets.  That seed is then used to brute-force a key set which
can be used to mount a denial of service attack.  In Perl 5.8.1 changes
were introduced to harden Perl to such attacks, and then later in
Perl 5.18.0 these features were enhanced and additional protections
added.</p>
<p>At the time of this writing, Perl 5.18.0 is considered to be
well-hardened against algorithmic complexity attacks on its hash
implementation.  This is largely owed to the following measures
mitigate attacks:</p>
<ul>
<li><a name="Hash-Seed-Randomization"></a><b>Hash Seed Randomization</b>
<p>In order to make it impossible to know what seed to generate an attack
key set for, this seed is randomly initialized at process start.  This
may be overridden by using the PERL_HASH_SEED environment variable, see
<a href="perlrun.html#PERL_HASH_SEED">PERL_HASH_SEED in perlrun</a>.  This environment variable controls how
items are actually stored, not how they are presented via
<code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code>, <code class="inline"><a class="l_k" href="functions/values.html">values</a></code> and <code class="inline"><a class="l_k" href="functions/each.html">each</a></code>.</p>
</li>
<li><a name="Hash-Traversal-Randomization"></a><b>Hash Traversal Randomization</b>
<p>Independent of which seed is used in the hash function, <code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code>,
<code class="inline"><a class="l_k" href="functions/values.html">values</a></code>, and <code class="inline"><a class="l_k" href="functions/each.html">each</a></code> return items in a per-hash randomized order.
Modifying a hash by insertion will change the iteration order of that hash.
This behavior can be overridden by using <code class="inline"><span class="i">hash_traversal_mask</span><span class="s">(</span><span class="s">)</span></code>
 from
<a href="Hash/Util.html">Hash::Util</a> or by using the PERL_PERTURB_KEYS environment variable,
see <a href="perlrun.html#PERL_PERTURB_KEYS">PERL_PERTURB_KEYS in perlrun</a>.  Note that this feature controls the
"visible" order of the keys, and not the actual order they are stored in.</p>
</li>
<li><a name="Bucket-Order-Perturbance"></a><b>Bucket Order Perturbance</b>
<p>When items collide into a given hash bucket the order they are stored in
the chain is no longer predictable in Perl 5.18.  This
has the intention to make it harder to observe a
collision.  This behavior can be overridden by using
the PERL_PERTURB_KEYS environment variable, see <a href="perlrun.html#PERL_PERTURB_KEYS">PERL_PERTURB_KEYS in perlrun</a>.</p>
</li>
<li><a name="New-Default-Hash-Function"></a><b>New Default Hash Function</b>
<p>The default hash function has been modified with the intention of making
it harder to infer the hash seed.</p>
</li>
<li><a name="Alternative-Hash-Functions"></a><b>Alternative Hash Functions</b>
<p>The source code includes multiple hash algorithms to choose from.  While we
believe that the default perl hash is robust to attack, we have included the
hash function Siphash as a fall-back option.  At the time of release of
Perl 5.18.0 Siphash is believed to be of cryptographic strength.  This is
not the default as it is much slower than the default hash.</p>
</li>
</ul>
<p>Without compiling a special Perl, there is no way to get the exact same
behavior of any versions prior to Perl 5.18.0.  The closest one can get
is by setting PERL_PERTURB_KEYS to 0 and setting the PERL_HASH_SEED
to a known value.  We do not advise those settings for production use
due to the above security considerations.</p>
<p><b>Perl has never guaranteed any ordering of the hash keys</b>, and
the ordering has already changed several times during the lifetime of
Perl 5.  Also, the ordering of hash keys has always been, and continues
to be, affected by the insertion order and the history of changes made
to the hash over its lifetime.</p>
<p>Also note that while the order of the hash elements might be
randomized, this "pseudo-ordering" should <b>not</b> be used for
applications like shuffling a list randomly (use <code class="inline"><span class="i">List::Util::shuffle</span><span class="s">(</span><span class="s">)</span></code>

for that, see <a href="List/Util.html">List::Util</a>, a standard core module since Perl 5.8.0;
or the CPAN module <code class="inline"><span class="w">Algorithm::Numerical::Shuffle</span></code>
), or for generating
permutations (use e.g. the CPAN modules <code class="inline"><span class="w">Algorithm::Permute</span></code>
 or
<code class="inline"><span class="w">Algorithm::FastPermute</span></code>
), or for any cryptographic applications.</p>
<p>Tied hashes may have their own ordering and algorithmic complexity
attacks.</p>
</li>
<li>
<p>Regular expressions - Perl's regular expression engine is so called NFA
(Non-deterministic Finite Automaton), which among other things means that
it can rather easily consume large amounts of both time and space if the
regular expression may match in several ways.  Careful crafting of the
regular expressions can help but quite often there really isn't much
one can do (the book "Mastering Regular Expressions" is required
reading, see <a href="perlfaq2.html">perlfaq2</a>).  Running out of space manifests itself by
Perl running out of memory.</p>
</li>
<li>
<p>Sorting - the quicksort algorithm used in Perls before 5.8.0 to
implement the sort() function is very easy to trick into misbehaving
so that it consumes a lot of time.  Starting from Perl 5.8.0 a different
sorting algorithm, mergesort, is used by default.  Mergesort cannot
misbehave on any input.</p>
</li>
</ul>
<p>See <a href="https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf">https://www.usenix.org/legacy/events/sec03/tech/full_papers/crosby/crosby.pdf</a> for more information,
and any computer science textbook on algorithmic complexity.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="perlrun.html">perlrun</a> for its description of cleaning up environment variables.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#SECURITY-VULNERABILITY-CONTACT-INFORMATION">SECURITY VULNERABILITY CONTACT INFORMATION</a><li><a href="#SECURITY-MECHANISMS-AND-CONCERNS">SECURITY MECHANISMS AND CONCERNS</a><ul><li><a href="#Taint-mode">Taint mode</a><li><a href="#Laundering-and-Detecting-Tainted-Data">Laundering and Detecting Tainted Data</a><li><a href="#Switches-On-the-%22%23!%22-Line">Switches On the "#!" Line</a><li><a href="#Taint-mode-and-%40INC">Taint mode and @INC</a><li><a href="#Cleaning-Up-Your-Path">Cleaning Up Your Path</a><li><a href="#Security-Bugs">Security Bugs</a><li><a href="#Protecting-Your-Programs">Protecting Your Programs</a><li><a href="#Unicode">Unicode</a><li><a href="#Algorithmic-Complexity-Attacks">Algorithmic Complexity Attacks</a></ul><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlsec';
  perldoc.pageAddress = 'perlsec.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>