3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
5 # I don't claim to know all about tainting. If anyone sees
6 # tests that I've missed here, please add them. But this is
7 # better than having no tests at all, right?
12 @INC = '../lib' if -d '../lib';
18 my $Is_VMS = $^O eq 'VMS';
19 my $Is_MSWin32 = $^O eq 'MSWin32';
20 my $Is_Dos = $^O eq 'dos';
21 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
22 $Is_MSWin32 ? '.\perl' : './perl';
23 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
27 for $x ('DCL$PATH', @MoreEnv) {
28 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
33 warn "# Note: logical name 'PATH' may have been deleted\n";
34 @ENV{keys %old} = values %old;
40 # The empty tainted value, for tainting strings
41 my $TAINT = substr($^X, 0, 0);
42 # A tainted zero, useful for tainting numbers
43 my $TAINT0 = 0 + $TAINT;
45 # This taints each argument passed. All must be lvalues.
46 # Side effect: It also stringifies them. :-(
48 for (@_) { $_ .= $TAINT }
51 # How to identify taint when you see it
53 not eval { join("",@_), kill 0; 1 };
59 for (@_) { return 0 unless tainted $_ }
64 my($serial, $boolean, $diag) = @_;
68 print "not ok $serial\n";
69 for (split m/^/m, $diag) {
74 or substr($diag, -1) eq "\n";
78 # We need an external program to call.
79 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$");
81 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
82 print PROG 'print "@ARGV\n"', "\n";
84 my $echo = "$Invoke_Perl $ECHO";
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.
92 $ENV{'DCL$PATH'} = '' if $Is_VMS;
95 delete @ENV{@MoreEnv};
98 test 1, eval { `$echo 1` } eq "1\n";
100 if ($Is_MSWin32 || $Is_VMS || $Is_Dos) {
101 print "# Environment tainting tests skipped\n";
102 for (2..5) { print "ok $_\n" }
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}/;
112 test 2, !@vars, "\$$vars[0]";
114 # tainted $TERM is unsafe only if it contains metachars
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}/, $@;
124 unless ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) {
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";
132 local $ENV{PATH} = $tmp;
133 test 6, eval { `$echo 1` } eq '';
134 test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
137 for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" }
141 $ENV{'DCL$PATH'} = $TAINT;
142 test 8, eval { `$echo 1` } eq '';
143 test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
145 $ENV{'DCL$PATH'} = $tmp;
146 test 10, eval { `$echo 1` } eq '';
147 test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
150 for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" }
152 $ENV{'DCL$PATH'} = '';
155 for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; }
159 # Let's see that we can taint and untaint as needed.
162 test 12, tainted $foo;
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;
168 test 13, not tainted $foo;
171 test 14, tainted $foo;
174 test 15, not any_tainted @list;
175 taint_these @list[1,3,5,7,9];
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];
180 ($foo) = $foo =~ /(.+)/;
181 test 19, not tainted $foo;
183 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
184 test 20, not tainted $foo;
185 test 21, $foo eq 'bar';
187 my $pi = 4 * atan2(1,1) + $TAINT0;
188 test 22, tainted $pi;
190 ($pi) = $pi =~ /(\d+\.\d+)/;
191 test 23, not tainted $pi;
192 test 24, sprintf("%.5f", $pi) eq '3.14159';
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.
199 open PROG, "> $arg" or die "Can't create $arg: $!";
201 eval { join('', @ARGV), kill 0 };
202 exit 0 if $@ =~ /^Insecure dependency/;
203 print "# Oops: \$@ was [$@]\n";
207 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
208 test 25, !$?, "Exited with status $?";
212 # Reading from a file should be tainted
215 test 26, open(FILE, $file), "Couldn't open '$file': $!";
218 sysread(FILE, $block, 100);
221 test 27, tainted $block;
222 test 28, tainted $line;
225 # Globs should be forbidden, except under VMS,
226 # which doesn't spawn an external program.
228 for (29..30) { print "ok $_\n"; }
231 my @globs = eval { <*> };
232 test 29, @globs == 0 && $@ =~ /^Insecure dependency/;
234 @globs = eval { glob '*' };
235 test 30, @globs == 0 && $@ =~ /^Insecure dependency/;
238 # Output of commands should be tainted
240 my $foo = `$echo abc`;
241 test 31, tainted $foo;
244 # Certain system variables should be tainted
246 test 32, all_tainted $^X, $0;
249 # Results of matching should all be untainted
251 my $foo = "abcdefghi" . $TAINT;
252 test 33, tainted $foo;
255 test 34, not any_tainted $`, $&, $';
257 $foo =~ /(...)(...)(...)/;
258 test 35, not any_tainted $1, $2, $3, $+;
260 my @bar = $foo =~ /(...)(...)(...)/;
261 test 36, not any_tainted @bar;
263 test 37, tainted $foo; # $foo should still be tainted!
264 test 38, $foo eq "abcdefghi";
267 # Operations which affect files can't use tainted data.
269 test 39, eval { chmod 0, $TAINT } eq '', 'chmod';
270 test 40, $@ =~ /^Insecure dependency/, $@;
272 # There is no feature test in $Config{} for truncate,
273 # so we allow for the possibility that it's missing.
274 test 41, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate';
275 test 42, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
277 test 43, eval { rename '', $TAINT } eq '', 'rename';
278 test 44, $@ =~ /^Insecure dependency/, $@;
280 test 45, eval { unlink $TAINT } eq '', 'unlink';
281 test 46, $@ =~ /^Insecure dependency/, $@;
283 test 47, eval { utime $TAINT } eq '', 'utime';
284 test 48, $@ =~ /^Insecure dependency/, $@;
286 if ($Config{d_chown}) {
287 test 49, eval { chown -1, -1, $TAINT } eq '', 'chown';
288 test 50, $@ =~ /^Insecure dependency/, $@;
291 for (49..50) { print "ok $_ # Skipped: chown() is not available\n" }
294 if ($Config{d_link}) {
295 test 51, eval { link $TAINT, '' } eq '', 'link';
296 test 52, $@ =~ /^Insecure dependency/, $@;
299 for (51..52) { print "ok $_ # Skipped: link() is not available\n" }
302 if ($Config{d_symlink}) {
303 test 53, eval { symlink $TAINT, '' } eq '', 'symlink';
304 test 54, $@ =~ /^Insecure dependency/, $@;
307 for (53..54) { print "ok $_ # Skipped: symlink() is not available\n" }
311 # Operations which affect directories can't use tainted data.
313 test 55, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir';
314 test 56, $@ =~ /^Insecure dependency/, $@;
316 test 57, eval { rmdir $TAINT } eq '', 'rmdir';
317 test 58, $@ =~ /^Insecure dependency/, $@;
319 test 59, eval { chdir $TAINT } eq '', 'chdir';
320 test 60, $@ =~ /^Insecure dependency/, $@;
322 if ($Config{d_chroot}) {
323 test 61, eval { chroot $TAINT } eq '', 'chroot';
324 test 62, $@ =~ /^Insecure dependency/, $@;
327 for (61..62) { print "ok $_ # Skipped: chroot() is not available\n" }
331 # Some operations using files can't use tainted data.
333 my $foo = "imaginary library" . $TAINT;
334 test 63, eval { require $foo } eq '', 'require';
335 test 64, $@ =~ /^Insecure dependency/, $@;
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
342 test 65, eval { open FOO, $foo } eq '', 'open for read';
343 test 66, $@ eq '', $@; # NB: This should be allowed
344 test 67, $! == ($Config{"archname"} !~ "djgpp" ? 2 : 22); # File not found
346 test 68, eval { open FOO, "> $foo" } eq '', 'open for write';
347 test 69, $@ =~ /^Insecure dependency/, $@;
350 # Commands to the system can't use tainted data
354 if ($^O eq 'amigaos') {
355 for (70..73) { print "ok $_ # Skipped: open('|') is not available\n" }
358 test 70, eval { open FOO, "| $foo" } eq '', 'popen to';
359 test 71, $@ =~ /^Insecure dependency/, $@;
361 test 72, eval { open FOO, "$foo |" } eq '', 'popen from';
362 test 73, $@ =~ /^Insecure dependency/, $@;
365 test 74, eval { exec $TAINT } eq '', 'exec';
366 test 75, $@ =~ /^Insecure dependency/, $@;
368 test 76, eval { system $TAINT } eq '', 'system';
369 test 77, $@ =~ /^Insecure dependency/, $@;
374 test 78, eval { `$echo 1$foo` } eq '', 'backticks';
375 test 79, $@ =~ /^Insecure dependency/, $@;
377 if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe
378 test 80, join('', eval { glob $foo } ) ne '', 'globbing';
379 test 81, $@ eq '', $@;
382 for (80..81) { print "ok $_ # Skipped: this is not VMS\n"; }
386 # Operations which affect processes can't use tainted data.
388 test 82, eval { kill 0, $TAINT } eq '', 'kill';
389 test 83, $@ =~ /^Insecure dependency/, $@;
391 if ($Config{d_setpgrp}) {
392 test 84, eval { setpgrp 0, $TAINT } eq '', 'setpgrp';
393 test 85, $@ =~ /^Insecure dependency/, $@;
396 for (84..85) { print "ok $_ # Skipped: setpgrp() is not available\n" }
399 if ($Config{d_setprior}) {
400 test 86, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority';
401 test 87, $@ =~ /^Insecure dependency/, $@;
404 for (86..87) { print "ok $_ # Skipped: setpriority() is not available\n" }
408 # Some miscellaneous operations can't use tainted data.
410 if ($Config{d_syscall}) {
411 test 88, eval { syscall $TAINT } eq '', 'syscall';
412 test 89, $@ =~ /^Insecure dependency/, $@;
415 for (88..89) { print "ok $_ # Skipped: syscall() is not available\n" }
422 my $temp = "./taintC$$";
424 test 90, open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
426 test 91, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl';
427 test 92, $@ =~ /^Insecure dependency/, $@;
429 if ($Config{d_fcntl}) {
430 test 93, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl';
431 test 94, $@ =~ /^Insecure dependency/, $@;
434 for (93..94) { print "ok $_ # Skipped: fcntl() is not available\n" }
441 # Some tests involving references
443 my $foo = 'abc' . $TAINT;
445 test 95, not tainted $fooref;
446 test 96, tainted $$fooref;
447 test 97, tainted $foo;
450 # Some tests involving assignment
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--);
470 # Test assignment and return of lists
472 my @foo = ("A", "tainted" . $TAINT, "B");
473 test 112, not tainted $foo[0];
474 test 113, tainted $foo[1];
475 test 114, not tainted $foo[2];
477 test 115, not tainted $bar[0];
478 test 116, tainted $bar[1];
479 test 117, not tainted $bar[2];
480 my @baz = eval { "A", "tainted" . $TAINT, "B" };
481 test 118, not tainted $baz[0];
482 test 119, tainted $baz[1];
483 test 120, not tainted $baz[2];
484 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
485 test 121, not tainted $plugh[0];
486 test 122, tainted $plugh[1];
487 test 123, not tainted $plugh[2];
488 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
489 test 124, not tainted ((&$nautilus)[0]);
490 test 125, tainted ((&$nautilus)[1]);
491 test 126, not tainted ((&$nautilus)[2]);
492 my @xyzzy = &$nautilus;
493 test 127, not tainted $xyzzy[0];
494 test 128, tainted $xyzzy[1];
495 test 129, not tainted $xyzzy[2];
496 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
497 test 130, not tainted ((&$red_october)[0]);
498 test 131, tainted ((&$red_october)[1]);
499 test 132, not tainted ((&$red_october)[2]);
500 my @corge = &$red_october;
501 test 133, not tainted $corge[0];
502 test 134, tainted $corge[1];
503 test 135, not tainted $corge[2];
506 # Test for system/library calls returning string data of dubious origin.
508 # No reliable %Config check for getpw*
509 if (eval { setpwent(); getpwent(); 1 }) {
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]);
524 print "ok 136 # Skipped: getpwent() is not available\n";
527 if ($Config{d_readdir}) { # pretty hard to imagine not
529 opendir(D, "op") or die "opendir: $!\n";
530 my $readdir = readdir(D);
531 test 137, tainted $readdir;
534 print "ok 137 # Skipped: readdir() is not available\n";
537 if ($Config{d_readlink} && $Config{d_symlink}) {
538 my $symlink = "sl$$";
540 symlink("/something/naughty", $symlink) or die "symlink: $!\n";
541 my $readlink = readlink($symlink);
542 test 138, tainted $readlink;
545 print "ok 138 # Skipped: readlink() or symlink() is not available\n";
549 # test bitwise ops (regression bug)
553 test 139, not tainted $j;
556 test 140, tainted $j;