t/op/my_stash.t should use test.pl instead of Test.pm
[p5sagit/p5-mst-13.2.git] / t / op / taint.t
1 #!./perl -T
2 #
3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4 #
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?
8 #
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib';
13 }
14
15 use strict;
16 use Config;
17 use File::Spec::Functions;
18
19 BEGIN { require './test.pl'; }
20 plan tests => 298;
21
22 $| = 1;
23
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
25
26 my ($old_env_path, $old_env_dcl_path, $old_env_term);
27 BEGIN {
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';
34   }
35   if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
36       && ($Config{d_shm} || $Config{d_msg})) {
37       eval { require IPC::SysV };
38       unless ($@) {
39           $ipcsysv++;
40           IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
41       }
42   }
43 }
44
45 my $Is_MacOS    = $^O eq 'MacOS';
46 my $Is_VMS      = $^O eq 'VMS';
47 my $Is_MSWin32  = $^O eq 'MSWin32';
48 my $Is_NetWare  = $^O eq 'NetWare';
49 my $Is_Dos      = $^O eq 'dos';
50 my $Is_Cygwin   = $^O eq 'cygwin';
51 my $Is_OpenBSD  = $^O eq 'openbsd';
52 my $Invoke_Perl = $Is_VMS      ? 'MCR Sys$Disk:[]Perl.exe' :
53                   $Is_MSWin32  ? '.\perl'               :
54                   $Is_MacOS    ? ':perl'                :
55                   $Is_NetWare  ? 'perl'                 : 
56                                  './perl'               ;
57 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
58
59 if ($Is_VMS) {
60     my (%old, $x);
61     for $x ('DCL$PATH', @MoreEnv) {
62         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
63     }
64     # VMS note:  PATH and TERM are automatically created by the C
65     # library in VMS on reference to the their keys in %ENV.
66     # There is currently no way to determine if they did not exist
67     # before this test was run.
68     eval <<EndOfCleanup;
69         END {
70             \$ENV{PATH} = \$old_env_path;
71             warn "# Note: logical name 'PATH' may have been created\n";
72             \$ENV{'TERM'} = \$old_env_term;
73             warn "# Note: logical name 'TERM' may have been created\n";
74             \@ENV{keys %old} = values %old;
75             if (defined \$old_env_dcl_path) {
76                 \$ENV{'DCL\$PATH'} = \$old_env_dcl_path;
77             } else {
78                 delete \$ENV{'DCL\$PATH'};
79             }
80         }
81 EndOfCleanup
82 }
83
84 # Sources of taint:
85 #   The empty tainted value, for tainting strings
86 my $TAINT = substr($^X, 0, 0);
87 #   A tainted zero, useful for tainting numbers
88 my $TAINT0;
89 {
90     no warnings;
91     $TAINT0 = 0 + $TAINT;
92 }
93
94 # This taints each argument passed. All must be lvalues.
95 # Side effect: It also stringifies them. :-(
96 sub taint_these (@) {
97     for (@_) { $_ .= $TAINT }
98 }
99
100 # How to identify taint when you see it
101 sub any_tainted (@) {
102     not eval { join("",@_), kill 0; 1 };
103 }
104 sub tainted ($) {
105     any_tainted @_;
106 }
107 sub all_tainted (@) {
108     for (@_) { return 0 unless tainted $_ }
109     1;
110 }
111
112
113 sub test ($;$) {
114     my($ok, $diag) = @_;
115
116     my $curr_test = curr_test();
117
118     if ($ok) {
119         print "ok $curr_test\n";
120     } else {
121         print "not ok $curr_test\n";
122         printf "# Failed test at line %d\n", (caller)[2];
123         for (split m/^/m, $diag) {
124             print "# $_";
125         }
126         print "\n" unless
127             $diag eq ''
128             or substr($diag, -1) eq "\n";
129     }
130
131     next_test();
132
133     return $ok;
134 }
135
136 # We need an external program to call.
137 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
138 END { unlink $ECHO }
139 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
140 print PROG 'print "@ARGV\n"', "\n";
141 close PROG;
142 my $echo = "$Invoke_Perl $ECHO";
143
144 my $TEST = catfile(curdir(), 'TEST');
145
146 # First, let's make sure that Perl is checking the dangerous
147 # environment variables. Maybe they aren't set yet, so we'll
148 # taint them ourselves.
149 {
150     $ENV{'DCL$PATH'} = '' if $Is_VMS;
151
152     if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') {
153         my $bcc_dir;
154         foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
155             if (-f "$dir/cc3250mt.dll") {
156                 $bcc_dir = $dir and last;
157             }
158         }
159         if (defined $bcc_dir) {
160             require File::Copy;
161             File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or
162                 die "$0: failed to copy cc3250mt.dll: $!\n";
163             eval q{
164                 END { unlink "cc3250mt.dll" }
165             };
166         }
167     }
168     $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
169     delete @ENV{@MoreEnv};
170     $ENV{TERM} = 'dumb';
171
172     test eval { `$echo 1` } eq "1\n";
173
174     SKIP: {
175         skip "Environment tainting tests skipped", 4
176           if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
177
178         my @vars = ('PATH', @MoreEnv);
179         while (my $v = $vars[0]) {
180             local $ENV{$v} = $TAINT;
181             last if eval { `$echo 1` };
182             last unless $@ =~ /^Insecure \$ENV{$v}/;
183             shift @vars;
184         }
185         test !@vars, "@vars";
186
187         # tainted $TERM is unsafe only if it contains metachars
188         local $ENV{TERM};
189         $ENV{TERM} = 'e=mc2';
190         test eval { `$echo 1` } eq "1\n";
191         $ENV{TERM} = 'e=mc2' . $TAINT;
192         test !eval { `$echo 1` };
193         test $@ =~ /^Insecure \$ENV{TERM}/, $@;
194     }
195
196     my $tmp;
197     if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
198         print "# all directories are writeable\n";
199     }
200     else {
201         $tmp = (grep { defined and -d and (stat _)[2] & 2 }
202                      qw(sys$scratch /tmp /var/tmp /usr/tmp),
203                      @ENV{qw(TMP TEMP)})[0]
204             or print "# can't find world-writeable directory to test PATH\n";
205     }
206
207     SKIP: {
208         skip "all directories are writeable", 2 unless $tmp;
209
210         local $ENV{PATH} = $tmp;
211         test !eval { `$echo 1` };
212         test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
213     }
214
215     SKIP: {
216         skip "This is not VMS", 4 unless $Is_VMS;
217
218         $ENV{'DCL$PATH'} = $TAINT;
219         test  eval { `$echo 1` } eq '';
220         test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
221         SKIP: {
222             skip q[can't find world-writeable directory to test DCL$PATH], 2
223               unless $tmp;
224
225             $ENV{'DCL$PATH'} = $tmp;
226             test eval { `$echo 1` } eq '';
227             test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
228         }
229         $ENV{'DCL$PATH'} = '';
230     }
231 }
232
233 # Let's see that we can taint and untaint as needed.
234 {
235     my $foo = $TAINT;
236     test tainted $foo;
237
238     # That was a sanity check. If it failed, stop the insanity!
239     die "Taint checks don't seem to be enabled" unless tainted $foo;
240
241     $foo = "foo";
242     test not tainted $foo;
243
244     taint_these($foo);
245     test tainted $foo;
246
247     my @list = 1..10;
248     test not any_tainted @list;
249     taint_these @list[1,3,5,7,9];
250     test any_tainted @list;
251     test all_tainted @list[1,3,5,7,9];
252     test not any_tainted @list[0,2,4,6,8];
253
254     ($foo) = $foo =~ /(.+)/;
255     test not tainted $foo;
256
257     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
258     test not tainted $foo;
259     test $foo eq 'bar';
260
261     {
262       use re 'taint';
263
264       ($foo) = ('bar' . $TAINT) =~ /(.+)/;
265       test tainted $foo;
266       test $foo eq 'bar';
267
268       $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
269       test tainted $foo;
270       test $foo eq 'bar';
271     }
272
273     $foo = $1 if 'bar' =~ /(.+)$TAINT/;
274     test tainted $foo;
275     test $foo eq 'bar';
276
277     my $pi = 4 * atan2(1,1) + $TAINT0;
278     test tainted $pi;
279
280     ($pi) = $pi =~ /(\d+\.\d+)/;
281     test not tainted $pi;
282     test sprintf("%.5f", $pi) eq '3.14159';
283 }
284
285 # How about command-line arguments? The problem is that we don't
286 # always get some, so we'll run another process with some.
287 SKIP: {
288     my $arg = tempfile();
289     open PROG, "> $arg" or die "Can't create $arg: $!";
290     print PROG q{
291         eval { join('', @ARGV), kill 0 };
292         exit 0 if $@ =~ /^Insecure dependency/;
293         print "# Oops: \$@ was [$@]\n";
294         exit 1;
295     };
296     close PROG;
297     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
298     test !$?, "Exited with status $?";
299     unlink $arg;
300 }
301
302 # Reading from a file should be tainted
303 {
304     test open(FILE, $TEST), "Couldn't open '$TEST': $!";
305
306     my $block;
307     sysread(FILE, $block, 100);
308     my $line = <FILE>;
309     close FILE;
310     test tainted $block;
311     test tainted $line;
312 }
313
314 # Globs should be forbidden, except under VMS,
315 #   which doesn't spawn an external program.
316 SKIP: {
317     skip "globs should be forbidden", 2 if 1 or $Is_VMS;
318
319     my @globs = eval { <*> };
320     test @globs == 0 && $@ =~ /^Insecure dependency/;
321
322     @globs = eval { glob '*' };
323     test @globs == 0 && $@ =~ /^Insecure dependency/;
324 }
325
326 # Output of commands should be tainted
327 {
328     my $foo = `$echo abc`;
329     test tainted $foo;
330 }
331
332 # Certain system variables should be tainted
333 {
334     test all_tainted $^X, $0;
335 }
336
337 # Results of matching should all be untainted
338 {
339     my $foo = "abcdefghi" . $TAINT;
340     test tainted $foo;
341
342     $foo =~ /def/;
343     test not any_tainted $`, $&, $';
344
345     $foo =~ /(...)(...)(...)/;
346     test not any_tainted $1, $2, $3, $+;
347
348     my @bar = $foo =~ /(...)(...)(...)/;
349     test not any_tainted @bar;
350
351     test tainted $foo;  # $foo should still be tainted!
352     test $foo eq "abcdefghi";
353 }
354
355 # Operations which affect files can't use tainted data.
356 {
357     test !eval { chmod 0, $TAINT }, 'chmod';
358     test $@ =~ /^Insecure dependency/, $@;
359
360     # There is no feature test in $Config{} for truncate,
361     #   so we allow for the possibility that it's missing.
362     test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
363     test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
364
365     test !eval { rename '', $TAINT }, 'rename';
366     test $@ =~ /^Insecure dependency/, $@;
367
368     test !eval { unlink $TAINT }, 'unlink';
369     test $@ =~ /^Insecure dependency/, $@;
370
371     test !eval { utime $TAINT }, 'utime';
372     test $@ =~ /^Insecure dependency/, $@;
373
374     SKIP: {
375         skip "chown() is not available", 2 unless $Config{d_chown};
376
377         test !eval { chown -1, -1, $TAINT }, 'chown';
378         test $@ =~ /^Insecure dependency/, $@;
379     }
380
381     SKIP: {
382         skip "link() is not available", 2 unless $Config{d_link};
383
384         test !eval { link $TAINT, '' }, 'link';
385         test $@ =~ /^Insecure dependency/, $@;
386     }
387
388     SKIP: {
389         skip "symlink() is not available", 2 unless $Config{d_symlink};
390
391         test !eval { symlink $TAINT, '' }, 'symlink';
392         test $@ =~ /^Insecure dependency/, $@;
393     }
394 }
395
396 # Operations which affect directories can't use tainted data.
397 {
398     test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
399     test $@ =~ /^Insecure dependency/, $@;
400
401     test !eval { rmdir $TAINT }, 'rmdir';
402     test $@ =~ /^Insecure dependency/, $@;
403
404     test !eval { chdir "foo".$TAINT }, 'chdir';
405     test $@ =~ /^Insecure dependency/, $@;
406
407     SKIP: {
408         skip "chroot() is not available", 2 unless $Config{d_chroot};
409
410         test !eval { chroot $TAINT }, 'chroot';
411         test $@ =~ /^Insecure dependency/, $@;
412     }
413 }
414
415 # Some operations using files can't use tainted data.
416 {
417     my $foo = "imaginary library" . $TAINT;
418     test !eval { require $foo }, 'require';
419     test $@ =~ /^Insecure dependency/, $@;
420
421     my $filename = tempfile();  # NB: $filename isn't tainted!
422     $foo = $filename . $TAINT;
423     unlink $filename;   # in any case
424
425     test !eval { open FOO, $foo }, 'open for read';
426     test $@ eq '', $@;          # NB: This should be allowed
427
428     # Try first new style but allow also old style.
429     # We do not want the whole taint.t to fail
430     # just because Errno possibly failing.
431     test eval('$!{ENOENT}') ||
432         $! == 2 || # File not found
433         ($Is_Dos && $! == 22) ||
434         ($^O eq 'mint' && $! == 33);
435
436     test !eval { open FOO, "> $foo" }, 'open for write';
437     test $@ =~ /^Insecure dependency/, $@;
438 }
439
440 # Commands to the system can't use tainted data
441 {
442     my $foo = $TAINT;
443
444     SKIP: {
445         skip "open('|') is not available", 4 if $^O eq 'amigaos';
446
447         test !eval { open FOO, "| x$foo" }, 'popen to';
448         test $@ =~ /^Insecure dependency/, $@;
449
450         test !eval { open FOO, "x$foo |" }, 'popen from';
451         test $@ =~ /^Insecure dependency/, $@;
452     }
453
454     test !eval { exec $TAINT }, 'exec';
455     test $@ =~ /^Insecure dependency/, $@;
456
457     test !eval { system $TAINT }, 'system';
458     test $@ =~ /^Insecure dependency/, $@;
459
460     $foo = "*";
461     taint_these $foo;
462
463     test !eval { `$echo 1$foo` }, 'backticks';
464     test $@ =~ /^Insecure dependency/, $@;
465
466     SKIP: {
467         # wildcard expansion doesn't invoke shell on VMS, so is safe
468         skip "This is not VMS", 2 unless $Is_VMS;
469     
470         test join('', eval { glob $foo } ) ne '', 'globbing';
471         test $@ eq '', $@;
472     }
473 }
474
475 # Operations which affect processes can't use tainted data.
476 {
477     test !eval { kill 0, $TAINT }, 'kill';
478     test $@ =~ /^Insecure dependency/, $@;
479
480     SKIP: {
481         skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
482
483         test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
484         test $@ =~ /^Insecure dependency/, $@;
485     }
486
487     SKIP: {
488         skip "setpriority() is not available", 2 unless $Config{d_setprior};
489
490         test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
491         test $@ =~ /^Insecure dependency/, $@;
492     }
493 }
494
495 # Some miscellaneous operations can't use tainted data.
496 {
497     SKIP: {
498         skip "syscall() is not available", 2 unless $Config{d_syscall};
499
500         test !eval { syscall $TAINT }, 'syscall';
501         test $@ =~ /^Insecure dependency/, $@;
502     }
503
504     {
505         my $foo = "x" x 979;
506         taint_these $foo;
507         local *FOO;
508         my $temp = tempfile();
509         test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
510
511         test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
512         test $@ =~ /^Insecure dependency/, $@;
513
514         SKIP: {
515             skip "fcntl() is not available", 2 unless $Config{d_fcntl};
516
517             test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
518             test $@ =~ /^Insecure dependency/, $@;
519         }
520
521         close FOO;
522     }
523 }
524
525 # Some tests involving references
526 {
527     my $foo = 'abc' . $TAINT;
528     my $fooref = \$foo;
529     test not tainted $fooref;
530     test tainted $$fooref;
531     test tainted $foo;
532 }
533
534 # Some tests involving assignment
535 {
536     my $foo = $TAINT0;
537     my $bar = $foo;
538     test all_tainted $foo, $bar;
539     test tainted($foo = $bar);
540     test tainted($bar = $bar);
541     test tainted($bar += $bar);
542     test tainted($bar -= $bar);
543     test tainted($bar *= $bar);
544     test tainted($bar++);
545     test tainted($bar /= $bar);
546     test tainted($bar += 0);
547     test tainted($bar -= 2);
548     test tainted($bar *= -1);
549     test tainted($bar /= 1);
550     test tainted($bar--);
551     test $bar == 0;
552 }
553
554 # Test assignment and return of lists
555 {
556     my @foo = ("A", "tainted" . $TAINT, "B");
557     test not tainted $foo[0];
558     test     tainted $foo[1];
559     test not tainted $foo[2];
560     my @bar = @foo;
561     test not tainted $bar[0];
562     test     tainted $bar[1];
563     test not tainted $bar[2];
564     my @baz = eval { "A", "tainted" . $TAINT, "B" };
565     test not tainted $baz[0];
566     test     tainted $baz[1];
567     test not tainted $baz[2];
568     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
569     test not tainted $plugh[0];
570     test     tainted $plugh[1];
571     test not tainted $plugh[2];
572     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
573     test not tainted ((&$nautilus)[0]);
574     test     tainted ((&$nautilus)[1]);
575     test not tainted ((&$nautilus)[2]);
576     my @xyzzy = &$nautilus;
577     test not tainted $xyzzy[0];
578     test     tainted $xyzzy[1];
579     test not tainted $xyzzy[2];
580     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
581     test not tainted ((&$red_october)[0]);
582     test     tainted ((&$red_october)[1]);
583     test not tainted ((&$red_october)[2]);
584     my @corge = &$red_october;
585     test not tainted $corge[0];
586     test     tainted $corge[1];
587     test not tainted $corge[2];
588 }
589
590 # Test for system/library calls returning string data of dubious origin.
591 {
592     # No reliable %Config check for getpw*
593     SKIP: {
594         skip "getpwent() is not available", 1 unless 
595           eval { setpwent(); getpwent() };
596
597         setpwent();
598         my @getpwent = getpwent();
599         die "getpwent: $!\n" unless (@getpwent);
600         test (    not tainted $getpwent[0]
601                   and     tainted $getpwent[1]
602                   and not tainted $getpwent[2]
603                   and not tainted $getpwent[3]
604                   and not tainted $getpwent[4]
605                   and not tainted $getpwent[5]
606                   and     tainted $getpwent[6]          # ge?cos
607                   and not tainted $getpwent[7]
608                   and     tainted $getpwent[8]);        # shell
609         endpwent();
610     }
611
612     SKIP: {
613         # pretty hard to imagine not
614         skip "readdir() is not available", 1 unless $Config{d_readdir};
615
616         local(*D);
617         opendir(D, "op") or die "opendir: $!\n";
618         my $readdir = readdir(D);
619         test tainted $readdir;
620         closedir(D);
621     }
622
623     SKIP: {
624         skip "readlink() or symlink() is not available" unless 
625           $Config{d_readlink} && $Config{d_symlink};
626
627         my $symlink = "sl$$";
628         unlink($symlink);
629         my $sl = "/something/naughty";
630         # it has to be a real path on Mac OS
631         $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
632         symlink($sl, $symlink) or die "symlink: $!\n";
633         my $readlink = readlink($symlink);
634         test tainted $readlink;
635         unlink($symlink);
636     }
637 }
638
639 # test bitwise ops (regression bug)
640 {
641     my $why = "y";
642     my $j = "x" | $why;
643     test not tainted $j;
644     $why = $TAINT."y";
645     $j = "x" | $why;
646     test     tainted $j;
647 }
648
649 # test target of substitution (regression bug)
650 {
651     my $why = $TAINT."y";
652     $why =~ s/y/z/;
653     test     tainted $why;
654
655     my $z = "[z]";
656     $why =~ s/$z/zee/;
657     test     tainted $why;
658
659     $why =~ s/e/'-'.$$/ge;
660     test     tainted $why;
661 }
662
663
664 SKIP: {
665     skip "no IPC::SysV", 2 unless $ipcsysv;
666
667     # test shmread
668     SKIP: {
669         skip "shm*() not available", 1 unless $Config{d_shm};
670
671         no strict 'subs';
672         my $sent = "foobar";
673         my $rcvd;
674         my $size = 2000;
675         my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
676
677         if (defined $id) {
678             if (shmwrite($id, $sent, 0, 60)) {
679                 if (shmread($id, $rcvd, 0, 60)) {
680                     substr($rcvd, index($rcvd, "\0")) = '';
681                 } else {
682                     warn "# shmread failed: $!\n";
683                 }
684             } else {
685                 warn "# shmwrite failed: $!\n";
686             }
687             shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
688         } else {
689             warn "# shmget failed: $!\n";
690         }
691
692         skip "SysV shared memory operation failed", 1 unless 
693           $rcvd eq $sent;
694
695         test tainted $rcvd;
696     }
697
698
699     # test msgrcv
700     SKIP: {
701         skip "msg*() not available", 1 unless $Config{d_msg};
702
703         no strict 'subs';
704         my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
705
706         my $sent      = "message";
707         my $type_sent = 1234;
708         my $rcvd;
709         my $type_rcvd;
710
711         if (defined $id) {
712             if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
713                 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
714                     ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
715                 } else {
716                     warn "# msgrcv failed: $!\n";
717                 }
718             } else {
719                 warn "# msgsnd failed: $!\n";
720             }
721             msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
722         } else {
723             warn "# msgget failed\n";
724         }
725
726         SKIP: {
727             skip "SysV message queue operation failed", 1
728               unless $rcvd eq $sent && $type_sent == $type_rcvd;
729
730             test tainted $rcvd;
731         }
732     }
733 }
734
735 {
736     # bug id 20001004.006
737
738     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
739     local $/;
740     my $a = <IN>;
741     my $b = <IN>;
742
743     ok tainted($a) && tainted($b) && !defined($b);
744
745     close IN;
746 }
747
748 {
749     # bug id 20001004.007
750
751     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
752     my $a = <IN>;
753
754     my $c = { a => 42,
755               b => $a };
756
757     ok !tainted($c->{a}) && tainted($c->{b});
758
759
760     my $d = { a => $a,
761               b => 42 };
762     ok tainted($d->{a}) && !tainted($d->{b});
763
764
765     my $e = { a => 42,
766               b => { c => $a, d => 42 } };
767     ok !tainted($e->{a}) &&
768        !tainted($e->{b}) &&
769         tainted($e->{b}->{c}) &&
770        !tainted($e->{b}->{d});
771
772     close IN;
773 }
774
775 {
776     # bug id 20010519.003
777
778     BEGIN {
779         use vars qw($has_fcntl);
780         eval { require Fcntl; import Fcntl; };
781         unless ($@) {
782             $has_fcntl = 1;
783         }
784     }
785
786     SKIP: {
787         skip "no Fcntl", 18 unless $has_fcntl;
788
789         my $evil = "foo" . $TAINT;
790
791         eval { sysopen(my $ro, $evil, &O_RDONLY) };
792         test $@ !~ /^Insecure dependency/, $@;
793         
794         eval { sysopen(my $wo, $evil, &O_WRONLY) };
795         test $@ =~ /^Insecure dependency/, $@;
796         
797         eval { sysopen(my $rw, $evil, &O_RDWR) };
798         test $@ =~ /^Insecure dependency/, $@;
799         
800         eval { sysopen(my $ap, $evil, &O_APPEND) };
801         test $@ =~ /^Insecure dependency/, $@;
802         
803         eval { sysopen(my $cr, $evil, &O_CREAT) };
804         test $@ =~ /^Insecure dependency/, $@;
805         
806         eval { sysopen(my $tr, $evil, &O_TRUNC) };
807         test $@ =~ /^Insecure dependency/, $@;
808         
809         eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
810         test $@ !~ /^Insecure dependency/, $@;
811         
812         eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
813         test $@ =~ /^Insecure dependency/, $@;
814
815         eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
816         test $@ =~ /^Insecure dependency/, $@;
817
818         eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
819         test $@ =~ /^Insecure dependency/, $@;
820         
821         eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
822         test $@ =~ /^Insecure dependency/, $@;
823
824         eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
825         test $@ =~ /^Insecure dependency/, $@;
826
827         eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
828         test $@ !~ /^Insecure dependency/, $@;
829         
830         eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
831         test $@ =~ /^Insecure dependency/, $@;
832         
833         eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
834         test $@ =~ /^Insecure dependency/, $@;
835         
836         eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
837         test $@ =~ /^Insecure dependency/, $@;
838         
839         eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
840         test $@ =~ /^Insecure dependency/, $@;
841
842         eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
843         test $@ =~ /^Insecure dependency/, $@;
844         
845         unlink("foo"); # not unlink($evil), because that would fail...
846     }
847 }
848
849 {
850     # bug 20010526.004
851
852     use warnings;
853
854     my $saw_warning = 0;
855     local $SIG{__WARN__} = sub { $saw_warning = 1 };
856
857     sub fmi {
858         my $divnum = shift()/1;
859         sprintf("%1.1f\n", $divnum);
860     }
861
862     fmi(21 . $TAINT);
863     fmi(37);
864     fmi(248);
865
866     test !$saw_warning;
867 }
868
869
870 {
871     # Bug ID 20010730.010
872
873     my $i = 0;
874
875     sub Tie::TIESCALAR {
876         my $class =  shift;
877         my $arg   =  shift;
878
879         bless \$arg => $class;
880     }
881
882     sub Tie::FETCH {
883         $i ++;
884         ${$_ [0]}
885     }
886
887  
888     package main;
889  
890     my $bar = "The Big Bright Green Pleasure Machine";
891     taint_these $bar;
892     tie my ($foo), Tie => $bar;
893
894     my $baz = $foo;
895
896     ok $i == 1;
897 }
898
899 {
900     # Check that all environment variables are tainted.
901     my @untainted;
902     while (my ($k, $v) = each %ENV) {
903         if (!tainted($v) &&
904             # These we have explicitly untainted or set earlier.
905             $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
906             push @untainted, "# '$k' = '$v'\n";
907         }
908     }
909     test @untainted == 0, "untainted:\n @untainted";
910 }
911
912
913 ok( ${^TAINT} == 1, '$^TAINT is on' );
914
915 eval { ${^TAINT} = 0 };
916 ok( ${^TAINT},  '$^TAINT is not assignable' );
917 ok( $@ =~ /^Modification of a read-only value attempted/,
918                                 'Assigning to ${^TAINT} fails' );
919
920 {
921     # bug 20011111.105
922     
923     my $re1 = qr/x$TAINT/;
924     test tainted $re1;
925     
926     my $re2 = qr/^$re1\z/;
927     test tainted $re2;
928     
929     my $re3 = "$re2";
930     test tainted $re3;
931 }
932
933 SKIP: {
934     skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
935
936     # bug 20010221.005
937     local $ENV{PATH} .= $TAINT;
938     eval { system { "echo" } "/arg0", "arg1" };
939     test $@ =~ /^Insecure \$ENV/;
940 }
941
942 TODO: {
943     todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
944       if $Is_VMS;
945
946     # bug 20020208.005 plus some single arg exec/system extras
947     my $err = qr/^Insecure dependency/ ;
948     test !eval { exec $TAINT, $TAINT }, 'exec';
949     test $@ =~ $err, $@;
950     test !eval { exec $TAINT $TAINT }, 'exec';
951     test $@ =~ $err, $@;
952     test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
953     test $@ =~ $err, $@;
954     test !eval { exec $TAINT 'notaint' }, 'exec';
955     test $@ =~ $err, $@;
956     test !eval { exec {'notaint'} $TAINT }, 'exec';
957     test $@ =~ $err, $@;
958
959     test !eval { system $TAINT, $TAINT }, 'system';
960     test $@ =~ $err, $@;
961     test !eval { system $TAINT $TAINT }, 'system';
962     test $@ =~ $err, $@;
963     test !eval { system $TAINT $TAINT, $TAINT }, 'system';
964     test $@ =~ $err, $@;
965     test !eval { system $TAINT 'notaint' }, 'system';
966     test $@ =~ $err, $@;
967     test !eval { system {'notaint'} $TAINT }, 'system';
968     test $@ =~ $err, $@;
969
970     eval { 
971         no warnings;
972         system("lskdfj does not exist","with","args"); 
973     };
974     test !$@;
975
976     SKIP: {
977         skip "no exec() on MacOS Classic" if $Is_MacOS;
978
979         eval { 
980             no warnings;
981             exec("lskdfj does not exist","with","args"); 
982         };
983         test !$@;
984     }
985
986     # If you add tests here update also the above skip block for VMS.
987 }
988
989 {
990     # [ID 20020704.001] taint propagation failure
991     use re 'taint';
992     $TAINT =~ /(.*)/;
993     test tainted(my $foo = $1);
994 }
995
996 {
997     # [perl #24291] this used to dump core
998     our %nonmagicalenv = ( PATH => "util" );
999     local *ENV = \%nonmagicalenv;
1000     eval { system("lskdfj"); };
1001     test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
1002     local *ENV = *nonmagicalenv;
1003     eval { system("lskdfj"); };
1004     test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
1005 }
1006 {
1007     # [perl #24248]
1008     $TAINT =~ /(.*)/;
1009     test !tainted($1);
1010     my $notaint = $1;
1011     test !tainted($notaint);
1012
1013     my $l;
1014     $notaint =~ /($notaint)/;
1015     $l = $1;
1016     test !tainted($1);
1017     test !tainted($l);
1018     $notaint =~ /($TAINT)/;
1019     $l = $1;
1020     test tainted($1);
1021     test tainted($l);
1022
1023     $TAINT =~ /($notaint)/;
1024     $l = $1;
1025     test !tainted($1);
1026     test !tainted($l);
1027     $TAINT =~ /($TAINT)/;
1028     $l = $1;
1029     test tainted($1);
1030     test tainted($l);
1031
1032     my $r;
1033     ($r = $TAINT) =~ /($notaint)/;
1034     test !tainted($1);
1035     ($r = $TAINT) =~ /($TAINT)/;
1036     test tainted($1);
1037
1038     #  [perl #24674]
1039     # accessing $^O  shoudn't taint it as a side-effect;
1040     # assigning tainted data to it is now an error
1041
1042     test !tainted($^O);
1043     if (!$^X) { } elsif ($^O eq 'bar') { }
1044     test !tainted($^O);
1045     eval '$^O = $^X';
1046     test $@ =~ /Insecure dependency in/;
1047 }
1048
1049 EFFECTIVELY_CONSTANTS: {
1050     my $tainted_number = 12 + $TAINT0;
1051     test tainted( $tainted_number );
1052
1053     # Even though it's always 0, it's still tainted
1054     my $tainted_product = $tainted_number * 0;
1055     test tainted( $tainted_product );
1056     test $tainted_product == 0;
1057 }
1058
1059 TERNARY_CONDITIONALS: {
1060     my $tainted_true  = $TAINT . "blah blah blah";
1061     my $tainted_false = $TAINT0;
1062     test tainted( $tainted_true );
1063     test tainted( $tainted_false );
1064
1065     my $result = $tainted_true ? "True" : "False";
1066     test $result eq "True";
1067     test !tainted( $result );
1068
1069     $result = $tainted_false ? "True" : "False";
1070     test $result eq "False";
1071     test !tainted( $result );
1072
1073     my $untainted_whatever = "The Fabulous Johnny Cash";
1074     my $tainted_whatever = "Soft Cell" . $TAINT;
1075
1076     $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1077     test $result eq "Soft Cell";
1078     test tainted( $result );
1079
1080     $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1081     test $result eq "The Fabulous Johnny Cash";
1082     test !tainted( $result );
1083 }
1084
1085 {
1086     # rt.perl.org 5900  $1 remains tainted if...
1087     # 1) The regular expression contains a scalar variable AND
1088     # 2) The regular expression appears in an elsif clause
1089
1090     my $foo = "abcdefghi" . $TAINT;
1091
1092     my $valid_chars = 'a-z';
1093     if ( $foo eq '' ) {
1094     }
1095     elsif ( $foo =~ /([$valid_chars]+)/o ) {
1096         test not tainted $1;
1097     }
1098
1099     if ( $foo eq '' ) {
1100     }
1101     elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1102         test not any_tainted @bar;
1103     }
1104 }
1105
1106 # at scope exit, a restored localised value should have its old
1107 # taint status, not the taint status of the current statement
1108
1109 {
1110     our $x99 = $^X;
1111     test tainted $x99;
1112
1113     $x99 = '';
1114     test not tainted $x99;
1115
1116     my $c = do { local $x99; $^X };
1117     test not tainted $x99;
1118 }
1119 {
1120     our $x99 = $^X;
1121     test tainted $x99;
1122
1123     my $c = do { local $x99; '' };
1124     test tainted $x99;
1125 }
1126
1127 # an mg_get of a tainted value during localization shouldn't taint the
1128 # statement
1129
1130 {
1131     eval { local $0, eval '1' };
1132     test $@ eq '';
1133 }
1134
1135 # [perl #8262] //g loops infinitely on tainted data
1136
1137 {
1138     my @a;
1139     local $::TODO = 1;
1140     $a[0] = $^X;
1141     my $i = 0;
1142     while($a[0]=~ m/(.)/g ) {
1143         last if $i++ > 10000;
1144     }
1145     cmp_ok $i, '<', 10000, "infinite m//g";
1146 }
1147
1148 SKIP:
1149 {
1150     my $got_dualvar;
1151     eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1152     skip "No Scalar::Util::dualvar" unless $got_dualvar;
1153     my $a = Scalar::Util::dualvar(3, $^X);
1154     my $b = $a + 5;
1155     is ($b, 8, "Arithmetic on tainted dualvars works");
1156 }
1157
1158 # opening '|-' should not trigger $ENV{PATH} check
1159
1160 {
1161     SKIP: {
1162         skip "fork() is not available", 3 unless $Config{'d_fork'};
1163         skip "opening |- is not stable on threaded OpenBSD with taint", 3
1164             if $Config{useithreads} && $Is_OpenBSD;
1165
1166         $ENV{'PATH'} = $TAINT;
1167         local $SIG{'PIPE'} = 'IGNORE';
1168         eval {
1169             my $pid = open my $pipe, '|-';
1170             if (!defined $pid) {
1171                 die "open failed: $!";
1172             }
1173             if (!$pid) {
1174                 kill 'KILL', $$;        # child suicide
1175             }
1176             close $pipe;
1177         };
1178         test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1179         test $@ eq '',               'pipe/fork/open/close failed';
1180         eval {
1181             open my $pipe, "|$Invoke_Perl -e 1";
1182             close $pipe;
1183         };
1184         test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1185     }
1186 }
1187
1188 {
1189     package AUTOLOAD_TAINT;
1190     sub AUTOLOAD {
1191         our $AUTOLOAD;
1192         return if $AUTOLOAD =~ /DESTROY/;
1193         if ($AUTOLOAD =~ /untainted/) {
1194             main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1195         } else {
1196             main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1197         }
1198     }
1199
1200     package main;
1201     my $o = bless [], 'AUTOLOAD_TAINT';
1202     $o->$TAINT;
1203     $o->untainted;
1204 }
1205
1206 {
1207     # tests for tainted format in s?printf
1208     eval { printf($TAINT . "# %s\n", "foo") };
1209     like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1210     eval { printf("# %s\n", $TAINT . "foo") };
1211     ok(!$@, q/printf accepts other tainted args/);
1212     eval { sprintf($TAINT . "# %s\n", "foo") };
1213     like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1214     eval { sprintf("# %s\n", $TAINT . "foo") };
1215     ok(!$@, q/sprintf accepts other tainted args/);
1216 }
1217
1218 {
1219     # 40708
1220     my $n  = 7e9;
1221     8e9 - $n;
1222
1223     my $val = $n;
1224     is ($val, '7000000000', 'Assignment to untainted variable');
1225     $val = $TAINT;
1226     $val = $n;
1227     is ($val, '7000000000', 'Assignment to tainted variable');
1228 }
1229
1230 {
1231     my $val = 0;
1232     my $tainted = '1' . $TAINT;
1233     eval '$val = eval $tainted;';
1234     is ($val, 0, "eval doesn't like tainted strings");
1235     like ($@, qr/^Insecure dependency in eval/);
1236
1237     # Rather nice code to get a tainted undef by from Rick Delaney
1238     open FH, "test.pl" or die $!;
1239     seek FH, 0, 2 or die $!;
1240     $tainted = <FH>;
1241
1242     eval 'eval $tainted';
1243     like ($@, qr/^Insecure dependency in eval/);
1244 }
1245
1246 foreach my $ord (78, 163, 256) {
1247     # 47195
1248     my $line = 'A1' . $TAINT . chr $ord;
1249     chop $line;
1250     is($line, 'A1');
1251     $line =~ /(A\S*)/;
1252     ok(!tainted($1), "\\S match with chr $ord");
1253 }
1254
1255 {
1256     # 59998
1257     sub cr { my $x = crypt($_[0], $_[1]); $x }
1258     sub co { my $x = ~$_[0]; $x }
1259     my ($a, $b);
1260     $a = cr('hello', 'foo' . $TAINT);
1261     $b = cr('hello', 'foo');
1262     ok(tainted($a),  "tainted crypt");
1263     ok(!tainted($b), "untainted crypt");
1264     $a = co('foo' . $TAINT);
1265     $b = co('foo');
1266     ok(tainted($a),  "tainted complement");
1267     ok(!tainted($b), "untainted complement");
1268 }
1269
1270 {
1271     my @data = qw(bonk zam zlonk qunckkk);
1272     # Clearly some sort of usenet bang-path
1273     my $string = $TAINT . join "!", @data;
1274
1275     ok(tainted($string), "tainted data");
1276
1277     my @got = split /!|,/, $string;
1278
1279     # each @got would be useful here, but I want the test for earlier perls
1280     for my $i (0 .. $#data) {
1281         ok(tainted($got[$i]), "tainted result $i");
1282         is($got[$i], $data[$i], "correct content $i");
1283     }
1284
1285     ok(tainted($string), "still tainted data");
1286
1287     my @got = split /[!,]/, $string;
1288
1289     # each @got would be useful here, but I want the test for earlier perls
1290     for my $i (0 .. $#data) {
1291         ok(tainted($got[$i]), "tainted result $i");
1292         is($got[$i], $data[$i], "correct content $i");
1293     }
1294
1295     ok(tainted($string), "still tainted data");
1296
1297     my @got = split /!/, $string;
1298
1299     # each @got would be useful here, but I want the test for earlier perls
1300     for my $i (0 .. $#data) {
1301         ok(tainted($got[$i]), "tainted result $i");
1302         is($got[$i], $data[$i], "correct content $i");
1303     }
1304 }
1305
1306 # This may bomb out with the alarm signal so keep it last
1307 SKIP: {
1308     skip "No alarm()"  unless $Config{d_alarm};
1309     # Test from RT #41831]
1310     # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
1311
1312     my $DATA = <<'END' . $TAINT;
1313 line1 is here
1314 line2 is here
1315 line3 is here
1316 line4 is here
1317
1318 END
1319
1320     #study $DATA;
1321
1322     ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
1323     ## perl is stuck in a regexp infinite loop!
1324
1325     alarm(10);
1326
1327     if ($DATA =~ /^line2.*line4/m) {
1328         fail("Should not be a match")
1329     } else {
1330         pass("Match on tainted multiline data should fail promptly");
1331     }
1332
1333     alarm(0);
1334 }
1335 __END__
1336 # Keep the previous test last