Commit | Line | Data |
1e422769 |
1 | #!./perl -T |
2 | # |
3 | # Taint tests by Tom Phoenix <rootbeer@teleport.com>. |
4 | # |
5 | # I don't claim to know all about tainting. If anyone sees |
9607fc9c |
6 | # tests that I've missed here, please add them. But this is |
1e422769 |
7 | # better than having no tests at all, right? |
8 | # |
9 | |
10 | BEGIN { |
11 | chdir 't' if -d 't'; |
12 | @INC = '../lib' if -d '../lib'; |
13 | } |
14 | |
15 | use strict; |
16 | use Config; |
17 | |
18 | my $Is_VMS = $^O eq 'VMS'; |
19 | my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : './perl'; |
20 | if ($Is_VMS) { |
2b572567 |
21 | my($olddcl) = $ENV{'DCL$PATH'} =~ /^(.*)$/; |
22 | my($oldifs) = $ENV{IFS} =~ /^(.*)$/; |
1e422769 |
23 | eval <<EndOfCleanup; |
24 | END { |
25 | \$ENV{PATH} = ''; |
26 | warn "# Note: logical name 'PATH' may have been deleted\n"; |
2b572567 |
27 | \$ENV{IFS} = "$oldifs"; |
28 | \$ENV{'DCL\$PATH'} = "$olddcl"; |
1e422769 |
29 | } |
30 | EndOfCleanup |
31 | } |
32 | |
33 | # Sources of taint: |
34 | # The empty tainted value, for tainting strings |
35 | my $TAINT = substr($^X, 0, 0); |
36 | # A tainted zero, useful for tainting numbers |
37 | my $TAINT0 = 0 + $TAINT; |
38 | |
39 | # This taints each argument passed. All must be lvalues. |
40 | # Side effect: It also stringifies them. :-( |
41 | sub taint_these (@) { |
42 | for (@_) { $_ .= $TAINT } |
43 | } |
44 | |
45 | # How to identify taint when you see it |
46 | sub any_tainted (@) { |
47 | not eval { join("",@_), kill 0; 1 }; |
48 | } |
49 | sub tainted ($) { |
50 | any_tainted @_; |
51 | } |
52 | sub all_tainted (@) { |
53 | for (@_) { return 0 unless tainted $_ } |
54 | 1; |
55 | } |
56 | |
57 | sub test ($$;$) { |
58 | my($serial, $boolean, $diag) = @_; |
59 | if ($boolean) { |
60 | print "ok $serial\n"; |
61 | } else { |
62 | print "not ok $serial\n"; |
63 | for (split m/^/m, $diag) { |
64 | print "# $_"; |
65 | } |
9607fc9c |
66 | print "\n" unless |
1e422769 |
67 | $diag eq '' |
68 | or substr($diag, -1) eq "\n"; |
69 | } |
70 | } |
71 | |
72 | # We need an external program to call. |
73 | my $ECHO = "./echo$$"; |
74 | END { unlink $ECHO } |
75 | open PROG, "> $ECHO" or die "Can't create $ECHO: $!"; |
76 | print PROG 'print "@ARGV\n"', "\n"; |
77 | close PROG; |
78 | my $echo = "$Invoke_Perl $ECHO"; |
79 | |
9607fc9c |
80 | print "1..98\n"; |
1e422769 |
81 | |
82 | # First, let's make sure that Perl is checking the dangerous |
83 | # environment variables. Maybe they aren't set yet, so we'll |
84 | # taint them ourselves. |
85 | { |
86 | $ENV{'DCL$PATH'} = '' if $Is_VMS; |
87 | |
88 | $ENV{PATH} = $TAINT; |
9607fc9c |
89 | $ENV{IFS} = " \t\n"; |
1e422769 |
90 | test 1, eval { `$echo 1` } eq ''; |
91 | test 2, $@ =~ /^Insecure \$ENV{PATH}/, $@; |
92 | |
93 | $ENV{PATH} = ''; |
94 | $ENV{IFS} = $TAINT; |
95 | test 3, eval { `$echo 1` } eq ''; |
96 | test 4, $@ =~ /^Insecure \$ENV{IFS}/, $@; |
97 | |
9607fc9c |
98 | my $tmp; |
99 | if ($^O eq 'os2' || $^O eq 'amigaos') { |
100 | print "# all directories are writeable\n"; |
101 | } |
102 | else { |
103 | $tmp = (grep { defined and -d and (stat _)[2] & 2 } |
104 | qw(/tmp /var/tmp /usr/tmp /sys$scratch), |
105 | @ENV{qw(TMP TEMP)})[0] |
106 | or print "# can't find world-writeable directory to test PATH\n"; |
107 | } |
108 | |
385588b3 |
109 | if ($tmp) { |
1e422769 |
110 | $ENV{PATH} = $tmp; |
9607fc9c |
111 | $ENV{IFS} = " \t\n"; |
1e422769 |
112 | test 5, eval { `$echo 1` } eq ''; |
113 | test 6, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@; |
114 | } |
115 | else { |
1e422769 |
116 | for (5..6) { print "ok $_\n" } |
117 | } |
118 | |
119 | $ENV{PATH} = ''; |
9607fc9c |
120 | $ENV{IFS} = " \t\n"; |
1e422769 |
121 | test 7, eval { `$echo 1` } eq "1\n"; |
122 | test 8, $@ eq '', $@; |
123 | |
124 | if ($Is_VMS) { |
125 | $ENV{'DCL$PATH'} = $TAINT; |
126 | test 9, eval { `$echo 1` } eq ''; |
127 | test 10, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@; |
9607fc9c |
128 | if ($tmp) { |
129 | $ENV{'DCL$PATH'} = $tmp; |
130 | test 11, eval { `$echo 1` } eq ''; |
131 | test 12, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@; |
132 | } |
133 | else { |
134 | print "# can't find world-writeable directory to test DCL\$PATH\n"; |
135 | for (11..12) { print "ok $_\n" } |
136 | } |
1e422769 |
137 | $ENV{'DCL$PATH'} = ''; |
138 | } |
139 | else { |
140 | print "# This is not VMS\n"; |
9607fc9c |
141 | for (9..12) { print "ok $_\n"; } |
1e422769 |
142 | } |
143 | } |
144 | |
145 | # Let's see that we can taint and untaint as needed. |
146 | { |
147 | my $foo = $TAINT; |
9607fc9c |
148 | test 13, tainted $foo; |
149 | |
150 | # That was a sanity check. If it failed, stop the insanity! |
151 | die "Taint checks don't seem to be enabled" unless tainted $foo; |
1e422769 |
152 | |
153 | $foo = "foo"; |
9607fc9c |
154 | test 14, not tainted $foo; |
1e422769 |
155 | |
156 | taint_these($foo); |
9607fc9c |
157 | test 15, tainted $foo; |
1e422769 |
158 | |
159 | my @list = 1..10; |
9607fc9c |
160 | test 16, not any_tainted @list; |
1e422769 |
161 | taint_these @list[1,3,5,7,9]; |
9607fc9c |
162 | test 17, any_tainted @list; |
163 | test 18, all_tainted @list[1,3,5,7,9]; |
164 | test 19, not any_tainted @list[0,2,4,6,8]; |
1e422769 |
165 | |
166 | ($foo) = $foo =~ /(.+)/; |
9607fc9c |
167 | test 20, not tainted $foo; |
1e422769 |
168 | |
169 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
9607fc9c |
170 | test 21, not tainted $foo; |
171 | test 22, $foo eq 'bar'; |
1e422769 |
172 | |
173 | my $pi = 4 * atan2(1,1) + $TAINT0; |
9607fc9c |
174 | test 23, tainted $pi; |
1e422769 |
175 | |
176 | ($pi) = $pi =~ /(\d+\.\d+)/; |
9607fc9c |
177 | test 24, not tainted $pi; |
178 | test 25, sprintf("%.5f", $pi) eq '3.14159'; |
1e422769 |
179 | } |
180 | |
181 | # How about command-line arguments? The problem is that we don't |
182 | # always get some, so we'll run another process with some. |
183 | { |
184 | my $arg = "./arg$$"; |
185 | open PROG, "> $arg" or die "Can't create $arg: $!"; |
186 | print PROG q{ |
187 | eval { join('', @ARGV), kill 0 }; |
188 | exit 0 if $@ =~ /^Insecure dependency/; |
189 | print "# Oops: \$@ was [$@]\n"; |
190 | exit 1; |
191 | }; |
192 | close PROG; |
193 | print `$Invoke_Perl "-T" $arg and some suspect arguments`; |
9607fc9c |
194 | test 26, !$?, "Exited with status $?"; |
1e422769 |
195 | unlink $arg; |
196 | } |
197 | |
198 | # Reading from a file should be tainted |
199 | { |
9607fc9c |
200 | my $file = './TEST'; |
201 | test 27, open(FILE, $file), "Couldn't open '$file': $!"; |
1e422769 |
202 | |
203 | my $block; |
204 | sysread(FILE, $block, 100); |
9607fc9c |
205 | my $line = <FILE>; |
1e422769 |
206 | close FILE; |
9607fc9c |
207 | test 28, tainted $block; |
208 | test 29, tainted $line; |
1e422769 |
209 | } |
210 | |
9607fc9c |
211 | # Globs should be tainted. |
1e422769 |
212 | { |
9607fc9c |
213 | # Some glob implementations need to spawn system programs. |
214 | local $ENV{PATH} = ''; |
215 | $ENV{PATH} = (-l '/bin' ? '' : '/bin:') . '/usr/bin' unless $Is_VMS; |
216 | |
1e422769 |
217 | my @globs = <*>; |
9607fc9c |
218 | test 30, all_tainted @globs; |
1e422769 |
219 | |
220 | @globs = glob '*'; |
9607fc9c |
221 | test 31, all_tainted @globs; |
1e422769 |
222 | } |
223 | |
224 | # Output of commands should be tainted |
225 | { |
226 | my $foo = `$echo abc`; |
9607fc9c |
227 | test 32, tainted $foo; |
1e422769 |
228 | } |
229 | |
230 | # Certain system variables should be tainted |
231 | { |
9607fc9c |
232 | test 33, all_tainted $^X, $0; |
1e422769 |
233 | } |
234 | |
235 | # Results of matching should all be untainted |
236 | { |
237 | my $foo = "abcdefghi" . $TAINT; |
9607fc9c |
238 | test 34, tainted $foo; |
1e422769 |
239 | |
240 | $foo =~ /def/; |
9607fc9c |
241 | test 35, not any_tainted $`, $&, $'; |
1e422769 |
242 | |
243 | $foo =~ /(...)(...)(...)/; |
9607fc9c |
244 | test 36, not any_tainted $1, $2, $3, $+; |
1e422769 |
245 | |
246 | my @bar = $foo =~ /(...)(...)(...)/; |
9607fc9c |
247 | test 37, not any_tainted @bar; |
1e422769 |
248 | |
9607fc9c |
249 | test 38, tainted $foo; # $foo should still be tainted! |
250 | test 39, $foo eq "abcdefghi"; |
1e422769 |
251 | } |
252 | |
253 | # Operations which affect files can't use tainted data. |
254 | { |
9607fc9c |
255 | test 40, eval { chmod 0, $TAINT } eq '', 'chmod'; |
1e422769 |
256 | test 41, $@ =~ /^Insecure dependency/, $@; |
257 | |
9607fc9c |
258 | # There is no feature test in $Config{} for truncate, |
259 | # so we allow for the possibility that it's missing. |
260 | test 42, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate'; |
261 | test 43, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@; |
1e422769 |
262 | |
9607fc9c |
263 | test 44, eval { rename '', $TAINT } eq '', 'rename'; |
1e422769 |
264 | test 45, $@ =~ /^Insecure dependency/, $@; |
265 | |
9607fc9c |
266 | test 46, eval { unlink $TAINT } eq '', 'unlink'; |
1e422769 |
267 | test 47, $@ =~ /^Insecure dependency/, $@; |
268 | |
9607fc9c |
269 | test 48, eval { utime $TAINT } eq '', 'utime'; |
270 | test 49, $@ =~ /^Insecure dependency/, $@; |
271 | |
1e422769 |
272 | if ($Config{d_chown}) { |
9607fc9c |
273 | test 50, eval { chown -1, -1, $TAINT } eq '', 'chown'; |
274 | test 51, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
275 | } |
276 | else { |
277 | print "# chown() is not available\n"; |
9607fc9c |
278 | for (50..51) { print "ok $_\n" } |
1e422769 |
279 | } |
280 | |
281 | if ($Config{d_link}) { |
9607fc9c |
282 | test 52, eval { link $TAINT, '' } eq '', 'link'; |
283 | test 53, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
284 | } |
285 | else { |
286 | print "# link() is not available\n"; |
9607fc9c |
287 | for (52..53) { print "ok $_\n" } |
1e422769 |
288 | } |
289 | |
290 | if ($Config{d_symlink}) { |
9607fc9c |
291 | test 54, eval { symlink $TAINT, '' } eq '', 'symlink'; |
292 | test 55, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
293 | } |
294 | else { |
295 | print "# symlink() is not available\n"; |
9607fc9c |
296 | for (54..55) { print "ok $_\n" } |
1e422769 |
297 | } |
298 | } |
299 | |
300 | # Operations which affect directories can't use tainted data. |
301 | { |
9607fc9c |
302 | test 56, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir'; |
1e422769 |
303 | test 57, $@ =~ /^Insecure dependency/, $@; |
304 | |
9607fc9c |
305 | test 58, eval { rmdir $TAINT } eq '', 'rmdir'; |
1e422769 |
306 | test 59, $@ =~ /^Insecure dependency/, $@; |
307 | |
9607fc9c |
308 | test 60, eval { chdir $TAINT } eq '', 'chdir'; |
309 | test 61, $@ =~ /^Insecure dependency/, $@; |
310 | |
1e422769 |
311 | if ($Config{d_chroot}) { |
9607fc9c |
312 | test 62, eval { chroot $TAINT } eq '', 'chroot'; |
313 | test 63, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
314 | } |
315 | else { |
316 | print "# chroot() is not available\n"; |
9607fc9c |
317 | for (62..63) { print "ok $_\n" } |
1e422769 |
318 | } |
319 | } |
320 | |
321 | # Some operations using files can't use tainted data. |
322 | { |
323 | my $foo = "imaginary library" . $TAINT; |
9607fc9c |
324 | test 64, eval { require $foo } eq '', 'require'; |
325 | test 65, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
326 | |
327 | my $filename = "./taintB$$"; # NB: $filename isn't tainted! |
328 | END { unlink $filename if defined $filename } |
329 | $foo = $filename . $TAINT; |
330 | unlink $filename; # in any case |
331 | |
9607fc9c |
332 | test 66, eval { open FOO, $foo } eq '', 'open for read'; |
333 | test 67, $@ eq '', $@; # NB: This should be allowed |
334 | test 68, $! == 2; # File not found |
1e422769 |
335 | |
9607fc9c |
336 | test 69, eval { open FOO, "> $foo" } eq '', 'open for write'; |
337 | test 70, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
338 | } |
339 | |
340 | # Commands to the system can't use tainted data |
341 | { |
342 | my $foo = $TAINT; |
343 | |
344 | if ($^O eq 'amigaos') { |
345 | print "# open(\"|\") is not available\n"; |
9607fc9c |
346 | for (71..74) { print "ok $_\n" } |
1e422769 |
347 | } |
348 | else { |
9607fc9c |
349 | test 71, eval { open FOO, "| $foo" } eq '', 'popen to'; |
1e422769 |
350 | test 72, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
351 | |
9607fc9c |
352 | test 73, eval { open FOO, "$foo |" } eq '', 'popen from'; |
353 | test 74, $@ =~ /^Insecure dependency/, $@; |
354 | } |
1e422769 |
355 | |
9607fc9c |
356 | test 75, eval { exec $TAINT } eq '', 'exec'; |
1e422769 |
357 | test 76, $@ =~ /^Insecure dependency/, $@; |
358 | |
9607fc9c |
359 | test 77, eval { system $TAINT } eq '', 'system'; |
360 | test 78, $@ =~ /^Insecure dependency/, $@; |
361 | |
1e422769 |
362 | $foo = "*"; |
363 | taint_these $foo; |
364 | |
9607fc9c |
365 | test 79, eval { `$echo 1$foo` } eq '', 'backticks'; |
366 | test 80, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
367 | |
368 | if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe |
9607fc9c |
369 | test 81, join('', eval { glob $foo } ) ne '', 'globbing'; |
370 | test 82, $@ eq '', $@; |
1e422769 |
371 | } |
372 | else { |
9607fc9c |
373 | test 81, join('', eval { glob $foo } ) eq '', 'globbing'; |
374 | test 82, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
375 | } |
376 | } |
377 | |
378 | # Operations which affect processes can't use tainted data. |
379 | { |
9607fc9c |
380 | test 83, eval { kill 0, $TAINT } eq '', 'kill'; |
381 | test 84, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
382 | |
383 | if ($Config{d_setpgrp}) { |
9607fc9c |
384 | test 85, eval { setpgrp 0, $TAINT } eq '', 'setpgrp'; |
385 | test 86, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
386 | } |
387 | else { |
388 | print "# setpgrp() is not available\n"; |
9607fc9c |
389 | for (85..86) { print "ok $_\n" } |
1e422769 |
390 | } |
391 | |
392 | if ($Config{d_setprior}) { |
9607fc9c |
393 | test 87, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority'; |
394 | test 88, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
395 | } |
396 | else { |
397 | print "# setpriority() is not available\n"; |
9607fc9c |
398 | for (87..88) { print "ok $_\n" } |
1e422769 |
399 | } |
400 | } |
401 | |
402 | # Some miscellaneous operations can't use tainted data. |
403 | { |
404 | if ($Config{d_syscall}) { |
9607fc9c |
405 | test 89, eval { syscall $TAINT } eq '', 'syscall'; |
406 | test 90, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
407 | } |
408 | else { |
409 | print "# syscall() is not available\n"; |
9607fc9c |
410 | for (89..90) { print "ok $_\n" } |
1e422769 |
411 | } |
412 | |
413 | { |
414 | my $foo = "x" x 979; |
415 | taint_these $foo; |
416 | local *FOO; |
417 | my $temp = "./taintC$$"; |
418 | END { unlink $temp } |
9607fc9c |
419 | test 91, open(FOO, "> $temp"), "Couldn't open $temp for write: $!"; |
1e422769 |
420 | |
9607fc9c |
421 | test 92, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl'; |
422 | test 93, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
423 | |
424 | if ($Config{d_fcntl}) { |
9607fc9c |
425 | test 94, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl'; |
426 | test 95, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
427 | } |
428 | else { |
429 | print "# fcntl() is not available\n"; |
9607fc9c |
430 | for (94..95) { print "ok $_\n" } |
1e422769 |
431 | } |
432 | |
433 | close FOO; |
434 | } |
435 | } |
436 | |
9607fc9c |
437 | # Some tests involving references |
1e422769 |
438 | { |
439 | my $foo = 'abc' . $TAINT; |
440 | my $fooref = \$foo; |
9607fc9c |
441 | test 96, not tainted $fooref; |
442 | test 97, tainted $$fooref; |
443 | test 98, tainted $foo; |
1e422769 |
444 | } |