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;
19 BEGIN { require './test.pl'; }
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
27 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
28 $ENV{PATH} = $ENV{PATH};
29 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
31 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
32 && ($Config{d_shm} || $Config{d_msg})) {
33 eval { require IPC::SysV };
36 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
41 my $Is_MacOS = $^O eq 'MacOS';
42 my $Is_VMS = $^O eq 'VMS';
43 my $Is_MSWin32 = $^O eq 'MSWin32';
44 my $Is_NetWare = $^O eq 'NetWare';
45 my $Is_Dos = $^O eq 'dos';
46 my $Is_Cygwin = $^O eq 'cygwin';
47 my $Is_OpenBSD = $^O eq 'openbsd';
48 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
49 $Is_MSWin32 ? '.\perl' :
51 $Is_NetWare ? 'perl' :
53 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
57 for $x ('DCL$PATH', @MoreEnv) {
58 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
62 \$ENV{PATH} = '' if $Config{d_setenv};
63 warn "# Note: logical name 'PATH' may have been deleted\n";
64 \@ENV{keys %old} = values %old;
70 # The empty tainted value, for tainting strings
71 my $TAINT = substr($^X, 0, 0);
72 # A tainted zero, useful for tainting numbers
79 # This taints each argument passed. All must be lvalues.
80 # Side effect: It also stringifies them. :-(
82 for (@_) { $_ .= $TAINT }
85 # How to identify taint when you see it
87 not eval { join("",@_), kill 0; 1 };
93 for (@_) { return 0 unless tainted $_ }
101 my $curr_test = curr_test();
104 print "ok $curr_test\n";
106 print "not ok $curr_test\n";
107 printf "# Failed test at line %d\n", (caller)[2];
108 for (split m/^/m, $diag) {
113 or substr($diag, -1) eq "\n";
121 # We need an external program to call.
122 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
124 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
125 print PROG 'print "@ARGV\n"', "\n";
127 my $echo = "$Invoke_Perl $ECHO";
129 my $TEST = catfile(curdir(), 'TEST');
131 # First, let's make sure that Perl is checking the dangerous
132 # environment variables. Maybe they aren't set yet, so we'll
133 # taint them ourselves.
135 $ENV{'DCL$PATH'} = '' if $Is_VMS;
137 if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') {
139 foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
140 if (-f "$dir/cc3250mt.dll") {
141 $bcc_dir = $dir and last;
144 if (defined $bcc_dir) {
146 File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or
147 die "$0: failed to copy cc3250mt.dll: $!\n";
149 END { unlink "cc3250mt.dll" }
153 if ($Is_Cygwin and (! -f 'cygwin1.dll' or ! -f 'cygcrypt-0.dll')) {
155 -f 'cygwin1.dll' or File::Copy::copy("/usr/bin/cygwin1.dll", '.') or
156 die "$0: failed to copy cygwin1.dll: $!\n";
157 -f 'cygcyrypt-0.dll' or File::Copy::copy("/usr/bin/cygcrypt-0.dll", '.') or
158 die "$0: failed to copy cygcrypt-0.dll: $!\n";
160 END { unlink "cygwin1.dll"; unlink "cygcrypt-0.dll"; }
164 delete @ENV{@MoreEnv};
167 test eval { `$echo 1` } eq "1\n";
170 skip "Environment tainting tests skipped", 4
171 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
173 my @vars = ('PATH', @MoreEnv);
174 while (my $v = $vars[0]) {
175 local $ENV{$v} = $TAINT;
176 last if eval { `$echo 1` };
177 last unless $@ =~ /^Insecure \$ENV{$v}/;
180 test !@vars, "@vars";
182 # tainted $TERM is unsafe only if it contains metachars
184 $ENV{TERM} = 'e=mc2';
185 test eval { `$echo 1` } eq "1\n";
186 $ENV{TERM} = 'e=mc2' . $TAINT;
187 test !eval { `$echo 1` };
188 test $@ =~ /^Insecure \$ENV{TERM}/, $@;
192 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
193 print "# all directories are writeable\n";
196 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
197 qw(sys$scratch /tmp /var/tmp /usr/tmp),
198 @ENV{qw(TMP TEMP)})[0]
199 or print "# can't find world-writeable directory to test PATH\n";
203 skip "all directories are writeable", 2 unless $tmp;
205 local $ENV{PATH} = $tmp;
206 test !eval { `$echo 1` };
207 test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
211 skip "This is not VMS", 4 unless $Is_VMS;
213 $ENV{'DCL$PATH'} = $TAINT;
214 test eval { `$echo 1` } eq '';
215 test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
217 skip q[can't find world-writeable directory to test DCL$PATH], 2
220 $ENV{'DCL$PATH'} = $tmp;
221 test eval { `$echo 1` } eq '';
222 test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
224 $ENV{'DCL$PATH'} = '';
228 # Let's see that we can taint and untaint as needed.
233 # That was a sanity check. If it failed, stop the insanity!
234 die "Taint checks don't seem to be enabled" unless tainted $foo;
237 test not tainted $foo;
243 test not any_tainted @list;
244 taint_these @list[1,3,5,7,9];
245 test any_tainted @list;
246 test all_tainted @list[1,3,5,7,9];
247 test not any_tainted @list[0,2,4,6,8];
249 ($foo) = $foo =~ /(.+)/;
250 test not tainted $foo;
252 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
253 test not tainted $foo;
259 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
263 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
268 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
272 my $pi = 4 * atan2(1,1) + $TAINT0;
275 ($pi) = $pi =~ /(\d+\.\d+)/;
276 test not tainted $pi;
277 test sprintf("%.5f", $pi) eq '3.14159';
280 # How about command-line arguments? The problem is that we don't
281 # always get some, so we'll run another process with some.
283 my $arg = catfile(curdir(), "arg$$");
284 open PROG, "> $arg" or die "Can't create $arg: $!";
286 eval { join('', @ARGV), kill 0 };
287 exit 0 if $@ =~ /^Insecure dependency/;
288 print "# Oops: \$@ was [$@]\n";
292 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
293 test !$?, "Exited with status $?";
297 # Reading from a file should be tainted
299 test open(FILE, $TEST), "Couldn't open '$TEST': $!";
302 sysread(FILE, $block, 100);
309 # Globs should be forbidden, except under VMS,
310 # which doesn't spawn an external program.
312 skip "globs should be forbidden", 2 if 1 or $Is_VMS;
314 my @globs = eval { <*> };
315 test @globs == 0 && $@ =~ /^Insecure dependency/;
317 @globs = eval { glob '*' };
318 test @globs == 0 && $@ =~ /^Insecure dependency/;
321 # Output of commands should be tainted
323 my $foo = `$echo abc`;
327 # Certain system variables should be tainted
329 test all_tainted $^X, $0;
332 # Results of matching should all be untainted
334 my $foo = "abcdefghi" . $TAINT;
338 test not any_tainted $`, $&, $';
340 $foo =~ /(...)(...)(...)/;
341 test not any_tainted $1, $2, $3, $+;
343 my @bar = $foo =~ /(...)(...)(...)/;
344 test not any_tainted @bar;
346 test tainted $foo; # $foo should still be tainted!
347 test $foo eq "abcdefghi";
350 # Operations which affect files can't use tainted data.
352 test !eval { chmod 0, $TAINT }, 'chmod';
353 test $@ =~ /^Insecure dependency/, $@;
355 # There is no feature test in $Config{} for truncate,
356 # so we allow for the possibility that it's missing.
357 test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
358 test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
360 test !eval { rename '', $TAINT }, 'rename';
361 test $@ =~ /^Insecure dependency/, $@;
363 test !eval { unlink $TAINT }, 'unlink';
364 test $@ =~ /^Insecure dependency/, $@;
366 test !eval { utime $TAINT }, 'utime';
367 test $@ =~ /^Insecure dependency/, $@;
370 skip "chown() is not available", 2 unless $Config{d_chown};
372 test !eval { chown -1, -1, $TAINT }, 'chown';
373 test $@ =~ /^Insecure dependency/, $@;
377 skip "link() is not available", 2 unless $Config{d_link};
379 test !eval { link $TAINT, '' }, 'link';
380 test $@ =~ /^Insecure dependency/, $@;
384 skip "symlink() is not available", 2 unless $Config{d_symlink};
386 test !eval { symlink $TAINT, '' }, 'symlink';
387 test $@ =~ /^Insecure dependency/, $@;
391 # Operations which affect directories can't use tainted data.
393 test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
394 test $@ =~ /^Insecure dependency/, $@;
396 test !eval { rmdir $TAINT }, 'rmdir';
397 test $@ =~ /^Insecure dependency/, $@;
399 test !eval { chdir "foo".$TAINT }, 'chdir';
400 test $@ =~ /^Insecure dependency/, $@;
403 skip "chroot() is not available", 2 unless $Config{d_chroot};
405 test !eval { chroot $TAINT }, 'chroot';
406 test $@ =~ /^Insecure dependency/, $@;
410 # Some operations using files can't use tainted data.
412 my $foo = "imaginary library" . $TAINT;
413 test !eval { require $foo }, 'require';
414 test $@ =~ /^Insecure dependency/, $@;
416 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
417 END { unlink $filename if defined $filename }
418 $foo = $filename . $TAINT;
419 unlink $filename; # in any case
421 test !eval { open FOO, $foo }, 'open for read';
422 test $@ eq '', $@; # NB: This should be allowed
424 # Try first new style but allow also old style.
425 # We do not want the whole taint.t to fail
426 # just because Errno possibly failing.
427 test eval('$!{ENOENT}') ||
428 $! == 2 || # File not found
429 ($Is_Dos && $! == 22) ||
430 ($^O eq 'mint' && $! == 33);
432 test !eval { open FOO, "> $foo" }, 'open for write';
433 test $@ =~ /^Insecure dependency/, $@;
436 # Commands to the system can't use tainted data
441 skip "open('|') is not available", 4 if $^O eq 'amigaos';
443 test !eval { open FOO, "| x$foo" }, 'popen to';
444 test $@ =~ /^Insecure dependency/, $@;
446 test !eval { open FOO, "x$foo |" }, 'popen from';
447 test $@ =~ /^Insecure dependency/, $@;
450 test !eval { exec $TAINT }, 'exec';
451 test $@ =~ /^Insecure dependency/, $@;
453 test !eval { system $TAINT }, 'system';
454 test $@ =~ /^Insecure dependency/, $@;
459 test !eval { `$echo 1$foo` }, 'backticks';
460 test $@ =~ /^Insecure dependency/, $@;
463 # wildcard expansion doesn't invoke shell on VMS, so is safe
464 skip "This is not VMS", 2 unless $Is_VMS;
466 test join('', eval { glob $foo } ) ne '', 'globbing';
471 # Operations which affect processes can't use tainted data.
473 test !eval { kill 0, $TAINT }, 'kill';
474 test $@ =~ /^Insecure dependency/, $@;
477 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
479 test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
480 test $@ =~ /^Insecure dependency/, $@;
484 skip "setpriority() is not available", 2 unless $Config{d_setprior};
486 test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
487 test $@ =~ /^Insecure dependency/, $@;
491 # Some miscellaneous operations can't use tainted data.
494 skip "syscall() is not available", 2 unless $Config{d_syscall};
496 test !eval { syscall $TAINT }, 'syscall';
497 test $@ =~ /^Insecure dependency/, $@;
504 my $temp = "./taintC$$";
506 test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
508 test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
509 test $@ =~ /^Insecure dependency/, $@;
512 skip "fcntl() is not available", 2 unless $Config{d_fcntl};
514 test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
515 test $@ =~ /^Insecure dependency/, $@;
522 # Some tests involving references
524 my $foo = 'abc' . $TAINT;
526 test not tainted $fooref;
527 test tainted $$fooref;
531 # Some tests involving assignment
535 test all_tainted $foo, $bar;
536 test tainted($foo = $bar);
537 test tainted($bar = $bar);
538 test tainted($bar += $bar);
539 test tainted($bar -= $bar);
540 test tainted($bar *= $bar);
541 test tainted($bar++);
542 test tainted($bar /= $bar);
543 test tainted($bar += 0);
544 test tainted($bar -= 2);
545 test tainted($bar *= -1);
546 test tainted($bar /= 1);
547 test tainted($bar--);
551 # Test assignment and return of lists
553 my @foo = ("A", "tainted" . $TAINT, "B");
554 test not tainted $foo[0];
555 test tainted $foo[1];
556 test not tainted $foo[2];
558 test not tainted $bar[0];
559 test tainted $bar[1];
560 test not tainted $bar[2];
561 my @baz = eval { "A", "tainted" . $TAINT, "B" };
562 test not tainted $baz[0];
563 test tainted $baz[1];
564 test not tainted $baz[2];
565 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
566 test not tainted $plugh[0];
567 test tainted $plugh[1];
568 test not tainted $plugh[2];
569 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
570 test not tainted ((&$nautilus)[0]);
571 test tainted ((&$nautilus)[1]);
572 test not tainted ((&$nautilus)[2]);
573 my @xyzzy = &$nautilus;
574 test not tainted $xyzzy[0];
575 test tainted $xyzzy[1];
576 test not tainted $xyzzy[2];
577 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
578 test not tainted ((&$red_october)[0]);
579 test tainted ((&$red_october)[1]);
580 test not tainted ((&$red_october)[2]);
581 my @corge = &$red_october;
582 test not tainted $corge[0];
583 test tainted $corge[1];
584 test not tainted $corge[2];
587 # Test for system/library calls returning string data of dubious origin.
589 # No reliable %Config check for getpw*
591 skip "getpwent() is not available", 1 unless
592 eval { setpwent(); getpwent() };
595 my @getpwent = getpwent();
596 die "getpwent: $!\n" unless (@getpwent);
597 test ( not tainted $getpwent[0]
598 and tainted $getpwent[1]
599 and not tainted $getpwent[2]
600 and not tainted $getpwent[3]
601 and not tainted $getpwent[4]
602 and not tainted $getpwent[5]
603 and tainted $getpwent[6] # ge?cos
604 and not tainted $getpwent[7]
605 and tainted $getpwent[8]); # shell
610 # pretty hard to imagine not
611 skip "readdir() is not available", 1 unless $Config{d_readdir};
614 opendir(D, "op") or die "opendir: $!\n";
615 my $readdir = readdir(D);
616 test tainted $readdir;
621 skip "readlink() or symlink() is not available" unless
622 $Config{d_readlink} && $Config{d_symlink};
624 my $symlink = "sl$$";
626 my $sl = "/something/naughty";
627 # it has to be a real path on Mac OS
628 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
629 symlink($sl, $symlink) or die "symlink: $!\n";
630 my $readlink = readlink($symlink);
631 test tainted $readlink;
636 # test bitwise ops (regression bug)
646 # test target of substitution (regression bug)
648 my $why = $TAINT."y";
656 $why =~ s/e/'-'.$$/ge;
662 skip "no IPC::SysV", 2 unless $ipcsysv;
666 skip "shm*() not available", 1 unless $Config{d_shm};
672 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
675 if (shmwrite($id, $sent, 0, 60)) {
676 if (shmread($id, $rcvd, 0, 60)) {
677 substr($rcvd, index($rcvd, "\0")) = '';
679 warn "# shmread failed: $!\n";
682 warn "# shmwrite failed: $!\n";
684 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
686 warn "# shmget failed: $!\n";
689 skip "SysV shared memory operation failed", 1 unless
698 skip "msg*() not available", 1 unless $Config{d_msg};
701 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
703 my $sent = "message";
704 my $type_sent = 1234;
709 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
710 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
711 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
713 warn "# msgrcv failed: $!\n";
716 warn "# msgsnd failed: $!\n";
718 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
720 warn "# msgget failed\n";
724 skip "SysV message queue operation failed", 1
725 unless $rcvd eq $sent && $type_sent == $type_rcvd;
733 # bug id 20001004.006
735 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
740 ok tainted($a) && tainted($b) && !defined($b);
746 # bug id 20001004.007
748 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
754 ok !tainted($c->{a}) && tainted($c->{b});
759 ok tainted($d->{a}) && !tainted($d->{b});
763 b => { c => $a, d => 42 } };
764 ok !tainted($e->{a}) &&
766 tainted($e->{b}->{c}) &&
767 !tainted($e->{b}->{d});
773 # bug id 20010519.003
776 use vars qw($has_fcntl);
777 eval { require Fcntl; import Fcntl; };
784 skip "no Fcntl", 18 unless $has_fcntl;
786 my $evil = "foo" . $TAINT;
788 eval { sysopen(my $ro, $evil, &O_RDONLY) };
789 test $@ !~ /^Insecure dependency/, $@;
791 eval { sysopen(my $wo, $evil, &O_WRONLY) };
792 test $@ =~ /^Insecure dependency/, $@;
794 eval { sysopen(my $rw, $evil, &O_RDWR) };
795 test $@ =~ /^Insecure dependency/, $@;
797 eval { sysopen(my $ap, $evil, &O_APPEND) };
798 test $@ =~ /^Insecure dependency/, $@;
800 eval { sysopen(my $cr, $evil, &O_CREAT) };
801 test $@ =~ /^Insecure dependency/, $@;
803 eval { sysopen(my $tr, $evil, &O_TRUNC) };
804 test $@ =~ /^Insecure dependency/, $@;
806 eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
807 test $@ !~ /^Insecure dependency/, $@;
809 eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
810 test $@ =~ /^Insecure dependency/, $@;
812 eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
813 test $@ =~ /^Insecure dependency/, $@;
815 eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
816 test $@ =~ /^Insecure dependency/, $@;
818 eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
819 test $@ =~ /^Insecure dependency/, $@;
821 eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
822 test $@ =~ /^Insecure dependency/, $@;
824 eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
825 test $@ !~ /^Insecure dependency/, $@;
827 eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
828 test $@ =~ /^Insecure dependency/, $@;
830 eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
831 test $@ =~ /^Insecure dependency/, $@;
833 eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
834 test $@ =~ /^Insecure dependency/, $@;
836 eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
837 test $@ =~ /^Insecure dependency/, $@;
839 eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
840 test $@ =~ /^Insecure dependency/, $@;
842 unlink("foo"); # not unlink($evil), because that would fail...
852 local $SIG{__WARN__} = sub { $saw_warning = 1 };
855 my $divnum = shift()/1;
856 sprintf("%1.1f\n", $divnum);
868 # Bug ID 20010730.010
876 bless \$arg => $class;
887 my $bar = "The Big Bright Green Pleasure Machine";
889 tie my ($foo), Tie => $bar;
897 # Check that all environment variables are tainted.
899 while (my ($k, $v) = each %ENV) {
901 # These we have explicitly untainted or set earlier.
902 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
903 push @untainted, "# '$k' = '$v'\n";
906 test @untainted == 0, "untainted:\n @untainted";
910 ok( ${^TAINT} == 1, '$^TAINT is on' );
912 eval { ${^TAINT} = 0 };
913 ok( ${^TAINT}, '$^TAINT is not assignable' );
914 ok( $@ =~ /^Modification of a read-only value attempted/,
915 'Assigning to ${^TAINT} fails' );
920 my $re1 = qr/x$TAINT/;
923 my $re2 = qr/^$re1\z/;
931 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
934 local $ENV{PATH} .= $TAINT;
935 eval { system { "echo" } "/arg0", "arg1" };
936 test $@ =~ /^Insecure \$ENV/;
940 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
943 # bug 20020208.005 plus some single arg exec/system extras
944 my $err = qr/^Insecure dependency/ ;
945 test !eval { exec $TAINT, $TAINT }, 'exec';
947 test !eval { exec $TAINT $TAINT }, 'exec';
949 test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
951 test !eval { exec $TAINT 'notaint' }, 'exec';
953 test !eval { exec {'notaint'} $TAINT }, 'exec';
956 test !eval { system $TAINT, $TAINT }, 'system';
958 test !eval { system $TAINT $TAINT }, 'system';
960 test !eval { system $TAINT $TAINT, $TAINT }, 'system';
962 test !eval { system $TAINT 'notaint' }, 'system';
964 test !eval { system {'notaint'} $TAINT }, 'system';
969 system("lskdfj does not exist","with","args");
974 skip "no exec() on MacOS Classic" if $Is_MacOS;
978 exec("lskdfj does not exist","with","args");
983 # If you add tests here update also the above skip block for VMS.
987 # [ID 20020704.001] taint propagation failure
990 test tainted(my $foo = $1);
994 # [perl #24291] this used to dump core
995 our %nonmagicalenv = ( PATH => "util" );
996 local *ENV = \%nonmagicalenv;
997 eval { system("lskdfj"); };
998 test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
999 local *ENV = *nonmagicalenv;
1000 eval { system("lskdfj"); };
1001 test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
1008 test !tainted($notaint);
1011 $notaint =~ /($notaint)/;
1015 $notaint =~ /($TAINT)/;
1020 $TAINT =~ /($notaint)/;
1024 $TAINT =~ /($TAINT)/;
1030 ($r = $TAINT) =~ /($notaint)/;
1032 ($r = $TAINT) =~ /($TAINT)/;
1036 # accessing $^O shoudn't taint it as a side-effect;
1037 # assigning tainted data to it is now an error
1040 if (!$^X) { } elsif ($^O eq 'bar') { }
1043 test $@ =~ /Insecure dependency in/;
1046 EFFECTIVELY_CONSTANTS: {
1047 my $tainted_number = 12 + $TAINT0;
1048 test tainted( $tainted_number );
1050 # Even though it's always 0, it's still tainted
1051 my $tainted_product = $tainted_number * 0;
1052 test tainted( $tainted_product );
1053 test $tainted_product == 0;
1056 TERNARY_CONDITIONALS: {
1057 my $tainted_true = $TAINT . "blah blah blah";
1058 my $tainted_false = $TAINT0;
1059 test tainted( $tainted_true );
1060 test tainted( $tainted_false );
1062 my $result = $tainted_true ? "True" : "False";
1063 test $result eq "True";
1064 test !tainted( $result );
1066 $result = $tainted_false ? "True" : "False";
1067 test $result eq "False";
1068 test !tainted( $result );
1070 my $untainted_whatever = "The Fabulous Johnny Cash";
1071 my $tainted_whatever = "Soft Cell" . $TAINT;
1073 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1074 test $result eq "Soft Cell";
1075 test tainted( $result );
1077 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1078 test $result eq "The Fabulous Johnny Cash";
1079 test !tainted( $result );
1083 # rt.perl.org 5900 $1 remains tainted if...
1084 # 1) The regular expression contains a scalar variable AND
1085 # 2) The regular expression appears in an elsif clause
1087 my $foo = "abcdefghi" . $TAINT;
1089 my $valid_chars = 'a-z';
1092 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1093 test not tainted $1;
1098 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1099 test not any_tainted @bar;
1103 # at scope exit, a restored localised value should have its old
1104 # taint status, not the taint status of the current statement
1111 test not tainted $x99;
1113 my $c = do { local $x99; $^X };
1114 test not tainted $x99;
1120 my $c = do { local $x99; '' };
1124 # an mg_get of a tainted value during localization shouldn't taint the
1128 eval { local $0, eval '1' };
1132 # [perl #8262] //g loops infinitely on tainted data
1139 while($a[0]=~ m/(.)/g ) {
1140 last if $i++ > 10000;
1142 cmp_ok $i, '<', 10000, "infinite m//g";
1148 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1149 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1150 my $a = Scalar::Util::dualvar(3, $^X);
1152 is ($b, 8, "Arithmetic on tainted dualvars works");
1155 # opening '|-' should not trigger $ENV{PATH} check
1159 skip "fork() is not available", 3 unless $Config{'d_fork'};
1160 skip "opening |- is not stable on threaded OpenBSD with taint", 3
1161 if $Config{useithreads} && $Is_OpenBSD;
1163 $ENV{'PATH'} = $TAINT;
1164 local $SIG{'PIPE'} = 'IGNORE';
1166 my $pid = open my $pipe, '|-';
1167 if (!defined $pid) {
1168 die "open failed: $!";
1171 kill 'KILL', $$; # child suicide
1175 test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1176 test $@ eq '', 'pipe/fork/open/close failed';
1178 open my $pipe, "|$Invoke_Perl -e 1";
1181 test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1186 package AUTOLOAD_TAINT;
1189 return if $AUTOLOAD =~ /DESTROY/;
1190 if ($AUTOLOAD =~ /untainted/) {
1191 main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1193 main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1198 my $o = bless [], 'AUTOLOAD_TAINT';
1204 # tests for tainted format in s?printf
1205 eval { printf($TAINT . "# %s\n", "foo") };
1206 like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1207 eval { printf("# %s\n", $TAINT . "foo") };
1208 ok(!$@, q/printf accepts other tainted args/);
1209 eval { sprintf($TAINT . "# %s\n", "foo") };
1210 like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1211 eval { sprintf("# %s\n", $TAINT . "foo") };
1212 ok(!$@, q/sprintf accepts other tainted args/);
1221 is ($val, '7000000000', 'Assignment to untainted variable');
1224 is ($val, '7000000000', 'Assignment to tainted variable');
1229 my $tainted = '1' . $TAINT;
1230 eval '$val = eval $tainted;';
1231 is ($val, 0, "eval doesn't like tainted strings");
1232 like ($@, qr/^Insecure dependency in eval/);
1234 # Rather nice code to get a tainted undef by from Rick Delaney
1235 open FH, "test.pl" or die $!;
1236 seek FH, 0, 2 or die $!;
1239 eval 'eval $tainted';
1240 like ($@, qr/^Insecure dependency in eval/);
1243 # This may bomb out with the alarm signal so keep it last
1245 skip "No alarm()" unless $Config{d_alarm};
1246 # Test from RT #41831]
1247 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
1249 my $DATA = <<'END' . $TAINT;
1259 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
1260 ## perl is stuck in a regexp infinite loop!
1264 if ($DATA =~ /^line2.*line4/m) {
1265 fail("Should not be a match")
1267 pass("Match on tainted multiline data should fail promptly");
1273 # Keep the previous test last