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?
17 use File::Spec::Functions;
23 # You have to do it this way or VMS will get confused.
24 print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
26 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
35 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
38 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
39 $ENV{PATH} = $ENV{PATH};
40 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
42 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
43 && ($Config{d_shm} || $Config{d_msg})) {
44 eval { require IPC::SysV };
47 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
52 my $Is_MacOS = $^O eq 'MacOS';
53 my $Is_VMS = $^O eq 'VMS';
54 my $Is_MSWin32 = $^O eq 'MSWin32';
55 my $Is_NetWare = $^O eq 'NetWare';
56 my $Is_Dos = $^O eq 'dos';
57 my $Is_Cygwin = $^O eq 'cygwin';
58 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
59 ($Is_MSWin32 ? '.\perl' :
61 ($Is_NetWare ? 'perl' : './perl'));
62 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
66 for $x ('DCL$PATH', @MoreEnv) {
67 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
71 \$ENV{PATH} = '' if $Config{d_setenv};
72 warn "# Note: logical name 'PATH' may have been deleted\n";
73 \@ENV{keys %old} = values %old;
79 # The empty tainted value, for tainting strings
80 my $TAINT = substr($^X, 0, 0);
81 # A tainted zero, useful for tainting numbers
82 my $TAINT0 = 0 + $TAINT;
84 # This taints each argument passed. All must be lvalues.
85 # Side effect: It also stringifies them. :-(
87 for (@_) { $_ .= $TAINT }
90 # How to identify taint when you see it
92 not eval { join("",@_), kill 0; 1 };
98 for (@_) { return 0 unless tainted $_ }
103 my($serial, $boolean, $diag) = @_;
105 print "ok $serial\n";
107 print "not ok $serial\n";
108 for (split m/^/m, $diag) {
113 or substr($diag, -1) eq "\n";
117 # We need an external program to call.
118 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
120 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
121 print PROG 'print "@ARGV\n"', "\n";
123 my $echo = "$Invoke_Perl $ECHO";
125 my $TEST = catfile(curdir(), 'TEST');
129 # First, let's make sure that Perl is checking the dangerous
130 # environment variables. Maybe they aren't set yet, so we'll
131 # taint them ourselves.
133 $ENV{'DCL$PATH'} = '' if $Is_VMS;
136 delete @ENV{@MoreEnv};
139 if ($Is_Cygwin && ! -f 'cygwin1.dll') {
140 system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
141 die "$0: failed to cp cygwin1.dll: $!\n";
143 END { unlink "cygwin1.dll" }
147 if ($Is_Cygwin && ! -f 'cygcrypt-0.dll' && -f '/usr/bin/cygcrypt-0.dll') {
148 system("/usr/bin/cp /usr/bin/cygcrypt-0.dll .") &&
149 die "$0: failed to cp cygcrypt-0.dll: $!\n";
151 END { unlink "cygcrypt-0.dll" }
155 test 1, eval { `$echo 1` } eq "1\n";
157 if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS) {
158 print "# Environment tainting tests skipped\n";
159 for (2..5) { print "ok $_\n" }
162 my @vars = ('PATH', @MoreEnv);
163 while (my $v = $vars[0]) {
164 local $ENV{$v} = $TAINT;
165 last if eval { `$echo 1` };
166 last unless $@ =~ /^Insecure \$ENV{$v}/;
169 test 2, !@vars, "\$$vars[0]";
171 # tainted $TERM is unsafe only if it contains metachars
173 $ENV{TERM} = 'e=mc2';
174 test 3, eval { `$echo 1` } eq "1\n";
175 $ENV{TERM} = 'e=mc2' . $TAINT;
176 test 4, eval { `$echo 1` } eq '';
177 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
181 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
182 print "# all directories are writeable\n";
185 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
186 qw(sys$scratch /tmp /var/tmp /usr/tmp),
187 @ENV{qw(TMP TEMP)})[0]
188 or print "# can't find world-writeable directory to test PATH\n";
192 local $ENV{PATH} = $tmp;
193 test 6, eval { `$echo 1` } eq '';
194 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
197 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
201 $ENV{'DCL$PATH'} = $TAINT;
202 test 8, eval { `$echo 1` } eq '';
203 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
205 $ENV{'DCL$PATH'} = $tmp;
206 test 10, eval { `$echo 1` } eq '';
207 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
210 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
212 $ENV{'DCL$PATH'} = '';
215 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
219 # Let's see that we can taint and untaint as needed.
222 test 12, tainted $foo;
224 # That was a sanity check. If it failed, stop the insanity!
225 die "Taint checks don't seem to be enabled" unless tainted $foo;
228 test 13, not tainted $foo;
231 test 14, tainted $foo;
234 test 15, not any_tainted @list;
235 taint_these @list[1,3,5,7,9];
236 test 16, any_tainted @list;
237 test 17, all_tainted @list[1,3,5,7,9];
238 test 18, not any_tainted @list[0,2,4,6,8];
240 ($foo) = $foo =~ /(.+)/;
241 test 19, not tainted $foo;
243 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
244 test 20, not tainted $foo;
245 test 21, $foo eq 'bar';
250 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
251 test 22, tainted $foo;
252 test 23, $foo eq 'bar';
254 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
255 test 24, tainted $foo;
256 test 25, $foo eq 'bar';
259 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
260 test 26, tainted $foo;
261 test 27, $foo eq 'bar';
263 my $pi = 4 * atan2(1,1) + $TAINT0;
264 test 28, tainted $pi;
266 ($pi) = $pi =~ /(\d+\.\d+)/;
267 test 29, not tainted $pi;
268 test 30, sprintf("%.5f", $pi) eq '3.14159';
271 # How about command-line arguments? The problem is that we don't
272 # always get some, so we'll run another process with some.
274 my $arg = catfile(curdir(), "arg$$");
275 open PROG, "> $arg" or die "Can't create $arg: $!";
277 eval { join('', @ARGV), kill 0 };
278 exit 0 if $@ =~ /^Insecure dependency/;
279 print "# Oops: \$@ was [$@]\n";
283 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
284 test 31, !$?, "Exited with status $?";
288 # Reading from a file should be tainted
290 test 32, open(FILE, $TEST), "Couldn't open '$TEST': $!";
293 sysread(FILE, $block, 100);
296 test 33, tainted $block;
297 test 34, tainted $line;
300 # Globs should be forbidden, except under VMS,
301 # which doesn't spawn an external program.
302 if (1 # built-in glob
304 for (35..36) { print "ok $_\n"; }
307 my @globs = eval { <*> };
308 test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
310 @globs = eval { glob '*' };
311 test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
314 # Output of commands should be tainted
316 my $foo = `$echo abc`;
317 test 37, tainted $foo;
320 # Certain system variables should be tainted
322 test 38, all_tainted $^X, $0;
325 # Results of matching should all be untainted
327 my $foo = "abcdefghi" . $TAINT;
328 test 39, tainted $foo;
331 test 40, not any_tainted $`, $&, $';
333 $foo =~ /(...)(...)(...)/;
334 test 41, not any_tainted $1, $2, $3, $+;
336 my @bar = $foo =~ /(...)(...)(...)/;
337 test 42, not any_tainted @bar;
339 test 43, tainted $foo; # $foo should still be tainted!
340 test 44, $foo eq "abcdefghi";
343 # Operations which affect files can't use tainted data.
345 test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
346 test 46, $@ =~ /^Insecure dependency/, $@;
348 # There is no feature test in $Config{} for truncate,
349 # so we allow for the possibility that it's missing.
350 test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
351 test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
353 test 49, eval { rename '', $TAINT } eq '', 'rename';
354 test 50, $@ =~ /^Insecure dependency/, $@;
356 test 51, eval { unlink $TAINT } eq '', 'unlink';
357 test 52, $@ =~ /^Insecure dependency/, $@;
359 test 53, eval { utime $TAINT } eq '', 'utime';
360 test 54, $@ =~ /^Insecure dependency/, $@;
362 if ($Config{d_chown}) {
363 test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
364 test 56, $@ =~ /^Insecure dependency/, $@;
367 for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
370 if ($Config{d_link}) {
371 test 57, eval { link $TAINT, '' } eq '', 'link';
372 test 58, $@ =~ /^Insecure dependency/, $@;
375 for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
378 if ($Config{d_symlink}) {
379 test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
380 test 60, $@ =~ /^Insecure dependency/, $@;
383 for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
387 # Operations which affect directories can't use tainted data.
389 test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
390 test 62, $@ =~ /^Insecure dependency/, $@;
392 test 63, eval { rmdir $TAINT } eq '', 'rmdir';
393 test 64, $@ =~ /^Insecure dependency/, $@;
395 test 65, eval { chdir $TAINT } eq '', 'chdir';
396 test 66, $@ =~ /^Insecure dependency/, $@;
398 if ($Config{d_chroot}) {
399 test 67, eval { chroot $TAINT } eq '', 'chroot';
400 test 68, $@ =~ /^Insecure dependency/, $@;
403 for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
407 # Some operations using files can't use tainted data.
409 my $foo = "imaginary library" . $TAINT;
410 test 69, eval { require $foo } eq '', 'require';
411 test 70, $@ =~ /^Insecure dependency/, $@;
413 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
414 END { unlink $filename if defined $filename }
415 $foo = $filename . $TAINT;
416 unlink $filename; # in any case
418 test 71, eval { open FOO, $foo } eq '', 'open for read';
419 test 72, $@ eq '', $@; # NB: This should be allowed
421 # Try first new style but allow also old style.
422 # We do not want the whole taint.t to fail
423 # just because Errno possibly failing.
424 test 73, eval('$!{ENOENT}') ||
425 $! == 2 || # File not found
426 ($Is_Dos && $! == 22) ||
427 ($^O eq 'mint' && $! == 33);
429 test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
430 test 75, $@ =~ /^Insecure dependency/, $@;
433 # Commands to the system can't use tainted data
437 if ($^O eq 'amigaos') {
438 for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
441 test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
442 test 77, $@ =~ /^Insecure dependency/, $@;
444 test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
445 test 79, $@ =~ /^Insecure dependency/, $@;
448 test 80, eval { exec $TAINT } eq '', 'exec';
449 test 81, $@ =~ /^Insecure dependency/, $@;
451 test 82, eval { system $TAINT } eq '', 'system';
452 test 83, $@ =~ /^Insecure dependency/, $@;
457 test 84, eval { `$echo 1$foo` } eq '', 'backticks';
458 test 85, $@ =~ /^Insecure dependency/, $@;
460 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
461 test 86, join('', eval { glob $foo } ) ne '', 'globbing';
462 test 87, $@ eq '', $@;
465 for (86..87) { print "ok $_ # Skipped: This is not VMS\n"; }
469 # Operations which affect processes can't use tainted data.
471 test 88, eval { kill 0, $TAINT } eq '', 'kill';
472 test 89, $@ =~ /^Insecure dependency/, $@;
474 if ($Config{d_setpgrp}) {
475 test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
476 test 91, $@ =~ /^Insecure dependency/, $@;
479 for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
482 if ($Config{d_setprior}) {
483 test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
484 test 93, $@ =~ /^Insecure dependency/, $@;
487 for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
491 # Some miscellaneous operations can't use tainted data.
493 if ($Config{d_syscall}) {
494 test 94, eval { syscall $TAINT } eq '', 'syscall';
495 test 95, $@ =~ /^Insecure dependency/, $@;
498 for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
505 my $temp = "./taintC$$";
507 test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
509 test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
510 test 98, $@ =~ /^Insecure dependency/, $@;
512 if ($Config{d_fcntl}) {
513 test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
514 test 100, $@ =~ /^Insecure dependency/, $@;
517 for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
524 # Some tests involving references
526 my $foo = 'abc' . $TAINT;
528 test 101, not tainted $fooref;
529 test 102, tainted $$fooref;
530 test 103, tainted $foo;
533 # Some tests involving assignment
537 test 104, all_tainted $foo, $bar;
538 test 105, tainted($foo = $bar);
539 test 106, tainted($bar = $bar);
540 test 107, tainted($bar += $bar);
541 test 108, tainted($bar -= $bar);
542 test 109, tainted($bar *= $bar);
543 test 110, tainted($bar++);
544 test 111, tainted($bar /= $bar);
545 test 112, tainted($bar += 0);
546 test 113, tainted($bar -= 2);
547 test 114, tainted($bar *= -1);
548 test 115, tainted($bar /= 1);
549 test 116, tainted($bar--);
553 # Test assignment and return of lists
555 my @foo = ("A", "tainted" . $TAINT, "B");
556 test 118, not tainted $foo[0];
557 test 119, tainted $foo[1];
558 test 120, not tainted $foo[2];
560 test 121, not tainted $bar[0];
561 test 122, tainted $bar[1];
562 test 123, not tainted $bar[2];
563 my @baz = eval { "A", "tainted" . $TAINT, "B" };
564 test 124, not tainted $baz[0];
565 test 125, tainted $baz[1];
566 test 126, not tainted $baz[2];
567 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
568 test 127, not tainted $plugh[0];
569 test 128, tainted $plugh[1];
570 test 129, not tainted $plugh[2];
571 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
572 test 130, not tainted ((&$nautilus)[0]);
573 test 131, tainted ((&$nautilus)[1]);
574 test 132, not tainted ((&$nautilus)[2]);
575 my @xyzzy = &$nautilus;
576 test 133, not tainted $xyzzy[0];
577 test 134, tainted $xyzzy[1];
578 test 135, not tainted $xyzzy[2];
579 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
580 test 136, not tainted ((&$red_october)[0]);
581 test 137, tainted ((&$red_october)[1]);
582 test 138, not tainted ((&$red_october)[2]);
583 my @corge = &$red_october;
584 test 139, not tainted $corge[0];
585 test 140, tainted $corge[1];
586 test 141, not tainted $corge[2];
589 # Test for system/library calls returning string data of dubious origin.
591 # No reliable %Config check for getpw*
592 if (eval { setpwent(); getpwent() }) {
594 my @getpwent = getpwent();
595 die "getpwent: $!\n" unless (@getpwent);
596 test 142,( not tainted $getpwent[0]
597 and tainted $getpwent[1]
598 and not tainted $getpwent[2]
599 and not tainted $getpwent[3]
600 and not tainted $getpwent[4]
601 and not tainted $getpwent[5]
602 and tainted $getpwent[6] # ge?cos
603 and not tainted $getpwent[7]
604 and tainted $getpwent[8]); # shell
607 for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
610 if ($Config{d_readdir}) { # pretty hard to imagine not
612 opendir(D, "op") or die "opendir: $!\n";
613 my $readdir = readdir(D);
614 test 143, tainted $readdir;
617 for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
620 if ($Config{d_readlink} && $Config{d_symlink}) {
621 my $symlink = "sl$$";
623 my $sl = "/something/naughty";
624 # it has to be a real path on Mac OS
625 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
626 symlink($sl, $symlink) or die "symlink: $!\n";
627 my $readlink = readlink($symlink);
628 test 144, tainted $readlink;
631 for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
635 # test bitwise ops (regression bug)
639 test 145, not tainted $j;
642 test 146, tainted $j;
645 # test target of substitution (regression bug)
647 my $why = $TAINT."y";
649 test 147, tainted $why;
653 test 148, tainted $why;
655 $why =~ s/e/'-'.$$/ge;
656 test 149, tainted $why;
662 print "ok 150 # skipped: no IPC::SysV\n";
665 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
670 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
673 if (shmwrite($id, $sent, 0, 60)) {
674 if (shmread($id, $rcvd, 0, 60)) {
675 substr($rcvd, index($rcvd, "\0")) = '';
677 warn "# shmread failed: $!\n";
680 warn "# shmwrite failed: $!\n";
682 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
684 warn "# shmget failed: $!\n";
687 if ($rcvd eq $sent) {
688 test 150, tainted $rcvd;
690 print "ok 150 # Skipped: SysV shared memory operation failed\n";
693 print "ok 150 # Skipped: SysV shared memory is not available\n";
700 print "ok 151 # skipped: no IPC::SysV\n";
703 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
705 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
707 my $sent = "message";
708 my $type_sent = 1234;
713 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
714 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
715 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
717 warn "# msgrcv failed: $!\n";
720 warn "# msgsnd failed: $!\n";
722 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
724 warn "# msgget failed\n";
727 if ($rcvd eq $sent && $type_sent == $type_rcvd) {
728 test 151, tainted $rcvd;
730 print "ok 151 # Skipped: SysV message queue operation failed\n";
733 print "ok 151 # Skipped: SysV message queues are not available\n";
738 # bug id 20001004.006
740 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
744 print "not " unless tainted($a) && tainted($b) && !defined($b);
750 # bug id 20001004.007
752 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
757 print "not " unless !tainted($c->{a}) && tainted($c->{b});
762 print "not " unless tainted($d->{a}) && !tainted($d->{b});
766 b => { c => $a, d => 42 } };
767 print "not " unless !tainted($e->{a}) &&
769 tainted($e->{b}->{c}) &&
770 !tainted($e->{b}->{d});
777 # bug id 20010519.003
780 use vars qw($has_fcntl);
781 eval { require Fcntl; import Fcntl; };
787 unless ($has_fcntl) {
789 print "ok $_ # Skip: no Fcntl (no dynaloading?)\n";
792 my $evil = "foo" . $TAINT;
794 eval { sysopen(my $ro, $evil, &O_RDONLY) };
795 test 156, $@ !~ /^Insecure dependency/, $@;
797 eval { sysopen(my $wo, $evil, &O_WRONLY) };
798 test 157, $@ =~ /^Insecure dependency/, $@;
800 eval { sysopen(my $rw, $evil, &O_RDWR) };
801 test 158, $@ =~ /^Insecure dependency/, $@;
803 eval { sysopen(my $ap, $evil, &O_APPEND) };
804 test 159, $@ =~ /^Insecure dependency/, $@;
806 eval { sysopen(my $cr, $evil, &O_CREAT) };
807 test 160, $@ =~ /^Insecure dependency/, $@;
809 eval { sysopen(my $tr, $evil, &O_TRUNC) };
810 test 161, $@ =~ /^Insecure dependency/, $@;
812 eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) };
813 test 162, $@ !~ /^Insecure dependency/, $@;
815 eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) };
816 test 163, $@ =~ /^Insecure dependency/, $@;
818 eval { sysopen(my $rw, "foo", &O_RDWR | $evil) };
819 test 164, $@ =~ /^Insecure dependency/, $@;
821 eval { sysopen(my $ap, "foo", &O_APPEND | $evil) };
822 test 165, $@ =~ /^Insecure dependency/, $@;
824 eval { sysopen(my $cr, "foo", &O_CREAT | $evil) };
825 test 166, $@ =~ /^Insecure dependency/, $@;
827 eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) };
828 test 167, $@ =~ /^Insecure dependency/, $@;
830 eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) };
831 test 168, $@ !~ /^Insecure dependency/, $@;
833 eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) };
834 test 169, $@ =~ /^Insecure dependency/, $@;
836 eval { sysopen(my $rw, "foo", &O_RDWR, $evil) };
837 test 170, $@ =~ /^Insecure dependency/, $@;
839 eval { sysopen(my $ap, "foo", &O_APPEND, $evil) };
840 test 171, $@ =~ /^Insecure dependency/, $@;
842 eval { sysopen(my $cr, "foo", &O_CREAT, $evil) };
843 test 172, $@ =~ /^Insecure dependency/, $@;
845 eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) };
846 test 173, $@ =~ /^Insecure dependency/, $@;
848 unlink("foo"); # not unlink($evil), because that would fail...
857 local $SIG{__WARN__} = sub { print "not " };
860 my $divnum = shift()/1;
861 sprintf("%1.1f\n", $divnum);
873 # Bug ID 20010730.010
881 bless \$arg => $class;
892 my $bar = "The Big Bright Green Pleasure Machine";
894 tie my ($foo), Tie => $bar;
898 print $i == 1 ? "ok 175\n" : "not ok 175\n"
903 # Check that all environment variables are tainted.
905 while (my ($k, $v) = each %ENV) {
907 # These we have explicitly untainted or set earlier.
908 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
909 push @untainted, "# '$k' = '$v'\n";
912 print @untainted == 0 ? "ok 176\n" : "not ok 176\n";
913 print "# untainted:\n", @untainted if @untainted;
917 ok( ${^TAINT} == 1, '$^TAINT is on' );
919 eval { ${^TAINT} = 0 };
920 ok( ${^TAINT}, '$^TAINT is not assignable' );
921 ok( $@ =~ /^Modification of a read-only value attempted/,
922 'Assigning to ${^TAINT} fails' );
927 my $re1 = qr/x$TAINT/;
928 test 180, tainted $re1;
930 my $re2 = qr/^$re1\z/;
931 test 181, tainted $re2;
934 test 182, tainted $re3;
938 print "ok 183 # Skipped: system {} has different semantics\n";
943 local $ENV{PATH} .= $TAINT;
944 eval { system { "echo" } "/arg0", "arg1" };
945 test 183, $@ =~ /^Insecure \$ENV/;
948 for (184..205) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
952 # bug 20020208.005 plus some extras
953 # single arg exec/system are tests 80-83
954 my $err = qr/^Insecure dependency/ ;
955 test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
956 test 185, $@ =~ $err, $@;
957 test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
958 test 187, $@ =~ $err, $@;
959 test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
960 test 189, $@ =~ $err, $@;
961 test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
962 test 191, $@ =~ $err, $@;
963 test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
964 test 193, $@ =~ $err, $@;
966 test 194, eval { system $TAINT, $TAINT } eq '', 'system';
967 test 195, $@ =~ $err, $@;
968 test 196, eval { system $TAINT $TAINT } eq '', 'system';
969 test 197, $@ =~ $err, $@;
970 test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'system';
971 test 199, $@ =~ $err, $@;
972 test 200, eval { system $TAINT 'notaint' } eq '', 'system';
973 test 201, $@ =~ $err, $@;
974 test 202, eval { system {'notaint'} $TAINT } eq '', 'system';
975 test 203, $@ =~ $err, $@;
977 eval { system("lskdfj does not exist","with","args"); };
980 print "ok 205 # no exec()\n";
982 eval { exec("lskdfj does not exist","with","args"); };
986 # If you add tests here update also the above skip block for VMS.
990 # [ID 20020704.001] taint propagation failure
993 test 206, tainted(my $foo = $1);
997 # [perl #24291] this used to dump core
998 our %nonmagicalenv = ( PATH => "util" );
999 local *ENV = \%nonmagicalenv;
1000 eval { system("lskdfj"); };
1001 test 207, $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
1002 local *ENV = *nonmagicalenv;
1003 eval { system("lskdfj"); };
1004 test 208, $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
1009 test 209, !tainted($1);
1011 test 210, !tainted($notaint);
1014 $notaint =~ /($notaint)/;
1016 test 211, !tainted($1);
1017 test 212, !tainted($l);
1018 $notaint =~ /($TAINT)/;
1020 test 213, tainted($1);
1021 test 214, tainted($l);
1023 $TAINT =~ /($notaint)/;
1025 test 215, !tainted($1);
1026 test 216, !tainted($l);
1027 $TAINT =~ /($TAINT)/;
1029 test 217, tainted($1);
1030 test 218, tainted($l);
1033 ($r = $TAINT) =~ /($notaint)/;
1034 test 219, !tainted($1);
1035 ($r = $TAINT) =~ /($TAINT)/;
1036 test 220, tainted($1);
1039 # accessing $^O shoudn't taint it as a side-effect;
1040 # assigning tainted data to it is now an error
1042 test 221, !tainted($^O);
1043 if (!$^X) { } elsif ($^O eq 'bar') { }
1044 test 222, !tainted($^O);
1046 test 223, $@ =~ /Insecure dependency in/;