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 # We do not want the whole taint.t to fail
19 # just because Errno possibly failing.
20 eval { require Errno; import Errno };
22 my $Is_VMS = $^O eq 'VMS';
23 my $Is_MSWin32 = $^O eq 'MSWin32';
24 my $Is_Dos = $^O eq 'dos';
25 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
26 $Is_MSWin32 ? '.\perl' : './perl';
27 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
31 for $x ('DCL$PATH', @MoreEnv) {
32 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
37 warn "# Note: logical name 'PATH' may have been deleted\n";
38 @ENV{keys %old} = values %old;
44 # The empty tainted value, for tainting strings
45 my $TAINT = substr($^X, 0, 0);
46 # A tainted zero, useful for tainting numbers
47 my $TAINT0 = 0 + $TAINT;
49 # This taints each argument passed. All must be lvalues.
50 # Side effect: It also stringifies them. :-(
52 for (@_) { $_ .= $TAINT }
55 # How to identify taint when you see it
57 not eval { join("",@_), kill 0; 1 };
63 for (@_) { return 0 unless tainted $_ }
68 my($serial, $boolean, $diag) = @_;
72 print "not ok $serial\n";
73 for (split m/^/m, $diag) {
78 or substr($diag, -1) eq "\n";
82 # We need an external program to call.
83 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
85 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
86 print PROG 'print "@ARGV\n"', "\n";
88 my $echo = "$Invoke_Perl $ECHO";
92 # First, let's make sure that Perl is checking the dangerous
93 # environment variables. Maybe they aren't set yet, so we'll
94 # taint them ourselves.
96 $ENV{'DCL$PATH'} = '' if $Is_VMS;
99 delete @ENV{@MoreEnv};
102 test 1, eval { `$echo 1` } eq "1\n";
104 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
105 print "# Environment tainting tests skipped\n";
106 for (2..5) { print "ok $_\n" }
109 my @vars = ('PATH', @MoreEnv);
110 while (my $v = $vars[0]) {
111 local $ENV{$v} = $TAINT;
112 last if eval { `$echo 1` };
113 last unless $@ =~ /^Insecure \$ENV{$v}/;
116 test 2, !@vars, "\$$vars[0]";
118 # tainted $TERM is unsafe only if it contains metachars
120 $ENV{TERM} = 'e=mc2';
121 test 3, eval { `$echo 1` } eq "1\n";
122 $ENV{TERM} = 'e=mc2' . $TAINT;
123 test 4, eval { `$echo 1` } eq '';
124 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
128 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
129 print "# all directories are writeable\n";
132 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
133 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
134 @ENV{qw(TMP TEMP)})[0]
135 or print "# can't find world-writeable directory to test PATH\n";
139 local $ENV{PATH} = $tmp;
140 test 6, eval { `$echo 1` } eq '';
141 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
144 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
148 $ENV{'DCL$PATH'} = $TAINT;
149 test 8, eval { `$echo 1` } eq '';
150 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
152 $ENV{'DCL$PATH'} = $tmp;
153 test 10, eval { `$echo 1` } eq '';
154 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
157 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
159 $ENV{'DCL$PATH'} = '';
162 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
166 # Let's see that we can taint and untaint as needed.
169 test 12, tainted $foo;
171 # That was a sanity check. If it failed, stop the insanity!
172 die "Taint checks don't seem to be enabled" unless tainted $foo;
175 test 13, not tainted $foo;
178 test 14, tainted $foo;
181 test 15, not any_tainted @list;
182 taint_these @list[1,3,5,7,9];
183 test 16, any_tainted @list;
184 test 17, all_tainted @list[1,3,5,7,9];
185 test 18, not any_tainted @list[0,2,4,6,8];
187 ($foo) = $foo =~ /(.+)/;
188 test 19, not tainted $foo;
190 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
191 test 20, not tainted $foo;
192 test 21, $foo eq 'bar';
197 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
198 test 22, tainted $foo;
199 test 23, $foo eq 'bar';
201 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
202 test 24, tainted $foo;
203 test 25, $foo eq 'bar';
206 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
207 test 26, tainted $foo;
208 test 27, $foo eq 'bar';
210 my $pi = 4 * atan2(1,1) + $TAINT0;
211 test 28, tainted $pi;
213 ($pi) = $pi =~ /(\d+\.\d+)/;
214 test 29, not tainted $pi;
215 test 30, sprintf("%.5f", $pi) eq '3.14159';
218 # How about command-line arguments? The problem is that we don't
219 # always get some, so we'll run another process with some.
222 open PROG, "> $arg" or die "Can't create $arg: $!";
224 eval { join('', @ARGV), kill 0 };
225 exit 0 if $@ =~ /^Insecure dependency/;
226 print "# Oops: \$@ was [$@]\n";
230 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
231 test 31, !$?, "Exited with status $?";
235 # Reading from a file should be tainted
238 test 32, open(FILE, $file), "Couldn't open '$file': $!";
241 sysread(FILE, $block, 100);
244 test 33, tainted $block;
245 test 34, tainted $line;
248 # Globs should be forbidden, except under VMS,
249 # which doesn't spawn an external program.
251 for (35..36) { print "ok $_\n"; }
254 my @globs = eval { <*> };
255 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
257 @globs = eval { glob '*' };
258 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
261 # Output of commands should be tainted
263 my $foo = `$echo abc`;
264 test 37, tainted $foo;
267 # Certain system variables should be tainted
269 test 38, all_tainted $^X, $0;
272 # Results of matching should all be untainted
274 my $foo = "abcdefghi" . $TAINT;
275 test 39, tainted $foo;
278 test 40, not any_tainted $`, $&, $';
280 $foo =~ /(...)(...)(...)/;
281 test 41, not any_tainted $1, $2, $3, $+;
283 my @bar = $foo =~ /(...)(...)(...)/;
284 test 42, not any_tainted @bar;
286 test 43, tainted $foo; # $foo should still be tainted!
287 test 44, $foo eq "abcdefghi";
290 # Operations which affect files can't use tainted data.
292 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
293 test 46, $@ =~ /^Insecure dependency/, $@;
295 # There is no feature test in $Config{} for truncate,
296 # so we allow for the possibility that it's missing.
297 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
298 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
300 test 49, eval { rename '', $TAINT } eq '', 'rename';
301 test 50, $@ =~ /^Insecure dependency/, $@;
303 test 51, eval { unlink $TAINT } eq '', 'unlink';
304 test 52, $@ =~ /^Insecure dependency/, $@;
306 test 53, eval { utime $TAINT } eq '', 'utime';
307 test 54, $@ =~ /^Insecure dependency/, $@;
309 if ($Config{d_chown}) {
310 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
311 test 56, $@ =~ /^Insecure dependency/, $@;
314 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
317 if ($Config{d_link}) {
318 test 57, eval { link $TAINT, '' } eq '', 'link';
319 test 58, $@ =~ /^Insecure dependency/, $@;
322 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
325 if ($Config{d_symlink}) {
326 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
327 test 60, $@ =~ /^Insecure dependency/, $@;
330 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
334 # Operations which affect directories can't use tainted data.
336 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
337 test 62, $@ =~ /^Insecure dependency/, $@;
339 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
340 test 64, $@ =~ /^Insecure dependency/, $@;
342 test 65, eval { chdir $TAINT } eq '', 'chdir';
343 test 66, $@ =~ /^Insecure dependency/, $@;
345 if ($Config{d_chroot}) {
346 test 67, eval { chroot $TAINT } eq '', 'chroot';
347 test 68, $@ =~ /^Insecure dependency/, $@;
350 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
354 # Some operations using files can't use tainted data.
356 my $foo = "imaginary library" . $TAINT;
357 test 69, eval { require $foo } eq '', 'require';
358 test 70, $@ =~ /^Insecure dependency/, $@;
360 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
361 END { unlink $filename if defined $filename }
362 $foo = $filename . $TAINT;
363 unlink $filename; # in any case
365 test 71, eval { open FOO, $foo } eq '', 'open for read';
366 test 72, $@ eq '', $@; # NB: This should be allowed
368 # Try first new style but allow also old style.
369 test 73, $!{ENOENT} ||
370 $! == 2 || # File not found
371 ($Is_Dos && $! == 22) ||
372 ($^O eq 'mint' && $! == 33);
374 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
375 test 75, $@ =~ /^Insecure dependency/, $@;
378 # Commands to the system can't use tainted data
382 if ($^O eq 'amigaos') {
383 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
386 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
387 test 77, $@ =~ /^Insecure dependency/, $@;
389 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
390 test 79, $@ =~ /^Insecure dependency/, $@;
393 test 80, eval { exec $TAINT } eq '', 'exec';
394 test 81, $@ =~ /^Insecure dependency/, $@;
396 test 82, eval { system $TAINT } eq '', 'system';
397 test 83, $@ =~ /^Insecure dependency/, $@;
402 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
403 test 85, $@ =~ /^Insecure dependency/, $@;
405 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
406 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
407 test 87, $@ eq '', $@;
410 for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; }
414 # Operations which affect processes can't use tainted data.
416 test 88, eval { kill 0, $TAINT } eq '', 'kill';
417 test 89, $@ =~ /^Insecure dependency/, $@;
419 if ($Config{d_setpgrp}) {
420 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
421 test 91, $@ =~ /^Insecure dependency/, $@;
424 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
427 if ($Config{d_setprior}) {
428 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
429 test 93, $@ =~ /^Insecure dependency/, $@;
432 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
436 # Some miscellaneous operations can't use tainted data.
438 if ($Config{d_syscall}) {
439 test 94, eval { syscall $TAINT } eq '', 'syscall';
440 test 95, $@ =~ /^Insecure dependency/, $@;
443 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
450 my $temp = "./taintC$$";
452 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
454 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
455 test 98, $@ =~ /^Insecure dependency/, $@;
457 if ($Config{d_fcntl}) {
458 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
459 test 100, $@ =~ /^Insecure dependency/, $@;
462 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
469 # Some tests involving references
471 my $foo = 'abc' . $TAINT;
473 test 101, not tainted $fooref;
474 test 102, tainted $$fooref;
475 test 103, tainted $foo;
478 # Some tests involving assignment
482 test 104, all_tainted $foo, $bar;
483 test 105, tainted($foo = $bar);
484 test 106, tainted($bar = $bar);
485 test 107, tainted($bar += $bar);
486 test 108, tainted($bar -= $bar);
487 test 109, tainted($bar *= $bar);
488 test 110, tainted($bar++);
489 test 111, tainted($bar /= $bar);
490 test 112, tainted($bar += 0);
491 test 113, tainted($bar -= 2);
492 test 114, tainted($bar *= -1);
493 test 115, tainted($bar /= 1);
494 test 116, tainted($bar--);
498 # Test assignment and return of lists
500 my @foo = ("A", "tainted" . $TAINT, "B");
501 test 118, not tainted $foo[0];
502 test 119, tainted $foo[1];
503 test 120, not tainted $foo[2];
505 test 121, not tainted $bar[0];
506 test 122, tainted $bar[1];
507 test 123, not tainted $bar[2];
508 my @baz = eval { "A", "tainted" . $TAINT, "B" };
509 test 124, not tainted $baz[0];
510 test 125, tainted $baz[1];
511 test 126, not tainted $baz[2];
512 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
513 test 127, not tainted $plugh[0];
514 test 128, tainted $plugh[1];
515 test 129, not tainted $plugh[2];
516 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
517 test 130, not tainted ((&$nautilus)[0]);
518 test 131, tainted ((&$nautilus)[1]);
519 test 132, not tainted ((&$nautilus)[2]);
520 my @xyzzy = &$nautilus;
521 test 133, not tainted $xyzzy[0];
522 test 134, tainted $xyzzy[1];
523 test 135, not tainted $xyzzy[2];
524 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
525 test 136, not tainted ((&$red_october)[0]);
526 test 137, tainted ((&$red_october)[1]);
527 test 138, not tainted ((&$red_october)[2]);
528 my @corge = &$red_october;
529 test 139, not tainted $corge[0];
530 test 140, tainted $corge[1];
531 test 141, not tainted $corge[2];
534 # Test for system/library calls returning string data of dubious origin.
536 # No reliable %Config check for getpw*
537 if (eval { setpwent(); getpwent(); 1 }) {
539 my @getpwent = getpwent();
540 die "getpwent: $!\n" unless (@getpwent);
541 test 142,( not tainted $getpwent[0]
542 and not tainted $getpwent[1]
543 and not tainted $getpwent[2]
544 and not tainted $getpwent[3]
545 and not tainted $getpwent[4]
546 and not tainted $getpwent[5]
547 and tainted $getpwent[6] # gecos
548 and not tainted $getpwent[7]
549 and not tainted $getpwent[8]);
552 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
555 if ($Config{d_readdir}) { # pretty hard to imagine not
557 opendir(D, "op") or die "opendir: $!\n";
558 my $readdir = readdir(D);
559 test 143, tainted $readdir;
562 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
565 if ($Config{d_readlink} && $Config{d_symlink}) {
566 my $symlink = "sl$$";
568 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
569 my $readlink = readlink($symlink);
570 test 144, tainted $readlink;
573 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
577 # test bitwise ops (regression bug)
581 test 145, not tainted $j;
584 test 146, tainted $j;
587 # test target of substitution (regression bug)
589 my $why = $TAINT."y";
591 test 147, tainted $why;
595 test 148, tainted $why;
597 $why =~ s/e/'-'.$$/ge;
598 test 149, tainted $why;