-DL and PERL_DEBUG_MSTATS unravelled
[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';
12 @INC = '../lib' if -d '../lib';
13}
14
15use strict;
16use Config;
17
18my $Is_VMS = $^O eq 'VMS';
68dc0745 19my $Is_MSWin32 = $^O eq 'MSWin32';
39e571d4 20my $Is_Dos = $^O eq 'dos';
68dc0745 21my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
22 $Is_MSWin32 ? '.\perl' : './perl';
c90c0ff4 23my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
7bac28a0 24
1e422769 25if ($Is_VMS) {
7bac28a0 26 my (%old, $x);
27 for $x ('DCL$PATH', @MoreEnv) {
28 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
29 }
1e422769 30 eval <<EndOfCleanup;
31 END {
32 \$ENV{PATH} = '';
33 warn "# Note: logical name 'PATH' may have been deleted\n";
7bac28a0 34 @ENV{keys %old} = values %old;
1e422769 35 }
36EndOfCleanup
37}
38
39# Sources of taint:
40# The empty tainted value, for tainting strings
41my $TAINT = substr($^X, 0, 0);
42# A tainted zero, useful for tainting numbers
43my $TAINT0 = 0 + $TAINT;
44
45# This taints each argument passed. All must be lvalues.
46# Side effect: It also stringifies them. :-(
47sub taint_these (@) {
48 for (@_) { $_ .= $TAINT }
49}
50
51# How to identify taint when you see it
52sub any_tainted (@) {
53 not eval { join("",@_), kill 0; 1 };
54}
55sub tainted ($) {
56 any_tainted @_;
57}
58sub all_tainted (@) {
59 for (@_) { return 0 unless tainted $_ }
60 1;
61}
62
63sub test ($$;$) {
64 my($serial, $boolean, $diag) = @_;
65 if ($boolean) {
66 print "ok $serial\n";
67 } else {
68 print "not ok $serial\n";
69 for (split m/^/m, $diag) {
70 print "# $_";
71 }
9607fc9c 72 print "\n" unless
1e422769 73 $diag eq ''
74 or substr($diag, -1) eq "\n";
75 }
76}
77
78# We need an external program to call.
5aabfad6 79my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
1e422769 80END { unlink $ECHO }
81open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
82print PROG 'print "@ARGV\n"', "\n";
83close PROG;
84my $echo = "$Invoke_Perl $ECHO";
85
48c036b1 86print "1..145\n";
1e422769 87
88# First, let's make sure that Perl is checking the dangerous
89# environment variables. Maybe they aren't set yet, so we'll
90# taint them ourselves.
91{
92 $ENV{'DCL$PATH'} = '' if $Is_VMS;
93
7bac28a0 94 $ENV{PATH} = '';
c90c0ff4 95 delete @ENV{@MoreEnv};
7bac28a0 96 $ENV{TERM} = 'dumb';
97
98 test 1, eval { `$echo 1` } eq "1\n";
99
39e571d4 100 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
7bac28a0 101 print "# Environment tainting tests skipped\n";
c90c0ff4 102 for (2..5) { print "ok $_\n" }
5aabfad6 103 }
104 else {
7bac28a0 105 my @vars = ('PATH', @MoreEnv);
106 while (my $v = $vars[0]) {
107 local $ENV{$v} = $TAINT;
108 last if eval { `$echo 1` };
109 last unless $@ =~ /^Insecure \$ENV{$v}/;
110 shift @vars;
111 }
112 test 2, !@vars, "\$$vars[0]";
c90c0ff4 113
114 # tainted $TERM is unsafe only if it contains metachars
115 local $ENV{TERM};
116 $ENV{TERM} = 'e=mc2';
117 test 3, eval { `$echo 1` } eq "1\n";
118 $ENV{TERM} = 'e=mc2' . $TAINT;
119 test 4, eval { `$echo 1` } eq '';
120 test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@;
5aabfad6 121 }
7bac28a0 122
9607fc9c 123 my $tmp;
48c036b1 124 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
125 print "# all directories are writeable\n";
126 }
127 else {
9607fc9c 128 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
129 qw(/tmp /var/tmp /usr/tmp /sys$scratch),
130 @ENV{qw(TMP TEMP)})[0]
131 or print "# can't find world-writeable directory to test PATH\n";
132 }
133
385588b3 134 if ($tmp) {
7bac28a0 135 local $ENV{PATH} = $tmp;
c90c0ff4 136 test 6, eval { `$echo 1` } eq '';
137 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
1e422769 138 }
139 else {
fac76ed7 140 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
1e422769 141 }
142
1e422769 143 if ($Is_VMS) {
144 $ENV{'DCL$PATH'} = $TAINT;
c90c0ff4 145 test 8, eval { `$echo 1` } eq '';
146 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
9607fc9c 147 if ($tmp) {
148 $ENV{'DCL$PATH'} = $tmp;
c90c0ff4 149 test 10, eval { `$echo 1` } eq '';
150 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
9607fc9c 151 }
152 else {
fac76ed7 153 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
9607fc9c 154 }
1e422769 155 $ENV{'DCL$PATH'} = '';
156 }
157 else {
fac76ed7 158 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
1e422769 159 }
160}
161
162# Let's see that we can taint and untaint as needed.
163{
164 my $foo = $TAINT;
c90c0ff4 165 test 12, tainted $foo;
9607fc9c 166
167 # That was a sanity check. If it failed, stop the insanity!
168 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769 169
170 $foo = "foo";
c90c0ff4 171 test 13, not tainted $foo;
1e422769 172
173 taint_these($foo);
c90c0ff4 174 test 14, tainted $foo;
1e422769 175
176 my @list = 1..10;
c90c0ff4 177 test 15, not any_tainted @list;
1e422769 178 taint_these @list[1,3,5,7,9];
c90c0ff4 179 test 16, any_tainted @list;
180 test 17, all_tainted @list[1,3,5,7,9];
181 test 18, not any_tainted @list[0,2,4,6,8];
1e422769 182
183 ($foo) = $foo =~ /(.+)/;
c90c0ff4 184 test 19, not tainted $foo;
1e422769 185
186 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
c90c0ff4 187 test 20, not tainted $foo;
188 test 21, $foo eq 'bar';
1e422769 189
48c036b1 190 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/t;
191 test 22, tainted $foo;
192 test 23, $foo eq 'bar';
193
1e422769 194 my $pi = 4 * atan2(1,1) + $TAINT0;
48c036b1 195 test 24, tainted $pi;
1e422769 196
197 ($pi) = $pi =~ /(\d+\.\d+)/;
48c036b1 198 test 25, not tainted $pi;
199 test 26, sprintf("%.5f", $pi) eq '3.14159';
1e422769 200}
201
202# How about command-line arguments? The problem is that we don't
203# always get some, so we'll run another process with some.
204{
205 my $arg = "./arg$$";
206 open PROG, "> $arg" or die "Can't create $arg: $!";
207 print PROG q{
208 eval { join('', @ARGV), kill 0 };
209 exit 0 if $@ =~ /^Insecure dependency/;
210 print "# Oops: \$@ was [$@]\n";
211 exit 1;
212 };
213 close PROG;
214 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
48c036b1 215 test 27, !$?, "Exited with status $?";
1e422769 216 unlink $arg;
217}
218
219# Reading from a file should be tainted
220{
9607fc9c 221 my $file = './TEST';
48c036b1 222 test 28, open(FILE, $file), "Couldn't open '$file': $!";
1e422769 223
224 my $block;
225 sysread(FILE, $block, 100);
9607fc9c 226 my $line = <FILE>;
1e422769 227 close FILE;
48c036b1 228 test 29, tainted $block;
229 test 30, tainted $line;
1e422769 230}
231
c90c0ff4 232# Globs should be forbidden, except under VMS,
233# which doesn't spawn an external program.
234if ($Is_VMS) {
235 for (29..30) { print "ok $_\n"; }
236}
237else {
7bac28a0 238 my @globs = eval { <*> };
48c036b1 239 test 31, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 240
7bac28a0 241 @globs = eval { glob '*' };
48c036b1 242 test 32, @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 243}
244
245# Output of commands should be tainted
246{
247 my $foo = `$echo abc`;
48c036b1 248 test 33, tainted $foo;
1e422769 249}
250
251# Certain system variables should be tainted
252{
48c036b1 253 test 34, all_tainted $^X, $0;
1e422769 254}
255
256# Results of matching should all be untainted
257{
258 my $foo = "abcdefghi" . $TAINT;
48c036b1 259 test 35, tainted $foo;
1e422769 260
261 $foo =~ /def/;
48c036b1 262 test 36, not any_tainted $`, $&, $';
1e422769 263
264 $foo =~ /(...)(...)(...)/;
48c036b1 265 test 37, not any_tainted $1, $2, $3, $+;
1e422769 266
267 my @bar = $foo =~ /(...)(...)(...)/;
48c036b1 268 test 38, not any_tainted @bar;
1e422769 269
48c036b1 270 test 39, tainted $foo; # $foo should still be tainted!
271 test 40, $foo eq "abcdefghi";
1e422769 272}
273
274# Operations which affect files can't use tainted data.
275{
48c036b1 276 test 41, eval { chmod 0, $TAINT } eq '', 'chmod';
277 test 42, $@ =~ /^Insecure dependency/, $@;
1e422769 278
9607fc9c 279 # There is no feature test in $Config{} for truncate,
280 # so we allow for the possibility that it's missing.
48c036b1 281 test 43, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
282 test 44, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 283
48c036b1 284 test 45, eval { rename '', $TAINT } eq '', 'rename';
c90c0ff4 285 test 46, $@ =~ /^Insecure dependency/, $@;
1e422769 286
48c036b1 287 test 47, eval { unlink $TAINT } eq '', 'unlink';
c90c0ff4 288 test 48, $@ =~ /^Insecure dependency/, $@;
9607fc9c 289
48c036b1 290 test 49, eval { utime $TAINT } eq '', 'utime';
291 test 50, $@ =~ /^Insecure dependency/, $@;
292
1e422769 293 if ($Config{d_chown}) {
48c036b1 294 test 51, eval { chown -1, -1, $TAINT } eq '', 'chown';
295 test 52, $@ =~ /^Insecure dependency/, $@;
1e422769 296 }
297 else {
48c036b1 298 for (51..52) { print "ok $_ # Skipped: chown() is not available\n" }
1e422769 299 }
300
301 if ($Config{d_link}) {
48c036b1 302 test 53, eval { link $TAINT, '' } eq '', 'link';
303 test 54, $@ =~ /^Insecure dependency/, $@;
1e422769 304 }
305 else {
48c036b1 306 for (53..54) { print "ok $_ # Skipped: link() is not available\n" }
1e422769 307 }
308
309 if ($Config{d_symlink}) {
48c036b1 310 test 55, eval { symlink $TAINT, '' } eq '', 'symlink';
311 test 56, $@ =~ /^Insecure dependency/, $@;
1e422769 312 }
313 else {
48c036b1 314 for (55..56) { print "ok $_ # Skipped: symlink() is not available\n" }
1e422769 315 }
316}
317
318# Operations which affect directories can't use tainted data.
319{
48c036b1 320 test 57, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
c90c0ff4 321 test 58, $@ =~ /^Insecure dependency/, $@;
1e422769 322
48c036b1 323 test 59, eval { rmdir $TAINT } eq '', 'rmdir';
c90c0ff4 324 test 60, $@ =~ /^Insecure dependency/, $@;
9607fc9c 325
48c036b1 326 test 61, eval { chdir $TAINT } eq '', 'chdir';
327 test 62, $@ =~ /^Insecure dependency/, $@;
328
1e422769 329 if ($Config{d_chroot}) {
48c036b1 330 test 63, eval { chroot $TAINT } eq '', 'chroot';
331 test 64, $@ =~ /^Insecure dependency/, $@;
1e422769 332 }
333 else {
48c036b1 334 for (63..64) { print "ok $_ # Skipped: chroot() is not available\n" }
1e422769 335 }
336}
337
338# Some operations using files can't use tainted data.
339{
340 my $foo = "imaginary library" . $TAINT;
48c036b1 341 test 65, eval { require $foo } eq '', 'require';
342 test 66, $@ =~ /^Insecure dependency/, $@;
1e422769 343
344 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
345 END { unlink $filename if defined $filename }
346 $foo = $filename . $TAINT;
347 unlink $filename; # in any case
348
48c036b1 349 test 67, eval { open FOO, $foo } eq '', 'open for read';
350 test 68, $@ eq '', $@; # NB: This should be allowed
87205358 351 test 69, $! == 2 || ($Is_Dos && $! == 22); # File not found
1e422769 352
48c036b1 353 test 70, eval { open FOO, "> $foo" } eq '', 'open for write';
354 test 71, $@ =~ /^Insecure dependency/, $@;
1e422769 355}
356
357# Commands to the system can't use tainted data
358{
359 my $foo = $TAINT;
360
361 if ($^O eq 'amigaos') {
48c036b1 362 for (72..75) { print "ok $_ # Skipped: open('|') is not available\n" }
1e422769 363 }
364 else {
48c036b1 365 test 72, eval { open FOO, "| $foo" } eq '', 'popen to';
c90c0ff4 366 test 73, $@ =~ /^Insecure dependency/, $@;
1e422769 367
48c036b1 368 test 74, eval { open FOO, "$foo |" } eq '', 'popen from';
369 test 75, $@ =~ /^Insecure dependency/, $@;
370 }
1e422769 371
48c036b1 372 test 76, eval { exec $TAINT } eq '', 'exec';
c90c0ff4 373 test 77, $@ =~ /^Insecure dependency/, $@;
9607fc9c 374
48c036b1 375 test 78, eval { system $TAINT } eq '', 'system';
376 test 79, $@ =~ /^Insecure dependency/, $@;
377
1e422769 378 $foo = "*";
379 taint_these $foo;
380
48c036b1 381 test 80, eval { `$echo 1$foo` } eq '', 'backticks';
382 test 81, $@ =~ /^Insecure dependency/, $@;
1e422769 383
384 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
48c036b1 385 test 82, join('', eval { glob $foo } ) ne '', 'globbing';
386 test 83, $@ eq '', $@;
1e422769 387 }
388 else {
48c036b1 389 for (82..83) { print "ok $_ # Skipped: this is not VMS\n"; }
1e422769 390 }
391}
392
393# Operations which affect processes can't use tainted data.
394{
48c036b1 395 test 84, eval { kill 0, $TAINT } eq '', 'kill';
396 test 85, $@ =~ /^Insecure dependency/, $@;
1e422769 397
398 if ($Config{d_setpgrp}) {
48c036b1 399 test 86, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
400 test 87, $@ =~ /^Insecure dependency/, $@;
1e422769 401 }
402 else {
48c036b1 403 for (86..87) { print "ok $_ # Skipped: setpgrp() is not available\n" }
1e422769 404 }
405
406 if ($Config{d_setprior}) {
48c036b1 407 test 88, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
408 test 89, $@ =~ /^Insecure dependency/, $@;
1e422769 409 }
410 else {
48c036b1 411 for (88..89) { print "ok $_ # Skipped: setpriority() is not available\n" }
1e422769 412 }
413}
414
415# Some miscellaneous operations can't use tainted data.
416{
417 if ($Config{d_syscall}) {
48c036b1 418 test 90, eval { syscall $TAINT } eq '', 'syscall';
419 test 91, $@ =~ /^Insecure dependency/, $@;
1e422769 420 }
421 else {
48c036b1 422 for (90..91) { print "ok $_ # Skipped: syscall() is not available\n" }
1e422769 423 }
424
425 {
426 my $foo = "x" x 979;
427 taint_these $foo;
428 local *FOO;
429 my $temp = "./taintC$$";
430 END { unlink $temp }
48c036b1 431 test 92, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 432
48c036b1 433 test 93, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
434 test 94, $@ =~ /^Insecure dependency/, $@;
1e422769 435
436 if ($Config{d_fcntl}) {
48c036b1 437 test 95, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
438 test 96, $@ =~ /^Insecure dependency/, $@;
1e422769 439 }
440 else {
48c036b1 441 for (95..96) { print "ok $_ # Skipped: fcntl() is not available\n" }
1e422769 442 }
443
444 close FOO;
445 }
446}
447
9607fc9c 448# Some tests involving references
1e422769 449{
450 my $foo = 'abc' . $TAINT;
451 my $fooref = \$foo;
48c036b1 452 test 97, not tainted $fooref;
453 test 98, tainted $$fooref;
454 test 99, tainted $foo;
1e422769 455}
54310121 456
457# Some tests involving assignment
458{
459 my $foo = $TAINT0;
460 my $bar = $foo;
48c036b1 461 test 100, all_tainted $foo, $bar;
462 test 101, tainted($foo = $bar);
463 test 102, tainted($bar = $bar);
464 test 103, tainted($bar += $bar);
465 test 104, tainted($bar -= $bar);
466 test 105, tainted($bar *= $bar);
467 test 106, tainted($bar++);
468 test 107, tainted($bar /= $bar);
469 test 108, tainted($bar += 0);
470 test 109, tainted($bar -= 2);
471 test 110, tainted($bar *= -1);
472 test 111, tainted($bar /= 1);
473 test 112, tainted($bar--);
474 test 113, $bar == 0;
54310121 475}
a1f49e72 476
477# Test assignment and return of lists
478{
479 my @foo = ("A", "tainted" . $TAINT, "B");
48c036b1 480 test 114, not tainted $foo[0];
481 test 115, tainted $foo[1];
482 test 116, not tainted $foo[2];
a1f49e72 483 my @bar = @foo;
48c036b1 484 test 117, not tainted $bar[0];
485 test 118, tainted $bar[1];
486 test 119, not tainted $bar[2];
a1f49e72 487 my @baz = eval { "A", "tainted" . $TAINT, "B" };
48c036b1 488 test 120, not tainted $baz[0];
489 test 121, tainted $baz[1];
490 test 122, not tainted $baz[2];
a1f49e72 491 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
48c036b1 492 test 123, not tainted $plugh[0];
493 test 124, tainted $plugh[1];
494 test 125, not tainted $plugh[2];
a1f49e72 495 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
48c036b1 496 test 126, not tainted ((&$nautilus)[0]);
497 test 127, tainted ((&$nautilus)[1]);
498 test 128, not tainted ((&$nautilus)[2]);
a1f49e72 499 my @xyzzy = &$nautilus;
48c036b1 500 test 129, not tainted $xyzzy[0];
501 test 130, tainted $xyzzy[1];
502 test 131, not tainted $xyzzy[2];
a1f49e72 503 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
48c036b1 504 test 132, not tainted ((&$red_october)[0]);
505 test 133, tainted ((&$red_october)[1]);
506 test 134, not tainted ((&$red_october)[2]);
a1f49e72 507 my @corge = &$red_october;
48c036b1 508 test 135, not tainted $corge[0];
509 test 136, tainted $corge[1];
510 test 137, not tainted $corge[2];
a1f49e72 511}
fb73857a 512
513# Test for system/library calls returning string data of dubious origin.
514{
515 # No reliable %Config check for getpw*
516 if (eval { setpwent(); getpwent(); 1 }) {
517 setpwent();
518 my @getpwent = getpwent();
519 die "getpwent: $!\n" unless (@getpwent);
48c036b1 520 test 138,( not tainted $getpwent[0]
fb73857a 521 and not tainted $getpwent[1]
522 and not tainted $getpwent[2]
523 and not tainted $getpwent[3]
524 and not tainted $getpwent[4]
525 and not tainted $getpwent[5]
526 and tainted $getpwent[6] # gecos
527 and not tainted $getpwent[7]
528 and not tainted $getpwent[8]);
529 endpwent();
530 } else {
48c036b1 531 print "ok 138 # Skipped: getpwent() is not available\n";
fb73857a 532 }
533
534 if ($Config{d_readdir}) { # pretty hard to imagine not
535 local(*D);
536 opendir(D, "op") or die "opendir: $!\n";
537 my $readdir = readdir(D);
48c036b1 538 test 139, tainted $readdir;
fb73857a 539 closedir(OP);
540 } else {
48c036b1 541 print "ok 139 # Skipped: readdir() is not available\n";
fb73857a 542 }
543
544 if ($Config{d_readlink} && $Config{d_symlink}) {
545 my $symlink = "sl$$";
546 unlink($symlink);
547 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
548 my $readlink = readlink($symlink);
48c036b1 549 test 140, tainted $readlink;
fb73857a 550 unlink($symlink);
551 } else {
48c036b1 552 print "ok 140 # Skipped: readlink() or symlink() is not available\n";
fb73857a 553 }
554}
555
556# test bitwise ops (regression bug)
557{
558 my $why = "y";
559 my $j = "x" | $why;
48c036b1 560 test 141, not tainted $j;
fb73857a 561 $why = $TAINT."y";
562 $j = "x" | $why;
48c036b1 563 test 142, tainted $j;
fb73857a 564}
565
48c036b1 566# test target of substitution (regression bug)
567{
568 my $why = $TAINT."y";
569 $why =~ s/y/z/;
570 test 143, tainted $why;
571
572 my $z = "[z]";
573 $why =~ s/$z/zee/;
574 test 144, tainted $why;
575
576 $why =~ s/e/'-'.$$/ge;
577 test 145, tainted $why;
578}