This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.000
[perl5.git] / pod / perlsec.pod
CommitLineData
a0d0e21e
LW
1=head1 NAME
2
3perlsec - Perl security
4
5=head1 DESCRIPTION
6
7Perl is designed to make it easy to write secure setuid and setgid
8scripts. Unlike shells, which are based on multiple substitution
9passes on each line of the script, Perl uses a more conventional
10evaluation scheme with fewer hidden "gotchas". Additionally, since the
11language has more built-in functionality, it has to rely less upon
12external (and possibly untrustworthy) programs to accomplish its
13purposes.
14
15Beyond the obvious problems that stem from giving special privileges to
16such flexible systems as scripts, on many operating systems, setuid
17scripts are inherently insecure right from the start. This is because
18that between the time that the kernel opens up the file to see what to
19run, and when the now setuid interpreter it ran turns around and reopens
20the file so it can interpret it, things may have changed, especially if
21you have symbolic links on your system.
22
23Fortunately, sometimes this kernel "feature" can be disabled.
24Unfortunately, there are two ways to disable it. The system can simply
25outlaw scripts with the setuid bit set, which doesn't help much.
26Alternately, it can simply ignore the setuid bit on scripts. If the
27latter is true, Perl can emulate the setuid and setgid mechanism when it
28notices the otherwise useless setuid/gid bits on Perl scripts. It does
29this via a special executable called B<suidperl> that is automatically
30invoked for you if it's needed.
31
32If, however, the kernel setuid script feature isn't disabled, Perl will
33complain loudly that your setuid script is insecure. You'll need to
34either disable the kernel setuid script feature, or put a C wrapper around
35the script. See the program B<wrapsuid> in the F<eg> directory of your
36Perl distribution for how to go about doing this.
37
38There are some systems on which setuid scripts are free of this inherent
39security bug. For example, recent releases of Solaris are like this. On
40such systems, when the kernel passes the name of the setuid script to open
41to the interpreter, rather than using a pathname subject to mettling, it
42instead passes /dev/fd/3. This is a special file already opened on the
43script, so that there can be no race condition for evil scripts to
44exploit. On these systems, Perl should be compiled with
45C<-DSETUID_SCRIPTS_ARE_SECURE_NOW>. The B<Configure> program that builds
46Perl tries to figure this out for itself.
47
48When Perl is executing a setuid script, it takes special precautions to
49prevent you from falling into any obvious traps. (In some ways, a Perl
50script is more secure than the corresponding C program.) Any command line
51argument, environment variable, or input is marked as "tainted", and may
52not be used, directly or indirectly, in any command that invokes a
53subshell, or in any command that modifies files, directories, or
54processes. Any variable that is set within an expression that has
55previously referenced a tainted value also becomes tainted (even if it is
56logically impossible for the tainted value to influence the variable).
57For example:
58
59 $foo = shift; # $foo is tainted
60 $bar = $foo,'bar'; # $bar is also tainted
61 $xxx = <>; # Tainted
62 $path = $ENV{'PATH'}; # Tainted, but see below
63 $abc = 'abc'; # Not tainted
64
65 system "echo $foo"; # Insecure
66 system "/bin/echo", $foo; # Secure (doesn't use sh)
67 system "echo $bar"; # Insecure
68 system "echo $abc"; # Insecure until PATH set
69
70 $ENV{'PATH'} = '/bin:/usr/bin';
71 $ENV{'IFS'} = '' if $ENV{'IFS'} ne '';
72
73 $path = $ENV{'PATH'}; # Not tainted
74 system "echo $abc"; # Is secure now!
75
76 open(FOO,"$foo"); # OK
77 open(FOO,">$foo"); # Not OK
78
79 open(FOO,"echo $foo|"); # Not OK, but...
80 open(FOO,"-|") || exec 'echo', $foo; # OK
81
82 $zzz = `echo $foo`; # Insecure, zzz tainted
83
84 unlink $abc,$foo; # Insecure
85 umask $foo; # Insecure
86
87 exec "echo $foo"; # Insecure
88 exec "echo", $foo; # Secure (doesn't use sh)
89 exec "sh", '-c', $foo; # Considered secure, alas
90
91The taintedness is associated with each scalar value, so some elements
92of an array can be tainted, and others not.
93
94If you try to do something insecure, you will get a fatal error saying
95something like "Insecure dependency" or "Insecure PATH". Note that you
96can still write an insecure system call or exec, but only by explicitly
97doing something like the last example above. You can also bypass the
98tainting mechanism by referencing subpatterns--Perl presumes that if
99you reference a substring using $1, $2, etc, you knew what you were
100doing when you wrote the pattern:
101
102 $ARGV[0] =~ /^-P(\w+)$/;
103 $printer = $1; # Not tainted
104
105This is fairly secure since C<\w+> doesn't match shell metacharacters.
106Use of C</.+/> would have been insecure, but Perl doesn't check for that,
107so you must be careful with your patterns. This is the I<ONLY> mechanism
108for untainting user supplied filenames if you want to do file operations
109on them (unless you make C<$E<gt>> equal to C<$E<lt>> ).
110
111For "Insecure PATH" messages, you need to set C<$ENV{'PATH}'> to a known
112value, and each directory in the path must be non-writable by the world.
113A frequently voiced gripe is that you can get this message even
114if the pathname to an executable is fully qualified. But Perl can't
115know that the executable in question isn't going to execute some other
116program depending on the PATH.
117
118It's also possible to get into trouble with other operations that don't
119care whether they use tainted values. Make judicious use of the file
120tests in dealing with any user-supplied filenames. When possible, do
121opens and such after setting C<$E<gt> = $E<lt>>. (Remember group IDs,
122too!) Perl doesn't prevent you from opening tainted filenames for reading,
123so be careful what you print out. The tainting mechanism is intended to
124prevent stupid mistakes, not to remove the need for thought.
125