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