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