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
|
<!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 href="static/css-20090810.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
</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="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="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>
<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>
•
<a href="index-modules-B.html">B</a>
•
<a href="index-modules-C.html">C</a>
•
<a href="index-modules-D.html">D</a>
•
<a href="index-modules-E.html">E</a>
<li>
<a href="index-modules-F.html">F</a>
•
<a href="index-modules-G.html">G</a>
•
<a href="index-modules-H.html">H</a>
•
<a href="index-modules-I.html">I</a>
•
<a href="index-modules-L.html">L</a>
<li>
<a href="index-modules-M.html">M</a>
•
<a href="index-modules-N.html">N</a>
•
<a href="index-modules-O.html">O</a>
•
<a href="index-modules-P.html">P</a>
•
<a href="index-modules-S.html">S</a>
<li>
<a href="index-modules-T.html">T</a>
•
<a href="index-modules-U.html">U</a>
•
<a href="index-modules-X.html">X</a>
</ul>
</div>
</div>
<div class="side_group">
<div class="side_panel links_panel">
<p>News / Blogs</p>
<ul>
<li><a href="http://perlbuzz.com">Perl Buzz</a></li>
<li><a href="http://ironman.enlightenedperl.org">Perl Iron Man</a></li>
<li><a href="http://perlsphere.net">Perlsphere</a></li>
<li><a href="http://planet.perl.org">Planet Perl</a></li>
<li><a href="http://news.perlfoundation.org/">TPF news</a></li>
<li><a href="http://use.perl.org">Use Perl</a></li>
</ul>
</div>
<div class="side_panel links_panel">
<p>See also</p>
<ul>
<li><a href="http://search.cpan.org">CPAN</a></li>
<li><a href="http://www.perl.org">Perl.org</a></li>
<li><a href="http://www.perl.com">Perl.com</a></li>
<li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
<li><a href="http://jobs.perl.org">Perl Jobs</a></li>
<li><a href="http://www.pm.org">Perl Mongers</a></li>
<li><a href="http://www.perlmonks.org">Perl Monks</a></li>
</ul>
</div>
<div class="side_panel links_panel">
<p>Contact</p>
<ul>
<li><a href="http://perl.jonallen.info">Site designed and maintained by Jon Allen (JJ)</a>
<li><a href="http://perl.jonallen.info">http://perl.jonallen.info</a>
</ul>
<ul>
<li><a href="http://perl.jonallen.info/projects/perldoc">View the project page and FAQs</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 10.1 documentation
</div>
<div id="page_links">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
• <a href="#" id="page_index_toggle">Show page index</a>
</div>
</div>
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-language.html">Language reference</a> >
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="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
perl5-security-report@perl.org with details. This points to a closed
subscription, unarchived mailing list. Please only use this address for
security issues in the Perl core, not for modules independently distributed on
CPAN.</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">->$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">&</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>-><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="cm">,</span> <span class="q">'bar'</span><span class="sc">;</span> <span class="c"># $hid is also tainted</span></li><li> <span class="i">$line</span> = <><span class="sc">;</span> <span class="c"># Tainted</span></li><li> <span class="i">$line</span> = <span class="q"><STDIN></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">"/home/me/bar"</span> or <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"><FOO></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">'PATH'</span>}<span class="sc">;</span> <span class="c"># Tainted, but see below</span></li><li> <span class="i">$data</span> = <span class="q">'abc'</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">"echo $arg"</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">"/bin/echo"</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't know about /bin/echo)</span></li><li> <a class="l_k" href="functions/system.html">system</a> <span class="q">"echo $hid"</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">"echo $data"</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">'PATH'</span>}<span class="sc">;</span> <span class="c"># $path now tainted</span></li><li></li><li> <span class="i">$ENV</span>{<span class="q">'PATH'</span>} = <span class="q">'/bin:/usr/bin'</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">'IFS'</span><span class="cm">,</span> <span class="q">'CDPATH'</span><span class="cm">,</span> <span class="q">'ENV'</span><span class="cm">,</span> <span class="q">'BASH_ENV'</span>}<span class="sc">;</span></li><li></li><li> <span class="i">$path</span> = <span class="i">$ENV</span>{<span class="q">'PATH'</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">"echo $data"</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">"< $arg"</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">"> $arg"</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">"echo $arg|"</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">"-|"</span><span class="s">)</span></li><li> or <a class="l_k" href="functions/exec.html">exec</a> <span class="q">'echo'</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">"echo $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">"echo"</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">"sh"</span><span class="cm">,</span> <span class="q">'-c'</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"><*.c></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">'*.c'</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 Perl releases older than 5.6.0 the <*.c> and glob('*.c') would</span></li><li> <span class="c"># have used an external program to do the filename expansion; but in</span></li><li> <span class="c"># either case the result is tainted since the list of filenames comes</span></li><li> <span class="c"># 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'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">"Untainted"</span> <span class="co">:</span> <span class="q">"Also untainted"</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">"Untainted"</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">"Also untainted"</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/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">"#"</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">""</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., that
you knew what you were doing when you wrote the 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">"Bad data in '$data'"</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 later
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">'PATH'</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="q">'-no_match_vars'</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"Can't fork: $!"</span> unless <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">"-|"</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li> if <span class="s">(</span><span class="i">$pid</span><span class="s">)</span> <span class="s">{</span> <span class="c"># parent</span></li><li> while <span class="s">(</span><span class="q"><KID></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> else <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">"Can't drop privileges"</span></li><li> unless <span class="i">$UID</span> == <span class="i">$EUID</span> && <span class="i">$GID</span> eq <span class="i">$EGID</span><span class="sc">;</span></li><li> <span class="i">$ENV</span>{<span class="w">PATH</span>} = <span class="q">"/bin:/usr/bin"</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">'myprog'</span><span class="cm">,</span> <span class="q">'arg1'</span><span class="cm">,</span> <span class="q">'arg2'</span></li><li> or <a class="l_k" href="functions/die.html">die</a> <span class="q">"can't exec myprog: $!"</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, 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.</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. If the
latter is true, Perl can emulate the setuid and setgid mechanism when it
notices the otherwise useless setuid/gid bits on Perl scripts. It does
this via a special executable called <i>suidperl</i> that is automatically
invoked for you if it's needed.</p>
<p><b>The use of suidperl is considered deprecated</b>, and will be removed
in Perl 5.12.0. It is <i>strongly</i> recommended that all code uses
the simplier and more secure C-wrappers described below.</p>
<p>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> #define REAL_PATH "/path/to/script"</li><li> main(ac, av)</li><li> char **av;</li><li> {</li><li> execv(REAL_PATH, av);</li><li> }</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>
<p>Prior to release 5.6.1 of Perl, bugs in the code of <i>suidperl</i> could
introduce a security hole. The use of <i>suidperl</i> is considered
deprecated, and will be removed in Perl 5.12.0.</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 Function - the algorithm used to "order" hash elements has been
changed several times during the development of Perl, mainly to be
reasonably fast. In Perl 5.8.1 also the security aspect was taken
into account.</p>
<p>In Perls before 5.8.1 one could rather easily generate data that as
hash keys would cause Perl to consume large amounts of time because
internal structure of hashes would badly degenerate. In Perl 5.8.1
the hash function is randomly perturbed by a pseudorandom seed which
makes generating such naughty hash keys harder.
See <a href="perlrun.html#PERL_HASH_SEED">"PERL_HASH_SEED" in perlrun</a> for more information.</p>
<p>In Perl 5.8.1 the random perturbation was done by default, but as of
5.8.2 it is only used on individual hashes if the internals detect the
insertion of pathological data. If one wants for some reason emulate the
old behaviour (and expose oneself to DoS attacks) one can set the
environment variable PERL_HASH_SEED to zero to disable the protection
(or any other integer to force a known perturbation, rather than random).
One possible reason for wanting to emulate the old behaviour is that in the
new behaviour consecutive runs of Perl will order hash keys differently,
which may confuse some applications (like Data::Dumper: the outputs of two
different runs are no longer identical).</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.</p>
<p>Also note that while the order of the hash elements might be
randomised, this "pseudoordering" should <b>not</b> be used for
applications like shuffling a list randomly (use List::Util::shuffle()
for that, see <a href="List/Util.html">List::Util</a>, a standard core module since Perl 5.8.0;
or the CPAN module Algorithm::Numerical::Shuffle), or for generating
permutations (use e.g. the CPAN modules Algorithm::Permute or
Algorithm::FastPermute), or for any cryptographic applications.</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="http://www.cs.rice.edu/~scrosby/hash/">http://www.cs.rice.edu/~scrosby/hash/</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">
<div id="page_index_header">
<div id="page_index_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
<div id="page_index_title"><span class="page_index_top">Page index</span></div>
<div id="page_index_topright"></div>
</div>
<div id="page_index_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">
<div id="page_index_bottomleft"></div>
<div id="page_index_bottom"><span class="page_index_bottom"></span></div>
<div id="page_index_resize"></div>
</div>
</div>
</div>
<div id="content_footer">
</div>
</div>
<div id="right_column">
<div class="side_group">
<div class="side_panel tools_panel">
<p>Recently read</p>
<div id="recent_pages"></div>
</div>
<div class="side_panel tools_panel">
<p>Tools</p>
<ul>
<li><a href="preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
</div>
</div>
</div>
<script language="JavaScript" src="static/combined-20090809.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(0);
perldoc.pageName = 'perlsec';
perldoc.pageAddress = 'perlsec.html';
perldoc.contentPage = 1;
</script>
</body>
</html>
|