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?
26 my ($old_env_path, $old_env_dcl_path, $old_env_term);
28 $old_env_path = $ENV{'PATH'};
29 $old_env_dcl_path = $ENV{'DCL$PATH'};
30 $old_env_term = $ENV{'TERM'};
31 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
32 $ENV{PATH} = $ENV{PATH};
33 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
35 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
36 && ($Config{d_shm} || $Config{d_msg})) {
37 eval { require IPC::SysV };
40 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
45 my $Is_VMS = $^O eq 'VMS';
46 my $Is_MSWin32 = $^O eq 'MSWin32';
47 my $Is_NetWare = $^O eq 'NetWare';
48 my $Is_Dos = $^O eq 'dos';
49 my $Is_Cygwin = $^O eq 'cygwin';
50 my $Is_OpenBSD = $^O eq 'openbsd';
51 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
52 $Is_MSWin32 ? '.\perl' :
53 $Is_NetWare ? 'perl' :
55 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
59 for $x ('DCL$PATH', @MoreEnv) {
60 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
62 # VMS note: PATH and TERM are automatically created by the C
63 # library in VMS on reference to the their keys in %ENV.
64 # There is currently no way to determine if they did not exist
65 # before this test was run.
68 \$ENV{PATH} = \$old_env_path;
69 warn "# Note: logical name 'PATH' may have been created\n";
70 \$ENV{'TERM'} = \$old_env_term;
71 warn "# Note: logical name 'TERM' may have been created\n";
72 \@ENV{keys %old} = values %old;
73 if (defined \$old_env_dcl_path) {
74 \$ENV{'DCL\$PATH'} = \$old_env_dcl_path;
76 delete \$ENV{'DCL\$PATH'};
83 # The empty tainted value, for tainting strings
84 my $TAINT = substr($^X, 0, 0);
85 # A tainted zero, useful for tainting numbers
92 # This taints each argument passed. All must be lvalues.
93 # Side effect: It also stringifies them. :-(
95 for (@_) { $_ .= $TAINT }
98 # How to identify taint when you see it
100 not eval { join("",@_), kill 0; 1 };
105 sub all_tainted (@) {
106 for (@_) { return 0 unless tainted $_ }
114 my $curr_test = curr_test();
117 print "ok $curr_test\n";
119 print "not ok $curr_test\n";
120 printf "# Failed test at line %d\n", (caller)[2];
121 for (split m/^/m, $diag) {
126 or substr($diag, -1) eq "\n";
134 # We need an external program to call.
135 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
137 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
138 print PROG 'print "@ARGV\n"', "\n";
140 my $echo = "$Invoke_Perl $ECHO";
142 my $TEST = catfile(curdir(), 'TEST');
144 # First, let's make sure that Perl is checking the dangerous
145 # environment variables. Maybe they aren't set yet, so we'll
146 # taint them ourselves.
148 $ENV{'DCL$PATH'} = '' if $Is_VMS;
150 if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') {
152 foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
153 if (-f "$dir/cc3250mt.dll") {
154 $bcc_dir = $dir and last;
157 if (defined $bcc_dir) {
159 File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or
160 die "$0: failed to copy cc3250mt.dll: $!\n";
162 END { unlink "cc3250mt.dll" }
166 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
167 delete @ENV{@MoreEnv};
170 test eval { `$echo 1` } eq "1\n";
173 skip "Environment tainting tests skipped", 4
174 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
176 my @vars = ('PATH', @MoreEnv);
177 while (my $v = $vars[0]) {
178 local $ENV{$v} = $TAINT;
179 last if eval { `$echo 1` };
180 last unless $@ =~ /^Insecure \$ENV{$v}/;
183 test !@vars, "@vars";
185 # tainted $TERM is unsafe only if it contains metachars
187 $ENV{TERM} = 'e=mc2';
188 test eval { `$echo 1` } eq "1\n";
189 $ENV{TERM} = 'e=mc2' . $TAINT;
190 test !eval { `$echo 1` };
191 test $@ =~ /^Insecure \$ENV{TERM}/, $@;
195 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
196 print "# all directories are writeable\n";
199 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
200 qw(sys$scratch /tmp /var/tmp /usr/tmp),
201 @ENV{qw(TMP TEMP)})[0]
202 or print "# can't find world-writeable directory to test PATH\n";
206 skip "all directories are writeable", 2 unless $tmp;
208 local $ENV{PATH} = $tmp;
209 test !eval { `$echo 1` };
210 test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
214 skip "This is not VMS", 4 unless $Is_VMS;
216 $ENV{'DCL$PATH'} = $TAINT;
217 test eval { `$echo 1` } eq '';
218 test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
220 skip q[can't find world-writeable directory to test DCL$PATH], 2
223 $ENV{'DCL$PATH'} = $tmp;
224 test eval { `$echo 1` } eq '';
225 test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
227 $ENV{'DCL$PATH'} = '';
231 # Let's see that we can taint and untaint as needed.
236 # That was a sanity check. If it failed, stop the insanity!
237 die "Taint checks don't seem to be enabled" unless tainted $foo;
240 test not tainted $foo;
246 test not any_tainted @list;
247 taint_these @list[1,3,5,7,9];
248 test any_tainted @list;
249 test all_tainted @list[1,3,5,7,9];
250 test not any_tainted @list[0,2,4,6,8];
252 ($foo) = $foo =~ /(.+)/;
253 test not tainted $foo;
255 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
256 test not tainted $foo;
262 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
266 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
271 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
275 my $pi = 4 * atan2(1,1) + $TAINT0;
278 ($pi) = $pi =~ /(\d+\.\d+)/;
279 test not tainted $pi;
280 test sprintf("%.5f", $pi) eq '3.14159';
283 # How about command-line arguments? The problem is that we don't
284 # always get some, so we'll run another process with some.
286 my $arg = tempfile();
287 open PROG, "> $arg" or die "Can't create $arg: $!";
289 eval { join('', @ARGV), kill 0 };
290 exit 0 if $@ =~ /^Insecure dependency/;
291 print "# Oops: \$@ was [$@]\n";
295 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
296 test !$?, "Exited with status $?";
300 # Reading from a file should be tainted
302 test open(FILE, $TEST), "Couldn't open '$TEST': $!";
305 sysread(FILE, $block, 100);
312 # Globs should be forbidden, except under VMS,
313 # which doesn't spawn an external program.
315 skip "globs should be forbidden", 2 if 1 or $Is_VMS;
317 my @globs = eval { <*> };
318 test @globs == 0 && $@ =~ /^Insecure dependency/;
320 @globs = eval { glob '*' };
321 test @globs == 0 && $@ =~ /^Insecure dependency/;
324 # Output of commands should be tainted
326 my $foo = `$echo abc`;
330 # Certain system variables should be tainted
332 test all_tainted $^X, $0;
335 # Results of matching should all be untainted
337 my $foo = "abcdefghi" . $TAINT;
341 test not any_tainted $`, $&, $';
343 $foo =~ /(...)(...)(...)/;
344 test not any_tainted $1, $2, $3, $+;
346 my @bar = $foo =~ /(...)(...)(...)/;
347 test not any_tainted @bar;
349 test tainted $foo; # $foo should still be tainted!
350 test $foo eq "abcdefghi";
353 # Operations which affect files can't use tainted data.
355 test !eval { chmod 0, $TAINT }, 'chmod';
356 test $@ =~ /^Insecure dependency/, $@;
358 # There is no feature test in $Config{} for truncate,
359 # so we allow for the possibility that it's missing.
360 test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
361 test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
363 test !eval { rename '', $TAINT }, 'rename';
364 test $@ =~ /^Insecure dependency/, $@;
366 test !eval { unlink $TAINT }, 'unlink';
367 test $@ =~ /^Insecure dependency/, $@;
369 test !eval { utime $TAINT }, 'utime';
370 test $@ =~ /^Insecure dependency/, $@;
373 skip "chown() is not available", 2 unless $Config{d_chown};
375 test !eval { chown -1, -1, $TAINT }, 'chown';
376 test $@ =~ /^Insecure dependency/, $@;
380 skip "link() is not available", 2 unless $Config{d_link};
382 test !eval { link $TAINT, '' }, 'link';
383 test $@ =~ /^Insecure dependency/, $@;
387 skip "symlink() is not available", 2 unless $Config{d_symlink};
389 test !eval { symlink $TAINT, '' }, 'symlink';
390 test $@ =~ /^Insecure dependency/, $@;
394 # Operations which affect directories can't use tainted data.
396 test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
397 test $@ =~ /^Insecure dependency/, $@;
399 test !eval { rmdir $TAINT }, 'rmdir';
400 test $@ =~ /^Insecure dependency/, $@;
402 test !eval { chdir "foo".$TAINT }, 'chdir';
403 test $@ =~ /^Insecure dependency/, $@;
406 skip "chroot() is not available", 2 unless $Config{d_chroot};
408 test !eval { chroot $TAINT }, 'chroot';
409 test $@ =~ /^Insecure dependency/, $@;
413 # Some operations using files can't use tainted data.
415 my $foo = "imaginary library" . $TAINT;
416 test !eval { require $foo }, 'require';
417 test $@ =~ /^Insecure dependency/, $@;
419 my $filename = tempfile(); # NB: $filename isn't tainted!
420 $foo = $filename . $TAINT;
421 unlink $filename; # in any case
423 test !eval { open FOO, $foo }, 'open for read';
424 test $@ eq '', $@; # NB: This should be allowed
426 # Try first new style but allow also old style.
427 # We do not want the whole taint.t to fail
428 # just because Errno possibly failing.
429 test eval('$!{ENOENT}') ||
430 $! == 2 || # File not found
431 ($Is_Dos && $! == 22);
433 test !eval { open FOO, "> $foo" }, 'open for write';
434 test $@ =~ /^Insecure dependency/, $@;
437 # Commands to the system can't use tainted data
442 skip "open('|') is not available", 4 if $^O eq 'amigaos';
444 test !eval { open FOO, "| x$foo" }, 'popen to';
445 test $@ =~ /^Insecure dependency/, $@;
447 test !eval { open FOO, "x$foo |" }, 'popen from';
448 test $@ =~ /^Insecure dependency/, $@;
451 test !eval { exec $TAINT }, 'exec';
452 test $@ =~ /^Insecure dependency/, $@;
454 test !eval { system $TAINT }, 'system';
455 test $@ =~ /^Insecure dependency/, $@;
460 test !eval { `$echo 1$foo` }, 'backticks';
461 test $@ =~ /^Insecure dependency/, $@;
464 # wildcard expansion doesn't invoke shell on VMS, so is safe
465 skip "This is not VMS", 2 unless $Is_VMS;
467 test join('', eval { glob $foo } ) ne '', 'globbing';
472 # Operations which affect processes can't use tainted data.
474 test !eval { kill 0, $TAINT }, 'kill';
475 test $@ =~ /^Insecure dependency/, $@;
478 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
480 test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
481 test $@ =~ /^Insecure dependency/, $@;
485 skip "setpriority() is not available", 2 unless $Config{d_setprior};
487 test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
488 test $@ =~ /^Insecure dependency/, $@;
492 # Some miscellaneous operations can't use tainted data.
495 skip "syscall() is not available", 2 unless $Config{d_syscall};
497 test !eval { syscall $TAINT }, 'syscall';
498 test $@ =~ /^Insecure dependency/, $@;
505 my $temp = tempfile();
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 symlink($sl, $symlink) or die "symlink: $!\n";
629 my $readlink = readlink($symlink);
630 test tainted $readlink;
635 # test bitwise ops (regression bug)
645 # test target of substitution (regression bug)
647 my $why = $TAINT."y";
655 $why =~ s/e/'-'.$$/ge;
661 skip "no IPC::SysV", 2 unless $ipcsysv;
665 skip "shm*() not available", 1 unless $Config{d_shm};
671 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
674 if (shmwrite($id, $sent, 0, 60)) {
675 if (shmread($id, $rcvd, 0, 60)) {
676 substr($rcvd, index($rcvd, "\0")) = '';
678 warn "# shmread failed: $!\n";
681 warn "# shmwrite failed: $!\n";
683 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
685 warn "# shmget failed: $!\n";
688 skip "SysV shared memory operation failed", 1 unless
697 skip "msg*() not available", 1 unless $Config{d_msg};
700 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
702 my $sent = "message";
703 my $type_sent = 1234;
708 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
709 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
710 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
712 warn "# msgrcv failed: $!\n";
715 warn "# msgsnd failed: $!\n";
717 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
719 warn "# msgget failed\n";
723 skip "SysV message queue operation failed", 1
724 unless $rcvd eq $sent && $type_sent == $type_rcvd;
732 # bug id 20001004.006
734 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
739 ok tainted($a) && tainted($b) && !defined($b);
745 # bug id 20001004.007
747 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
753 ok !tainted($c->{a}) && tainted($c->{b});
758 ok tainted($d->{a}) && !tainted($d->{b});
762 b => { c => $a, d => 42 } };
763 ok !tainted($e->{a}) &&
765 tainted($e->{b}->{c}) &&
766 !tainted($e->{b}->{d});
772 # bug id 20010519.003
775 use vars qw($has_fcntl);
776 eval { require Fcntl; import Fcntl; };
783 skip "no Fcntl", 18 unless $has_fcntl;
785 my $evil = "foo" . $TAINT;
787 eval { sysopen(my $ro, $evil, &O_RDONLY) };
788 test $@ !~ /^Insecure dependency/, $@;
790 eval { sysopen(my $wo, $evil, &O_WRONLY) };
791 test $@ =~ /^Insecure dependency/, $@;
793 eval { sysopen(my $rw, $evil, &O_RDWR) };
794 test $@ =~ /^Insecure dependency/, $@;
796 eval { sysopen(my $ap, $evil, &O_APPEND) };
797 test $@ =~ /^Insecure dependency/, $@;
799 eval { sysopen(my $cr, $evil, &O_CREAT) };
800 test $@ =~ /^Insecure dependency/, $@;
802 eval { sysopen(my $tr, $evil, &O_TRUNC) };
803 test $@ =~ /^Insecure dependency/, $@;
805 eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
806 test $@ !~ /^Insecure dependency/, $@;
808 eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
809 test $@ =~ /^Insecure dependency/, $@;
811 eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
812 test $@ =~ /^Insecure dependency/, $@;
814 eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
815 test $@ =~ /^Insecure dependency/, $@;
817 eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
818 test $@ =~ /^Insecure dependency/, $@;
820 eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
821 test $@ =~ /^Insecure dependency/, $@;
823 eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
824 test $@ !~ /^Insecure dependency/, $@;
826 eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
827 test $@ =~ /^Insecure dependency/, $@;
829 eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
830 test $@ =~ /^Insecure dependency/, $@;
832 eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
833 test $@ =~ /^Insecure dependency/, $@;
835 eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
836 test $@ =~ /^Insecure dependency/, $@;
838 eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
839 test $@ =~ /^Insecure dependency/, $@;
841 unlink("foo"); # not unlink($evil), because that would fail...
851 local $SIG{__WARN__} = sub { $saw_warning = 1 };
854 my $divnum = shift()/1;
855 sprintf("%1.1f\n", $divnum);
867 # Bug ID 20010730.010
875 bless \$arg => $class;
886 my $bar = "The Big Bright Green Pleasure Machine";
888 tie my ($foo), Tie => $bar;
896 # Check that all environment variables are tainted.
898 while (my ($k, $v) = each %ENV) {
900 # These we have explicitly untainted or set earlier.
901 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
902 push @untainted, "# '$k' = '$v'\n";
905 test @untainted == 0, "untainted:\n @untainted";
909 ok( ${^TAINT} == 1, '$^TAINT is on' );
911 eval { ${^TAINT} = 0 };
912 ok( ${^TAINT}, '$^TAINT is not assignable' );
913 ok( $@ =~ /^Modification of a read-only value attempted/,
914 'Assigning to ${^TAINT} fails' );
919 my $re1 = qr/x$TAINT/;
922 my $re2 = qr/^$re1\z/;
930 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
933 local $ENV{PATH} .= $TAINT;
934 eval { system { "echo" } "/arg0", "arg1" };
935 test $@ =~ /^Insecure \$ENV/;
939 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
942 # bug 20020208.005 plus some single arg exec/system extras
943 my $err = qr/^Insecure dependency/ ;
944 test !eval { exec $TAINT, $TAINT }, 'exec';
946 test !eval { exec $TAINT $TAINT }, 'exec';
948 test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
950 test !eval { exec $TAINT 'notaint' }, 'exec';
952 test !eval { exec {'notaint'} $TAINT }, 'exec';
955 test !eval { system $TAINT, $TAINT }, 'system';
957 test !eval { system $TAINT $TAINT }, 'system';
959 test !eval { system $TAINT $TAINT, $TAINT }, 'system';
961 test !eval { system $TAINT 'notaint' }, 'system';
963 test !eval { system {'notaint'} $TAINT }, 'system';
968 system("lskdfj does not exist","with","args");
974 exec("lskdfj does not exist","with","args");
978 # If you add tests here update also the above skip block for VMS.
982 # [ID 20020704.001] taint propagation failure
985 test tainted(my $foo = $1);
989 # [perl #24291] this used to dump core
990 our %nonmagicalenv = ( PATH => "util" );
991 local *ENV = \%nonmagicalenv;
992 eval { system("lskdfj"); };
993 test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
994 local *ENV = *nonmagicalenv;
995 eval { system("lskdfj"); };
996 test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
1003 test !tainted($notaint);
1006 $notaint =~ /($notaint)/;
1010 $notaint =~ /($TAINT)/;
1015 $TAINT =~ /($notaint)/;
1019 $TAINT =~ /($TAINT)/;
1025 ($r = $TAINT) =~ /($notaint)/;
1027 ($r = $TAINT) =~ /($TAINT)/;
1031 # accessing $^O shoudn't taint it as a side-effect;
1032 # assigning tainted data to it is now an error
1035 if (!$^X) { } elsif ($^O eq 'bar') { }
1038 test $@ =~ /Insecure dependency in/;
1041 EFFECTIVELY_CONSTANTS: {
1042 my $tainted_number = 12 + $TAINT0;
1043 test tainted( $tainted_number );
1045 # Even though it's always 0, it's still tainted
1046 my $tainted_product = $tainted_number * 0;
1047 test tainted( $tainted_product );
1048 test $tainted_product == 0;
1051 TERNARY_CONDITIONALS: {
1052 my $tainted_true = $TAINT . "blah blah blah";
1053 my $tainted_false = $TAINT0;
1054 test tainted( $tainted_true );
1055 test tainted( $tainted_false );
1057 my $result = $tainted_true ? "True" : "False";
1058 test $result eq "True";
1059 test !tainted( $result );
1061 $result = $tainted_false ? "True" : "False";
1062 test $result eq "False";
1063 test !tainted( $result );
1065 my $untainted_whatever = "The Fabulous Johnny Cash";
1066 my $tainted_whatever = "Soft Cell" . $TAINT;
1068 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1069 test $result eq "Soft Cell";
1070 test tainted( $result );
1072 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1073 test $result eq "The Fabulous Johnny Cash";
1074 test !tainted( $result );
1078 # rt.perl.org 5900 $1 remains tainted if...
1079 # 1) The regular expression contains a scalar variable AND
1080 # 2) The regular expression appears in an elsif clause
1082 my $foo = "abcdefghi" . $TAINT;
1084 my $valid_chars = 'a-z';
1087 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1088 test not tainted $1;
1093 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1094 test not any_tainted @bar;
1098 # at scope exit, a restored localised value should have its old
1099 # taint status, not the taint status of the current statement
1106 test not tainted $x99;
1108 my $c = do { local $x99; $^X };
1109 test not tainted $x99;
1115 my $c = do { local $x99; '' };
1119 # an mg_get of a tainted value during localization shouldn't taint the
1123 eval { local $0, eval '1' };
1127 # [perl #8262] //g loops infinitely on tainted data
1134 while($a[0]=~ m/(.)/g ) {
1135 last if $i++ > 10000;
1137 cmp_ok $i, '<', 10000, "infinite m//g";
1143 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1144 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1145 my $a = Scalar::Util::dualvar(3, $^X);
1147 is ($b, 8, "Arithmetic on tainted dualvars works");
1150 # opening '|-' should not trigger $ENV{PATH} check
1154 skip "fork() is not available", 3 unless $Config{'d_fork'};
1155 skip "opening |- is not stable on threaded OpenBSD with taint", 3
1156 if $Config{useithreads} && $Is_OpenBSD;
1158 $ENV{'PATH'} = $TAINT;
1159 local $SIG{'PIPE'} = 'IGNORE';
1161 my $pid = open my $pipe, '|-';
1162 if (!defined $pid) {
1163 die "open failed: $!";
1166 kill 'KILL', $$; # child suicide
1170 test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1171 test $@ eq '', 'pipe/fork/open/close failed';
1173 open my $pipe, "|$Invoke_Perl -e 1";
1176 test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1181 package AUTOLOAD_TAINT;
1184 return if $AUTOLOAD =~ /DESTROY/;
1185 if ($AUTOLOAD =~ /untainted/) {
1186 main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1188 main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1193 my $o = bless [], 'AUTOLOAD_TAINT';
1199 # tests for tainted format in s?printf
1200 eval { printf($TAINT . "# %s\n", "foo") };
1201 like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1202 eval { printf("# %s\n", $TAINT . "foo") };
1203 ok(!$@, q/printf accepts other tainted args/);
1204 eval { sprintf($TAINT . "# %s\n", "foo") };
1205 like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1206 eval { sprintf("# %s\n", $TAINT . "foo") };
1207 ok(!$@, q/sprintf accepts other tainted args/);
1216 is ($val, '7000000000', 'Assignment to untainted variable');
1219 is ($val, '7000000000', 'Assignment to tainted variable');
1224 my $tainted = '1' . $TAINT;
1225 eval '$val = eval $tainted;';
1226 is ($val, 0, "eval doesn't like tainted strings");
1227 like ($@, qr/^Insecure dependency in eval/);
1229 # Rather nice code to get a tainted undef by from Rick Delaney
1230 open FH, "test.pl" or die $!;
1231 seek FH, 0, 2 or die $!;
1234 eval 'eval $tainted';
1235 like ($@, qr/^Insecure dependency in eval/);
1238 foreach my $ord (78, 163, 256) {
1240 my $line = 'A1' . $TAINT . chr $ord;
1244 ok(!tainted($1), "\\S match with chr $ord");
1249 sub cr { my $x = crypt($_[0], $_[1]); $x }
1250 sub co { my $x = ~$_[0]; $x }
1252 $a = cr('hello', 'foo' . $TAINT);
1253 $b = cr('hello', 'foo');
1254 ok(tainted($a), "tainted crypt");
1255 ok(!tainted($b), "untainted crypt");
1256 $a = co('foo' . $TAINT);
1258 ok(tainted($a), "tainted complement");
1259 ok(!tainted($b), "untainted complement");
1263 my @data = qw(bonk zam zlonk qunckkk);
1264 # Clearly some sort of usenet bang-path
1265 my $string = $TAINT . join "!", @data;
1267 ok(tainted($string), "tainted data");
1269 my @got = split /!|,/, $string;
1271 # each @got would be useful here, but I want the test for earlier perls
1272 for my $i (0 .. $#data) {
1273 ok(tainted($got[$i]), "tainted result $i");
1274 is($got[$i], $data[$i], "correct content $i");
1277 ok(tainted($string), "still tainted data");
1279 my @got = split /[!,]/, $string;
1281 # each @got would be useful here, but I want the test for earlier perls
1282 for my $i (0 .. $#data) {
1283 ok(tainted($got[$i]), "tainted result $i");
1284 is($got[$i], $data[$i], "correct content $i");
1287 ok(tainted($string), "still tainted data");
1289 my @got = split /!/, $string;
1291 # each @got would be useful here, but I want the test for earlier perls
1292 for my $i (0 .. $#data) {
1293 ok(tainted($got[$i]), "tainted result $i");
1294 is($got[$i], $data[$i], "correct content $i");
1298 # Bug RT #52552 - broken by change at git commit id f337b08
1300 my $x = $TAINT. q{print "Hello world\n"};
1301 my $y = pack "a*", $x;
1302 ok(tainted($y), "pack a* preserves tainting");
1304 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
1305 ok(tainted($z), "pack A* preserves tainting");
1307 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
1308 ok(tainted($zz), "pack a*a* preserves tainting");
1311 # This may bomb out with the alarm signal so keep it last
1313 skip "No alarm()" unless $Config{d_alarm};
1314 # Test from RT #41831]
1315 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
1317 my $DATA = <<'END' . $TAINT;
1327 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
1328 ## perl is stuck in a regexp infinite loop!
1332 if ($DATA =~ /^line2.*line4/m) {
1333 fail("Should not be a match")
1335 pass("Match on tainted multiline data should fail promptly");
1341 # Keep the previous test last