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