| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLSEC 1" |
| 132 | .TH PERLSEC 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perlsec \- Perl security |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | Perl is designed to make it easy to program securely even when running |
| 138 | with extra privileges, like setuid or setgid programs. Unlike most |
| 139 | command line shells, which are based on multiple substitution passes on |
| 140 | each line of the script, Perl uses a more conventional evaluation scheme |
| 141 | with fewer hidden snags. Additionally, because the language has more |
| 142 | builtin functionality, it can rely less upon external (and possibly |
| 143 | untrustworthy) programs to accomplish its purposes. |
| 144 | .PP |
| 145 | Perl automatically enables a set of special security checks, called \fItaint |
| 146 | mode\fR, when it detects its program running with differing real and effective |
| 147 | user or group IDs. The setuid bit in Unix permissions is mode 04000, the |
| 148 | setgid bit mode 02000; either or both may be set. You can also enable taint |
| 149 | mode explicitly by using the \fB\-T\fR command line flag. This flag is |
| 150 | \&\fIstrongly\fR suggested for server programs and any program run on behalf of |
| 151 | someone else, such as a \s-1CGI\s0 script. Once taint mode is on, it's on for |
| 152 | the remainder of your script. |
| 153 | .PP |
| 154 | While in this mode, Perl takes special precautions called \fItaint |
| 155 | checks\fR to prevent both obvious and subtle traps. Some of these checks |
| 156 | are reasonably simple, such as verifying that path directories aren't |
| 157 | writable by others; careful programmers have always used checks like |
| 158 | these. Other checks, however, are best supported by the language itself, |
| 159 | and it is these checks especially that contribute to making a set-id Perl |
| 160 | program more secure than the corresponding C program. |
| 161 | .PP |
| 162 | You may not use data derived from outside your program to affect |
| 163 | something else outside your program\*(--at least, not by accident. All |
| 164 | command line arguments, environment variables, locale information (see |
| 165 | perllocale), results of certain system calls (\fIreaddir()\fR, |
| 166 | \&\fIreadlink()\fR, the variable of \fIshmread()\fR, the messages returned by |
| 167 | \&\fImsgrcv()\fR, the password, gcos and shell fields returned by the |
| 168 | \&\fIgetpwxxx()\fR calls), and all file input are marked as \*(L"tainted\*(R". |
| 169 | Tainted data may not be used directly or indirectly in any command |
| 170 | that invokes a sub\-shell, nor in any command that modifies files, |
| 171 | directories, or processes, \fBwith the following exceptions\fR: |
| 172 | .IP "\(bu" 4 |
| 173 | If you pass more than one argument to either \f(CW\*(C`system\*(C'\fR or \f(CW\*(C`exec\*(C'\fR, |
| 174 | the arguments are checked for taintedness \fBbut\fR the operation will still |
| 175 | be attempted, emitting an optional warning. This will be fatal in a |
| 176 | future version of perl so do not rely on it to bypass the tainting |
| 177 | mechanism. |
| 178 | .IP "\(bu" 4 |
| 179 | Arguments to \f(CW\*(C`print\*(C'\fR and \f(CW\*(C`syswrite\*(C'\fR are \fBnot\fR checked for taintedness. |
| 180 | .IP "\(bu" 4 |
| 181 | Symbolic methods |
| 182 | .Sp |
| 183 | .Vb 1 |
| 184 | \& $obj->$method(@args); |
| 185 | .Ve |
| 186 | .Sp |
| 187 | and symbolic sub references |
| 188 | .Sp |
| 189 | .Vb 2 |
| 190 | \& &{$foo}(@args); |
| 191 | \& $foo->(@args); |
| 192 | .Ve |
| 193 | .Sp |
| 194 | are not checked for taintedness. This requires extra carefulness |
| 195 | unless you want external data to affect your control flow. Unless |
| 196 | you carefully limit what these symbolic values are, people are able |
| 197 | to call functions \fBoutside\fR your Perl code, such as POSIX::system, |
| 198 | in which case they are able to run arbitrary external code. |
| 199 | .PP |
| 200 | The value of an expression containing tainted data will itself be |
| 201 | tainted, even if it is logically impossible for the tainted data to |
| 202 | affect the value. |
| 203 | .PP |
| 204 | Because taintedness is associated with each scalar value, some |
| 205 | elements of an array can be tainted and others not. |
| 206 | .PP |
| 207 | For example: |
| 208 | .PP |
| 209 | .Vb 8 |
| 210 | \& $arg = shift; # $arg is tainted |
| 211 | \& $hid = $arg, 'bar'; # $hid is also tainted |
| 212 | \& $line = <>; # Tainted |
| 213 | \& $line = <STDIN>; # Also tainted |
| 214 | \& open FOO, "/home/me/bar" or die $!; |
| 215 | \& $line = <FOO>; # Still tainted |
| 216 | \& $path = $ENV{'PATH'}; # Tainted, but see below |
| 217 | \& $data = 'abc'; # Not tainted |
| 218 | .Ve |
| 219 | .PP |
| 220 | .Vb 5 |
| 221 | \& system "echo $arg"; # Insecure |
| 222 | \& system "/bin/echo", $arg; # Allowed but considered insecure |
| 223 | \& # (Perl doesn't know about /bin/echo) |
| 224 | \& system "echo $hid"; # Insecure |
| 225 | \& system "echo $data"; # Insecure until PATH set |
| 226 | .Ve |
| 227 | .PP |
| 228 | .Vb 1 |
| 229 | \& $path = $ENV{'PATH'}; # $path now tainted |
| 230 | .Ve |
| 231 | .PP |
| 232 | .Vb 2 |
| 233 | \& $ENV{'PATH'} = '/bin:/usr/bin'; |
| 234 | \& delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; |
| 235 | .Ve |
| 236 | .PP |
| 237 | .Vb 2 |
| 238 | \& $path = $ENV{'PATH'}; # $path now NOT tainted |
| 239 | \& system "echo $data"; # Is secure now! |
| 240 | .Ve |
| 241 | .PP |
| 242 | .Vb 2 |
| 243 | \& open(FOO, "< $arg"); # OK - read-only file |
| 244 | \& open(FOO, "> $arg"); # Not OK - trying to write |
| 245 | .Ve |
| 246 | .PP |
| 247 | .Vb 3 |
| 248 | \& open(FOO,"echo $arg|"); # Not OK |
| 249 | \& open(FOO,"-|") |
| 250 | \& or exec 'echo', $arg; # Allowed but not really OK |
| 251 | .Ve |
| 252 | .PP |
| 253 | .Vb 1 |
| 254 | \& $shout = `echo $arg`; # Insecure, $shout now tainted |
| 255 | .Ve |
| 256 | .PP |
| 257 | .Vb 2 |
| 258 | \& unlink $data, $arg; # Insecure |
| 259 | \& umask $arg; # Insecure |
| 260 | .Ve |
| 261 | .PP |
| 262 | .Vb 3 |
| 263 | \& exec "echo $arg"; # Insecure |
| 264 | \& exec "echo", $arg; # Allowed but considered insecure |
| 265 | \& exec "sh", '-c', $arg; # Considered secure, alas! |
| 266 | .Ve |
| 267 | .PP |
| 268 | .Vb 2 |
| 269 | \& @files = <*.c>; # insecure (uses readdir() or similar) |
| 270 | \& @files = glob('*.c'); # insecure (uses readdir() or similar) |
| 271 | .Ve |
| 272 | .PP |
| 273 | .Vb 4 |
| 274 | \& # In Perl releases older than 5.6.0 the <*.c> and glob('*.c') would |
| 275 | \& # have used an external program to do the filename expansion; but in |
| 276 | \& # either case the result is tainted since the list of filenames comes |
| 277 | \& # from outside of the program. |
| 278 | .Ve |
| 279 | .PP |
| 280 | .Vb 2 |
| 281 | \& $bad = ($arg, 23); # $bad will be tainted |
| 282 | \& $arg, `true`; # Insecure (although it isn't really) |
| 283 | .Ve |
| 284 | .PP |
| 285 | If you try to do something insecure, you will get a fatal error saying |
| 286 | something like \*(L"Insecure dependency\*(R" or \*(L"Insecure \f(CW$ENV\fR{\s-1PATH\s0}\*(R". Note that you |
| 287 | can still write an insecure \fBsystem\fR or \fBexec\fR, but only by explicitly |
| 288 | doing something like the \*(L"considered secure\*(R" example above. This will not |
| 289 | be possible in a future version of Perl. |
| 290 | .Sh "Laundering and Detecting Tainted Data" |
| 291 | .IX Subsection "Laundering and Detecting Tainted Data" |
| 292 | To test whether a variable contains tainted data, and whose use would |
| 293 | thus trigger an \*(L"Insecure dependency\*(R" message, you can use the |
| 294 | \&\fItainted()\fR function of the Scalar::Util module, available in your |
| 295 | nearby \s-1CPAN\s0 mirror, and included in Perl starting from the release 5.8.0. |
| 296 | Or you may be able to use the following \fI\fIis_tainted()\fI\fR function. |
| 297 | .PP |
| 298 | .Vb 3 |
| 299 | \& sub is_tainted { |
| 300 | \& return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 }; |
| 301 | \& } |
| 302 | .Ve |
| 303 | .PP |
| 304 | This function makes use of the fact that the presence of tainted data |
| 305 | anywhere within an expression renders the entire expression tainted. It |
| 306 | would be inefficient for every operator to test every argument for |
| 307 | taintedness. Instead, the slightly more efficient and conservative |
| 308 | approach is used that if any tainted value has been accessed within the |
| 309 | same expression, the whole expression is considered tainted. |
| 310 | .PP |
| 311 | But testing for taintedness gets you only so far. Sometimes you have just |
| 312 | to clear your data's taintedness. The only way to bypass the tainting |
| 313 | mechanism is by referencing subpatterns from a regular expression match. |
| 314 | Perl presumes that if you reference a substring using \f(CW$1\fR, \f(CW$2\fR, etc., that |
| 315 | you knew what you were doing when you wrote the pattern. That means using |
| 316 | a bit of thought\*(--don't just blindly untaint anything, or you defeat the |
| 317 | entire mechanism. It's better to verify that the variable has only good |
| 318 | characters (for certain values of \*(L"good\*(R") rather than checking whether it |
| 319 | has any bad characters. That's because it's far too easy to miss bad |
| 320 | characters that you never thought of. |
| 321 | .PP |
| 322 | Here's a test to make sure that the data contains nothing but \*(L"word\*(R" |
| 323 | characters (alphabetics, numerics, and underscores), a hyphen, an at sign, |
| 324 | or a dot. |
| 325 | .PP |
| 326 | .Vb 5 |
| 327 | \& if ($data =~ /^([-\e@\ew.]+)$/) { |
| 328 | \& $data = $1; # $data now untainted |
| 329 | \& } else { |
| 330 | \& die "Bad data in $data"; # log this somewhere |
| 331 | \& } |
| 332 | .Ve |
| 333 | .PP |
| 334 | This is fairly secure because \f(CW\*(C`/\ew+/\*(C'\fR doesn't normally match shell |
| 335 | metacharacters, nor are dot, dash, or at going to mean something special |
| 336 | to the shell. Use of \f(CW\*(C`/.+/\*(C'\fR would have been insecure in theory because |
| 337 | it lets everything through, but Perl doesn't check for that. The lesson |
| 338 | is that when untainting, you must be exceedingly careful with your patterns. |
| 339 | Laundering data using regular expression is the \fIonly\fR mechanism for |
| 340 | untainting dirty data, unless you use the strategy detailed below to fork |
| 341 | a child of lesser privilege. |
| 342 | .PP |
| 343 | The example does not untaint \f(CW$data\fR if \f(CW\*(C`use locale\*(C'\fR is in effect, |
| 344 | because the characters matched by \f(CW\*(C`\ew\*(C'\fR are determined by the locale. |
| 345 | Perl considers that locale definitions are untrustworthy because they |
| 346 | contain data from outside the program. If you are writing a |
| 347 | locale-aware program, and want to launder data with a regular expression |
| 348 | containing \f(CW\*(C`\ew\*(C'\fR, put \f(CW\*(C`no locale\*(C'\fR ahead of the expression in the same |
| 349 | block. See \*(L"\s-1SECURITY\s0\*(R" in perllocale for further discussion and examples. |
| 350 | .ie n .Sh "Switches On the ""#!"" Line" |
| 351 | .el .Sh "Switches On the ``#!'' Line" |
| 352 | .IX Subsection "Switches On the #! Line" |
| 353 | When you make a script executable, in order to make it usable as a |
| 354 | command, the system will pass switches to perl from the script's #! |
| 355 | line. Perl checks that any command line switches given to a setuid |
| 356 | (or setgid) script actually match the ones set on the #! line. Some |
| 357 | Unix and Unix-like environments impose a one-switch limit on the #! |
| 358 | line, so you may need to use something like \f(CW\*(C`\-wU\*(C'\fR instead of \f(CW\*(C`\-w \-U\*(C'\fR |
| 359 | under such systems. (This issue should arise only in Unix or |
| 360 | Unix-like environments that support #! and setuid or setgid scripts.) |
| 361 | .Sh "Cleaning Up Your Path" |
| 362 | .IX Subsection "Cleaning Up Your Path" |
| 363 | For "Insecure \f(CW$ENV{PATH}\fR" messages, you need to set \f(CW$ENV{'PATH'}\fR to a |
| 364 | known value, and each directory in the path must be non-writable by others |
| 365 | than its owner and group. You may be surprised to get this message even |
| 366 | if the pathname to your executable is fully qualified. This is \fInot\fR |
| 367 | generated because you didn't supply a full path to the program; instead, |
| 368 | it's generated because you never set your \s-1PATH\s0 environment variable, or |
| 369 | you didn't set it to something that was safe. Because Perl can't |
| 370 | guarantee that the executable in question isn't itself going to turn |
| 371 | around and execute some other program that is dependent on your \s-1PATH\s0, it |
| 372 | makes sure you set the \s-1PATH\s0. |
| 373 | .PP |
| 374 | The \s-1PATH\s0 isn't the only environment variable which can cause problems. |
| 375 | Because some shells may use the variables \s-1IFS\s0, \s-1CDPATH\s0, \s-1ENV\s0, and |
| 376 | \&\s-1BASH_ENV\s0, Perl checks that those are either empty or untainted when |
| 377 | starting subprocesses. You may wish to add something like this to your |
| 378 | setid and taint-checking scripts. |
| 379 | .PP |
| 380 | .Vb 1 |
| 381 | \& delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer |
| 382 | .Ve |
| 383 | .PP |
| 384 | It's also possible to get into trouble with other operations that don't |
| 385 | care whether they use tainted values. Make judicious use of the file |
| 386 | tests in dealing with any user-supplied filenames. When possible, do |
| 387 | opens and such \fBafter\fR properly dropping any special user (or group!) |
| 388 | privileges. Perl doesn't prevent you from opening tainted filenames for reading, |
| 389 | so be careful what you print out. The tainting mechanism is intended to |
| 390 | prevent stupid mistakes, not to remove the need for thought. |
| 391 | .PP |
| 392 | Perl does not call the shell to expand wild cards when you pass \fBsystem\fR |
| 393 | and \fBexec\fR explicit parameter lists instead of strings with possible shell |
| 394 | wildcards in them. Unfortunately, the \fBopen\fR, \fBglob\fR, and |
| 395 | backtick functions provide no such alternate calling convention, so more |
| 396 | subterfuge will be required. |
| 397 | .PP |
| 398 | Perl provides a reasonably safe way to open a file or pipe from a setuid |
| 399 | or setgid program: just create a child process with reduced privilege who |
| 400 | does the dirty work for you. First, fork a child using the special |
| 401 | \&\fBopen\fR syntax that connects the parent and child by a pipe. Now the |
| 402 | child resets its \s-1ID\s0 set and any other per-process attributes, like |
| 403 | environment variables, umasks, current working directories, back to the |
| 404 | originals or known safe values. Then the child process, which no longer |
| 405 | has any special permissions, does the \fBopen\fR or other system call. |
| 406 | Finally, the child passes the data it managed to access back to the |
| 407 | parent. Because the file or pipe was opened in the child while running |
| 408 | under less privilege than the parent, it's not apt to be tricked into |
| 409 | doing something it shouldn't. |
| 410 | .PP |
| 411 | Here's a way to do backticks reasonably safely. Notice how the \fBexec\fR is |
| 412 | not called with a string that the shell could expand. This is by far the |
| 413 | best way to call something that might be subjected to shell escapes: just |
| 414 | never call the shell at all. |
| 415 | .PP |
| 416 | .Vb 25 |
| 417 | \& use English '-no_match_vars'; |
| 418 | \& die "Can't fork: $!" unless defined($pid = open(KID, "-|")); |
| 419 | \& if ($pid) { # parent |
| 420 | \& while (<KID>) { |
| 421 | \& # do something |
| 422 | \& } |
| 423 | \& close KID; |
| 424 | \& } else { |
| 425 | \& my @temp = ($EUID, $EGID); |
| 426 | \& my $orig_uid = $UID; |
| 427 | \& my $orig_gid = $GID; |
| 428 | \& $EUID = $UID; |
| 429 | \& $EGID = $GID; |
| 430 | \& # Drop privileges |
| 431 | \& $UID = $orig_uid; |
| 432 | \& $GID = $orig_gid; |
| 433 | \& # Make sure privs are really gone |
| 434 | \& ($EUID, $EGID) = @temp; |
| 435 | \& die "Can't drop privileges" |
| 436 | \& unless $UID == $EUID && $GID eq $EGID; |
| 437 | \& $ENV{PATH} = "/bin:/usr/bin"; # Minimal PATH. |
| 438 | \& # Consider sanitizing the environment even more. |
| 439 | \& exec 'myprog', 'arg1', 'arg2' |
| 440 | \& or die "can't exec myprog: $!"; |
| 441 | \& } |
| 442 | .Ve |
| 443 | .PP |
| 444 | A similar strategy would work for wildcard expansion via \f(CW\*(C`glob\*(C'\fR, although |
| 445 | you can use \f(CW\*(C`readdir\*(C'\fR instead. |
| 446 | .PP |
| 447 | Taint checking is most useful when although you trust yourself not to have |
| 448 | written a program to give away the farm, you don't necessarily trust those |
| 449 | who end up using it not to try to trick it into doing something bad. This |
| 450 | is the kind of security checking that's useful for set-id programs and |
| 451 | programs launched on someone else's behalf, like \s-1CGI\s0 programs. |
| 452 | .PP |
| 453 | This is quite different, however, from not even trusting the writer of the |
| 454 | code not to try to do something evil. That's the kind of trust needed |
| 455 | when someone hands you a program you've never seen before and says, \*(L"Here, |
| 456 | run this.\*(R" For that kind of safety, check out the Safe module, |
| 457 | included standard in the Perl distribution. This module allows the |
| 458 | programmer to set up special compartments in which all system operations |
| 459 | are trapped and namespace access is carefully controlled. |
| 460 | .Sh "Security Bugs" |
| 461 | .IX Subsection "Security Bugs" |
| 462 | Beyond the obvious problems that stem from giving special privileges to |
| 463 | systems as flexible as scripts, on many versions of Unix, set-id scripts |
| 464 | are inherently insecure right from the start. The problem is a race |
| 465 | condition in the kernel. Between the time the kernel opens the file to |
| 466 | see which interpreter to run and when the (now\-set\-id) interpreter turns |
| 467 | around and reopens the file to interpret it, the file in question may have |
| 468 | changed, especially if you have symbolic links on your system. |
| 469 | .PP |
| 470 | Fortunately, sometimes this kernel \*(L"feature\*(R" can be disabled. |
| 471 | Unfortunately, there are two ways to disable it. The system can simply |
| 472 | outlaw scripts with any set-id bit set, which doesn't help much. |
| 473 | Alternately, it can simply ignore the set-id bits on scripts. If the |
| 474 | latter is true, Perl can emulate the setuid and setgid mechanism when it |
| 475 | notices the otherwise useless setuid/gid bits on Perl scripts. It does |
| 476 | this via a special executable called \fBsuidperl\fR that is automatically |
| 477 | invoked for you if it's needed. |
| 478 | .PP |
| 479 | However, if the kernel set-id script feature isn't disabled, Perl will |
| 480 | complain loudly that your set-id script is insecure. You'll need to |
| 481 | either disable the kernel set-id script feature, or put a C wrapper around |
| 482 | the script. A C wrapper is just a compiled program that does nothing |
| 483 | except call your Perl program. Compiled programs are not subject to the |
| 484 | kernel bug that plagues set-id scripts. Here's a simple wrapper, written |
| 485 | in C: |
| 486 | .PP |
| 487 | .Vb 6 |
| 488 | \& #define REAL_PATH "/path/to/script" |
| 489 | \& main(ac, av) |
| 490 | \& char **av; |
| 491 | \& { |
| 492 | \& execv(REAL_PATH, av); |
| 493 | \& } |
| 494 | .Ve |
| 495 | .PP |
| 496 | Compile this wrapper into a binary executable and then make \fIit\fR rather |
| 497 | than your script setuid or setgid. |
| 498 | .PP |
| 499 | In recent years, vendors have begun to supply systems free of this |
| 500 | inherent security bug. On such systems, when the kernel passes the name |
| 501 | of the set-id script to open to the interpreter, rather than using a |
| 502 | pathname subject to meddling, it instead passes \fI/dev/fd/3\fR. This is a |
| 503 | special file already opened on the script, so that there can be no race |
| 504 | condition for evil scripts to exploit. On these systems, Perl should be |
| 505 | compiled with \f(CW\*(C`\-DSETUID_SCRIPTS_ARE_SECURE_NOW\*(C'\fR. The \fBConfigure\fR |
| 506 | program that builds Perl tries to figure this out for itself, so you |
| 507 | should never have to specify this yourself. Most modern releases of |
| 508 | SysVr4 and \s-1BSD\s0 4.4 use this approach to avoid the kernel race condition. |
| 509 | .PP |
| 510 | Prior to release 5.6.1 of Perl, bugs in the code of \fBsuidperl\fR could |
| 511 | introduce a security hole. |
| 512 | .Sh "Protecting Your Programs" |
| 513 | .IX Subsection "Protecting Your Programs" |
| 514 | There are a number of ways to hide the source to your Perl programs, |
| 515 | with varying levels of \*(L"security\*(R". |
| 516 | .PP |
| 517 | First of all, however, you \fIcan't\fR take away read permission, because |
| 518 | the source code has to be readable in order to be compiled and |
| 519 | interpreted. (That doesn't mean that a \s-1CGI\s0 script's source is |
| 520 | readable by people on the web, though.) So you have to leave the |
| 521 | permissions at the socially friendly 0755 level. This lets |
| 522 | people on your local system only see your source. |
| 523 | .PP |
| 524 | Some people mistakenly regard this as a security problem. If your program does |
| 525 | insecure things, and relies on people not knowing how to exploit those |
| 526 | insecurities, it is not secure. It is often possible for someone to |
| 527 | determine the insecure things and exploit them without viewing the |
| 528 | source. Security through obscurity, the name for hiding your bugs |
| 529 | instead of fixing them, is little security indeed. |
| 530 | .PP |
| 531 | You can try using encryption via source filters (Filter::* from \s-1CPAN\s0, |
| 532 | or Filter::Util::Call and Filter::Simple since Perl 5.8). |
| 533 | But crackers might be able to decrypt it. You can try using the byte |
| 534 | code compiler and interpreter described below, but crackers might be |
| 535 | able to de-compile it. You can try using the native-code compiler |
| 536 | described below, but crackers might be able to disassemble it. These |
| 537 | pose varying degrees of difficulty to people wanting to get at your |
| 538 | code, but none can definitively conceal it (this is true of every |
| 539 | language, not just Perl). |
| 540 | .PP |
| 541 | If you're concerned about people profiting from your code, then the |
| 542 | bottom line is that nothing but a restrictive licence will give you |
| 543 | legal security. License your software and pepper it with threatening |
| 544 | statements like \*(L"This is unpublished proprietary software of \s-1XYZ\s0 Corp. |
| 545 | Your access to it does not give you permission to use it blah blah |
| 546 | blah.\*(R" You should see a lawyer to be sure your licence's wording will |
| 547 | stand up in court. |
| 548 | .Sh "Unicode" |
| 549 | .IX Subsection "Unicode" |
| 550 | Unicode is a new and complex technology and one may easily overlook |
| 551 | certain security pitfalls. See perluniintro for an overview and |
| 552 | perlunicode for details, and \*(L"Security Implications of Unicode\*(R" in perlunicode for security implications in particular. |
| 553 | .SH "SEE ALSO" |
| 554 | .IX Header "SEE ALSO" |
| 555 | perlrun for its description of cleaning up environment variables. |