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