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