Penguin
Annotated edit history of perlsec(1) version 1, including all changes. View license author blame.
Rev Author # Line
1 perry 1 PERLSEC
2 !!!PERLSEC
3 NAME
4 DESCRIPTION
5 SEE ALSO
6 ----
7 !!NAME
8
9
10 perlsec - Perl security
11 !!DESCRIPTION
12
13
14 Perl is designed to make it easy to program securely even
15 when running with extra privileges, like setuid or setgid
16 programs. Unlike most command line shells, which are based
17 on multiple substitution passes on each line of the script,
18 Perl uses a more conventional evaluation scheme with fewer
19 hidden snags. Additionally, because the language has more
20 builtin functionality, it can rely less upon external (and
21 possibly untrustworthy) programs to accomplish its
22 purposes.
23
24
25 Perl automatically enables a set of special security checks,
26 called ''taint mode'', when it detects its program
27 running with differing real and effective user or group IDs.
28 The setuid bit in Unix permissions is mode 04000, the setgid
29 bit mode 02000; either or both may be set. You can also
30 enable taint mode explicitly by using the __-T__ command
31 line flag. This flag is ''strongly'' suggested for server
32 programs and any program run on behalf of someone else, such
33 as a CGI script. Once taint mode is on, it's
34 on for the remainder of your script.
35
36
37 While in this mode, Perl takes special precautions called
38 ''taint checks'' to prevent both obvious and subtle
39 traps. Some of these checks are reasonably simple, such as
40 verifying that path directories aren't writable by others;
41 careful programmers have always used checks like these.
42 Other checks, however, are best supported by the language
43 itself, and it is these checks especially that contribute to
44 making a set-id Perl program more secure than the
45 corresponding C program.
46
47
48 You may not use data derived from outside your program to
49 affect something else outside your program--at least, not by
50 accident. All command line arguments, environment variables,
51 locale information (see perllocale), results of certain
52 system calls (''readdir()'', ''readlink()'', the
53 variable of ''shmread()'', the messages returned by
54 ''msgrcv()'', the password, gcos and shell fields
55 returned by the ''getpwxxx()'' calls), and all file input
56 are marked as ``tainted''. Tainted data may not be used
57 directly or indirectly in any command that invokes a
58 sub-shell, nor in any command that modifies files,
59 directories, or processes, __with the following
60 exceptions__:
61
62
63 If you pass a list of arguments to either system or
64 exec, the elements of that list are __not__
65 checked for taintedness.
66
67
68 Arguments to print and syswrite are
69 __not__ checked for taintedness.
70
71
72 Any variable set to a value derived from tainted data will
73 itself be tainted, even if it is logically impossible for
74 the tainted data to alter the variable. Because taintedness
75 is associated with each scalar value, some elements of an
76 array can be tainted and others not.
77
78
79 For example:
80
81
82 $arg = shift; # $arg is tainted
83 $hid = $arg, 'bar'; # $hid is also tainted
84 $line =
85 system
86 $path = $ENV{'PATH'}; # $path now tainted
87 $ENV{'PATH'} = '/bin:/usr/bin';
88 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
89 $path = $ENV{'PATH'}; # $path now NOT tainted
90 system
91 open(FOO,
92 open(FOO,
93 $shout = `echo $arg`; # Insecure, $shout now tainted
94 unlink $data, $arg; # Insecure
95 umask $arg; # Insecure
96 exec
97 @files =
98 If you try to do something insecure, you will get a fatal error saying something like ``Insecure dependency'' or ``Insecure $ENV{ PATH }''. Note that you can still write an insecure __system__ or __exec__, but only by explicitly doing something like the ``considered secure'' example above.
99
100
101 __Laundering and Detecting Tainted Data__
102
103
104 To test whether a variable contains tainted data, and whose
105 use would thus trigger an ``Insecure dependency'' message,
106 check your nearby CPAN mirror for the
107 ''Taint.pm'' module, which should become available around
108 November 1997. Or you may be able to use the following
109 ''is_tainted()'' function.
110
111
112 sub is_tainted {
113 return ! eval {
114 join('',@_), kill 0;
115 1;
116 };
117 }
118 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.
119
120
121 But testing for taintedness gets you only so far. Sometimes
122 you have just to clear your data's taintedness. The only way
123 to bypass the tainting mechanism is by referencing
124 subpatterns from a regular expression match. Perl presumes
125 that if you reference a substring using $1,
126 $2, etc., that you knew what you were doing when
127 you wrote the pattern. That means using a bit of
128 thought--don't just blindly untaint anything, or you defeat
129 the entire mechanism. It's better to verify that the
130 variable has only good characters (for certain values of
131 ``good'') rather than checking whether it has any bad
132 characters. That's because it's far too easy to miss bad
133 characters that you never thought of.
134
135
136 Here's a test to make sure that the data contains nothing
137 but ``word'' characters (alphabetics, numerics, and
138 underscores), a hyphen, an at sign, or a dot.
139
140
141 if ($data =~ /^([[-@w.]+)$/) {
142 $data = $1; # $data now untainted
143 } else {
144 die
145 This is fairly secure because /w+/ doesn't normally match shell metacharacters, nor are dot, dash, or at going to mean something special to the shell. Use of /.+/ 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 ''only'' mechanism for untainting dirty data, unless you use the strategy detailed below to fork a child of lesser privilege.
146
147
148 The example does not untaint $data if use
149 locale is in effect, because the characters matched by
150 w are determined by the locale. Perl considers that
151 locale definitions are untrustworthy because they contain
152 data from outside the program. If you are writing a
153 locale-aware program, and want to launder data with a
154 regular expression containing w, put no
155 locale ahead of the expression in the same block. See
156 `` SECURITY '' in perllocale for further
157 discussion and examples.
158
159
160 __Switches On the ``#!'' Line__
161
162
163 When you make a script executable, in order to make it
164 usable as a command, the system will pass switches to perl
165 from the script's #! line. Perl checks that any command line
166 switches given to a setuid (or setgid) script actually match
167 the ones set on the #! line. Some Unix and Unix-like
168 environments impose a one-switch limit on the #! line, so
169 you may need to use something like -wU instead of
170 -w -U under such systems. (This issue should arise
171 only in Unix or Unix-like environments that support #! and
172 setuid or setgid scripts.)
173
174
175 __Cleaning Up Your Path__
176
177
178 For $ENV{PATH}
179 $ENV{'PATH'} to a known value, and each
180 directory in the path must be non-writable by others than
181 its owner and group. You may be surprised to get this
182 message even if the pathname to your executable is fully
183 qualified. This is ''not'' generated because you didn't
184 supply a full path to the program; instead, it's generated
185 because you never set your PATH environment
186 variable, or you didn't set it to something that was safe.
187 Because Perl can't guarantee that the executable in question
188 isn't itself going to turn around and execute some other
189 program that is dependent on your PATH , it
190 makes sure you set the PATH .
191
192
193 The PATH isn't the only environment variable
194 which can cause problems. Because some shells may use the
195 variables IFS , CDPATH ,
196 ENV , and BASH_ENV , Perl
197 checks that those are either empty or untainted when
198 starting subprocesses. You may wish to add something like
199 this to your setid and taint-checking scripts.
200
201
202 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer
203 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 __after__ 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.
204
205
206 Perl does not call the shell to expand wild cards when you
207 pass __system__ and __exec__ explicit parameter lists
208 instead of strings with possible shell wildcards in them.
209 Unfortunately, the __open__, __glob__, and backtick
210 functions provide no such alternate calling convention, so
211 more subterfuge will be required.
212
213
214 Perl provides a reasonably safe way to open a file or pipe
215 from a setuid or setgid program: just create a child process
216 with reduced privilege who does the dirty work for you.
217 First, fork a child using the special __open__ syntax
218 that connects the parent and child by a pipe. Now the child
219 resets its ID set and any other per-process
220 attributes, like environment variables, umasks, current
221 working directories, back to the originals or known safe
222 values. Then the child process, which no longer has any
223 special permissions, does the __open__ or other system
224 call. Finally, the child passes the data it managed to
225 access back to the parent. Because the file or pipe was
226 opened in the child while running under less privilege than
227 the parent, it's not apt to be tricked into doing something
228 it shouldn't.
229
230
231 Here's a way to do backticks reasonably safely. Notice how
232 the __exec__ is not called with a string that the shell
233 could expand. This is by far the best way to call something
234 that might be subjected to shell escapes: just never call
235 the shell at all.
236
237
238 use English;
239 die
240 A similar strategy would work for wildcard expansion via glob, although you can use readdir instead.
241
242
243 Taint checking is most useful when although you trust
244 yourself not to have written a program to give away the
245 farm, you don't necessarily trust those who end up using it
246 not to try to trick it into doing something bad. This is the
247 kind of security checking that's useful for set-id programs
248 and programs launched on someone else's behalf, like
249 CGI programs.
250
251
252 This is quite different, however, from not even trusting the
253 writer of the code not to try to do something evil. That's
254 the kind of trust needed when someone hands you a program
255 you've never seen before and says, ``Here, run this.'' For
256 that kind of safety, check out the Safe module, included
257 standard in the Perl distribution. This module allows the
258 programmer to set up special compartments in which all
259 system operations are trapped and namespace access is
260 carefully controlled.
261
262
263 __Security Bugs__
264
265
266 Beyond the obvious problems that stem from giving special
267 privileges to systems as flexible as scripts, on many
268 versions of Unix, set-id scripts are inherently insecure
269 right from the start. The problem is a race condition in the
270 kernel. Between the time the kernel opens the file to see
271 which interpreter to run and when the (now-set-id)
272 interpreter turns around and reopens the file to interpret
273 it, the file in question may have changed, especially if you
274 have symbolic links on your system.
275
276
277 Fortunately, sometimes this kernel ``feature'' can be
278 disabled. Unfortunately, there are two ways to disable it.
279 The system can simply outlaw scripts with any set-id bit
280 set, which doesn't help much. Alternately, it can simply
281 ignore the set-id bits on scripts. If the latter is true,
282 Perl can emulate the setuid and setgid mechanism when it
283 notices the otherwise useless setuid/gid bits on Perl
284 scripts. It does this via a special executable called
285 __suidperl__ that is automatically invoked for you if
286 it's needed.
287
288
289 However, if the kernel set-id script feature isn't disabled,
290 Perl will complain loudly that your set-id script is
291 insecure. You'll need to either disable the kernel set-id
292 script feature, or put a C wrapper around the script. A C
293 wrapper is just a compiled program that does nothing except
294 call your Perl program. Compiled programs are not subject to
295 the kernel bug that plagues set-id scripts. Here's a simple
296 wrapper, written in C:
297
298
299 #define REAL_PATH
300 Compile this wrapper into a binary executable and then make ''it'' rather than your script setuid or setgid.
301
302
303 In recent years, vendors have begun to supply systems free
304 of this inherent security bug. On such systems, when the
305 kernel passes the name of the set-id script to open to the
306 interpreter, rather than using a pathname subject to
307 meddling, it instead passes ''/dev/fd/3''. This is a
308 special file already opened on the script, so that there can
309 be no race condition for evil scripts to exploit. On these
310 systems, Perl should be compiled with
311 -DSETUID_SCRIPTS_ARE_SECURE_NOW. The
312 __Configure__ program that builds Perl tries to figure
313 this out for itself, so you should never have to specify
314 this yourself. Most modern releases of SysVr4 and
315 BSD 4.4 use this approach to avoid the kernel
316 race condition.
317
318
319 Prior to release 5.6.1 of Perl, bugs in the code of
320 __suidperl__ could introduce a security
321 hole.
322
323
324 __Protecting Your Programs__
325
326
327 There are a number of ways to hide the source to your Perl
328 programs, with varying levels of ``security''.
329
330
331 First of all, however, you ''can't'' take away read
332 permission, because the source code has to be readable in
333 order to be compiled and interpreted. (That doesn't mean
334 that a CGI script's source is readable by
335 people on the web, though.) So you have to leave the
336 permissions at the socially friendly 0755 level. This lets
337 people on your local system only see your
338 source.
339
340
341 Some people mistakenly regard this as a security problem. If
342 your program does insecure things, and relies on people not
343 knowing how to exploit those insecurities, it is not secure.
344 It is often possible for someone to determine the insecure
345 things and exploit them without viewing the source. Security
346 through obscurity, the name for hiding your bugs instead of
347 fixing them, is little security indeed.
348
349
350 You can try using encryption via source filters (Filter::*
351 from CPAN ). But crackers might be able to
352 decrypt it. You can try using the byte code compiler and
353 interpreter described below, but crackers might be able to
354 de-compile it. You can try using the native-code compiler
355 described below, but crackers might be able to disassemble
356 it. These pose varying degrees of difficulty to people
357 wanting to get at your code, but none can definitively
358 conceal it (this is true of every language, not just
359 Perl).
360
361
362 If you're concerned about people profiting from your code,
363 then the bottom line is that nothing but a restrictive
364 licence will give you legal security. License your software
365 and pepper it with threatening statements like ``This is
366 unpublished proprietary software of XYZ Corp.
367 Your access to it does not give you permission to use it
368 blah blah blah.'' You should see a lawyer to be sure your
369 licence's wording will stand up in court.
370 !!SEE ALSO
371
372
373 perlrun for its description of cleaning up environment
374 variables.
375 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.