Add a new taint error, "%ENV is aliased to %s".
[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 my $test = 177;
20 sub ok ($;$) {
21     my($ok, $name) = @_;
22
23     # You have to do it this way or VMS will get confused.
24     print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
25
26     printf "# Failed test at line %d\n", (caller)[2] unless $ok;
27
28     $test++;
29     return $ok;
30 }
31
32
33 $| = 1;
34
35 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
36
37 BEGIN {
38   if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
39       $ENV{PATH} = $ENV{PATH};
40       $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
41   }
42   if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
43       && ($Config{d_shm} || $Config{d_msg})) {
44       eval { require IPC::SysV };
45       unless ($@) {
46           $ipcsysv++;
47           IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
48       }
49   }
50 }
51
52 my $Is_MacOS = $^O eq 'MacOS';
53 my $Is_VMS = $^O eq 'VMS';
54 my $Is_MSWin32 = $^O eq 'MSWin32';
55 my $Is_NetWare = $^O eq 'NetWare';
56 my $Is_Dos = $^O eq 'dos';
57 my $Is_Cygwin = $^O eq 'cygwin';
58 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
59                   ($Is_MSWin32 ? '.\perl' :
60                   $Is_MacOS ? ':perl' :
61                   ($Is_NetWare ? 'perl' : './perl'));
62 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
63
64 if ($Is_VMS) {
65     my (%old, $x);
66     for $x ('DCL$PATH', @MoreEnv) {
67         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
68     }
69     eval <<EndOfCleanup;
70         END {
71             \$ENV{PATH} = '' if $Config{d_setenv};
72             warn "# Note: logical name 'PATH' may have been deleted\n";
73             \@ENV{keys %old} = values %old;
74         }
75 EndOfCleanup
76 }
77
78 # Sources of taint:
79 #   The empty tainted value, for tainting strings
80 my $TAINT = substr($^X, 0, 0);
81 #   A tainted zero, useful for tainting numbers
82 my $TAINT0 = 0 + $TAINT;
83
84 # This taints each argument passed. All must be lvalues.
85 # Side effect: It also stringifies them. :-(
86 sub taint_these (@) {
87     for (@_) { $_ .= $TAINT }
88 }
89
90 # How to identify taint when you see it
91 sub any_tainted (@) {
92     not eval { join("",@_), kill 0; 1 };
93 }
94 sub tainted ($) {
95     any_tainted @_;
96 }
97 sub all_tainted (@) {
98     for (@_) { return 0 unless tainted $_ }
99     1;
100 }
101
102 sub test ($$;$) {
103     my($serial, $boolean, $diag) = @_;
104     if ($boolean) {
105         print "ok $serial\n";
106     } else {
107         print "not ok $serial\n";
108         for (split m/^/m, $diag) {
109             print "# $_";
110         }
111         print "\n" unless
112             $diag eq ''
113             or substr($diag, -1) eq "\n";
114     }
115 }
116
117 # We need an external program to call.
118 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
119 END { unlink $ECHO }
120 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
121 print PROG 'print "@ARGV\n"', "\n";
122 close PROG;
123 my $echo = "$Invoke_Perl $ECHO";
124
125 my $TEST = catfile(curdir(), 'TEST');
126
127 print "1..208\n";
128
129 # First, let's make sure that Perl is checking the dangerous
130 # environment variables. Maybe they aren't set yet, so we'll
131 # taint them ourselves.
132 {
133     $ENV{'DCL$PATH'} = '' if $Is_VMS;
134
135     $ENV{PATH} = '';
136     delete @ENV{@MoreEnv};
137     $ENV{TERM} = 'dumb';
138
139     if ($Is_Cygwin && ! -f 'cygwin1.dll') {
140         system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
141             die "$0: failed to cp cygwin1.dll: $!\n";
142         END { unlink "cygwin1.dll" } # yes, done for all platforms...
143     }
144
145     test 1, eval { `$echo 1` } eq "1\n";
146
147     if ($Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS) {
148         print "# Environment tainting tests skipped\n";
149         for (2..5) { print "ok $_\n" }
150     }
151     else {
152         my @vars = ('PATH', @MoreEnv);
153         while (my $v = $vars[0]) {
154             local $ENV{$v} = $TAINT;
155             last if eval { `$echo 1` };
156             last unless $@ =~ /^Insecure \$ENV{$v}/;
157             shift @vars;
158         }
159         test 2, !@vars, "\$$vars[0]";
160
161         # tainted $TERM is unsafe only if it contains metachars
162         local $ENV{TERM};
163         $ENV{TERM} = 'e=mc2';
164         test 3, eval { `$echo 1` } eq "1\n";
165         $ENV{TERM} = 'e=mc2' . $TAINT;
166         test 4, eval { `$echo 1` } eq '';
167         test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
168     }
169
170     my $tmp;
171     if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
172         print "# all directories are writeable\n";
173     }
174     else {
175         $tmp = (grep { defined and -d and (stat _)[2] & 2 }
176                      qw(sys$scratch /tmp /var/tmp /usr/tmp),
177                      @ENV{qw(TMP TEMP)})[0]
178             or print "# can't find world-writeable directory to test PATH\n";
179     }
180
181     if ($tmp) {
182         local $ENV{PATH} = $tmp;
183         test 6, eval { `$echo 1` } eq '';
184         test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
185     }
186     else {
187         for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
188     }
189
190     if ($Is_VMS) {
191         $ENV{'DCL$PATH'} = $TAINT;
192         test 8,  eval { `$echo 1` } eq '';
193         test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
194         if ($tmp) {
195             $ENV{'DCL$PATH'} = $tmp;
196             test 10, eval { `$echo 1` } eq '';
197             test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
198         }
199         else {
200             for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
201         }
202         $ENV{'DCL$PATH'} = '';
203     }
204     else {
205         for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
206     }
207 }
208
209 # Let's see that we can taint and untaint as needed.
210 {
211     my $foo = $TAINT;
212     test 12, tainted $foo;
213
214     # That was a sanity check. If it failed, stop the insanity!
215     die "Taint checks don't seem to be enabled" unless tainted $foo;
216
217     $foo = "foo";
218     test 13, not tainted $foo;
219
220     taint_these($foo);
221     test 14, tainted $foo;
222
223     my @list = 1..10;
224     test 15, not any_tainted @list;
225     taint_these @list[1,3,5,7,9];
226     test 16, any_tainted @list;
227     test 17, all_tainted @list[1,3,5,7,9];
228     test 18, not any_tainted @list[0,2,4,6,8];
229
230     ($foo) = $foo =~ /(.+)/;
231     test 19, not tainted $foo;
232
233     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
234     test 20, not tainted $foo;
235     test 21, $foo eq 'bar';
236
237     {
238       use re 'taint';
239
240       ($foo) = ('bar' . $TAINT) =~ /(.+)/;
241       test 22, tainted $foo;
242       test 23, $foo eq 'bar';
243
244       $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
245       test 24, tainted $foo;
246       test 25, $foo eq 'bar';
247     }
248
249     $foo = $1 if 'bar' =~ /(.+)$TAINT/;
250     test 26, tainted $foo;
251     test 27, $foo eq 'bar';
252
253     my $pi = 4 * atan2(1,1) + $TAINT0;
254     test 28, tainted $pi;
255
256     ($pi) = $pi =~ /(\d+\.\d+)/;
257     test 29, not tainted $pi;
258     test 30, sprintf("%.5f", $pi) eq '3.14159';
259 }
260
261 # How about command-line arguments? The problem is that we don't
262 # always get some, so we'll run another process with some.
263 SKIP: {
264     my $arg = catfile(curdir(), "arg$$");
265     open PROG, "> $arg" or die "Can't create $arg: $!";
266     print PROG q{
267         eval { join('', @ARGV), kill 0 };
268         exit 0 if $@ =~ /^Insecure dependency/;
269         print "# Oops: \$@ was [$@]\n";
270         exit 1;
271     };
272     close PROG;
273     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
274     test 31, !$?, "Exited with status $?";
275     unlink $arg;
276 }
277
278 # Reading from a file should be tainted
279 {
280     test 32, open(FILE, $TEST), "Couldn't open '$TEST': $!";
281
282     my $block;
283     sysread(FILE, $block, 100);
284     my $line = <FILE>;
285     close FILE;
286     test 33, tainted $block;
287     test 34, tainted $line;
288 }
289
290 # Globs should be forbidden, except under VMS,
291 #   which doesn't spawn an external program.
292 if (1  # built-in glob
293     or $Is_VMS) {
294     for (35..36) { print "ok $_\n"; }
295 }
296 else {
297     my @globs = eval { <*> };
298     test 35, @globs == 0 && $@ =~ /^Insecure dependency/;
299
300     @globs = eval { glob '*' };
301     test 36, @globs == 0 && $@ =~ /^Insecure dependency/;
302 }
303
304 # Output of commands should be tainted
305 {
306     my $foo = `$echo abc`;
307     test 37, tainted $foo;
308 }
309
310 # Certain system variables should be tainted
311 {
312     test 38, all_tainted $^X, $0;
313 }
314
315 # Results of matching should all be untainted
316 {
317     my $foo = "abcdefghi" . $TAINT;
318     test 39, tainted $foo;
319
320     $foo =~ /def/;
321     test 40, not any_tainted $`, $&, $';
322
323     $foo =~ /(...)(...)(...)/;
324     test 41, not any_tainted $1, $2, $3, $+;
325
326     my @bar = $foo =~ /(...)(...)(...)/;
327     test 42, not any_tainted @bar;
328
329     test 43, tainted $foo;      # $foo should still be tainted!
330     test 44, $foo eq "abcdefghi";
331 }
332
333 # Operations which affect files can't use tainted data.
334 {
335     test 45, eval { chmod 0, $TAINT } eq '', 'chmod';
336     test 46, $@ =~ /^Insecure dependency/, $@;
337
338     # There is no feature test in $Config{} for truncate,
339     #   so we allow for the possibility that it's missing.
340     test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
341     test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
342
343     test 49, eval { rename '', $TAINT } eq '', 'rename';
344     test 50, $@ =~ /^Insecure dependency/, $@;
345
346     test 51, eval { unlink $TAINT } eq '', 'unlink';
347     test 52, $@ =~ /^Insecure dependency/, $@;
348
349     test 53, eval { utime $TAINT } eq '', 'utime';
350     test 54, $@ =~ /^Insecure dependency/, $@;
351
352     if ($Config{d_chown}) {
353         test 55, eval { chown -1, -1, $TAINT } eq '', 'chown';
354         test 56, $@ =~ /^Insecure dependency/, $@;
355     }
356     else {
357         for (55..56) { print "ok $_ # Skipped: chown() is not available\n" }
358     }
359
360     if ($Config{d_link}) {
361         test 57, eval { link $TAINT, '' } eq '', 'link';
362         test 58, $@ =~ /^Insecure dependency/, $@;
363     }
364     else {
365         for (57..58) { print "ok $_ # Skipped: link() is not available\n" }
366     }
367
368     if ($Config{d_symlink}) {
369         test 59, eval { symlink $TAINT, '' } eq '', 'symlink';
370         test 60, $@ =~ /^Insecure dependency/, $@;
371     }
372     else {
373         for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" }
374     }
375 }
376
377 # Operations which affect directories can't use tainted data.
378 {
379     test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
380     test 62, $@ =~ /^Insecure dependency/, $@;
381
382     test 63, eval { rmdir $TAINT } eq '', 'rmdir';
383     test 64, $@ =~ /^Insecure dependency/, $@;
384
385     test 65, eval { chdir $TAINT } eq '', 'chdir';
386     test 66, $@ =~ /^Insecure dependency/, $@;
387
388     if ($Config{d_chroot}) {
389         test 67, eval { chroot $TAINT } eq '', 'chroot';
390         test 68, $@ =~ /^Insecure dependency/, $@;
391     }
392     else {
393         for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" }
394     }
395 }
396
397 # Some operations using files can't use tainted data.
398 {
399     my $foo = "imaginary library" . $TAINT;
400     test 69, eval { require $foo } eq '', 'require';
401     test 70, $@ =~ /^Insecure dependency/, $@;
402
403     my $filename = "./taintB$$";        # NB: $filename isn't tainted!
404     END { unlink $filename if defined $filename }
405     $foo = $filename . $TAINT;
406     unlink $filename;   # in any case
407
408     test 71, eval { open FOO, $foo } eq '', 'open for read';
409     test 72, $@ eq '', $@;              # NB: This should be allowed
410
411     # Try first new style but allow also old style.
412     # We do not want the whole taint.t to fail
413     # just because Errno possibly failing.
414     test 73, eval('$!{ENOENT}') ||
415         $! == 2 || # File not found
416         ($Is_Dos && $! == 22) ||
417         ($^O eq 'mint' && $! == 33);
418
419     test 74, eval { open FOO, "> $foo" } eq '', 'open for write';
420     test 75, $@ =~ /^Insecure dependency/, $@;
421 }
422
423 # Commands to the system can't use tainted data
424 {
425     my $foo = $TAINT;
426
427     if ($^O eq 'amigaos') {
428         for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" }
429     }
430     else {
431         test 76, eval { open FOO, "| x$foo" } eq '', 'popen to';
432         test 77, $@ =~ /^Insecure dependency/, $@;
433
434         test 78, eval { open FOO, "x$foo |" } eq '', 'popen from';
435         test 79, $@ =~ /^Insecure dependency/, $@;
436     }
437
438     test 80, eval { exec $TAINT } eq '', 'exec';
439     test 81, $@ =~ /^Insecure dependency/, $@;
440
441     test 82, eval { system $TAINT } eq '', 'system';
442     test 83, $@ =~ /^Insecure dependency/, $@;
443
444     $foo = "*";
445     taint_these $foo;
446
447     test 84, eval { `$echo 1$foo` } eq '', 'backticks';
448     test 85, $@ =~ /^Insecure dependency/, $@;
449
450     if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
451         test 86, join('', eval { glob $foo } ) ne '', 'globbing';
452         test 87, $@ eq '', $@;
453     }
454     else {
455         for (86..87) { print "ok $_ # Skipped: This is not VMS\n"; }
456     }
457 }
458
459 # Operations which affect processes can't use tainted data.
460 {
461     test 88, eval { kill 0, $TAINT } eq '', 'kill';
462     test 89, $@ =~ /^Insecure dependency/, $@;
463
464     if ($Config{d_setpgrp}) {
465         test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
466         test 91, $@ =~ /^Insecure dependency/, $@;
467     }
468     else {
469         for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" }
470     }
471
472     if ($Config{d_setprior}) {
473         test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
474         test 93, $@ =~ /^Insecure dependency/, $@;
475     }
476     else {
477         for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" }
478     }
479 }
480
481 # Some miscellaneous operations can't use tainted data.
482 {
483     if ($Config{d_syscall}) {
484         test 94, eval { syscall $TAINT } eq '', 'syscall';
485         test 95, $@ =~ /^Insecure dependency/, $@;
486     }
487     else {
488         for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" }
489     }
490
491     {
492         my $foo = "x" x 979;
493         taint_these $foo;
494         local *FOO;
495         my $temp = "./taintC$$";
496         END { unlink $temp }
497         test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
498
499         test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
500         test 98, $@ =~ /^Insecure dependency/, $@;
501
502         if ($Config{d_fcntl}) {
503             test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
504             test 100, $@ =~ /^Insecure dependency/, $@;
505         }
506         else {
507             for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" }
508         }
509
510         close FOO;
511     }
512 }
513
514 # Some tests involving references
515 {
516     my $foo = 'abc' . $TAINT;
517     my $fooref = \$foo;
518     test 101, not tainted $fooref;
519     test 102, tainted $$fooref;
520     test 103, tainted $foo;
521 }
522
523 # Some tests involving assignment
524 {
525     my $foo = $TAINT0;
526     my $bar = $foo;
527     test 104, all_tainted $foo, $bar;
528     test 105, tainted($foo = $bar);
529     test 106, tainted($bar = $bar);
530     test 107, tainted($bar += $bar);
531     test 108, tainted($bar -= $bar);
532     test 109, tainted($bar *= $bar);
533     test 110, tainted($bar++);
534     test 111, tainted($bar /= $bar);
535     test 112, tainted($bar += 0);
536     test 113, tainted($bar -= 2);
537     test 114, tainted($bar *= -1);
538     test 115, tainted($bar /= 1);
539     test 116, tainted($bar--);
540     test 117, $bar == 0;
541 }
542
543 # Test assignment and return of lists
544 {
545     my @foo = ("A", "tainted" . $TAINT, "B");
546     test 118, not tainted $foo[0];
547     test 119,     tainted $foo[1];
548     test 120, not tainted $foo[2];
549     my @bar = @foo;
550     test 121, not tainted $bar[0];
551     test 122,     tainted $bar[1];
552     test 123, not tainted $bar[2];
553     my @baz = eval { "A", "tainted" . $TAINT, "B" };
554     test 124, not tainted $baz[0];
555     test 125,     tainted $baz[1];
556     test 126, not tainted $baz[2];
557     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
558     test 127, not tainted $plugh[0];
559     test 128,     tainted $plugh[1];
560     test 129, not tainted $plugh[2];
561     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
562     test 130, not tainted ((&$nautilus)[0]);
563     test 131,     tainted ((&$nautilus)[1]);
564     test 132, not tainted ((&$nautilus)[2]);
565     my @xyzzy = &$nautilus;
566     test 133, not tainted $xyzzy[0];
567     test 134,     tainted $xyzzy[1];
568     test 135, not tainted $xyzzy[2];
569     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
570     test 136, not tainted ((&$red_october)[0]);
571     test 137,     tainted ((&$red_october)[1]);
572     test 138, not tainted ((&$red_october)[2]);
573     my @corge = &$red_october;
574     test 139, not tainted $corge[0];
575     test 140,     tainted $corge[1];
576     test 141, not tainted $corge[2];
577 }
578
579 # Test for system/library calls returning string data of dubious origin.
580 {
581     # No reliable %Config check for getpw*
582     if (eval { setpwent(); getpwent() }) {
583         setpwent();
584         my @getpwent = getpwent();
585         die "getpwent: $!\n" unless (@getpwent);
586         test 142,(    not tainted $getpwent[0]
587                   and     tainted $getpwent[1]
588                   and not tainted $getpwent[2]
589                   and not tainted $getpwent[3]
590                   and not tainted $getpwent[4]
591                   and not tainted $getpwent[5]
592                   and     tainted $getpwent[6]          # ge?cos
593                   and not tainted $getpwent[7]
594                   and     tainted $getpwent[8]);        # shell
595         endpwent();
596     } else {
597         for (142) { print "ok $_ # Skipped: getpwent() is not available\n" }
598     }
599
600     if ($Config{d_readdir}) { # pretty hard to imagine not
601         local(*D);
602         opendir(D, "op") or die "opendir: $!\n";
603         my $readdir = readdir(D);
604         test 143, tainted $readdir;
605         closedir(OP);
606     } else {
607         for (143) { print "ok $_ # Skipped: readdir() is not available\n" }
608     }
609
610     if ($Config{d_readlink} && $Config{d_symlink}) {
611         my $symlink = "sl$$";
612         unlink($symlink);
613         my $sl = "/something/naughty";
614         # it has to be a real path on Mac OS
615         $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
616         symlink($sl, $symlink) or die "symlink: $!\n";
617         my $readlink = readlink($symlink);
618         test 144, tainted $readlink;
619         unlink($symlink);
620     } else {
621         for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; }
622     }
623 }
624
625 # test bitwise ops (regression bug)
626 {
627     my $why = "y";
628     my $j = "x" | $why;
629     test 145, not tainted $j;
630     $why = $TAINT."y";
631     $j = "x" | $why;
632     test 146,     tainted $j;
633 }
634
635 # test target of substitution (regression bug)
636 {
637     my $why = $TAINT."y";
638     $why =~ s/y/z/;
639     test 147,     tainted $why;
640
641     my $z = "[z]";
642     $why =~ s/$z/zee/;
643     test 148,     tainted $why;
644
645     $why =~ s/e/'-'.$$/ge;
646     test 149,     tainted $why;
647 }
648
649 # test shmread
650 {
651     unless ($ipcsysv) {
652         print "ok 150 # skipped: no IPC::SysV\n";
653         last;
654     }
655     if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_shm}) {
656         no strict 'subs';
657         my $sent = "foobar";
658         my $rcvd;
659         my $size = 2000;
660         my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
661
662         if (defined $id) {
663             if (shmwrite($id, $sent, 0, 60)) {
664                 if (shmread($id, $rcvd, 0, 60)) {
665                     substr($rcvd, index($rcvd, "\0")) = '';
666                 } else {
667                     warn "# shmread failed: $!\n";
668                 }
669             } else {
670                 warn "# shmwrite failed: $!\n";
671             }
672             shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
673         } else {
674             warn "# shmget failed: $!\n";
675         }
676
677         if ($rcvd eq $sent) {
678             test 150, tainted $rcvd;
679         } else {
680             print "ok 150 # Skipped: SysV shared memory operation failed\n";
681         }
682     } else {
683         print "ok 150 # Skipped: SysV shared memory is not available\n";
684     }
685 }
686
687 # test msgrcv
688 {
689     unless ($ipcsysv) {
690         print "ok 151 # skipped: no IPC::SysV\n";
691         last;
692     }
693     if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ && $Config{d_msg}) {
694         no strict 'subs';
695         my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
696
697         my $sent      = "message";
698         my $type_sent = 1234;
699         my $rcvd;
700         my $type_rcvd;
701
702         if (defined $id) {
703             if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
704                 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
705                     ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
706                 } else {
707                     warn "# msgrcv failed: $!\n";
708                 }
709             } else {
710                 warn "# msgsnd failed: $!\n";
711             }
712             msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
713         } else {
714             warn "# msgget failed\n";
715         }
716
717         if ($rcvd eq $sent && $type_sent == $type_rcvd) {
718             test 151, tainted $rcvd;
719         } else {
720             print "ok 151 # Skipped: SysV message queue operation failed\n";
721         }
722     } else {
723         print "ok 151 # Skipped: SysV message queues are not available\n";
724     }
725 }
726
727 {
728     # bug id 20001004.006
729
730     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
731     local $/;
732     my $a = <IN>;
733     my $b = <IN>;
734     print "not " unless tainted($a) && tainted($b) && !defined($b);
735     print "ok 152\n";
736     close IN;
737 }
738
739 {
740     # bug id 20001004.007
741
742     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
743     my $a = <IN>;
744
745     my $c = { a => 42,
746               b => $a };
747     print "not " unless !tainted($c->{a}) && tainted($c->{b});
748     print "ok 153\n";
749
750     my $d = { a => $a,
751               b => 42 };
752     print "not " unless tainted($d->{a}) && !tainted($d->{b});
753     print "ok 154\n";
754
755     my $e = { a => 42,
756               b => { c => $a, d => 42 } };
757     print "not " unless !tainted($e->{a}) &&
758                         !tainted($e->{b}) &&
759                          tainted($e->{b}->{c}) &&
760                         !tainted($e->{b}->{d});
761     print "ok 155\n";
762
763     close IN;
764 }
765
766 {
767     # bug id 20010519.003
768
769     BEGIN {
770         use vars qw($has_fcntl);
771         eval { require Fcntl; import Fcntl; };
772         unless ($@) {
773             $has_fcntl = 1;
774         }
775     }
776
777     unless ($has_fcntl) {
778         for (156..173) {
779             print "ok $_ # Skip: no Fcntl (no dynaloading?)\n";
780         }
781     } else {
782         my $evil = "foo" . $TAINT;
783
784         eval { sysopen(my $ro, $evil, &O_RDONLY) };
785         test 156, $@ !~ /^Insecure dependency/, $@;
786         
787         eval { sysopen(my $wo, $evil, &O_WRONLY) };
788         test 157, $@ =~ /^Insecure dependency/, $@;
789         
790         eval { sysopen(my $rw, $evil, &O_RDWR) };
791         test 158, $@ =~ /^Insecure dependency/, $@;
792         
793         eval { sysopen(my $ap, $evil, &O_APPEND) };
794         test 159, $@ =~ /^Insecure dependency/, $@;
795         
796         eval { sysopen(my $cr, $evil, &O_CREAT) };
797         test 160, $@ =~ /^Insecure dependency/, $@;
798         
799         eval { sysopen(my $tr, $evil, &O_TRUNC) };
800         test 161, $@ =~ /^Insecure dependency/, $@;
801         
802         eval { sysopen(my $ro, "foo", &O_RDONLY | $evil) };
803         test 162, $@ !~ /^Insecure dependency/, $@;
804         
805         eval { sysopen(my $wo, "foo", &O_WRONLY | $evil) };
806         test 163, $@ =~ /^Insecure dependency/, $@;
807
808         eval { sysopen(my $rw, "foo", &O_RDWR | $evil) };
809         test 164, $@ =~ /^Insecure dependency/, $@;
810
811         eval { sysopen(my $ap, "foo", &O_APPEND | $evil) };
812         test 165, $@ =~ /^Insecure dependency/, $@;
813         
814         eval { sysopen(my $cr, "foo", &O_CREAT | $evil) };
815         test 166, $@ =~ /^Insecure dependency/, $@;
816
817         eval { sysopen(my $tr, "foo", &O_TRUNC | $evil) };
818         test 167, $@ =~ /^Insecure dependency/, $@;
819
820         eval { sysopen(my $ro, "foo", &O_RDONLY, $evil) };
821         test 168, $@ !~ /^Insecure dependency/, $@;
822         
823         eval { sysopen(my $wo, "foo", &O_WRONLY, $evil) };
824         test 169, $@ =~ /^Insecure dependency/, $@;
825         
826         eval { sysopen(my $rw, "foo", &O_RDWR, $evil) };
827         test 170, $@ =~ /^Insecure dependency/, $@;
828         
829         eval { sysopen(my $ap, "foo", &O_APPEND, $evil) };
830         test 171, $@ =~ /^Insecure dependency/, $@;
831         
832         eval { sysopen(my $cr, "foo", &O_CREAT, $evil) };
833         test 172, $@ =~ /^Insecure dependency/, $@;
834
835         eval { sysopen(my $tr, "foo", &O_TRUNC, $evil) };
836         test 173, $@ =~ /^Insecure dependency/, $@;
837         
838         unlink("foo"); # not unlink($evil), because that would fail...
839     }
840 }
841
842 {
843     # bug 20010526.004
844
845     use warnings;
846
847     local $SIG{__WARN__} = sub { print "not " };
848
849     sub fmi {
850         my $divnum = shift()/1;
851         sprintf("%1.1f\n", $divnum);
852     }
853
854     fmi(21 . $TAINT);
855     fmi(37);
856     fmi(248);
857
858     print "ok 174\n";
859 }
860
861
862 {
863     # Bug ID 20010730.010
864
865     my $i = 0;
866
867     sub Tie::TIESCALAR {
868         my $class =  shift;
869         my $arg   =  shift;
870
871         bless \$arg => $class;
872     }
873
874     sub Tie::FETCH {
875         $i ++;
876         ${$_ [0]}
877     }
878
879  
880     package main;
881  
882     my $bar = "The Big Bright Green Pleasure Machine";
883     taint_these $bar;
884     tie my ($foo), Tie => $bar;
885
886     my $baz = $foo;
887
888     print $i == 1 ? "ok 175\n" : "not ok 175\n"
889
890 }
891
892 {
893     # Check that all environment variables are tainted.
894     my @untainted;
895     while (my ($k, $v) = each %ENV) {
896         if (!tainted($v) &&
897             # These we have explicitly untainted or set earlier.
898             $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
899             push @untainted, "# '$k' = '$v'\n";
900         }
901     }
902     print @untainted == 0 ? "ok 176\n" : "not ok 176\n";
903     print "# untainted:\n", @untainted if @untainted; 
904 }
905
906
907 ok( ${^TAINT} == 1, '$^TAINT is on' );
908
909 eval { ${^TAINT} = 0 };
910 ok( ${^TAINT},  '$^TAINT is not assignable' );
911 ok( $@ =~ /^Modification of a read-only value attempted/,
912                                 'Assigning to ${^TAINT} fails' );
913
914 {
915     # bug 20011111.105
916     
917     my $re1 = qr/x$TAINT/;
918     test 180, tainted $re1;
919     
920     my $re2 = qr/^$re1\z/;
921     test 181, tainted $re2;
922     
923     my $re3 = "$re2";
924     test 182, tainted $re3;
925 }
926
927 if ($Is_MSWin32) {
928     print "ok 183 # Skipped: system {} has different semantics\n"; 
929 }
930 else
931 {
932     # bug 20010221.005
933     local $ENV{PATH} .= $TAINT;
934     eval { system { "echo" } "/arg0", "arg1" };
935     test 183, $@ =~ /^Insecure \$ENV/;
936 }
937 if ($Is_VMS) {
938     for (184..205) {print "not ok $_ # TODO tainted %ENV warning occludes tainted arguments warning\n";}
939 }
940 else 
941 {
942     # bug 20020208.005 plus some extras
943     # single arg exec/system are tests 80-83
944     my $err = qr/^Insecure dependency/ ;
945     test 184, eval { exec $TAINT, $TAINT } eq '', 'exec';
946     test 185, $@ =~ $err, $@;
947     test 186, eval { exec $TAINT $TAINT } eq '', 'exec';
948     test 187, $@ =~ $err, $@;
949     test 188, eval { exec $TAINT $TAINT, $TAINT } eq '', 'exec';
950     test 189, $@ =~ $err, $@;
951     test 190, eval { exec $TAINT 'notaint' } eq '', 'exec';
952     test 191, $@ =~ $err, $@;
953     test 192, eval { exec {'notaint'} $TAINT } eq '', 'exec';
954     test 193, $@ =~ $err, $@;
955
956     test 194, eval { system $TAINT, $TAINT } eq '', 'system';
957     test 195, $@ =~ $err, $@;
958     test 196, eval { system $TAINT $TAINT } eq '', 'system';
959     test 197, $@ =~ $err, $@;
960     test 198, eval { system $TAINT $TAINT, $TAINT } eq '', 'system';
961     test 199, $@ =~ $err, $@;
962     test 200, eval { system $TAINT 'notaint' } eq '', 'system';
963     test 201, $@ =~ $err, $@;
964     test 202, eval { system {'notaint'} $TAINT } eq '', 'system';
965     test 203, $@ =~ $err, $@;
966
967     eval { system("lskdfj does not exist","with","args"); };
968     test 204, $@ eq '';
969     if ($Is_MacOS) {
970         print "ok 205 # no exec()\n";
971     } else {
972         eval { exec("lskdfj does not exist","with","args"); };
973         test 205, $@ eq '';
974     }
975
976     # If you add tests here update also the above skip block for VMS.
977 }
978
979 {
980     # [ID 20020704.001] taint propagation failure
981     use re 'taint';
982     $TAINT =~ /(.*)/;
983     test 206, tainted(my $foo = $1);
984 }
985
986 {
987     # [perl #24291] this used to dump core
988     our %nonmagicalenv = ( PATH => "util" );
989     local *ENV = \%nonmagicalenv;
990     eval { system("lskdfj"); };
991     test 207, $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
992     local *ENV = *nonmagicalenv;
993     eval { system("lskdfj"); };
994     test 208, $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
995 }