<LI><A HREF="#protecting your programs">Protecting Your Programs</A></LI>
</UL>
<LI><A HREF="#see also">SEE ALSO</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>perlsec - Perl security</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></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>
<P>Perl automatically enables a set of special security checks, called <EM>taint
mode</EM>, 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 <STRONG>-T</STRONG> command line flag. This flag is
<EM>strongly</EM> 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 <EM>taint
checks</EM> 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="../../lib/Pod/perllocale.html">the perllocale manpage</A>), results of certain system calls (readdir(),
readlink(), the variable of shmread(), the messages returned by
msgrcv(), the password, gcos and shell fields returned by the
<CODE>getpwxxx()</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. (<STRONG>Important exception</STRONG>: If you pass a list
of arguments to either <A HREF="../../lib/Pod/perlfunc.html#item_system"><CODE>system</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_exec"><CODE>exec</CODE></A>, the elements of that list
are <STRONG>NOT</STRONG> checked for taintedness.) Any variable set to a value
derived from tainted data will itself be tainted, even if it is
logically impossible for the tainted data to alter the variable.
Because taintedness is associated with each scalar value, some
elements of an array can be tainted and others not.</P>
<P>For example:</P>
<PRE>
$arg = shift; # $arg is tainted
$hid = $arg, 'bar'; # $hid is also tainted
$line = <>; # Tainted
$line = <STDIN>; # Also tainted
open FOO, "/home/me/bar" or die $!;
$line = <FOO>; # Still tainted
$path = $ENV{'PATH'}; # Tainted, but see below
$data = 'abc'; # Not tainted</PRE>
<PRE>
system "echo $arg"; # Insecure
system "/bin/echo", $arg; # Secure (doesn't use sh)
system "echo $hid"; # Insecure
system "echo $data"; # Insecure until PATH set</PRE>