3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
5 # I don't claim to know all about tainting. If anyone sees
6 # tests that I've missed here, please add them. But this is
7 # better than having no tests at all, right?
12 @INC = '../lib' if -d '../lib';
18 my $Is_VMS = $^O eq 'VMS';
19 my $Is_MSWin32 = $^O eq 'MSWin32';
20 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
21 $Is_MSWin32 ? '.\perl' : './perl';
22 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
26 for $x ('DCL$PATH', @MoreEnv) {
27 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
32 warn "# Note: logical name 'PATH' may have been deleted\n";
33 @ENV{keys %old} = values %old;
39 # The empty tainted value, for tainting strings
40 my $TAINT = substr($^X, 0, 0);
41 # A tainted zero, useful for tainting numbers
42 my $TAINT0 = 0 + $TAINT;
44 # This taints each argument passed. All must be lvalues.
45 # Side effect: It also stringifies them. :-(
47 for (@_) { $_ .= $TAINT }
50 # How to identify taint when you see it
52 not eval { join("",@_), kill 0; 1 };
58 for (@_) { return 0 unless tainted $_ }
63 my($serial, $boolean, $diag) = @_;
67 print "not ok $serial\n";
68 for (split m/^/m, $diag) {
73 or substr($diag, -1) eq "\n";
77 # We need an external program to call.
78 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
80 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
81 print PROG 'print "@ARGV\n"', "\n";
83 my $echo = "$Invoke_Perl $ECHO";
87 # First, let's make sure that Perl is checking the dangerous
88 # environment variables. Maybe they aren't set yet, so we'll
89 # taint them ourselves.
91 $ENV{'DCL$PATH'} = '' if $Is_VMS;
94 delete @ENV{@MoreEnv};
97 test 1, eval { `$echo 1` } eq "1\n";
100 print "# Environment tainting tests skipped\n";
101 for (2..5) { print "ok $_\n" }
104 my @vars = ('PATH', @MoreEnv);
105 while (my $v = $vars[0]) {
106 local $ENV{$v} = $TAINT;
107 last if eval { `$echo 1` };
108 last unless $@ =~ /^Insecure \$ENV{$v}/;
111 test 2, !@vars, "\$$vars[0]";
113 # tainted $TERM is unsafe only if it contains metachars
115 $ENV{TERM} = 'e=mc2';
116 test 3, eval { `$echo 1` } eq "1\n";
117 $ENV{TERM} = 'e=mc2' . $TAINT;
118 test 4, eval { `$echo 1` } eq '';
119 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
123 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32) {
124 print "# all directories are writeable\n";
127 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
128 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
129 @ENV{qw(TMP TEMP)})[0]
130 or print "# can't find world-writeable directory to test PATH\n";
134 local $ENV{PATH} = $tmp;
135 test 6, eval { `$echo 1` } eq '';
136 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
139 for (6..7) { print "ok $_\n" }
143 $ENV{'DCL$PATH'} = $TAINT;
144 test 8, eval { `$echo 1` } eq '';
145 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
147 $ENV{'DCL$PATH'} = $tmp;
148 test 10, eval { `$echo 1` } eq '';
149 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
152 print "# can't find world-writeable directory to test DCL\$PATH\n";
153 for (10..11) { print "ok $_\n" }
155 $ENV{'DCL$PATH'} = '';
158 print "# This is not VMS\n";
159 for (8..11) { print "ok $_\n"; }
163 # Let's see that we can taint and untaint as needed.
166 test 12, tainted $foo;
168 # That was a sanity check. If it failed, stop the insanity!
169 die "Taint checks don't seem to be enabled" unless tainted $foo;
172 test 13, not tainted $foo;
175 test 14, tainted $foo;
178 test 15, not any_tainted @list;
179 taint_these @list[1,3,5,7,9];
180 test 16, any_tainted @list;
181 test 17, all_tainted @list[1,3,5,7,9];
182 test 18, not any_tainted @list[0,2,4,6,8];
184 ($foo) = $foo =~ /(.+)/;
185 test 19, not tainted $foo;
187 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
188 test 20, not tainted $foo;
189 test 21, $foo eq 'bar';
191 my $pi = 4 * atan2(1,1) + $TAINT0;
192 test 22, tainted $pi;
194 ($pi) = $pi =~ /(\d+\.\d+)/;
195 test 23, not tainted $pi;
196 test 24, sprintf("%.5f", $pi) eq '3.14159';
199 # How about command-line arguments? The problem is that we don't
200 # always get some, so we'll run another process with some.
203 open PROG, "> $arg" or die "Can't create $arg: $!";
205 eval { join('', @ARGV), kill 0 };
206 exit 0 if $@ =~ /^Insecure dependency/;
207 print "# Oops: \$@ was [$@]\n";
211 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
212 test 25, !$?, "Exited with status $?";
216 # Reading from a file should be tainted
219 test 26, open(FILE, $file), "Couldn't open '$file': $!";
222 sysread(FILE, $block, 100);
225 test 27, tainted $block;
226 test 28, tainted $line;
229 # Globs should be forbidden, except under VMS,
230 # which doesn't spawn an external program.
232 for (29..30) { print "ok $_\n"; }
235 my @globs = eval { <*> };
236 test 29, @globs == 0 && $@ =~ /^Insecure dependency/;
238 @globs = eval { glob '*' };
239 test 30, @globs == 0 && $@ =~ /^Insecure dependency/;
242 # Output of commands should be tainted
244 my $foo = `$echo abc`;
245 test 31, tainted $foo;
248 # Certain system variables should be tainted
250 test 32, all_tainted $^X, $0;
253 # Results of matching should all be untainted
255 my $foo = "abcdefghi" . $TAINT;
256 test 33, tainted $foo;
259 test 34, not any_tainted $`, $&, $';
261 $foo =~ /(...)(...)(...)/;
262 test 35, not any_tainted $1, $2, $3, $+;
264 my @bar = $foo =~ /(...)(...)(...)/;
265 test 36, not any_tainted @bar;
267 test 37, tainted $foo; # $foo should still be tainted!
268 test 38, $foo eq "abcdefghi";
271 # Operations which affect files can't use tainted data.
273 test 39, eval { chmod 0, $TAINT } eq '', 'chmod';
274 test 40, $@ =~ /^Insecure dependency/, $@;
276 # There is no feature test in $Config{} for truncate,
277 # so we allow for the possibility that it's missing.
278 test 41, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
279 test 42, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
281 test 43, eval { rename '', $TAINT } eq '', 'rename';
282 test 44, $@ =~ /^Insecure dependency/, $@;
284 test 45, eval { unlink $TAINT } eq '', 'unlink';
285 test 46, $@ =~ /^Insecure dependency/, $@;
287 test 47, eval { utime $TAINT } eq '', 'utime';
288 test 48, $@ =~ /^Insecure dependency/, $@;
290 if ($Config{d_chown}) {
291 test 49, eval { chown -1, -1, $TAINT } eq '', 'chown';
292 test 50, $@ =~ /^Insecure dependency/, $@;
295 print "# chown() is not available\n";
296 for (49..50) { print "ok $_\n" }
299 if ($Config{d_link}) {
300 test 51, eval { link $TAINT, '' } eq '', 'link';
301 test 52, $@ =~ /^Insecure dependency/, $@;
304 print "# link() is not available\n";
305 for (51..52) { print "ok $_\n" }
308 if ($Config{d_symlink}) {
309 test 53, eval { symlink $TAINT, '' } eq '', 'symlink';
310 test 54, $@ =~ /^Insecure dependency/, $@;
313 print "# symlink() is not available\n";
314 for (53..54) { print "ok $_\n" }
318 # Operations which affect directories can't use tainted data.
320 test 55, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
321 test 56, $@ =~ /^Insecure dependency/, $@;
323 test 57, eval { rmdir $TAINT } eq '', 'rmdir';
324 test 58, $@ =~ /^Insecure dependency/, $@;
326 test 59, eval { chdir $TAINT } eq '', 'chdir';
327 test 60, $@ =~ /^Insecure dependency/, $@;
329 if ($Config{d_chroot}) {
330 test 61, eval { chroot $TAINT } eq '', 'chroot';
331 test 62, $@ =~ /^Insecure dependency/, $@;
334 print "# chroot() is not available\n";
335 for (61..62) { print "ok $_\n" }
339 # Some operations using files can't use tainted data.
341 my $foo = "imaginary library" . $TAINT;
342 test 63, eval { require $foo } eq '', 'require';
343 test 64, $@ =~ /^Insecure dependency/, $@;
345 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
346 END { unlink $filename if defined $filename }
347 $foo = $filename . $TAINT;
348 unlink $filename; # in any case
350 test 65, eval { open FOO, $foo } eq '', 'open for read';
351 test 66, $@ eq '', $@; # NB: This should be allowed
352 test 67, $! == 2; # File not found
354 test 68, eval { open FOO, "> $foo" } eq '', 'open for write';
355 test 69, $@ =~ /^Insecure dependency/, $@;
358 # Commands to the system can't use tainted data
362 if ($^O eq 'amigaos') {
363 print "# open(\"|\") is not available\n";
364 for (70..73) { print "ok $_\n" }
367 test 70, eval { open FOO, "| $foo" } eq '', 'popen to';
368 test 71, $@ =~ /^Insecure dependency/, $@;
370 test 72, eval { open FOO, "$foo |" } eq '', 'popen from';
371 test 73, $@ =~ /^Insecure dependency/, $@;
374 test 74, eval { exec $TAINT } eq '', 'exec';
375 test 75, $@ =~ /^Insecure dependency/, $@;
377 test 76, eval { system $TAINT } eq '', 'system';
378 test 77, $@ =~ /^Insecure dependency/, $@;
383 test 78, eval { `$echo 1$foo` } eq '', 'backticks';
384 test 79, $@ =~ /^Insecure dependency/, $@;
386 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
387 test 80, join('', eval { glob $foo } ) ne '', 'globbing';
388 test 81, $@ eq '', $@;
391 for (80..81) { print "ok $_\n"; }
395 # Operations which affect processes can't use tainted data.
397 test 82, eval { kill 0, $TAINT } eq '', 'kill';
398 test 83, $@ =~ /^Insecure dependency/, $@;
400 if ($Config{d_setpgrp}) {
401 test 84, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
402 test 85, $@ =~ /^Insecure dependency/, $@;
405 print "# setpgrp() is not available\n";
406 for (84..85) { print "ok $_\n" }
409 if ($Config{d_setprior}) {
410 test 86, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
411 test 87, $@ =~ /^Insecure dependency/, $@;
414 print "# setpriority() is not available\n";
415 for (86..87) { print "ok $_\n" }
419 # Some miscellaneous operations can't use tainted data.
421 if ($Config{d_syscall}) {
422 test 88, eval { syscall $TAINT } eq '', 'syscall';
423 test 89, $@ =~ /^Insecure dependency/, $@;
426 print "# syscall() is not available\n";
427 for (88..89) { print "ok $_\n" }
434 my $temp = "./taintC$$";
436 test 90, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
438 test 91, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
439 test 92, $@ =~ /^Insecure dependency/, $@;
441 if ($Config{d_fcntl}) {
442 test 93, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
443 test 94, $@ =~ /^Insecure dependency/, $@;
446 print "# fcntl() is not available\n";
447 for (93..94) { print "ok $_\n" }
454 # Some tests involving references
456 my $foo = 'abc' . $TAINT;
458 test 95, not tainted $fooref;
459 test 96, tainted $$fooref;
460 test 97, tainted $foo;
463 # Some tests involving assignment
467 test 98, all_tainted $foo, $bar;
468 test 99, tainted($foo = $bar);
469 test 100, tainted($bar = $bar);
470 test 101, tainted($bar += $bar);
471 test 102, tainted($bar -= $bar);
472 test 103, tainted($bar *= $bar);
473 test 104, tainted($bar++);
474 test 105, tainted($bar /= $bar);
475 test 106, tainted($bar += 0);
476 test 107, tainted($bar -= 2);
477 test 108, tainted($bar *= -1);
478 test 109, tainted($bar /= 1);
479 test 110, tainted($bar--);
483 # Test assignment and return of lists
485 my @foo = ("A", "tainted" . $TAINT, "B");
486 test 112, not tainted $foo[0];
487 test 113, tainted $foo[1];
488 test 114, not tainted $foo[2];
490 test 115, not tainted $bar[0];
491 test 116, tainted $bar[1];
492 test 117, not tainted $bar[2];
493 my @baz = eval { "A", "tainted" . $TAINT, "B" };
494 test 118, not tainted $baz[0];
495 test 119, tainted $baz[1];
496 test 120, not tainted $baz[2];
497 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
498 test 121, not tainted $plugh[0];
499 test 122, tainted $plugh[1];
500 test 123, not tainted $plugh[2];
501 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
502 test 124, not tainted ((&$nautilus)[0]);
503 test 125, tainted ((&$nautilus)[1]);
504 test 126, not tainted ((&$nautilus)[2]);
505 my @xyzzy = &$nautilus;
506 test 127, not tainted $xyzzy[0];
507 test 128, tainted $xyzzy[1];
508 test 129, not tainted $xyzzy[2];
509 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
510 test 130, not tainted ((&$red_october)[0]);
511 test 131, tainted ((&$red_october)[1]);
512 test 132, not tainted ((&$red_october)[2]);
513 my @corge = &$red_october;
514 test 133, not tainted $corge[0];
515 test 134, tainted $corge[1];
516 test 135, not tainted $corge[2];