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