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 $Is_Dos = $^O eq 'dos';
21 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
22 $Is_MSWin32 ? '.\perl' : './perl';
23 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
27 for $x ('DCL$PATH', @MoreEnv) {
28 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
33 warn "# Note: logical name 'PATH' may have been deleted\n";
34 @ENV{keys %old} = values %old;
40 # The empty tainted value, for tainting strings
41 my $TAINT = substr($^X, 0, 0);
42 # A tainted zero, useful for tainting numbers
43 my $TAINT0 = 0 + $TAINT;
45 # This taints each argument passed. All must be lvalues.
46 # Side effect: It also stringifies them. :-(
48 for (@_) { $_ .= $TAINT }
51 # How to identify taint when you see it
53 not eval { join("",@_), kill 0; 1 };
59 for (@_) { return 0 unless tainted $_ }
64 my($serial, $boolean, $diag) = @_;
68 print "not ok $serial\n";
69 for (split m/^/m, $diag) {
74 or substr($diag, -1) eq "\n";
78 # We need an external program to call.
79 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
81 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
82 print PROG 'print "@ARGV\n"', "\n";
84 my $echo = "$Invoke_Perl $ECHO";
88 # First, let's make sure that Perl is checking the dangerous
89 # environment variables. Maybe they aren't set yet, so we'll
90 # taint them ourselves.
92 $ENV{'DCL$PATH'} = '' if $Is_VMS;
95 delete @ENV{@MoreEnv};
98 test 1, eval { `$echo 1` } eq "1\n";
100 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
101 print "# Environment tainting tests skipped\n";
102 for (2..5) { print "ok $_\n" }
105 my @vars = ('PATH', @MoreEnv);
106 while (my $v = $vars[0]) {
107 local $ENV{$v} = $TAINT;
108 last if eval { `$echo 1` };
109 last unless $@ =~ /^Insecure \$ENV{$v}/;
112 test 2, !@vars, "\$$vars[0]";
114 # tainted $TERM is unsafe only if it contains metachars
116 $ENV{TERM} = 'e=mc2';
117 test 3, eval { `$echo 1` } eq "1\n";
118 $ENV{TERM} = 'e=mc2' . $TAINT;
119 test 4, eval { `$echo 1` } eq '';
120 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
124 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
125 print "# all directories are writeable\n";
128 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
129 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
130 @ENV{qw(TMP TEMP)})[0]
131 or print "# can't find world-writeable directory to test PATH\n";
135 local $ENV{PATH} = $tmp;
136 test 6, eval { `$echo 1` } eq '';
137 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
140 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
144 $ENV{'DCL$PATH'} = $TAINT;
145 test 8, eval { `$echo 1` } eq '';
146 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
148 $ENV{'DCL$PATH'} = $tmp;
149 test 10, eval { `$echo 1` } eq '';
150 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
153 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
155 $ENV{'DCL$PATH'} = '';
158 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
162 # Let's see that we can taint and untaint as needed.
165 test 12, tainted $foo;
167 # That was a sanity check. If it failed, stop the insanity!
168 die "Taint checks don't seem to be enabled" unless tainted $foo;
171 test 13, not tainted $foo;
174 test 14, tainted $foo;
177 test 15, not any_tainted @list;
178 taint_these @list[1,3,5,7,9];
179 test 16, any_tainted @list;
180 test 17, all_tainted @list[1,3,5,7,9];
181 test 18, not any_tainted @list[0,2,4,6,8];
183 ($foo) = $foo =~ /(.+)/;
184 test 19, not tainted $foo;
186 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
187 test 20, not tainted $foo;
188 test 21, $foo eq 'bar';
190 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/t;
191 test 22, tainted $foo;
192 test 23, $foo eq 'bar';
194 my $pi = 4 * atan2(1,1) + $TAINT0;
195 test 24, tainted $pi;
197 ($pi) = $pi =~ /(\d+\.\d+)/;
198 test 25, not tainted $pi;
199 test 26, sprintf("%.5f", $pi) eq '3.14159';
202 # How about command-line arguments? The problem is that we don't
203 # always get some, so we'll run another process with some.
206 open PROG, "> $arg" or die "Can't create $arg: $!";
208 eval { join('', @ARGV), kill 0 };
209 exit 0 if $@ =~ /^Insecure dependency/;
210 print "# Oops: \$@ was [$@]\n";
214 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
215 test 27, !$?, "Exited with status $?";
219 # Reading from a file should be tainted
222 test 28, open(FILE, $file), "Couldn't open '$file': $!";
225 sysread(FILE, $block, 100);
228 test 29, tainted $block;
229 test 30, tainted $line;
232 # Globs should be forbidden, except under VMS,
233 # which doesn't spawn an external program.
235 for (29..30) { print "ok $_\n"; }
238 my @globs = eval { <*> };
239 test 31, @globs == 0 && $@ =~ /^Insecure dependency/;
241 @globs = eval { glob '*' };
242 test 32, @globs == 0 && $@ =~ /^Insecure dependency/;
245 # Output of commands should be tainted
247 my $foo = `$echo abc`;
248 test 33, tainted $foo;
251 # Certain system variables should be tainted
253 test 34, all_tainted $^X, $0;
256 # Results of matching should all be untainted
258 my $foo = "abcdefghi" . $TAINT;
259 test 35, tainted $foo;
262 test 36, not any_tainted $`, $&, $';
264 $foo =~ /(...)(...)(...)/;
265 test 37, not any_tainted $1, $2, $3, $+;
267 my @bar = $foo =~ /(...)(...)(...)/;
268 test 38, not any_tainted @bar;
270 test 39, tainted $foo; # $foo should still be tainted!
271 test 40, $foo eq "abcdefghi";
274 # Operations which affect files can't use tainted data.
276 test 41, eval { chmod 0, $TAINT } eq '', 'chmod';
277 test 42, $@ =~ /^Insecure dependency/, $@;
279 # There is no feature test in $Config{} for truncate,
280 # so we allow for the possibility that it's missing.
281 test 43, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
282 test 44, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
284 test 45, eval { rename '', $TAINT } eq '', 'rename';
285 test 46, $@ =~ /^Insecure dependency/, $@;
287 test 47, eval { unlink $TAINT } eq '', 'unlink';
288 test 48, $@ =~ /^Insecure dependency/, $@;
290 test 49, eval { utime $TAINT } eq '', 'utime';
291 test 50, $@ =~ /^Insecure dependency/, $@;
293 if ($Config{d_chown}) {
294 test 51, eval { chown -1, -1, $TAINT } eq '', 'chown';
295 test 52, $@ =~ /^Insecure dependency/, $@;
298 for (51..52) { print "ok $_ # Skipped: chown() is not available\n" }
301 if ($Config{d_link}) {
302 test 53, eval { link $TAINT, '' } eq '', 'link';
303 test 54, $@ =~ /^Insecure dependency/, $@;
306 for (53..54) { print "ok $_ # Skipped: link() is not available\n" }
309 if ($Config{d_symlink}) {
310 test 55, eval { symlink $TAINT, '' } eq '', 'symlink';
311 test 56, $@ =~ /^Insecure dependency/, $@;
314 for (55..56) { print "ok $_ # Skipped: symlink() is not available\n" }
318 # Operations which affect directories can't use tainted data.
320 test 57, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
321 test 58, $@ =~ /^Insecure dependency/, $@;
323 test 59, eval { rmdir $TAINT } eq '', 'rmdir';
324 test 60, $@ =~ /^Insecure dependency/, $@;
326 test 61, eval { chdir $TAINT } eq '', 'chdir';
327 test 62, $@ =~ /^Insecure dependency/, $@;
329 if ($Config{d_chroot}) {
330 test 63, eval { chroot $TAINT } eq '', 'chroot';
331 test 64, $@ =~ /^Insecure dependency/, $@;
334 for (63..64) { print "ok $_ # Skipped: chroot() is not available\n" }
338 # Some operations using files can't use tainted data.
340 my $foo = "imaginary library" . $TAINT;
341 test 65, eval { require $foo } eq '', 'require';
342 test 66, $@ =~ /^Insecure dependency/, $@;
344 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
345 END { unlink $filename if defined $filename }
346 $foo = $filename . $TAINT;
347 unlink $filename; # in any case
349 test 67, eval { open FOO, $foo } eq '', 'open for read';
350 test 68, $@ eq '', $@; # NB: This should be allowed
351 test 69, $! == 2; # File not found
353 test 70, eval { open FOO, "> $foo" } eq '', 'open for write';
354 test 71, $@ =~ /^Insecure dependency/, $@;
357 # Commands to the system can't use tainted data
361 if ($^O eq 'amigaos') {
362 for (72..75) { print "ok $_ # Skipped: open('|') is not available\n" }
365 test 72, eval { open FOO, "| $foo" } eq '', 'popen to';
366 test 73, $@ =~ /^Insecure dependency/, $@;
368 test 74, eval { open FOO, "$foo |" } eq '', 'popen from';
369 test 75, $@ =~ /^Insecure dependency/, $@;
372 test 76, eval { exec $TAINT } eq '', 'exec';
373 test 77, $@ =~ /^Insecure dependency/, $@;
375 test 78, eval { system $TAINT } eq '', 'system';
376 test 79, $@ =~ /^Insecure dependency/, $@;
381 test 80, eval { `$echo 1$foo` } eq '', 'backticks';
382 test 81, $@ =~ /^Insecure dependency/, $@;
384 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
385 test 82, join('', eval { glob $foo } ) ne '', 'globbing';
386 test 83, $@ eq '', $@;
389 for (82..83) { print "ok $_ # Skipped: this is not VMS\n"; }
393 # Operations which affect processes can't use tainted data.
395 test 84, eval { kill 0, $TAINT } eq '', 'kill';
396 test 85, $@ =~ /^Insecure dependency/, $@;
398 if ($Config{d_setpgrp}) {
399 test 86, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
400 test 87, $@ =~ /^Insecure dependency/, $@;
403 for (86..87) { print "ok $_ # Skipped: setpgrp() is not available\n" }
406 if ($Config{d_setprior}) {
407 test 88, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
408 test 89, $@ =~ /^Insecure dependency/, $@;
411 for (88..89) { print "ok $_ # Skipped: setpriority() is not available\n" }
415 # Some miscellaneous operations can't use tainted data.
417 if ($Config{d_syscall}) {
418 test 90, eval { syscall $TAINT } eq '', 'syscall';
419 test 91, $@ =~ /^Insecure dependency/, $@;
422 for (90..91) { print "ok $_ # Skipped: syscall() is not available\n" }
429 my $temp = "./taintC$$";
431 test 92, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
433 test 93, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
434 test 94, $@ =~ /^Insecure dependency/, $@;
436 if ($Config{d_fcntl}) {
437 test 95, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
438 test 96, $@ =~ /^Insecure dependency/, $@;
441 for (95..96) { print "ok $_ # Skipped: fcntl() is not available\n" }
448 # Some tests involving references
450 my $foo = 'abc' . $TAINT;
452 test 97, not tainted $fooref;
453 test 98, tainted $$fooref;
454 test 99, tainted $foo;
457 # Some tests involving assignment
461 test 100, all_tainted $foo, $bar;
462 test 101, tainted($foo = $bar);
463 test 102, tainted($bar = $bar);
464 test 103, tainted($bar += $bar);
465 test 104, tainted($bar -= $bar);
466 test 105, tainted($bar *= $bar);
467 test 106, tainted($bar++);
468 test 107, tainted($bar /= $bar);
469 test 108, tainted($bar += 0);
470 test 109, tainted($bar -= 2);
471 test 110, tainted($bar *= -1);
472 test 111, tainted($bar /= 1);
473 test 112, tainted($bar--);
477 # Test assignment and return of lists
479 my @foo = ("A", "tainted" . $TAINT, "B");
480 test 114, not tainted $foo[0];
481 test 115, tainted $foo[1];
482 test 116, not tainted $foo[2];
484 test 117, not tainted $bar[0];
485 test 118, tainted $bar[1];
486 test 119, not tainted $bar[2];
487 my @baz = eval { "A", "tainted" . $TAINT, "B" };
488 test 120, not tainted $baz[0];
489 test 121, tainted $baz[1];
490 test 122, not tainted $baz[2];
491 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
492 test 123, not tainted $plugh[0];
493 test 124, tainted $plugh[1];
494 test 125, not tainted $plugh[2];
495 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
496 test 126, not tainted ((&$nautilus)[0]);
497 test 127, tainted ((&$nautilus)[1]);
498 test 128, not tainted ((&$nautilus)[2]);
499 my @xyzzy = &$nautilus;
500 test 129, not tainted $xyzzy[0];
501 test 130, tainted $xyzzy[1];
502 test 131, not tainted $xyzzy[2];
503 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
504 test 132, not tainted ((&$red_october)[0]);
505 test 133, tainted ((&$red_october)[1]);
506 test 134, not tainted ((&$red_october)[2]);
507 my @corge = &$red_october;
508 test 135, not tainted $corge[0];
509 test 136, tainted $corge[1];
510 test 137, not tainted $corge[2];
513 # Test for system/library calls returning string data of dubious origin.
515 # No reliable %Config check for getpw*
516 if (eval { setpwent(); getpwent(); 1 }) {
518 my @getpwent = getpwent();
519 die "getpwent: $!\n" unless (@getpwent);
520 test 138,( not tainted $getpwent[0]
521 and not tainted $getpwent[1]
522 and not tainted $getpwent[2]
523 and not tainted $getpwent[3]
524 and not tainted $getpwent[4]
525 and not tainted $getpwent[5]
526 and tainted $getpwent[6] # gecos
527 and not tainted $getpwent[7]
528 and not tainted $getpwent[8]);
531 print "ok 138 # Skipped: getpwent() is not available\n";
534 if ($Config{d_readdir}) { # pretty hard to imagine not
536 opendir(D, "op") or die "opendir: $!\n";
537 my $readdir = readdir(D);
538 test 139, tainted $readdir;
541 print "ok 139 # Skipped: readdir() is not available\n";
544 if ($Config{d_readlink} && $Config{d_symlink}) {
545 my $symlink = "sl$$";
547 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
548 my $readlink = readlink($symlink);
549 test 140, tainted $readlink;
552 print "ok 140 # Skipped: readlink() or symlink() is not available\n";
556 # test bitwise ops (regression bug)
560 test 141, not tainted $j;
563 test 142, tainted $j;
566 # test target of substitution (regression bug)
568 my $why = $TAINT."y";
570 test 143, tainted $why;
574 test 144, tainted $why;
576 $why =~ s/e/'-'.$$/ge;
577 test 145, tainted $why;